2 * Unit tests for the firing policy object API.
4 * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
6 * SPDX-License-Identifier: LGPL-2.1-only
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>
24 int lttng_opt_quiet
= 1;
25 int lttng_opt_verbose
;
30 static void test_firing_policy_every_n(void)
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
;
42 lttng_payload_init(&payload
);
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
);
48 "Firing policy 'every n' A created: interval: %" PRIu64
,
51 "Firing policy 'every n' B created: interval: %" PRIu64
,
54 "Firing policy 'every n' C created: interval: %" PRIu64
,
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");
62 status
= lttng_firing_policy_every_n_get_interval(NULL
, NULL
);
63 ok(status
== LTTNG_FIRING_POLICY_STATUS_INVALID
,
64 "Get interval returns INVALID");
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");
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");
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");
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");
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");
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)");
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");
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)");
113 /* Serialization and create_from buffer. */
114 ok(lttng_firing_policy_serialize(policy_a
, &payload
) == 0,
115 "Serializing firing policy");
117 struct lttng_payload_view view
=
118 lttng_payload_view_from_payload(
121 ok(lttng_firing_policy_create_from_payload(
122 &view
, &policy_from_buffer
) > 0 &&
123 policy_from_buffer
!= NULL
,
124 "Deserializing firing policy");
127 ok(lttng_firing_policy_is_equal(policy_a
, policy_from_buffer
),
128 "Original and deserialized instances are equal");
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
);
137 static void test_firing_policy_once_after_n(void)
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
;
149 lttng_payload_init(&payload
);
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
);
155 "Firing policy every n A created: threshold: %" PRIu64
,
158 "Firing policy every n B created: threshold: %" PRIu64
,
161 "Firing policy every n C created: threshold: %" PRIu64
,
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");
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");
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");
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");
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");
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");
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");
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)");
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");
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)");
220 /* Serialization and create_from buffer. */
221 ok(lttng_firing_policy_serialize(policy_a
, &payload
) == 0,
222 "Serializing firing policy");
224 struct lttng_payload_view view
=
225 lttng_payload_view_from_payload(
228 ok(lttng_firing_policy_create_from_payload(
229 &view
, &policy_from_buffer
) > 0 &&
230 policy_from_buffer
!= NULL
,
231 "Deserializing firing policy");
234 ok(lttng_firing_policy_is_equal(policy_a
, policy_from_buffer
),
235 "Original and deserialized instances are equal");
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
);
244 int main(int argc
, const char *argv
[])
246 plan_tests(NUM_TESTS
);
247 test_firing_policy_every_n();
248 test_firing_policy_once_after_n();
249 return exit_status();