Tests: fix: leak of rate policy in rate policy unit tests
[lttng-tools.git] / tests / unit / test_rate_policy.c
1 /*
2 * Unit tests for the rate 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/rate-policy-internal.h>
21 #include <lttng/action/rate-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_rate_policy_every_n(void)
31 {
32 enum lttng_rate_policy_status status;
33 struct lttng_rate_policy *policy_a = NULL; /* Interval of 100. */
34 struct lttng_rate_policy *policy_b = NULL; /* Interval of 100 */
35 struct lttng_rate_policy *policy_c = NULL; /* Interval of 1 */
36 struct lttng_rate_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_rate_policy_every_n_create(interval_a_b);
45 policy_b = lttng_rate_policy_every_n_create(interval_a_b);
46 policy_c = lttng_rate_policy_every_n_create(interval_c);
47 ok(policy_a != NULL,
48 "Rate policy every n A created: interval: %" PRIu64,
49 interval_a_b);
50 ok(policy_b != NULL,
51 "Rate policy every n B created: interval: %" PRIu64,
52 interval_a_b);
53 ok(policy_c != NULL,
54 "Rate policy every n C created: interval: %" PRIu64,
55 interval_c);
56
57 ok(LTTNG_RATE_POLICY_TYPE_EVERY_N ==
58 lttng_rate_policy_get_type(policy_a),
59 "Type is LTTNG_RATE_POLICY_TYPE_EVERY_N");
60
61 /* Getter tests */
62 status = lttng_rate_policy_every_n_get_interval(NULL, NULL);
63 ok(status == LTTNG_RATE_POLICY_STATUS_INVALID,
64 "Get interval returns INVALID");
65
66 status = lttng_rate_policy_every_n_get_interval(NULL, &interval_query);
67 ok(status == LTTNG_RATE_POLICY_STATUS_INVALID,
68 "Get interval returns INVALID");
69
70 status = lttng_rate_policy_every_n_get_interval(policy_a, NULL);
71 ok(status == LTTNG_RATE_POLICY_STATUS_INVALID,
72 "Get interval returns INVALID");
73
74 status = lttng_rate_policy_every_n_get_interval(
75 policy_a, &interval_query);
76 ok(status == LTTNG_RATE_POLICY_STATUS_OK &&
77 interval_query == interval_a_b,
78 " Getting interval A");
79
80 status = lttng_rate_policy_every_n_get_interval(
81 policy_b, &interval_query);
82 ok(status == LTTNG_RATE_POLICY_STATUS_OK &&
83 interval_query == interval_a_b,
84 " Getting interval B");
85
86 status = lttng_rate_policy_every_n_get_interval(
87 policy_c, &interval_query);
88 ok(status == LTTNG_RATE_POLICY_STATUS_OK &&
89 interval_query == interval_c,
90 " Getting interval C");
91
92 /* is_equal tests */
93 /* TODO: this is the behaviour introduced by the
94 * lttng_condition_is_equal back in 2017 do we want to fix this and
95 * return true if both are NULL?
96 */
97 ok(false == lttng_rate_policy_is_equal(NULL, NULL),
98 "is equal (NULL,NULL)");
99 ok(false == lttng_rate_policy_is_equal(policy_a, NULL),
100 "is equal (object, NULL)");
101 ok(false == lttng_rate_policy_is_equal(NULL, policy_a),
102 " is equal (NULL, object)");
103 ok(true == lttng_rate_policy_is_equal(policy_a, policy_a),
104 "is equal (object A, object A)");
105
106 ok(true == lttng_rate_policy_is_equal(policy_a, policy_b),
107 "is equal (object A, object B");
108 ok(true == lttng_rate_policy_is_equal(policy_b, policy_a),
109 "is equal (object B, object A");
110
111 ok(false == lttng_rate_policy_is_equal(policy_a, policy_c),
112 "is equal (object A, object C)");
113 ok(false == lttng_rate_policy_is_equal(policy_c, policy_a),
114 "is equal (object C, object A)");
115
116 /* Serialization and create_from buffer. */
117 ok(lttng_rate_policy_serialize(policy_a, &payload) == 0, "Serializing");
118 {
119 struct lttng_payload_view view =
120 lttng_payload_view_from_payload(
121 &payload, 0, -1);
122
123 ok(lttng_rate_policy_create_from_payload(
124 &view, &policy_from_buffer) > 0 &&
125 policy_from_buffer != NULL,
126 "Deserializing");
127 }
128
129 ok(lttng_rate_policy_is_equal(policy_a, policy_from_buffer),
130 "serialized and from buffer are equal");
131
132 lttng_rate_policy_destroy(policy_a);
133 lttng_rate_policy_destroy(policy_b);
134 lttng_rate_policy_destroy(policy_c);
135 lttng_rate_policy_destroy(policy_from_buffer);
136 lttng_payload_reset(&payload);
137 }
138
139 static void test_rate_policy_once_after_n(void)
140 {
141 enum lttng_rate_policy_status status;
142 struct lttng_rate_policy *policy_a = NULL; /* Threshold of 100. */
143 struct lttng_rate_policy *policy_b = NULL; /* threshold of 100 */
144 struct lttng_rate_policy *policy_c = NULL; /* threshold of 1 */
145 struct lttng_rate_policy *policy_from_buffer = NULL;
146 uint64_t threshold_a_b = 100;
147 uint64_t threshold_c = 1;
148 uint64_t threshold_query = 0;
149 struct lttng_payload payload;
150
151 lttng_payload_init(&payload);
152
153 policy_a = lttng_rate_policy_once_after_n_create(threshold_a_b);
154 policy_b = lttng_rate_policy_once_after_n_create(threshold_a_b);
155 policy_c = lttng_rate_policy_once_after_n_create(threshold_c);
156 ok(policy_a != NULL,
157 "Rate policy every n A created: threshold: %" PRIu64,
158 threshold_a_b);
159 ok(policy_b != NULL,
160 "Rate policy every n B created: threshold: %" PRIu64,
161 threshold_a_b);
162 ok(policy_c != NULL,
163 "Rate policy every n C created: threshold: %" PRIu64,
164 threshold_c);
165
166 ok(LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N ==
167 lttng_rate_policy_get_type(policy_a),
168 "Type is LTTNG_RATE_POLICY_TYPE_once_after_n");
169
170 /* Getter tests */
171 status = lttng_rate_policy_once_after_n_get_threshold(NULL, NULL);
172 ok(status == LTTNG_RATE_POLICY_STATUS_INVALID,
173 "Get threshold returns INVALID");
174
175 status = lttng_rate_policy_once_after_n_get_threshold(
176 NULL, &threshold_query);
177 ok(status == LTTNG_RATE_POLICY_STATUS_INVALID,
178 "Get threshold returns INVALID");
179
180 status = lttng_rate_policy_once_after_n_get_threshold(policy_a, NULL);
181 ok(status == LTTNG_RATE_POLICY_STATUS_INVALID,
182 "Get threshold returns INVALID");
183
184 status = lttng_rate_policy_once_after_n_get_threshold(
185 policy_a, &threshold_query);
186 ok(status == LTTNG_RATE_POLICY_STATUS_OK &&
187 threshold_query == threshold_a_b,
188 " Getting threshold A");
189
190 status = lttng_rate_policy_once_after_n_get_threshold(
191 policy_b, &threshold_query);
192 ok(status == LTTNG_RATE_POLICY_STATUS_OK &&
193 threshold_query == threshold_a_b,
194 " Getting threshold B");
195
196 status = lttng_rate_policy_once_after_n_get_threshold(
197 policy_c, &threshold_query);
198 ok(status == LTTNG_RATE_POLICY_STATUS_OK &&
199 threshold_query == threshold_c,
200 " Getting threshold C");
201
202 /* is_equal tests */
203 /* TODO: this is the behaviour introduced by the
204 * lttng_condition_is_equal back in 2017 do we want to fix this and
205 * return true if both are NULL?
206 */
207 ok(false == lttng_rate_policy_is_equal(NULL, NULL),
208 "is equal (NULL,NULL)");
209 ok(false == lttng_rate_policy_is_equal(policy_a, NULL),
210 "is equal (object, NULL)");
211 ok(false == lttng_rate_policy_is_equal(NULL, policy_a),
212 " is equal (NULL, object)");
213 ok(true == lttng_rate_policy_is_equal(policy_a, policy_a),
214 "is equal (object A, object A)");
215
216 ok(true == lttng_rate_policy_is_equal(policy_a, policy_b),
217 "is equal (object A, object B");
218 ok(true == lttng_rate_policy_is_equal(policy_b, policy_a),
219 "is equal (object B, object A");
220
221 ok(false == lttng_rate_policy_is_equal(policy_a, policy_c),
222 "is equal (object A, object C)");
223 ok(false == lttng_rate_policy_is_equal(policy_c, policy_a),
224 "is equal (object C, object A)");
225
226 /* Serialization and create_from buffer. */
227 ok(lttng_rate_policy_serialize(policy_a, &payload) == 0, "Serializing");
228 {
229 struct lttng_payload_view view =
230 lttng_payload_view_from_payload(
231 &payload, 0, -1);
232
233 ok(lttng_rate_policy_create_from_payload(
234 &view, &policy_from_buffer) > 0 &&
235 policy_from_buffer != NULL,
236 "Deserializing");
237 }
238
239 ok(lttng_rate_policy_is_equal(policy_a, policy_from_buffer),
240 "serialized and from buffer are equal");
241
242 lttng_rate_policy_destroy(policy_a);
243 lttng_rate_policy_destroy(policy_b);
244 lttng_rate_policy_destroy(policy_c);
245 lttng_rate_policy_destroy(policy_from_buffer);
246 lttng_payload_reset(&payload);
247 }
248
249 int main(int argc, const char *argv[])
250 {
251 plan_tests(NUM_TESTS);
252 test_rate_policy_every_n();
253 test_rate_policy_once_after_n();
254 return exit_status();
255 }
This page took 0.033982 seconds and 4 git commands to generate.