4 * Unit tests for the notification API.
6 * Copyright (C) 2017 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
8 * SPDX-License-Identifier: MIT
12 #include <common/macros.hpp>
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>
29 int lttng_opt_quiet
= 1;
30 int lttng_opt_verbose
;
35 static void test_condition_buffer_usage(struct lttng_condition
*buffer_usage_condition
)
37 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
38 const char *session_name
= nullptr;
39 const char *channel_name
= nullptr;
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
;
45 LTTNG_ASSERT(buffer_usage_condition
);
47 diag("Validating initialization");
48 status
= lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition
,
50 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Threshold ratio is unset");
52 status
= lttng_condition_buffer_usage_get_threshold(buffer_usage_condition
,
54 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Threshold byte is unset");
56 status
= lttng_condition_buffer_usage_get_session_name(buffer_usage_condition
,
58 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Session name is unset");
59 ok(!session_name
, "Session name is null");
61 status
= lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition
,
63 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Channel name is unset");
64 ok(!session_name
, "Channel name is null");
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");
69 diag("Testing session name set/get");
70 status
= lttng_condition_buffer_usage_set_session_name(nullptr, "Test");
71 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set null condition on set session name");
72 status
= lttng_condition_buffer_usage_get_session_name(nullptr, &session_name
);
73 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Get session name with null condition");
74 ok(!session_name
, "Session name is null");
75 status
= lttng_condition_buffer_usage_get_session_name(buffer_usage_condition
,
77 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Session name is unset");
78 ok(!session_name
, "Session name is null");
80 status
= lttng_condition_buffer_usage_set_session_name(buffer_usage_condition
, nullptr);
81 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set null session name");
82 status
= lttng_condition_buffer_usage_get_session_name(buffer_usage_condition
,
84 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Session name is unset");
85 ok(!session_name
, "Session name is null");
87 status
= lttng_condition_buffer_usage_set_session_name(buffer_usage_condition
, "");
88 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set empty session name");
89 status
= lttng_condition_buffer_usage_get_session_name(buffer_usage_condition
,
91 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Session name is unset");
92 ok(!session_name
, "Session name is null");
95 lttng_condition_buffer_usage_set_session_name(buffer_usage_condition
, "session420");
96 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Set session name session420");
97 status
= lttng_condition_buffer_usage_get_session_name(buffer_usage_condition
,
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");
104 * Test second set on session_name. Test invalid set and validate that
105 * the value is still the previous good one.
108 status
= lttng_condition_buffer_usage_set_session_name(buffer_usage_condition
, "");
109 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set session name to empty");
110 status
= lttng_condition_buffer_usage_get_session_name(buffer_usage_condition
,
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");
116 diag("Testing channel name set/get");
117 status
= lttng_condition_buffer_usage_set_channel_name(nullptr, "Test");
118 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set null condition on set channel name");
119 status
= lttng_condition_buffer_usage_get_channel_name(nullptr, &channel_name
);
120 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Get channel name with null condition");
121 status
= lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition
,
123 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Channel name is unset");
124 ok(!channel_name
, "Channel name is null");
126 status
= lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition
, nullptr);
127 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set null channel name");
128 status
= lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition
,
130 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Channel name is unset");
131 ok(!channel_name
, "Channel name is null");
133 status
= lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition
, "");
134 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set empty channel name");
135 status
= lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition
,
137 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Channel name is unset");
138 ok(!channel_name
, "Channel name is null");
141 lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition
, "channel420");
142 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Set channel name channel420");
143 status
= lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition
,
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");
150 * Test second set on channel_name. Test invalid set and validate that
151 * the value is still the previous good one.
154 status
= lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition
, "");
155 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set channel name to empty");
156 status
= lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition
,
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");
162 diag("Testing threshold ratio set/get");
163 status
= lttng_condition_buffer_usage_set_threshold_ratio(nullptr, 0.420);
164 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set threshold ratio with null condition");
165 status
= lttng_condition_buffer_usage_get_threshold_ratio(nullptr, &threshold_ratio
);
166 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Get threshold ratio with null condition");
167 status
= lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition
,
169 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Threshold ratio is unset");
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");
173 status
= lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition
,
175 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Threshold ratio is unset");
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");
179 status
= lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition
,
181 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Threshold ratio is unset");
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");
185 status
= lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition
,
187 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Threshold ratio is set");
188 ok(threshold_ratio
== 1.0, "Threshold ratio is 1.0");
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");
192 status
= lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition
,
194 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Threshold ratio is set");
195 ok(threshold_ratio
== 0.0, "Threshold ratio is 0.0");
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");
199 status
= lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition
,
201 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Threshold ratio is set");
202 ok(threshold_ratio
== 0.420, "Threshold ratio is 0.420");
204 diag("Testing threshold bytes set/get");
205 status
= lttng_condition_buffer_usage_set_threshold(nullptr, 100000);
206 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set threshold with null condition");
207 status
= lttng_condition_buffer_usage_get_threshold(nullptr, &threshold_bytes
);
208 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Get threshold value with null condition ");
209 status
= lttng_condition_buffer_usage_get_threshold(buffer_usage_condition
,
211 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Threshold is unset");
213 status
= lttng_condition_buffer_usage_set_threshold(buffer_usage_condition
, 100000);
214 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Set threshold > 0");
215 status
= lttng_condition_buffer_usage_get_threshold(buffer_usage_condition
,
217 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Threshold is set");
218 ok(threshold_bytes
== 100000, "Threshold is 100000");
220 status
= lttng_condition_buffer_usage_set_threshold(buffer_usage_condition
, UINT64_MAX
);
221 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Set threshold UINT64_MAX");
222 status
= lttng_condition_buffer_usage_get_threshold(buffer_usage_condition
,
224 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Threshold is set");
225 ok(threshold_bytes
== UINT64_MAX
, "Threshold is UINT64_MAX");
227 status
= lttng_condition_buffer_usage_set_threshold(buffer_usage_condition
, 0);
228 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Set threshold == 0");
229 status
= lttng_condition_buffer_usage_get_threshold(buffer_usage_condition
,
231 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Threshold is set");
232 ok(threshold_bytes
== 0, "Threshold is %d", 0);
235 * Test value of threshold ration, since we overwrote it with a byte
236 * threshold. Make sure it gets squashed.
238 diag("Testing interaction between byte and ratio thresholds");
240 threshold_ratio
= -1.0;
241 status
= lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition
,
243 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Threshold ratio is unset");
244 ok(threshold_ratio
== -1.0, "Threshold ratio is untouched");
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");
249 status
= lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition
,
251 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Threshold ratio is set");
252 ok(threshold_ratio
== 0.420, "Threshold ratio is 0.420");
254 threshold_bytes
= 420;
255 status
= lttng_condition_buffer_usage_get_threshold(buffer_usage_condition
,
257 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Threshold is unset");
258 ok(threshold_bytes
== 420, "Threshold is untouched");
260 diag("Testing domain type set/get");
261 status
= lttng_condition_buffer_usage_set_domain_type(nullptr, LTTNG_DOMAIN_UST
);
262 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set domain type with null condition");
263 status
= lttng_condition_buffer_usage_get_domain_type(nullptr, &domain_type
);
264 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Get domain type with null condition");
266 status
= lttng_condition_buffer_usage_set_domain_type(buffer_usage_condition
,
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");
272 status
= lttng_condition_buffer_usage_set_domain_type(buffer_usage_condition
,
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");
280 static void test_condition_buffer_usage_low()
282 struct lttng_condition
*buffer_usage_low
= nullptr;
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");
288 ok(lttng_condition_get_type(buffer_usage_low
) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
,
289 "Condition is of type \"low buffer usage\"");
291 test_condition_buffer_usage(buffer_usage_low
);
293 lttng_condition_destroy(buffer_usage_low
);
296 static void test_condition_buffer_usage_high()
298 struct lttng_condition
*buffer_usage_high
= nullptr;
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");
304 ok(lttng_condition_get_type(buffer_usage_high
) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
,
305 "Condition is of type \"high buffer usage\"");
307 test_condition_buffer_usage(buffer_usage_high
);
309 lttng_condition_destroy(buffer_usage_high
);
312 static void test_trigger()
314 struct lttng_action
*notify_action
= nullptr;
315 struct lttng_condition
*buffer_usage_high
= nullptr;
316 struct lttng_trigger
*trigger
= nullptr;
318 notify_action
= lttng_action_notify_create();
319 buffer_usage_high
= lttng_condition_buffer_usage_high_create();
321 trigger
= lttng_trigger_create(nullptr, nullptr);
322 ok(!trigger
, "lttng_trigger_create(NULL, NULL) returns null");
323 trigger
= lttng_trigger_create(buffer_usage_high
, nullptr);
324 ok(!trigger
, "lttng_trigger_create(NON-NULL, NULL) returns null");
325 trigger
= lttng_trigger_create(nullptr, notify_action
);
326 ok(!trigger
, "lttng_trigger_create(NULL, NON-NULL) returns null");
328 trigger
= lttng_trigger_create(buffer_usage_high
, notify_action
);
329 ok(trigger
, "lttng_trigger_create(NON-NULL, NON-NULL) returns an object");
331 lttng_action_destroy(notify_action
);
332 lttng_condition_destroy(buffer_usage_high
);
333 lttng_trigger_destroy(trigger
);
338 plan_tests(NUM_TESTS
);
339 test_condition_buffer_usage_low();
340 test_condition_buffer_usage_high();
342 return exit_status();