2 * Unit tests for the rate policy object API.
4 * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
6 * SPDX-License-Identifier: LGPL-2.1-only
10 #include <common/payload-view.hpp>
11 #include <common/payload.hpp>
13 #include <lttng/action/rate-policy-internal.hpp>
14 #include <lttng/action/rate-policy.h>
23 int lttng_opt_quiet
= 1;
24 int lttng_opt_verbose
;
29 static void test_rate_policy_every_n(void)
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
;
41 lttng_payload_init(&payload
);
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
, "Rate policy every n A created: interval: %" PRIu64
, interval_a_b
);
47 ok(policy_b
!= NULL
, "Rate policy every n B created: interval: %" PRIu64
, interval_a_b
);
48 ok(policy_c
!= NULL
, "Rate policy every n C created: interval: %" PRIu64
, interval_c
);
50 ok(LTTNG_RATE_POLICY_TYPE_EVERY_N
== lttng_rate_policy_get_type(policy_a
),
51 "Type is LTTNG_RATE_POLICY_TYPE_EVERY_N");
54 status
= lttng_rate_policy_every_n_get_interval(NULL
, NULL
);
55 ok(status
== LTTNG_RATE_POLICY_STATUS_INVALID
, "Get interval returns INVALID");
57 status
= lttng_rate_policy_every_n_get_interval(NULL
, &interval_query
);
58 ok(status
== LTTNG_RATE_POLICY_STATUS_INVALID
, "Get interval returns INVALID");
60 status
= lttng_rate_policy_every_n_get_interval(policy_a
, NULL
);
61 ok(status
== LTTNG_RATE_POLICY_STATUS_INVALID
, "Get interval returns INVALID");
63 status
= lttng_rate_policy_every_n_get_interval(policy_a
, &interval_query
);
64 ok(status
== LTTNG_RATE_POLICY_STATUS_OK
&& interval_query
== interval_a_b
,
65 " Getting interval A");
67 status
= lttng_rate_policy_every_n_get_interval(policy_b
, &interval_query
);
68 ok(status
== LTTNG_RATE_POLICY_STATUS_OK
&& interval_query
== interval_a_b
,
69 " Getting interval B");
71 status
= lttng_rate_policy_every_n_get_interval(policy_c
, &interval_query
);
72 ok(status
== LTTNG_RATE_POLICY_STATUS_OK
&& interval_query
== interval_c
,
73 " Getting interval C");
76 /* TODO: this is the behaviour introduced by the
77 * lttng_condition_is_equal back in 2017 do we want to fix this and
78 * return true if both are NULL?
80 ok(false == lttng_rate_policy_is_equal(NULL
, NULL
), "is equal (NULL,NULL)");
81 ok(false == lttng_rate_policy_is_equal(policy_a
, NULL
), "is equal (object, NULL)");
82 ok(false == lttng_rate_policy_is_equal(NULL
, policy_a
), " is equal (NULL, object)");
83 ok(true == lttng_rate_policy_is_equal(policy_a
, policy_a
), "is equal (object A, object A)");
85 ok(true == lttng_rate_policy_is_equal(policy_a
, policy_b
), "is equal (object A, object B");
86 ok(true == lttng_rate_policy_is_equal(policy_b
, policy_a
), "is equal (object B, object A");
88 ok(false == lttng_rate_policy_is_equal(policy_a
, policy_c
),
89 "is equal (object A, object C)");
90 ok(false == lttng_rate_policy_is_equal(policy_c
, policy_a
),
91 "is equal (object C, object A)");
93 /* Serialization and create_from buffer. */
94 ok(lttng_rate_policy_serialize(policy_a
, &payload
) == 0, "Serializing");
96 struct lttng_payload_view view
= lttng_payload_view_from_payload(&payload
, 0, -1);
98 ok(lttng_rate_policy_create_from_payload(&view
, &policy_from_buffer
) > 0 &&
99 policy_from_buffer
!= NULL
,
103 ok(lttng_rate_policy_is_equal(policy_a
, policy_from_buffer
),
104 "serialized and from buffer are equal");
106 lttng_rate_policy_destroy(policy_a
);
107 lttng_rate_policy_destroy(policy_b
);
108 lttng_rate_policy_destroy(policy_c
);
109 lttng_rate_policy_destroy(policy_from_buffer
);
110 lttng_payload_reset(&payload
);
113 static void test_rate_policy_once_after_n(void)
115 enum lttng_rate_policy_status status
;
116 struct lttng_rate_policy
*policy_a
= NULL
; /* Threshold of 100. */
117 struct lttng_rate_policy
*policy_b
= NULL
; /* threshold of 100 */
118 struct lttng_rate_policy
*policy_c
= NULL
; /* threshold of 1 */
119 struct lttng_rate_policy
*policy_from_buffer
= NULL
;
120 uint64_t threshold_a_b
= 100;
121 uint64_t threshold_c
= 1;
122 uint64_t threshold_query
= 0;
123 struct lttng_payload payload
;
125 lttng_payload_init(&payload
);
127 policy_a
= lttng_rate_policy_once_after_n_create(threshold_a_b
);
128 policy_b
= lttng_rate_policy_once_after_n_create(threshold_a_b
);
129 policy_c
= lttng_rate_policy_once_after_n_create(threshold_c
);
130 ok(policy_a
!= NULL
, "Rate policy every n A created: threshold: %" PRIu64
, threshold_a_b
);
131 ok(policy_b
!= NULL
, "Rate policy every n B created: threshold: %" PRIu64
, threshold_a_b
);
132 ok(policy_c
!= NULL
, "Rate policy every n C created: threshold: %" PRIu64
, threshold_c
);
134 ok(LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
== lttng_rate_policy_get_type(policy_a
),
135 "Type is LTTNG_RATE_POLICY_TYPE_once_after_n");
138 status
= lttng_rate_policy_once_after_n_get_threshold(NULL
, NULL
);
139 ok(status
== LTTNG_RATE_POLICY_STATUS_INVALID
, "Get threshold returns INVALID");
141 status
= lttng_rate_policy_once_after_n_get_threshold(NULL
, &threshold_query
);
142 ok(status
== LTTNG_RATE_POLICY_STATUS_INVALID
, "Get threshold returns INVALID");
144 status
= lttng_rate_policy_once_after_n_get_threshold(policy_a
, NULL
);
145 ok(status
== LTTNG_RATE_POLICY_STATUS_INVALID
, "Get threshold returns INVALID");
147 status
= lttng_rate_policy_once_after_n_get_threshold(policy_a
, &threshold_query
);
148 ok(status
== LTTNG_RATE_POLICY_STATUS_OK
&& threshold_query
== threshold_a_b
,
149 " Getting threshold A");
151 status
= lttng_rate_policy_once_after_n_get_threshold(policy_b
, &threshold_query
);
152 ok(status
== LTTNG_RATE_POLICY_STATUS_OK
&& threshold_query
== threshold_a_b
,
153 " Getting threshold B");
155 status
= lttng_rate_policy_once_after_n_get_threshold(policy_c
, &threshold_query
);
156 ok(status
== LTTNG_RATE_POLICY_STATUS_OK
&& threshold_query
== threshold_c
,
157 " Getting threshold C");
160 ok(false == lttng_rate_policy_is_equal(NULL
, NULL
), "is equal (NULL,NULL)");
161 ok(false == lttng_rate_policy_is_equal(policy_a
, NULL
), "is equal (object, NULL)");
162 ok(false == lttng_rate_policy_is_equal(NULL
, policy_a
), " is equal (NULL, object)");
163 ok(true == lttng_rate_policy_is_equal(policy_a
, policy_a
), "is equal (object A, object A)");
165 ok(true == lttng_rate_policy_is_equal(policy_a
, policy_b
), "is equal (object A, object B");
166 ok(true == lttng_rate_policy_is_equal(policy_b
, policy_a
), "is equal (object B, object A");
168 ok(false == lttng_rate_policy_is_equal(policy_a
, policy_c
),
169 "is equal (object A, object C)");
170 ok(false == lttng_rate_policy_is_equal(policy_c
, policy_a
),
171 "is equal (object C, object A)");
173 /* Serialization and create_from buffer. */
174 ok(lttng_rate_policy_serialize(policy_a
, &payload
) == 0, "Serializing");
176 struct lttng_payload_view view
= lttng_payload_view_from_payload(&payload
, 0, -1);
178 ok(lttng_rate_policy_create_from_payload(&view
, &policy_from_buffer
) > 0 &&
179 policy_from_buffer
!= NULL
,
183 ok(lttng_rate_policy_is_equal(policy_a
, policy_from_buffer
),
184 "serialized and from buffer are equal");
186 lttng_rate_policy_destroy(policy_a
);
187 lttng_rate_policy_destroy(policy_b
);
188 lttng_rate_policy_destroy(policy_c
);
189 lttng_rate_policy_destroy(policy_from_buffer
);
190 lttng_payload_reset(&payload
);
195 plan_tests(NUM_TESTS
);
196 test_rate_policy_every_n();
197 test_rate_policy_once_after_n();
198 return exit_status();