Commit | Line | Data |
---|---|---|
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 | ||
c9e313bc SM |
10 | #include <common/payload-view.hpp> |
11 | #include <common/payload.hpp> | |
28ab034a | 12 | |
c9e313bc | 13 | #include <lttng/action/rate-policy-internal.hpp> |
7f4d5b07 JR |
14 | #include <lttng/action/rate-policy.h> |
15 | ||
28ab034a JG |
16 | #include <inttypes.h> |
17 | #include <stdio.h> | |
18 | #include <string.h> | |
19 | #include <tap/tap.h> | |
20 | #include <unistd.h> | |
21 | ||
7f4d5b07 JR |
22 | /* For error.h. */ |
23 | int lttng_opt_quiet = 1; | |
24 | int lttng_opt_verbose; | |
25 | int lttng_opt_mi; | |
26 | ||
27 | #define NUM_TESTS 42 | |
28 | ||
cd9adb8b | 29 | static void test_rate_policy_every_n() |
7f4d5b07 JR |
30 | { |
31 | enum lttng_rate_policy_status status; | |
cd9adb8b JG |
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; | |
7f4d5b07 JR |
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); | |
cd9adb8b JG |
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); | |
28ab034a JG |
49 | |
50 | ok(LTTNG_RATE_POLICY_TYPE_EVERY_N == lttng_rate_policy_get_type(policy_a), | |
51 | "Type is LTTNG_RATE_POLICY_TYPE_EVERY_N"); | |
7f4d5b07 JR |
52 | |
53 | /* Getter tests */ | |
cd9adb8b | 54 | status = lttng_rate_policy_every_n_get_interval(nullptr, nullptr); |
28ab034a | 55 | ok(status == LTTNG_RATE_POLICY_STATUS_INVALID, "Get interval returns INVALID"); |
7f4d5b07 | 56 | |
cd9adb8b | 57 | status = lttng_rate_policy_every_n_get_interval(nullptr, &interval_query); |
28ab034a | 58 | ok(status == LTTNG_RATE_POLICY_STATUS_INVALID, "Get interval returns INVALID"); |
7f4d5b07 | 59 | |
cd9adb8b | 60 | status = lttng_rate_policy_every_n_get_interval(policy_a, nullptr); |
28ab034a JG |
61 | ok(status == LTTNG_RATE_POLICY_STATUS_INVALID, "Get interval returns INVALID"); |
62 | ||
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"); | |
66 | ||
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"); | |
70 | ||
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"); | |
7f4d5b07 JR |
74 | |
75 | /* is_equal tests */ | |
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? | |
79 | */ | |
cd9adb8b JG |
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)"); | |
28ab034a JG |
83 | ok(true == lttng_rate_policy_is_equal(policy_a, policy_a), "is equal (object A, object A)"); |
84 | ||
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"); | |
7f4d5b07 JR |
87 | |
88 | ok(false == lttng_rate_policy_is_equal(policy_a, policy_c), | |
28ab034a | 89 | "is equal (object A, object C)"); |
7f4d5b07 | 90 | ok(false == lttng_rate_policy_is_equal(policy_c, policy_a), |
28ab034a | 91 | "is equal (object C, object A)"); |
7f4d5b07 JR |
92 | |
93 | /* Serialization and create_from buffer. */ | |
94 | ok(lttng_rate_policy_serialize(policy_a, &payload) == 0, "Serializing"); | |
95 | { | |
28ab034a JG |
96 | struct lttng_payload_view view = lttng_payload_view_from_payload(&payload, 0, -1); |
97 | ||
98 | ok(lttng_rate_policy_create_from_payload(&view, &policy_from_buffer) > 0 && | |
cd9adb8b | 99 | policy_from_buffer != nullptr, |
28ab034a | 100 | "Deserializing"); |
7f4d5b07 JR |
101 | } |
102 | ||
103 | ok(lttng_rate_policy_is_equal(policy_a, policy_from_buffer), | |
28ab034a | 104 | "serialized and from buffer are equal"); |
7f4d5b07 JR |
105 | |
106 | lttng_rate_policy_destroy(policy_a); | |
107 | lttng_rate_policy_destroy(policy_b); | |
108 | lttng_rate_policy_destroy(policy_c); | |
1f38e81c | 109 | lttng_rate_policy_destroy(policy_from_buffer); |
7f4d5b07 JR |
110 | lttng_payload_reset(&payload); |
111 | } | |
112 | ||
cd9adb8b | 113 | static void test_rate_policy_once_after_n() |
7f4d5b07 JR |
114 | { |
115 | enum lttng_rate_policy_status status; | |
cd9adb8b JG |
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; | |
7f4d5b07 JR |
120 | uint64_t threshold_a_b = 100; |
121 | uint64_t threshold_c = 1; | |
122 | uint64_t threshold_query = 0; | |
123 | struct lttng_payload payload; | |
124 | ||
125 | lttng_payload_init(&payload); | |
126 | ||
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); | |
cd9adb8b JG |
130 | ok(policy_a != nullptr, |
131 | "Rate policy every n A created: threshold: %" PRIu64, | |
132 | threshold_a_b); | |
133 | ok(policy_b != nullptr, | |
134 | "Rate policy every n B created: threshold: %" PRIu64, | |
135 | threshold_a_b); | |
136 | ok(policy_c != nullptr, "Rate policy every n C created: threshold: %" PRIu64, threshold_c); | |
28ab034a JG |
137 | |
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"); | |
7f4d5b07 JR |
140 | |
141 | /* Getter tests */ | |
cd9adb8b | 142 | status = lttng_rate_policy_once_after_n_get_threshold(nullptr, nullptr); |
28ab034a | 143 | ok(status == LTTNG_RATE_POLICY_STATUS_INVALID, "Get threshold returns INVALID"); |
7f4d5b07 | 144 | |
cd9adb8b | 145 | status = lttng_rate_policy_once_after_n_get_threshold(nullptr, &threshold_query); |
28ab034a | 146 | ok(status == LTTNG_RATE_POLICY_STATUS_INVALID, "Get threshold returns INVALID"); |
7f4d5b07 | 147 | |
cd9adb8b | 148 | status = lttng_rate_policy_once_after_n_get_threshold(policy_a, nullptr); |
28ab034a JG |
149 | ok(status == LTTNG_RATE_POLICY_STATUS_INVALID, "Get threshold returns INVALID"); |
150 | ||
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"); | |
154 | ||
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"); | |
158 | ||
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"); | |
7f4d5b07 JR |
162 | |
163 | /* is_equal tests */ | |
cd9adb8b JG |
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)"); | |
28ab034a JG |
167 | ok(true == lttng_rate_policy_is_equal(policy_a, policy_a), "is equal (object A, object A)"); |
168 | ||
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"); | |
7f4d5b07 JR |
171 | |
172 | ok(false == lttng_rate_policy_is_equal(policy_a, policy_c), | |
28ab034a | 173 | "is equal (object A, object C)"); |
7f4d5b07 | 174 | ok(false == lttng_rate_policy_is_equal(policy_c, policy_a), |
28ab034a | 175 | "is equal (object C, object A)"); |
7f4d5b07 JR |
176 | |
177 | /* Serialization and create_from buffer. */ | |
178 | ok(lttng_rate_policy_serialize(policy_a, &payload) == 0, "Serializing"); | |
179 | { | |
28ab034a JG |
180 | struct lttng_payload_view view = lttng_payload_view_from_payload(&payload, 0, -1); |
181 | ||
182 | ok(lttng_rate_policy_create_from_payload(&view, &policy_from_buffer) > 0 && | |
cd9adb8b | 183 | policy_from_buffer != nullptr, |
28ab034a | 184 | "Deserializing"); |
7f4d5b07 JR |
185 | } |
186 | ||
187 | ok(lttng_rate_policy_is_equal(policy_a, policy_from_buffer), | |
28ab034a | 188 | "serialized and from buffer are equal"); |
7f4d5b07 JR |
189 | |
190 | lttng_rate_policy_destroy(policy_a); | |
191 | lttng_rate_policy_destroy(policy_b); | |
192 | lttng_rate_policy_destroy(policy_c); | |
1f38e81c | 193 | lttng_rate_policy_destroy(policy_from_buffer); |
7f4d5b07 JR |
194 | lttng_payload_reset(&payload); |
195 | } | |
196 | ||
cd9adb8b | 197 | int main() |
7f4d5b07 JR |
198 | { |
199 | plan_tests(NUM_TESTS); | |
200 | test_rate_policy_every_n(); | |
201 | test_rate_policy_once_after_n(); | |
202 | return exit_status(); | |
203 | } |