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