4 * Unit tests for the notification API.
6 * Copyright (C) 2017 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
8 * SPDX-License-Identifier: MIT
19 #include <lttng/action/action.h>
20 #include <lttng/action/notify.h>
21 #include <lttng/condition/buffer-usage.h>
22 #include <lttng/condition/condition.h>
23 #include <lttng/domain.h>
24 #include <lttng/notification/notification.h>
25 #include <lttng/trigger/trigger.h>
27 #include <common/macros.h>
30 int lttng_opt_quiet
= 1;
31 int lttng_opt_verbose
;
36 static void test_condition_buffer_usage(
37 struct lttng_condition
*buffer_usage_condition
)
39 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
40 const char *session_name
= NULL
;
41 const char *channel_name
= NULL
;
42 enum lttng_domain_type domain_type
;
43 /* Start at a non zero value to validate initialization */
44 double threshold_ratio
;
45 uint64_t threshold_bytes
;
47 LTTNG_ASSERT(buffer_usage_condition
);
49 diag("Validating initialization");
50 status
= lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition
, &threshold_ratio
);
51 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Threshold ratio is unset");
53 status
= lttng_condition_buffer_usage_get_threshold(buffer_usage_condition
, &threshold_bytes
);
54 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Threshold byte is unset");
56 status
= lttng_condition_buffer_usage_get_session_name(buffer_usage_condition
, &session_name
);
57 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Session name is unset");
58 ok(!session_name
, "Session name is null");
60 status
= lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition
, &channel_name
);
61 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Channel name is unset");
62 ok(!session_name
, "Channel name is null");
64 status
= lttng_condition_buffer_usage_get_domain_type(buffer_usage_condition
, &domain_type
);
65 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Domain name is unset");
67 diag("Testing session name set/get");
68 status
= lttng_condition_buffer_usage_set_session_name(NULL
, "Test");
69 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set null condition on set session name");
70 status
= lttng_condition_buffer_usage_get_session_name(NULL
, &session_name
);
71 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Get session name with null condition");
72 ok(!session_name
, "Session name is null");
73 status
= lttng_condition_buffer_usage_get_session_name(buffer_usage_condition
, &session_name
);
74 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Session name is unset");
75 ok(!session_name
, "Session name is null");
77 status
= lttng_condition_buffer_usage_set_session_name(buffer_usage_condition
, NULL
);
78 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set null session name");
79 status
= lttng_condition_buffer_usage_get_session_name(buffer_usage_condition
, &session_name
);
80 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Session name is unset");
81 ok(!session_name
, "Session name is null");
83 status
= lttng_condition_buffer_usage_set_session_name(buffer_usage_condition
, "");
84 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set empty session name");
85 status
= lttng_condition_buffer_usage_get_session_name(buffer_usage_condition
, &session_name
);
86 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Session name is unset");
87 ok(!session_name
, "Session name is null");
89 status
= lttng_condition_buffer_usage_set_session_name(buffer_usage_condition
, "session420");
90 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Set session name session420");
91 status
= lttng_condition_buffer_usage_get_session_name(buffer_usage_condition
, &session_name
);
92 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Session name is set");
93 ok(session_name
, "Session name has a value");
94 ok(strcmp("session420", session_name
) == 0, "Session name is %s", "session420");
97 * Test second set on session_name. Test invalid set and validate that
98 * the value is still the previous good one.
101 status
= lttng_condition_buffer_usage_set_session_name(buffer_usage_condition
, "");
102 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set session name to empty");
103 status
= lttng_condition_buffer_usage_get_session_name(buffer_usage_condition
, &session_name
);
104 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Session name is still set");
105 ok(session_name
, "Session name has a value");
106 ok(strcmp("session420", session_name
) == 0, "Session is still name is %s", "session420");
108 diag("Testing channel name set/get");
109 status
= lttng_condition_buffer_usage_set_channel_name(NULL
, "Test");
110 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set null condition on set channel name");
111 status
= lttng_condition_buffer_usage_get_channel_name(NULL
, &channel_name
);
112 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Get channel name with null condition");
113 status
= lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition
, &channel_name
);
114 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Channel name is unset");
115 ok(!channel_name
, "Channel name is null");
117 status
= lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition
, NULL
);
118 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set null channel name");
119 status
= lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition
, &channel_name
);
120 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Channel name is unset");
121 ok(!channel_name
, "Channel name is null");
123 status
= lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition
, "");
124 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set empty channel name");
125 status
= lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition
, &channel_name
);
126 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Channel name is unset");
127 ok(!channel_name
, "Channel name is null");
129 status
= lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition
, "channel420");
130 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Set channel name channel420");
131 status
= lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition
, &channel_name
);
132 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Channel name is set");
133 ok(channel_name
, "Channel name has a value");
134 ok(strcmp("channel420", channel_name
) == 0, "Channel name is %s", "channel420");
137 * Test second set on channel_name. Test invalid set and validate that
138 * the value is still the previous good one.
141 status
= lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition
, "");
142 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set channel name to empty");
143 status
= lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition
, &channel_name
);
144 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Channel name is still set");
145 ok(channel_name
, "Channel name has a value");
146 ok(strcmp("channel420", channel_name
) == 0, "Channel is still name is %s", "channel420");
148 diag("Testing threshold ratio set/get");
149 status
= lttng_condition_buffer_usage_set_threshold_ratio(NULL
, 0.420);
150 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set threshold ratio with null condition");
151 status
= lttng_condition_buffer_usage_get_threshold_ratio(NULL
, &threshold_ratio
);
152 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Get threshold ratio with null condition");
153 status
= lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition
, &threshold_ratio
);
154 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Threshold ratio is unset");
156 status
= lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition
, -100.0);
157 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set threshold ratio < 0");
158 status
= lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition
, &threshold_ratio
);
159 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Threshold ratio is unset");
161 status
= lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition
, 200.0);
162 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set Threshold ratio > 1");
163 status
= lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition
, &threshold_ratio
);
164 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Threshold ratio is unset");
166 status
= lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition
, 1.0);
167 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Set threshold ratio == 1.0");
168 status
= lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition
, &threshold_ratio
);
169 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Threshold ratio is set");
170 ok(threshold_ratio
== 1.0, "Threshold ratio is 1.0");
172 status
= lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition
, 0.0);
173 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Set threshold ratio == 0.0");
174 status
= lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition
, &threshold_ratio
);
175 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Threshold ratio is set");
176 ok(threshold_ratio
== 0.0, "Threshold ratio is 0.0");
178 status
= lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition
, 0.420);
179 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Set threshold ratio == 0.420");
180 status
= lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition
, &threshold_ratio
);
181 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Threshold ratio is set");
182 ok(threshold_ratio
== 0.420, "Threshold ratio is 0.420");
184 diag("Testing threshold bytes set/get");
185 status
= lttng_condition_buffer_usage_set_threshold(NULL
, 100000);
186 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set threshold with null condition");
187 status
= lttng_condition_buffer_usage_get_threshold(NULL
, &threshold_bytes
);
188 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Get threshold value with null condition ");
189 status
= lttng_condition_buffer_usage_get_threshold(buffer_usage_condition
, &threshold_bytes
);
190 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Threshold is unset");
192 status
= lttng_condition_buffer_usage_set_threshold(buffer_usage_condition
, 100000);
193 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Set threshold > 0");
194 status
= lttng_condition_buffer_usage_get_threshold(buffer_usage_condition
, &threshold_bytes
);
195 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Threshold is set");
196 ok(threshold_bytes
== 100000, "Threshold is 100000");
198 status
= lttng_condition_buffer_usage_set_threshold(buffer_usage_condition
, UINT64_MAX
);
199 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Set threshold UINT64_MAX");
200 status
= lttng_condition_buffer_usage_get_threshold(buffer_usage_condition
, &threshold_bytes
);
201 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Threshold is set");
202 ok(threshold_bytes
== UINT64_MAX
, "Threshold is UINT64_MAX");
204 status
= lttng_condition_buffer_usage_set_threshold(buffer_usage_condition
, 0);
205 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Set threshold == 0");
206 status
= lttng_condition_buffer_usage_get_threshold(buffer_usage_condition
, &threshold_bytes
);
207 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Threshold is set");
208 ok(threshold_bytes
== 0, "Threshold is %d", 0);
211 * Test value of threshold ration, since we overwrote it with a byte
212 * threshold. Make sure it gets squashed.
214 diag("Testing interaction between byte and ratio thresholds");
216 threshold_ratio
= -1.0;
217 status
= lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition
, &threshold_ratio
);
218 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Threshold ratio is unset");
219 ok(threshold_ratio
== -1.0, "Threshold ratio is untouched");
221 /* Set a ratio to validate that the byte threshold is now unset */
222 status
= lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition
, 0.420);
223 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Set threshold ratio == 0.420");
224 status
= lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition
, &threshold_ratio
);
225 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Threshold ratio is set");
226 ok(threshold_ratio
== 0.420, "Threshold ratio is 0.420");
228 threshold_bytes
= 420;
229 status
= lttng_condition_buffer_usage_get_threshold(buffer_usage_condition
, &threshold_bytes
);
230 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Threshold is unset");
231 ok(threshold_bytes
== 420, "Threshold is untouched");
233 diag("Testing domain type set/get");
234 status
= lttng_condition_buffer_usage_set_domain_type(NULL
, LTTNG_DOMAIN_UST
);
235 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set domain type with null condition");
236 status
= lttng_condition_buffer_usage_get_domain_type(NULL
, &domain_type
);
237 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Get domain type with null condition");
239 status
= lttng_condition_buffer_usage_set_domain_type(buffer_usage_condition
, LTTNG_DOMAIN_NONE
);
240 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set domain type as LTTNG_DOMAIN_NONE");
241 status
= lttng_condition_buffer_usage_get_domain_type(buffer_usage_condition
, &domain_type
);
242 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Domain type is unset");
244 status
= lttng_condition_buffer_usage_set_domain_type(buffer_usage_condition
, LTTNG_DOMAIN_UST
);
245 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Set domain type as LTTNG_DOMAIN_UST");
246 status
= lttng_condition_buffer_usage_get_domain_type(buffer_usage_condition
, &domain_type
);
247 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Domain type is set");
248 ok(domain_type
== LTTNG_DOMAIN_UST
, "Domain type is LTTNG_DOMAIN_UST");
251 static void test_condition_buffer_usage_low(void)
253 struct lttng_condition
*buffer_usage_low
= NULL
;
255 diag("Testing lttng_condition_buffer_usage_low_create");
256 buffer_usage_low
= lttng_condition_buffer_usage_low_create();
257 ok(buffer_usage_low
, "Condition allocated");
259 ok(lttng_condition_get_type(buffer_usage_low
) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
, "Condition is of type \"low buffer usage\"");
261 test_condition_buffer_usage(buffer_usage_low
);
263 lttng_condition_destroy(buffer_usage_low
);
266 static void test_condition_buffer_usage_high(void)
268 struct lttng_condition
*buffer_usage_high
= NULL
;
270 diag("Testing lttng_condition_buffer_usage_high_create");
271 buffer_usage_high
= lttng_condition_buffer_usage_high_create();
272 ok(buffer_usage_high
, "High buffer usage condition allocated");
274 ok(lttng_condition_get_type(buffer_usage_high
) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
, "Condition is of type \"high buffer usage\"");
276 test_condition_buffer_usage(buffer_usage_high
);
278 lttng_condition_destroy(buffer_usage_high
);
281 static void test_trigger(void)
283 struct lttng_action
*notify_action
= NULL
;
284 struct lttng_condition
*buffer_usage_high
= NULL
;
285 struct lttng_trigger
*trigger
= NULL
;
287 notify_action
= lttng_action_notify_create();
288 buffer_usage_high
= lttng_condition_buffer_usage_high_create();
290 trigger
= lttng_trigger_create(NULL
, NULL
);
291 ok(!trigger
, "lttng_trigger_create(NULL, NULL) returns null");
292 trigger
= lttng_trigger_create(buffer_usage_high
, NULL
);
293 ok(!trigger
, "lttng_trigger_create(NON-NULL, NULL) returns null");
294 trigger
= lttng_trigger_create(NULL
, notify_action
);
295 ok(!trigger
, "lttng_trigger_create(NULL, NON-NULL) returns null");
297 trigger
= lttng_trigger_create(buffer_usage_high
, notify_action
);
298 ok(trigger
, "lttng_trigger_create(NON-NULL, NON-NULL) returns an object");
300 lttng_action_destroy(notify_action
);
301 lttng_condition_destroy(buffer_usage_high
);
302 lttng_trigger_destroy(trigger
);
306 int main(int argc
, const char *argv
[])
308 plan_tests(NUM_TESTS
);
309 test_condition_buffer_usage_low();
310 test_condition_buffer_usage_high();
312 return exit_status();