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()
31 enum lttng_rate_policy_status status
;
32 struct lttng_rate_policy
*policy_a
= nullptr; /* Interval of 100. */
33 struct lttng_rate_policy
*policy_b
= nullptr; /* Interval of 100 */
34 struct lttng_rate_policy
*policy_c
= nullptr; /* Interval of 1 */
35 struct lttng_rate_policy
*policy_from_buffer
= nullptr;
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
!= nullptr, "Rate policy every n A created: interval: %" PRIu64
, interval_a_b
);
47 ok(policy_b
!= nullptr, "Rate policy every n B created: interval: %" PRIu64
, interval_a_b
);
48 ok(policy_c
!= nullptr, "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(nullptr, nullptr);
55 ok(status
== LTTNG_RATE_POLICY_STATUS_INVALID
, "Get interval returns INVALID");
57 status
= lttng_rate_policy_every_n_get_interval(nullptr, &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
, nullptr);
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(nullptr, nullptr), "is equal (NULL,NULL)");
81 ok(false == lttng_rate_policy_is_equal(policy_a
, nullptr), "is equal (object, NULL)");
82 ok(false == lttng_rate_policy_is_equal(nullptr, 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
!= nullptr,
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()
115 enum lttng_rate_policy_status status
;
116 struct lttng_rate_policy
*policy_a
= nullptr; /* Threshold of 100. */
117 struct lttng_rate_policy
*policy_b
= nullptr; /* threshold of 100 */
118 struct lttng_rate_policy
*policy_c
= nullptr; /* threshold of 1 */
119 struct lttng_rate_policy
*policy_from_buffer
= nullptr;
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
!= nullptr,
131 "Rate policy every n A created: threshold: %" PRIu64
,
133 ok(policy_b
!= nullptr,
134 "Rate policy every n B created: threshold: %" PRIu64
,
136 ok(policy_c
!= nullptr, "Rate policy every n C created: threshold: %" PRIu64
, threshold_c
);
138 ok(LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
== lttng_rate_policy_get_type(policy_a
),
139 "Type is LTTNG_RATE_POLICY_TYPE_once_after_n");
142 status
= lttng_rate_policy_once_after_n_get_threshold(nullptr, nullptr);
143 ok(status
== LTTNG_RATE_POLICY_STATUS_INVALID
, "Get threshold returns INVALID");
145 status
= lttng_rate_policy_once_after_n_get_threshold(nullptr, &threshold_query
);
146 ok(status
== LTTNG_RATE_POLICY_STATUS_INVALID
, "Get threshold returns INVALID");
148 status
= lttng_rate_policy_once_after_n_get_threshold(policy_a
, nullptr);
149 ok(status
== LTTNG_RATE_POLICY_STATUS_INVALID
, "Get threshold returns INVALID");
151 status
= lttng_rate_policy_once_after_n_get_threshold(policy_a
, &threshold_query
);
152 ok(status
== LTTNG_RATE_POLICY_STATUS_OK
&& threshold_query
== threshold_a_b
,
153 " Getting threshold A");
155 status
= lttng_rate_policy_once_after_n_get_threshold(policy_b
, &threshold_query
);
156 ok(status
== LTTNG_RATE_POLICY_STATUS_OK
&& threshold_query
== threshold_a_b
,
157 " Getting threshold B");
159 status
= lttng_rate_policy_once_after_n_get_threshold(policy_c
, &threshold_query
);
160 ok(status
== LTTNG_RATE_POLICY_STATUS_OK
&& threshold_query
== threshold_c
,
161 " Getting threshold C");
164 ok(false == lttng_rate_policy_is_equal(nullptr, nullptr), "is equal (NULL,NULL)");
165 ok(false == lttng_rate_policy_is_equal(policy_a
, nullptr), "is equal (object, NULL)");
166 ok(false == lttng_rate_policy_is_equal(nullptr, policy_a
), " is equal (NULL, object)");
167 ok(true == lttng_rate_policy_is_equal(policy_a
, policy_a
), "is equal (object A, object A)");
169 ok(true == lttng_rate_policy_is_equal(policy_a
, policy_b
), "is equal (object A, object B");
170 ok(true == lttng_rate_policy_is_equal(policy_b
, policy_a
), "is equal (object B, object A");
172 ok(false == lttng_rate_policy_is_equal(policy_a
, policy_c
),
173 "is equal (object A, object C)");
174 ok(false == lttng_rate_policy_is_equal(policy_c
, policy_a
),
175 "is equal (object C, object A)");
177 /* Serialization and create_from buffer. */
178 ok(lttng_rate_policy_serialize(policy_a
, &payload
) == 0, "Serializing");
180 struct lttng_payload_view view
= lttng_payload_view_from_payload(&payload
, 0, -1);
182 ok(lttng_rate_policy_create_from_payload(&view
, &policy_from_buffer
) > 0 &&
183 policy_from_buffer
!= nullptr,
187 ok(lttng_rate_policy_is_equal(policy_a
, policy_from_buffer
),
188 "serialized and from buffer are equal");
190 lttng_rate_policy_destroy(policy_a
);
191 lttng_rate_policy_destroy(policy_b
);
192 lttng_rate_policy_destroy(policy_c
);
193 lttng_rate_policy_destroy(policy_from_buffer
);
194 lttng_payload_reset(&payload
);
199 plan_tests(NUM_TESTS
);
200 test_rate_policy_every_n();
201 test_rate_policy_once_after_n();
202 return exit_status();