Commit | Line | Data |
---|---|---|
e44b5645 JR |
1 | /* |
2 | * Unit tests for the firing policy object API. | |
3 | * | |
4 | * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com> | |
5 | * | |
6 | * SPDX-License-Identifier: LGPL-2.1-only | |
7 | * | |
8 | */ | |
9 | ||
10 | #include <assert.h> | |
11 | #include <inttypes.h> | |
12 | #include <stdio.h> | |
13 | #include <string.h> | |
14 | #include <unistd.h> | |
15 | ||
16 | #include <tap/tap.h> | |
17 | ||
18 | #include <common/payload-view.h> | |
19 | #include <common/payload.h> | |
20 | #include <lttng/action/firing-policy-internal.h> | |
21 | #include <lttng/action/firing-policy.h> | |
22 | ||
23 | /* For error.h. */ | |
24 | int lttng_opt_quiet = 1; | |
25 | int lttng_opt_verbose; | |
26 | int lttng_opt_mi; | |
27 | ||
28 | #define NUM_TESTS 42 | |
29 | ||
30 | static void test_firing_policy_every_n(void) | |
31 | { | |
32 | enum lttng_firing_policy_status status; | |
33 | struct lttng_firing_policy *policy_a = NULL; /* Interval of 100. */ | |
34 | struct lttng_firing_policy *policy_b = NULL; /* Interval of 100. */ | |
35 | struct lttng_firing_policy *policy_c = NULL; /* Interval of 1. */ | |
36 | struct lttng_firing_policy *policy_from_buffer = NULL; | |
37 | uint64_t interval_a_b = 100; | |
38 | uint64_t interval_c = 1; | |
39 | uint64_t interval_query = 0; | |
40 | struct lttng_payload payload; | |
41 | ||
42 | lttng_payload_init(&payload); | |
43 | ||
44 | policy_a = lttng_firing_policy_every_n_create(interval_a_b); | |
45 | policy_b = lttng_firing_policy_every_n_create(interval_a_b); | |
46 | policy_c = lttng_firing_policy_every_n_create(interval_c); | |
47 | ok(policy_a != NULL, | |
48 | "Firing policy 'every n' A created: interval: %" PRIu64, | |
49 | interval_a_b); | |
50 | ok(policy_b != NULL, | |
51 | "Firing policy 'every n' B created: interval: %" PRIu64, | |
52 | interval_a_b); | |
53 | ok(policy_c != NULL, | |
54 | "Firing policy 'every n' C created: interval: %" PRIu64, | |
55 | interval_c); | |
56 | ||
57 | ok(LTTNG_FIRING_POLICY_TYPE_EVERY_N == | |
58 | lttng_firing_policy_get_type(policy_a), | |
59 | "Type is LTTNG_FIRING_POLICY_TYPE_EVERY_N"); | |
60 | ||
61 | /* Getter tests */ | |
62 | status = lttng_firing_policy_every_n_get_interval(NULL, NULL); | |
63 | ok(status == LTTNG_FIRING_POLICY_STATUS_INVALID, | |
64 | "Get interval returns INVALID"); | |
65 | ||
66 | status = lttng_firing_policy_every_n_get_interval( | |
67 | NULL, &interval_query); | |
68 | ok(status == LTTNG_FIRING_POLICY_STATUS_INVALID, | |
69 | "Get interval returns INVALID"); | |
70 | ||
71 | status = lttng_firing_policy_every_n_get_interval(policy_a, NULL); | |
72 | ok(status == LTTNG_FIRING_POLICY_STATUS_INVALID, | |
73 | "Get interval returns INVALID"); | |
74 | ||
75 | status = lttng_firing_policy_every_n_get_interval( | |
76 | policy_a, &interval_query); | |
77 | ok(status == LTTNG_FIRING_POLICY_STATUS_OK && | |
78 | interval_query == interval_a_b, | |
79 | "Getting interval A"); | |
80 | ||
81 | status = lttng_firing_policy_every_n_get_interval( | |
82 | policy_b, &interval_query); | |
83 | ok(status == LTTNG_FIRING_POLICY_STATUS_OK && | |
84 | interval_query == interval_a_b, | |
85 | "Getting interval B"); | |
86 | ||
87 | status = lttng_firing_policy_every_n_get_interval( | |
88 | policy_c, &interval_query); | |
89 | ok(status == LTTNG_FIRING_POLICY_STATUS_OK && | |
90 | interval_query == interval_c, | |
91 | "Getting interval C"); | |
92 | ||
93 | /* is_equal tests */ | |
94 | ok(false == lttng_firing_policy_is_equal(NULL, NULL), | |
95 | "is equal (NULL,NULL)"); | |
96 | ok(false == lttng_firing_policy_is_equal(policy_a, NULL), | |
97 | "is equal (object, NULL)"); | |
98 | ok(false == lttng_firing_policy_is_equal(NULL, policy_a), | |
99 | "is equal (NULL, object)"); | |
100 | ok(true == lttng_firing_policy_is_equal(policy_a, policy_a), | |
101 | "is equal (object A, object A)"); | |
102 | ||
103 | ok(true == lttng_firing_policy_is_equal(policy_a, policy_b), | |
104 | "is equal (object A, object B"); | |
105 | ok(true == lttng_firing_policy_is_equal(policy_b, policy_a), | |
106 | "is equal (object B, object A"); | |
107 | ||
108 | ok(false == lttng_firing_policy_is_equal(policy_a, policy_c), | |
109 | "is equal (object A, object C)"); | |
110 | ok(false == lttng_firing_policy_is_equal(policy_c, policy_a), | |
111 | "is equal (object C, object A)"); | |
112 | ||
113 | /* Serialization and create_from buffer. */ | |
114 | ok(lttng_firing_policy_serialize(policy_a, &payload) == 0, | |
115 | "Serializing firing policy"); | |
116 | { | |
117 | struct lttng_payload_view view = | |
118 | lttng_payload_view_from_payload( | |
119 | &payload, 0, -1); | |
120 | ||
121 | ok(lttng_firing_policy_create_from_payload( | |
122 | &view, &policy_from_buffer) > 0 && | |
123 | policy_from_buffer != NULL, | |
124 | "Deserializing firing policy"); | |
125 | } | |
126 | ||
127 | ok(lttng_firing_policy_is_equal(policy_a, policy_from_buffer), | |
128 | "Original and deserialized instances are equal"); | |
129 | ||
130 | lttng_firing_policy_destroy(policy_a); | |
131 | lttng_firing_policy_destroy(policy_b); | |
132 | lttng_firing_policy_destroy(policy_c); | |
133 | lttng_firing_policy_destroy(policy_from_buffer); | |
134 | lttng_payload_reset(&payload); | |
135 | } | |
136 | ||
137 | static void test_firing_policy_once_after_n(void) | |
138 | { | |
139 | enum lttng_firing_policy_status status; | |
140 | struct lttng_firing_policy *policy_a = NULL; /* Threshold of 100. */ | |
141 | struct lttng_firing_policy *policy_b = NULL; /* threshold of 100 */ | |
142 | struct lttng_firing_policy *policy_c = NULL; /* threshold of 1 */ | |
143 | struct lttng_firing_policy *policy_from_buffer = NULL; | |
144 | uint64_t threshold_a_b = 100; | |
145 | uint64_t threshold_c = 1; | |
146 | uint64_t threshold_query = 0; | |
147 | struct lttng_payload payload; | |
148 | ||
149 | lttng_payload_init(&payload); | |
150 | ||
151 | policy_a = lttng_firing_policy_once_after_n_create(threshold_a_b); | |
152 | policy_b = lttng_firing_policy_once_after_n_create(threshold_a_b); | |
153 | policy_c = lttng_firing_policy_once_after_n_create(threshold_c); | |
154 | ok(policy_a != NULL, | |
155 | "Firing policy every n A created: threshold: %" PRIu64, | |
156 | threshold_a_b); | |
157 | ok(policy_b != NULL, | |
158 | "Firing policy every n B created: threshold: %" PRIu64, | |
159 | threshold_a_b); | |
160 | ok(policy_c != NULL, | |
161 | "Firing policy every n C created: threshold: %" PRIu64, | |
162 | threshold_c); | |
163 | ||
164 | ok(LTTNG_FIRING_POLICY_TYPE_ONCE_AFTER_N == | |
165 | lttng_firing_policy_get_type(policy_a), | |
166 | "Type is LTTNG_FIRING_POLICY_TYPE_once_after_n"); | |
167 | ||
168 | /* Getter tests */ | |
169 | status = lttng_firing_policy_once_after_n_get_threshold(NULL, NULL); | |
170 | ok(status == LTTNG_FIRING_POLICY_STATUS_INVALID, | |
171 | "Get threshold returns INVALID"); | |
172 | ||
173 | status = lttng_firing_policy_once_after_n_get_threshold( | |
174 | NULL, &threshold_query); | |
175 | ok(status == LTTNG_FIRING_POLICY_STATUS_INVALID, | |
176 | "Get threshold returns INVALID"); | |
177 | ||
178 | status = lttng_firing_policy_once_after_n_get_threshold(policy_a, NULL); | |
179 | ok(status == LTTNG_FIRING_POLICY_STATUS_INVALID, | |
180 | "Get threshold returns INVALID"); | |
181 | ||
182 | status = lttng_firing_policy_once_after_n_get_threshold( | |
183 | policy_a, &threshold_query); | |
184 | ok(status == LTTNG_FIRING_POLICY_STATUS_OK && | |
185 | threshold_query == threshold_a_b, | |
186 | "Getting threshold A"); | |
187 | ||
188 | status = lttng_firing_policy_once_after_n_get_threshold( | |
189 | policy_b, &threshold_query); | |
190 | ok(status == LTTNG_FIRING_POLICY_STATUS_OK && | |
191 | threshold_query == threshold_a_b, | |
192 | "Getting threshold B"); | |
193 | ||
194 | status = lttng_firing_policy_once_after_n_get_threshold( | |
195 | policy_c, &threshold_query); | |
196 | ok(status == LTTNG_FIRING_POLICY_STATUS_OK && | |
197 | threshold_query == threshold_c, | |
198 | "Getting threshold C"); | |
199 | ||
200 | /* is_equal tests */ | |
201 | ok(false == lttng_firing_policy_is_equal(NULL, NULL), | |
202 | "is equal (NULL,NULL)"); | |
203 | ok(false == lttng_firing_policy_is_equal(policy_a, NULL), | |
204 | "is equal (object, NULL)"); | |
205 | ok(false == lttng_firing_policy_is_equal(NULL, policy_a), | |
206 | "is equal (NULL, object)"); | |
207 | ok(true == lttng_firing_policy_is_equal(policy_a, policy_a), | |
208 | "is equal (object A, object A)"); | |
209 | ||
210 | ok(true == lttng_firing_policy_is_equal(policy_a, policy_b), | |
211 | "is equal (object A, object B"); | |
212 | ok(true == lttng_firing_policy_is_equal(policy_b, policy_a), | |
213 | "is equal (object B, object A"); | |
214 | ||
215 | ok(false == lttng_firing_policy_is_equal(policy_a, policy_c), | |
216 | "is equal (object A, object C)"); | |
217 | ok(false == lttng_firing_policy_is_equal(policy_c, policy_a), | |
218 | "is equal (object C, object A)"); | |
219 | ||
220 | /* Serialization and create_from buffer. */ | |
221 | ok(lttng_firing_policy_serialize(policy_a, &payload) == 0, | |
222 | "Serializing firing policy"); | |
223 | { | |
224 | struct lttng_payload_view view = | |
225 | lttng_payload_view_from_payload( | |
226 | &payload, 0, -1); | |
227 | ||
228 | ok(lttng_firing_policy_create_from_payload( | |
229 | &view, &policy_from_buffer) > 0 && | |
230 | policy_from_buffer != NULL, | |
231 | "Deserializing firing policy"); | |
232 | } | |
233 | ||
234 | ok(lttng_firing_policy_is_equal(policy_a, policy_from_buffer), | |
235 | "Original and deserialized instances are equal"); | |
236 | ||
237 | lttng_firing_policy_destroy(policy_a); | |
238 | lttng_firing_policy_destroy(policy_b); | |
239 | lttng_firing_policy_destroy(policy_c); | |
240 | lttng_firing_policy_destroy(policy_from_buffer); | |
241 | lttng_payload_reset(&payload); | |
242 | } | |
243 | ||
244 | int main(int argc, const char *argv[]) | |
245 | { | |
246 | plan_tests(NUM_TESTS); | |
247 | test_firing_policy_every_n(); | |
248 | test_firing_policy_once_after_n(); | |
249 | return exit_status(); | |
250 | } |