docs: Add supported versions and fix-backport policy
[lttng-tools.git] / tests / unit / test_notification.cpp
CommitLineData
572cb877
JR
1/*
2 * test_notification.c
3 *
4 * Unit tests for the notification API.
5 *
6 * Copyright (C) 2017 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
7 *
9d16b343 8 * SPDX-License-Identifier: MIT
572cb877 9 *
572cb877
JR
10 */
11
28ab034a 12#include <common/macros.hpp>
572cb877
JR
13
14#include <lttng/action/action.h>
15#include <lttng/action/notify.h>
16#include <lttng/condition/buffer-usage.h>
17#include <lttng/condition/condition.h>
18#include <lttng/domain.h>
19#include <lttng/notification/notification.h>
20#include <lttng/trigger/trigger.h>
21
28ab034a
JG
22#include <inttypes.h>
23#include <stdio.h>
24#include <string.h>
25#include <tap/tap.h>
26#include <unistd.h>
a0377dfe 27
572cb877
JR
28/* For error.h */
29int lttng_opt_quiet = 1;
30int lttng_opt_verbose;
31int lttng_opt_mi;
32
9cff59ec 33#define NUM_TESTS 180
572cb877 34
28ab034a 35static void test_condition_buffer_usage(struct lttng_condition *buffer_usage_condition)
572cb877
JR
36{
37 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
cd9adb8b
JG
38 const char *session_name = nullptr;
39 const char *channel_name = nullptr;
572cb877
JR
40 enum lttng_domain_type domain_type;
41 /* Start at a non zero value to validate initialization */
42 double threshold_ratio;
43 uint64_t threshold_bytes;
44
a0377dfe 45 LTTNG_ASSERT(buffer_usage_condition);
572cb877
JR
46
47 diag("Validating initialization");
28ab034a
JG
48 status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition,
49 &threshold_ratio);
572cb877
JR
50 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold ratio is unset");
51
28ab034a
JG
52 status = lttng_condition_buffer_usage_get_threshold(buffer_usage_condition,
53 &threshold_bytes);
572cb877
JR
54 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold byte is unset");
55
28ab034a
JG
56 status = lttng_condition_buffer_usage_get_session_name(buffer_usage_condition,
57 &session_name);
572cb877
JR
58 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Session name is unset");
59 ok(!session_name, "Session name is null");
60
28ab034a
JG
61 status = lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition,
62 &channel_name);
572cb877
JR
63 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Channel name is unset");
64 ok(!session_name, "Channel name is null");
65
66 status = lttng_condition_buffer_usage_get_domain_type(buffer_usage_condition, &domain_type);
67 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Domain name is unset");
68
69 diag("Testing session name set/get");
cd9adb8b 70 status = lttng_condition_buffer_usage_set_session_name(nullptr, "Test");
572cb877 71 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set null condition on set session name");
cd9adb8b 72 status = lttng_condition_buffer_usage_get_session_name(nullptr, &session_name);
572cb877
JR
73 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Get session name with null condition");
74 ok(!session_name, "Session name is null");
28ab034a
JG
75 status = lttng_condition_buffer_usage_get_session_name(buffer_usage_condition,
76 &session_name);
572cb877
JR
77 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Session name is unset");
78 ok(!session_name, "Session name is null");
79
cd9adb8b 80 status = lttng_condition_buffer_usage_set_session_name(buffer_usage_condition, nullptr);
572cb877 81 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set null session name");
28ab034a
JG
82 status = lttng_condition_buffer_usage_get_session_name(buffer_usage_condition,
83 &session_name);
572cb877
JR
84 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Session name is unset");
85 ok(!session_name, "Session name is null");
86
87 status = lttng_condition_buffer_usage_set_session_name(buffer_usage_condition, "");
88 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set empty session name");
28ab034a
JG
89 status = lttng_condition_buffer_usage_get_session_name(buffer_usage_condition,
90 &session_name);
572cb877
JR
91 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Session name is unset");
92 ok(!session_name, "Session name is null");
93
28ab034a
JG
94 status =
95 lttng_condition_buffer_usage_set_session_name(buffer_usage_condition, "session420");
572cb877 96 ok(status == LTTNG_CONDITION_STATUS_OK, "Set session name session420");
28ab034a
JG
97 status = lttng_condition_buffer_usage_get_session_name(buffer_usage_condition,
98 &session_name);
572cb877
JR
99 ok(status == LTTNG_CONDITION_STATUS_OK, "Session name is set");
100 ok(session_name, "Session name has a value");
101 ok(strcmp("session420", session_name) == 0, "Session name is %s", "session420");
102
103 /*
104 * Test second set on session_name. Test invalid set and validate that
105 * the value is still the previous good one.
106 */
107
108 status = lttng_condition_buffer_usage_set_session_name(buffer_usage_condition, "");
109 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set session name to empty");
28ab034a
JG
110 status = lttng_condition_buffer_usage_get_session_name(buffer_usage_condition,
111 &session_name);
572cb877
JR
112 ok(status == LTTNG_CONDITION_STATUS_OK, "Session name is still set");
113 ok(session_name, "Session name has a value");
114 ok(strcmp("session420", session_name) == 0, "Session is still name is %s", "session420");
115
116 diag("Testing channel name set/get");
cd9adb8b 117 status = lttng_condition_buffer_usage_set_channel_name(nullptr, "Test");
572cb877 118 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set null condition on set channel name");
cd9adb8b 119 status = lttng_condition_buffer_usage_get_channel_name(nullptr, &channel_name);
572cb877 120 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Get channel name with null condition");
28ab034a
JG
121 status = lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition,
122 &channel_name);
572cb877
JR
123 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Channel name is unset");
124 ok(!channel_name, "Channel name is null");
125
cd9adb8b 126 status = lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition, nullptr);
572cb877 127 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set null channel name");
28ab034a
JG
128 status = lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition,
129 &channel_name);
572cb877
JR
130 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Channel name is unset");
131 ok(!channel_name, "Channel name is null");
132
133 status = lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition, "");
134 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set empty channel name");
28ab034a
JG
135 status = lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition,
136 &channel_name);
572cb877
JR
137 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Channel name is unset");
138 ok(!channel_name, "Channel name is null");
139
28ab034a
JG
140 status =
141 lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition, "channel420");
572cb877 142 ok(status == LTTNG_CONDITION_STATUS_OK, "Set channel name channel420");
28ab034a
JG
143 status = lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition,
144 &channel_name);
572cb877
JR
145 ok(status == LTTNG_CONDITION_STATUS_OK, "Channel name is set");
146 ok(channel_name, "Channel name has a value");
147 ok(strcmp("channel420", channel_name) == 0, "Channel name is %s", "channel420");
148
149 /*
150 * Test second set on channel_name. Test invalid set and validate that
151 * the value is still the previous good one.
152 */
153
154 status = lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition, "");
155 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set channel name to empty");
28ab034a
JG
156 status = lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition,
157 &channel_name);
572cb877
JR
158 ok(status == LTTNG_CONDITION_STATUS_OK, "Channel name is still set");
159 ok(channel_name, "Channel name has a value");
160 ok(strcmp("channel420", channel_name) == 0, "Channel is still name is %s", "channel420");
161
162 diag("Testing threshold ratio set/get");
cd9adb8b 163 status = lttng_condition_buffer_usage_set_threshold_ratio(nullptr, 0.420);
572cb877 164 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set threshold ratio with null condition");
cd9adb8b 165 status = lttng_condition_buffer_usage_get_threshold_ratio(nullptr, &threshold_ratio);
572cb877 166 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Get threshold ratio with null condition");
28ab034a
JG
167 status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition,
168 &threshold_ratio);
572cb877
JR
169 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold ratio is unset");
170
171 status = lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition, -100.0);
172 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set threshold ratio < 0");
28ab034a
JG
173 status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition,
174 &threshold_ratio);
572cb877
JR
175 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold ratio is unset");
176
177 status = lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition, 200.0);
178 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set Threshold ratio > 1");
28ab034a
JG
179 status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition,
180 &threshold_ratio);
572cb877
JR
181 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold ratio is unset");
182
183 status = lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition, 1.0);
184 ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold ratio == 1.0");
28ab034a
JG
185 status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition,
186 &threshold_ratio);
572cb877
JR
187 ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold ratio is set");
188 ok(threshold_ratio == 1.0, "Threshold ratio is 1.0");
189
190 status = lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition, 0.0);
191 ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold ratio == 0.0");
28ab034a
JG
192 status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition,
193 &threshold_ratio);
572cb877
JR
194 ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold ratio is set");
195 ok(threshold_ratio == 0.0, "Threshold ratio is 0.0");
196
197 status = lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition, 0.420);
198 ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold ratio == 0.420");
28ab034a
JG
199 status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition,
200 &threshold_ratio);
572cb877
JR
201 ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold ratio is set");
202 ok(threshold_ratio == 0.420, "Threshold ratio is 0.420");
203
204 diag("Testing threshold bytes set/get");
cd9adb8b 205 status = lttng_condition_buffer_usage_set_threshold(nullptr, 100000);
572cb877 206 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set threshold with null condition");
cd9adb8b 207 status = lttng_condition_buffer_usage_get_threshold(nullptr, &threshold_bytes);
572cb877 208 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Get threshold value with null condition ");
28ab034a
JG
209 status = lttng_condition_buffer_usage_get_threshold(buffer_usage_condition,
210 &threshold_bytes);
572cb877
JR
211 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold is unset");
212
213 status = lttng_condition_buffer_usage_set_threshold(buffer_usage_condition, 100000);
214 ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold > 0");
28ab034a
JG
215 status = lttng_condition_buffer_usage_get_threshold(buffer_usage_condition,
216 &threshold_bytes);
572cb877 217 ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold is set");
9f4a25d3 218 ok(threshold_bytes == 100000, "Threshold is 100000");
572cb877
JR
219
220 status = lttng_condition_buffer_usage_set_threshold(buffer_usage_condition, UINT64_MAX);
221 ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold UINT64_MAX");
28ab034a
JG
222 status = lttng_condition_buffer_usage_get_threshold(buffer_usage_condition,
223 &threshold_bytes);
572cb877
JR
224 ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold is set");
225 ok(threshold_bytes == UINT64_MAX, "Threshold is UINT64_MAX");
226
227 status = lttng_condition_buffer_usage_set_threshold(buffer_usage_condition, 0);
228 ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold == 0");
28ab034a
JG
229 status = lttng_condition_buffer_usage_get_threshold(buffer_usage_condition,
230 &threshold_bytes);
572cb877
JR
231 ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold is set");
232 ok(threshold_bytes == 0, "Threshold is %d", 0);
233
234 /*
235 * Test value of threshold ration, since we overwrote it with a byte
236 * threshold. Make sure it gets squashed.
237 */
238 diag("Testing interaction between byte and ratio thresholds");
239
240 threshold_ratio = -1.0;
28ab034a
JG
241 status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition,
242 &threshold_ratio);
572cb877
JR
243 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold ratio is unset");
244 ok(threshold_ratio == -1.0, "Threshold ratio is untouched");
245
246 /* Set a ratio to validate that the byte threshold is now unset */
247 status = lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition, 0.420);
248 ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold ratio == 0.420");
28ab034a
JG
249 status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition,
250 &threshold_ratio);
572cb877
JR
251 ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold ratio is set");
252 ok(threshold_ratio == 0.420, "Threshold ratio is 0.420");
253
254 threshold_bytes = 420;
28ab034a
JG
255 status = lttng_condition_buffer_usage_get_threshold(buffer_usage_condition,
256 &threshold_bytes);
572cb877
JR
257 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold is unset");
258 ok(threshold_bytes == 420, "Threshold is untouched");
259
260 diag("Testing domain type set/get");
cd9adb8b 261 status = lttng_condition_buffer_usage_set_domain_type(nullptr, LTTNG_DOMAIN_UST);
572cb877 262 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set domain type with null condition");
cd9adb8b 263 status = lttng_condition_buffer_usage_get_domain_type(nullptr, &domain_type);
572cb877
JR
264 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Get domain type with null condition");
265
28ab034a
JG
266 status = lttng_condition_buffer_usage_set_domain_type(buffer_usage_condition,
267 LTTNG_DOMAIN_NONE);
572cb877
JR
268 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set domain type as LTTNG_DOMAIN_NONE");
269 status = lttng_condition_buffer_usage_get_domain_type(buffer_usage_condition, &domain_type);
270 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Domain type is unset");
271
28ab034a
JG
272 status = lttng_condition_buffer_usage_set_domain_type(buffer_usage_condition,
273 LTTNG_DOMAIN_UST);
572cb877
JR
274 ok(status == LTTNG_CONDITION_STATUS_OK, "Set domain type as LTTNG_DOMAIN_UST");
275 status = lttng_condition_buffer_usage_get_domain_type(buffer_usage_condition, &domain_type);
276 ok(status == LTTNG_CONDITION_STATUS_OK, "Domain type is set");
277 ok(domain_type == LTTNG_DOMAIN_UST, "Domain type is LTTNG_DOMAIN_UST");
278}
279
cd9adb8b 280static void test_condition_buffer_usage_low()
572cb877 281{
cd9adb8b 282 struct lttng_condition *buffer_usage_low = nullptr;
572cb877
JR
283
284 diag("Testing lttng_condition_buffer_usage_low_create");
285 buffer_usage_low = lttng_condition_buffer_usage_low_create();
286 ok(buffer_usage_low, "Condition allocated");
287
28ab034a
JG
288 ok(lttng_condition_get_type(buffer_usage_low) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW,
289 "Condition is of type \"low buffer usage\"");
572cb877
JR
290
291 test_condition_buffer_usage(buffer_usage_low);
292
293 lttng_condition_destroy(buffer_usage_low);
294}
295
cd9adb8b 296static void test_condition_buffer_usage_high()
572cb877 297{
cd9adb8b 298 struct lttng_condition *buffer_usage_high = nullptr;
572cb877
JR
299
300 diag("Testing lttng_condition_buffer_usage_high_create");
301 buffer_usage_high = lttng_condition_buffer_usage_high_create();
302 ok(buffer_usage_high, "High buffer usage condition allocated");
303
28ab034a
JG
304 ok(lttng_condition_get_type(buffer_usage_high) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH,
305 "Condition is of type \"high buffer usage\"");
572cb877
JR
306
307 test_condition_buffer_usage(buffer_usage_high);
308
309 lttng_condition_destroy(buffer_usage_high);
310}
311
cd9adb8b 312static void test_trigger()
572cb877 313{
cd9adb8b
JG
314 struct lttng_action *notify_action = nullptr;
315 struct lttng_condition *buffer_usage_high = nullptr;
316 struct lttng_trigger *trigger = nullptr;
572cb877
JR
317
318 notify_action = lttng_action_notify_create();
319 buffer_usage_high = lttng_condition_buffer_usage_high_create();
320
cd9adb8b 321 trigger = lttng_trigger_create(nullptr, nullptr);
572cb877 322 ok(!trigger, "lttng_trigger_create(NULL, NULL) returns null");
cd9adb8b 323 trigger = lttng_trigger_create(buffer_usage_high, nullptr);
572cb877 324 ok(!trigger, "lttng_trigger_create(NON-NULL, NULL) returns null");
cd9adb8b 325 trigger = lttng_trigger_create(nullptr, notify_action);
572cb877
JR
326 ok(!trigger, "lttng_trigger_create(NULL, NON-NULL) returns null");
327
328 trigger = lttng_trigger_create(buffer_usage_high, notify_action);
329 ok(trigger, "lttng_trigger_create(NON-NULL, NON-NULL) returns an object");
330
331 lttng_action_destroy(notify_action);
332 lttng_condition_destroy(buffer_usage_high);
333 lttng_trigger_destroy(trigger);
334}
335
cd9adb8b 336int main()
572cb877
JR
337{
338 plan_tests(NUM_TESTS);
339 test_condition_buffer_usage_low();
340 test_condition_buffer_usage_high();
572cb877
JR
341 test_trigger();
342 return exit_status();
343}
This page took 0.063178 seconds and 4 git commands to generate.