Tests: notification: regroup event generators utils in a single file
[lttng-tools.git] / tests / regression / tools / notification / notification.c
CommitLineData
434f8068
JR
1/*
2 * notification.c
3 *
4 * Tests suite for LTTng notification API
5 *
6 * Copyright (C) 2017 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
7 *
9d16b343 8 * SPDX-License-Identifier: MIT
434f8068 9 *
434f8068
JR
10 */
11
12#include <assert.h>
13#include <math.h>
14#include <stdbool.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <unistd.h>
19#include <inttypes.h>
20#include <sys/types.h>
21#include <sys/stat.h>
22#include <fcntl.h>
854382b8 23#include <signal.h>
854382b8 24#include <poll.h>
434f8068 25
edf4b93e 26#include <common/compat/errno.h>
434f8068
JR
27#include <lttng/action/action.h>
28#include <lttng/action/notify.h>
29#include <lttng/condition/buffer-usage.h>
30#include <lttng/condition/condition.h>
31#include <lttng/condition/evaluation.h>
32#include <lttng/domain.h>
33#include <lttng/endpoint.h>
34#include <lttng/lttng-error.h>
35#include <lttng/notification/channel.h>
36#include <lttng/notification/notification.h>
37#include <lttng/trigger/trigger.h>
25040abc 38#include <lttng/lttng.h>
434f8068
JR
39
40#include <tap/tap.h>
41
42#define NUM_TESTS 104
854382b8 43
434f8068
JR
44int nb_args = 0;
45int named_pipe_args_start = 0;
7344b6a3 46pid_t app_pid = 0;
854382b8
JR
47const char *app_state_file = NULL;
48
49static
50void wait_on_file(const char *path, bool file_exist)
51{
52 if (!path) {
53 return;
54 }
55 for (;;) {
56 int ret;
57 struct stat buf;
58
59 ret = stat(path, &buf);
60 if (ret == -1 && errno == ENOENT) {
61 if (file_exist) {
f0784451
FD
62 /*
63 * The file does not exist. wait a bit and
64 * continue looping until it does.
65 */
66 (void) poll(NULL, 0, 10);
67 continue;
854382b8 68 }
f0784451
FD
69
70 /*
71 * File does not exist and the exit condition we want.
72 * Break from the loop and return.
73 */
74 break;
854382b8
JR
75 }
76 if (ret) {
77 perror("stat");
78 exit(EXIT_FAILURE);
79 }
f0784451
FD
80 /*
81 * stat() returned 0, so the file exists. break now only if
82 * that's the exit condition we want.
83 */
84 if (file_exist) {
85 break;
86 }
854382b8
JR
87 }
88}
434f8068 89
f12eb9c1 90static
434f8068
JR
91int write_pipe(const char *path, uint8_t data)
92{
93 int ret = 0;
94 int fd = 0;
95
96 fd = open(path, O_WRONLY | O_NONBLOCK);
97 if (fd < 0) {
98 perror("Could not open consumer control named pipe");
99 goto end;
100 }
101
102 ret = write(fd, &data , sizeof(data));
103 if (ret < 1) {
104 perror("Named pipe write failed");
eff748d0
JR
105 if (close(fd)) {
106 perror("Named pipe close failed");
107 }
434f8068
JR
108 ret = -1;
109 goto end;
110 }
111
112 ret = close(fd);
113 if (ret < 0) {
114 perror("Name pipe closing failed");
115 ret = -1;
116 goto end;
117 }
118end:
119 return ret;
120}
121
f12eb9c1 122static
434f8068
JR
123int stop_consumer(const char **argv)
124{
9df6c82a
JG
125 int ret = 0, i;
126
127 for (i = named_pipe_args_start; i < nb_args; i++) {
434f8068
JR
128 ret = write_pipe(argv[i], 49);
129 }
130 return ret;
131}
132
f12eb9c1 133static
434f8068
JR
134int resume_consumer(const char **argv)
135{
9df6c82a
JG
136 int ret = 0, i;
137
138 for (i = named_pipe_args_start; i < nb_args; i++) {
434f8068
JR
139 ret = write_pipe(argv[i], 0);
140 }
141 return ret;
142}
143
f12eb9c1
SM
144static
145int suspend_application(void)
854382b8
JR
146{
147 int ret;
148 struct stat buf;
149
150 if (!stat(app_state_file, &buf)) {
151 fail("App is already in a suspended state.");
152 ret = -1;
153 goto error;
154 }
155
156 /*
157 * Send SIGUSR1 to application instructing it to bypass tracepoint.
158 */
7344b6a3
FD
159 assert(app_pid > 1);
160
854382b8
JR
161 ret = kill(app_pid, SIGUSR1);
162 if (ret) {
163 fail("SIGUSR1 failed. errno %d", errno);
164 ret = -1;
165 goto error;
166 }
167
168 wait_on_file(app_state_file, true);
169
170error:
171 return ret;
172
173}
174
f12eb9c1 175static
a3ecaea8 176int resume_application(void)
854382b8
JR
177{
178 int ret;
179 struct stat buf;
180
181 ret = stat(app_state_file, &buf);
182 if (ret == -1 && errno == ENOENT) {
183 fail("State file does not exist");
184 goto error;
185 }
186 if (ret) {
187 perror("stat");
188 goto error;
189 }
190
7344b6a3
FD
191 assert(app_pid > 1);
192
854382b8
JR
193 ret = kill(app_pid, SIGUSR1);
194 if (ret) {
195 fail("SIGUSR1 failed. errno %d", errno);
196 ret = -1;
197 goto error;
198 }
199
200 wait_on_file(app_state_file, false);
201
202error:
203 return ret;
204
205}
206
207
f12eb9c1 208static
434f8068
JR
209void test_triggers_buffer_usage_condition(const char *session_name,
210 const char *channel_name,
211 enum lttng_domain_type domain_type,
212 enum lttng_condition_type condition_type)
213{
9df6c82a 214 unsigned int test_vector_size = 5, i;
434f8068
JR
215 enum lttng_condition_status condition_status;
216 struct lttng_action *action;
217
218 /* Set-up */
219 action = lttng_action_notify_create();
220 if (!action) {
221 fail("Setup error on action creation");
222 goto end;
223 }
224
225 /* Test lttng_register_trigger with null value */
226 ok(lttng_register_trigger(NULL) == -LTTNG_ERR_INVALID, "Registering a NULL trigger fails as expected");
227
228 /* Test: register a trigger */
9df6c82a
JG
229
230 for (i = 0; i < pow(2,test_vector_size); i++) {
434f8068
JR
231 int loop_ret = 0;
232 char *test_tuple_string = NULL;
233 unsigned int mask_position = 0;
234 bool session_name_set = false;
235 bool channel_name_set = false;
236 bool threshold_ratio_set = false;
237 bool threshold_byte_set = false;
238 bool domain_type_set = false;
239
240 struct lttng_trigger *trigger = NULL;
241 struct lttng_condition *condition = NULL;
242
243 /* Create base condition */
244 switch (condition_type) {
245 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
246 condition = lttng_condition_buffer_usage_low_create();
247 break;
248 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
249 condition = lttng_condition_buffer_usage_high_create();
250 break;
251 default:
252 loop_ret = 1;
253 goto loop_end;
254 }
255
256 if (!condition) {
257 loop_ret = 1;
258 goto loop_end;
259
260 }
261
262 /* Prepare the condition for trigger registration test */
263
264 /* Set session name */
265 if ((1 << mask_position) & i) {
266 condition_status = lttng_condition_buffer_usage_set_session_name(
267 condition, session_name);
268 if (condition_status != LTTNG_CONDITION_STATUS_OK) {
269 loop_ret = 1;
270 goto loop_end;
271 }
272 session_name_set = true;
273 }
274 mask_position++;
275
276 /* Set channel name */
277 if ((1 << mask_position) & i) {
278 condition_status = lttng_condition_buffer_usage_set_channel_name(
279 condition, channel_name);
280 if (condition_status != LTTNG_CONDITION_STATUS_OK) {
281 loop_ret = 1;
282 goto loop_end;
283 }
284 channel_name_set = true;
285 }
286 mask_position++;
287
288 /* Set threshold ratio */
289 if ((1 << mask_position) & i) {
290 condition_status = lttng_condition_buffer_usage_set_threshold_ratio(
291 condition, 0.0);
292 if (condition_status != LTTNG_CONDITION_STATUS_OK) {
293 loop_ret = 1;
294 goto loop_end;
295 }
296 threshold_ratio_set = true;
297 }
298 mask_position++;
299
300 /* Set threshold byte */
301 if ((1 << mask_position) & i) {
302 condition_status = lttng_condition_buffer_usage_set_threshold(
303 condition, 0);
304 if (condition_status != LTTNG_CONDITION_STATUS_OK) {
305 loop_ret = 1;
306 goto loop_end;
307 }
308 threshold_byte_set = true;
309 }
310 mask_position++;
311
312 /* Set domain type */
313 if ((1 << mask_position) & i) {
314 condition_status = lttng_condition_buffer_usage_set_domain_type(
315 condition, LTTNG_DOMAIN_UST);
316 if (condition_status != LTTNG_CONDITION_STATUS_OK) {
317 loop_ret = 1;
318 goto loop_end;
319 }
320 domain_type_set = true;
321 }
322
323 /* Safety check */
324 if (mask_position != test_vector_size -1) {
325 assert("Logic error for test vector generation");
326 }
327
9479b7a7 328 loop_ret = asprintf(&test_tuple_string, "session name %s, channel name %s, threshold ratio %s, threshold byte %s, domain type %s",
434f8068
JR
329 session_name_set ? "set" : "unset",
330 channel_name_set ? "set" : "unset",
331 threshold_ratio_set ? "set" : "unset",
332 threshold_byte_set ? "set" : "unset",
333 domain_type_set? "set" : "unset");
334 if (!test_tuple_string || loop_ret < 0) {
335 loop_ret = 1;
336 goto loop_end;
337 }
338
339 /* Create trigger */
340 trigger = lttng_trigger_create(condition, action);
341 if (!trigger) {
342 loop_ret = 1;
343 goto loop_end;
344 }
345
346 loop_ret = lttng_register_trigger(trigger);
347
348loop_end:
349 if (loop_ret == 1) {
350 fail("Setup error occurred for tuple: %s", test_tuple_string);
351 goto loop_cleanup;
352 }
353
354 /* This combination happens three times */
355 if (session_name_set && channel_name_set
356 && (threshold_ratio_set || threshold_byte_set)
357 && domain_type_set) {
358 ok(loop_ret == 0, "Trigger is registered: %s", test_tuple_string);
359
360 /*
361 * Test that a trigger cannot be registered
362 * multiple time.
363 */
364 loop_ret = lttng_register_trigger(trigger);
365 ok(loop_ret == -LTTNG_ERR_TRIGGER_EXISTS, "Re-register trigger fails as expected: %s", test_tuple_string);
366
367 /* Test that a trigger can be unregistered */
368 loop_ret = lttng_unregister_trigger(trigger);
369 ok(loop_ret == 0, "Unregister trigger: %s", test_tuple_string);
370
371 /*
372 * Test that unregistration of a non-previously
373 * registered trigger fail.
374 */
375 loop_ret = lttng_unregister_trigger(trigger);
43e0c204 376 ok(loop_ret == -LTTNG_ERR_TRIGGER_NOT_FOUND, "Unregister of a non-registered trigger fails as expected: %s", test_tuple_string);
434f8068
JR
377 } else {
378 ok(loop_ret == -LTTNG_ERR_INVALID_TRIGGER, "Trigger is invalid as expected and cannot be registered: %s", test_tuple_string);
379 }
380
381loop_cleanup:
382 free(test_tuple_string);
383 lttng_trigger_destroy(trigger);
384 lttng_condition_destroy(condition);
385 }
386
387end:
388 lttng_action_destroy(action);
389}
390
ff2b03c8
JG
391static
392void wait_data_pending(const char *session_name)
393{
394 int ret;
395
396 do {
397 ret = lttng_data_pending(session_name);
398 assert(ret >= 0);
399 } while (ret != 0);
400}
401
f12eb9c1 402static
17423302
FD
403int setup_buffer_usage_condition(struct lttng_condition *condition,
404 const char *condition_name,
405 const char *session_name,
406 const char *channel_name,
407 const enum lttng_domain_type domain_type)
408{
409 enum lttng_condition_status condition_status;
410 int ret = 0;
411
412 condition_status = lttng_condition_buffer_usage_set_session_name(
413 condition, session_name);
414 if (condition_status != LTTNG_CONDITION_STATUS_OK) {
415 fail("Failed to set session name on creation of condition `%s`",
416 condition_name);
417 ret = -1;
418 goto end;
419 }
420
421 condition_status = lttng_condition_buffer_usage_set_channel_name(
422 condition, channel_name);
423 if (condition_status != LTTNG_CONDITION_STATUS_OK) {
424 fail("Failed to set channel name on creation of condition `%s`",
425 condition_name);
426 ret = -1;
427 goto end;
428 }
429
430 condition_status = lttng_condition_buffer_usage_set_domain_type(
431 condition, domain_type);
432 if (condition_status != LTTNG_CONDITION_STATUS_OK) {
433 fail("Failed to set domain type on creation of condition `%s`",
434 condition_name);
435 ret = -1;
436 goto end;
437 }
438
439end:
440 return ret;
441}
442
443static
90fcb2ec
FD
444void test_invalid_channel_subscription(
445 const enum lttng_domain_type domain_type)
434f8068 446{
434f8068
JR
447 enum lttng_condition_status condition_status;
448 enum lttng_notification_channel_status nc_status;
434f8068 449 struct lttng_condition *dummy_condition = NULL;
90fcb2ec
FD
450 struct lttng_condition *dummy_invalid_condition = NULL;
451 struct lttng_notification_channel *notification_channel = NULL;
452 int ret = 0;
434f8068 453
90fcb2ec
FD
454 notification_channel = lttng_notification_channel_create(
455 lttng_session_daemon_notification_endpoint);
456 ok(notification_channel, "Notification channel object creation");
457 if (!notification_channel) {
434f8068
JR
458 goto end;
459 }
460
90fcb2ec
FD
461 /*
462 * Create a dummy, empty (thus invalid) condition to test error paths.
463 */
434f8068
JR
464 dummy_invalid_condition = lttng_condition_buffer_usage_low_create();
465 if (!dummy_invalid_condition) {
466 fail("Setup error on condition creation");
467 goto end;
468 }
469
90fcb2ec
FD
470 /*
471 * Test subscription and unsubscription of an invalid condition to/from
472 * a channel.
473 */
474 nc_status = lttng_notification_channel_subscribe(
475 notification_channel, dummy_invalid_condition);
476 ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID,
477 "Subscribing to an invalid condition");
478
479 nc_status = lttng_notification_channel_unsubscribe(
480 notification_channel, dummy_invalid_condition);
481 ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID,
482 "Unsubscribing from an invalid condition");
483
434f8068
JR
484 /* Create a valid dummy condition with a ratio of 0.5 */
485 dummy_condition = lttng_condition_buffer_usage_low_create();
486 if (!dummy_condition) {
487 fail("Setup error on dummy_condition creation");
488 goto end;
434f8068 489 }
90fcb2ec 490
434f8068
JR
491 condition_status = lttng_condition_buffer_usage_set_threshold_ratio(
492 dummy_condition, 0.5);
493 if (condition_status != LTTNG_CONDITION_STATUS_OK) {
494 fail("Setup error on condition creation");
495 goto end;
496 }
497
17423302 498 ret = setup_buffer_usage_condition(dummy_condition, "dummy_condition",
90fcb2ec 499 "dummy_session", "dummy_channel", domain_type);
17423302
FD
500 if (ret) {
501 fail("Setup error on dummy condition creation");
434f8068
JR
502 goto end;
503 }
504
90fcb2ec
FD
505 /*
506 * Test subscription and unsubscription to/from a channel with invalid
507 * parameters.
508 */
509 nc_status = lttng_notification_channel_subscribe(NULL, NULL);
510 ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID,
511 "Notification channel subscription is invalid: NULL, NULL");
512
513 nc_status = lttng_notification_channel_subscribe(
514 notification_channel, NULL);
515 ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID,
516 "Notification channel subscription is invalid: NON-NULL, NULL");
517
518 nc_status = lttng_notification_channel_subscribe(NULL, dummy_condition);
519 ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID,
520 "Notification channel subscription is invalid: NULL, NON-NULL");
521
522 nc_status = lttng_notification_channel_unsubscribe(
523 notification_channel, dummy_condition);
524 ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION,
525 "Unsubscribing from a valid unknown condition");
526
527end:
528 lttng_notification_channel_destroy(notification_channel);
529 lttng_condition_destroy(dummy_invalid_condition);
530 lttng_condition_destroy(dummy_condition);
531 return;
532}
533
3780613c
FD
534enum buffer_usage_type {
535 BUFFER_USAGE_TYPE_LOW,
536 BUFFER_USAGE_TYPE_HIGH,
537};
538
539static int register_buffer_usage_notify_trigger(const char *session_name,
90fcb2ec
FD
540 const char *channel_name,
541 const enum lttng_domain_type domain_type,
3780613c
FD
542 enum buffer_usage_type buffer_usage_type,
543 double ratio,
544 struct lttng_condition **condition,
545 struct lttng_action **action,
546 struct lttng_trigger **trigger)
90fcb2ec 547{
90fcb2ec 548 enum lttng_condition_status condition_status;
3780613c
FD
549 struct lttng_action *tmp_action = NULL;
550 struct lttng_condition *tmp_condition = NULL;
551 struct lttng_trigger *tmp_trigger = NULL;
552 int ret = 0;
90fcb2ec
FD
553
554 /* Set-up */
3780613c 555 tmp_action = lttng_action_notify_create();
90fcb2ec
FD
556 if (!action) {
557 fail("Setup error on action creation");
3780613c
FD
558 ret = -1;
559 goto error;
434f8068
JR
560 }
561
3780613c
FD
562 if (buffer_usage_type == BUFFER_USAGE_TYPE_LOW) {
563 tmp_condition = lttng_condition_buffer_usage_low_create();
564 } else {
565 tmp_condition = lttng_condition_buffer_usage_high_create();
434f8068
JR
566 }
567
3780613c
FD
568 if (!tmp_condition) {
569 fail("Setup error on condition creation");
570 ret = -1;
571 goto error;
434f8068
JR
572 }
573
3780613c 574 /* Set the buffer usage threashold */
434f8068 575 condition_status = lttng_condition_buffer_usage_set_threshold_ratio(
3780613c 576 tmp_condition, ratio);
434f8068 577 if (condition_status != LTTNG_CONDITION_STATUS_OK) {
3780613c
FD
578 fail("Setup error on condition creation");
579 ret = -1;
580 goto error;
434f8068
JR
581 }
582
3780613c 583 ret = setup_buffer_usage_condition(tmp_condition, "condition_name",
17423302
FD
584 session_name, channel_name, domain_type);
585 if (ret) {
3780613c
FD
586 fail("Setup error on condition creation");
587 ret = -1;
588 goto error;
434f8068
JR
589 }
590
3780613c
FD
591 /* Register the trigger for condition. */
592 tmp_trigger = lttng_trigger_create(tmp_condition, tmp_action);
593 if (!tmp_trigger) {
594 fail("Setup error on trigger creation");
595 ret = -1;
596 goto error;
434f8068
JR
597 }
598
3780613c 599 ret = lttng_register_trigger(tmp_trigger);
434f8068 600 if (ret) {
3780613c
FD
601 fail("Setup error on trigger registration");
602 ret = -1;
603 goto error;
434f8068
JR
604 }
605
3780613c
FD
606 *condition = tmp_condition;
607 *trigger = tmp_trigger;
608 *action = tmp_action;
609 goto end;
434f8068 610
3780613c
FD
611error:
612 lttng_action_destroy(tmp_action);
613 lttng_condition_destroy(tmp_condition);
614 lttng_trigger_destroy(tmp_trigger);
615
616end:
617 return ret;
618}
619
fa448dbe
FD
620static void test_subscription_twice(const char *session_name,
621 const char *channel_name,
622 const enum lttng_domain_type domain_type)
623{
624 int ret = 0;
625 enum lttng_notification_channel_status nc_status;
626
627 struct lttng_action *action = NULL;
628 struct lttng_notification_channel *notification_channel = NULL;
629 struct lttng_trigger *trigger = NULL;
630
631 struct lttng_condition *condition = NULL;
632
633 ret = register_buffer_usage_notify_trigger(session_name, channel_name,
634 domain_type, BUFFER_USAGE_TYPE_LOW, 0.99, &condition,
635 &action, &trigger);
636 if (ret) {
637 fail("Setup error on trigger registration");
638 goto end;
639 }
640
641 /* Begin testing. */
642 notification_channel = lttng_notification_channel_create(
643 lttng_session_daemon_notification_endpoint);
644 ok(notification_channel, "Notification channel object creation");
645 if (!notification_channel) {
646 goto end;
647 }
648
649 /* Subscribe a valid condition. */
650 nc_status = lttng_notification_channel_subscribe(
651 notification_channel, condition);
652 ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_OK,
653 "Subscribe to condition");
654
655 /* Subscribing again should fail. */
656 nc_status = lttng_notification_channel_subscribe(
657 notification_channel, condition);
658 ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED,
659 "Subscribe to a condition for which subscription was already done");
660
661end:
662 lttng_unregister_trigger(trigger);
663 lttng_trigger_destroy(trigger);
664 lttng_notification_channel_destroy(notification_channel);
665 lttng_action_destroy(action);
666 lttng_condition_destroy(condition);
667}
668
40ac6709 669static void test_buffer_usage_notification_channel(const char *session_name,
3780613c
FD
670 const char *channel_name,
671 const enum lttng_domain_type domain_type,
672 const char **argv)
673{
674 int ret = 0;
675 enum lttng_notification_channel_status nc_status;
676
677 struct lttng_action *low_action = NULL;
678 struct lttng_action *high_action = NULL;
679 struct lttng_notification *notification = NULL;
680 struct lttng_notification_channel *notification_channel = NULL;
681 struct lttng_trigger *low_trigger = NULL;
682 struct lttng_trigger *high_trigger = NULL;
683
684 struct lttng_condition *low_condition = NULL;
685 struct lttng_condition *high_condition = NULL;
686
687 const double low_ratio = 0.0;
688 const double high_ratio = 0.90;
689
690 ret = register_buffer_usage_notify_trigger(session_name, channel_name,
691 domain_type, BUFFER_USAGE_TYPE_LOW, low_ratio,
692 &low_condition, &low_action, &low_trigger);
693 if (ret) {
694 fail("Setup error on low trigger registration");
434f8068
JR
695 goto end;
696 }
697
3780613c
FD
698 ret = register_buffer_usage_notify_trigger(session_name, channel_name,
699 domain_type, BUFFER_USAGE_TYPE_HIGH, high_ratio,
700 &high_condition, &high_action, &high_trigger);
434f8068
JR
701 if (ret) {
702 fail("Setup error on high trigger registration");
703 goto end;
704 }
705
706 /* Begin testing */
a6ae2480
FD
707 notification_channel = lttng_notification_channel_create(
708 lttng_session_daemon_notification_endpoint);
434f8068
JR
709 ok(notification_channel, "Notification channel object creation");
710 if (!notification_channel) {
711 goto end;
712 }
713
434f8068 714 /* Subscribe a valid low condition */
a6ae2480
FD
715 nc_status = lttng_notification_channel_subscribe(
716 notification_channel, low_condition);
717 ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_OK,
40ac6709 718 "Subscribe to low condition");
434f8068
JR
719
720 /* Subscribe a valid high condition */
a6ae2480
FD
721 nc_status = lttng_notification_channel_subscribe(
722 notification_channel, high_condition);
723 ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_OK,
40ac6709 724 "Subscribe to high condition");
434f8068 725
7676e0d2
FD
726 resume_application();
727
434f8068 728 /* Wait for notification to happen */
434f8068 729 stop_consumer(argv);
ff2b03c8 730 lttng_start_tracing(session_name);
434f8068
JR
731
732 /* Wait for high notification */
67c93c15 733 do {
a6ae2480
FD
734 nc_status = lttng_notification_channel_get_next_notification(
735 notification_channel, &notification);
67c93c15 736 } while (nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_INTERRUPTED);
a6ae2480
FD
737 ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_OK && notification &&
738 lttng_condition_get_type(lttng_notification_get_condition(
739 notification)) ==
740 LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH,
434f8068
JR
741 "High notification received after intermediary communication");
742 lttng_notification_destroy(notification);
743 notification = NULL;
744
854382b8 745 suspend_application();
ff2b03c8 746 lttng_stop_tracing_no_wait(session_name);
434f8068 747 resume_consumer(argv);
ff2b03c8 748 wait_data_pending(session_name);
434f8068
JR
749
750 /*
751 * Test that communication still work even if there is notification
752 * waiting for consumption.
753 */
754
a6ae2480
FD
755 nc_status = lttng_notification_channel_unsubscribe(
756 notification_channel, low_condition);
757 ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_OK,
758 "Unsubscribe with pending notification");
434f8068 759
a6ae2480
FD
760 nc_status = lttng_notification_channel_subscribe(
761 notification_channel, low_condition);
762 ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_OK,
763 "Subscribe with pending notification");
434f8068 764
67c93c15 765 do {
a6ae2480
FD
766 nc_status = lttng_notification_channel_get_next_notification(
767 notification_channel, &notification);
67c93c15 768 } while (nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_INTERRUPTED);
a6ae2480
FD
769 ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_OK && notification &&
770 lttng_condition_get_type(lttng_notification_get_condition(
771 notification)) ==
772 LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW,
434f8068
JR
773 "Low notification received after intermediary communication");
774 lttng_notification_destroy(notification);
775 notification = NULL;
776
777 /* Stop consumer to force a high notification */
ff2b03c8 778 stop_consumer(argv);
854382b8 779 resume_application();
434f8068 780 lttng_start_tracing(session_name);
434f8068 781
67c93c15 782 do {
a6ae2480
FD
783 nc_status = lttng_notification_channel_get_next_notification(
784 notification_channel, &notification);
67c93c15 785 } while (nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_INTERRUPTED);
434f8068 786 ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_OK && notification &&
a6ae2480
FD
787 lttng_condition_get_type(lttng_notification_get_condition(
788 notification)) ==
789 LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH,
434f8068
JR
790 "High notification received after intermediary communication");
791 lttng_notification_destroy(notification);
792 notification = NULL;
793
854382b8 794 suspend_application();
ff2b03c8 795 lttng_stop_tracing_no_wait(session_name);
434f8068 796 resume_consumer(argv);
ff2b03c8 797 wait_data_pending(session_name);
434f8068 798
67c93c15 799 do {
a6ae2480
FD
800 nc_status = lttng_notification_channel_get_next_notification(
801 notification_channel, &notification);
67c93c15 802 } while (nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_INTERRUPTED);
434f8068 803 ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_OK && notification &&
a6ae2480
FD
804 lttng_condition_get_type(lttng_notification_get_condition(
805 notification)) ==
806 LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW,
434f8068
JR
807 "Low notification received after re-subscription");
808 lttng_notification_destroy(notification);
809 notification = NULL;
810
ff2b03c8 811 stop_consumer(argv);
854382b8 812 resume_application();
434f8068
JR
813 /* Stop consumer to force a high notification */
814 lttng_start_tracing(session_name);
434f8068 815
67c93c15 816 do {
a6ae2480
FD
817 nc_status = lttng_notification_channel_get_next_notification(
818 notification_channel, &notification);
67c93c15 819 } while (nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_INTERRUPTED);
434f8068 820 ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_OK && notification &&
a6ae2480
FD
821 lttng_condition_get_type(lttng_notification_get_condition(
822 notification)) ==
823 LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH,
434f8068
JR
824 "High notification");
825 lttng_notification_destroy(notification);
826 notification = NULL;
827
ff2b03c8 828 suspend_application();
7676e0d2
FD
829
830 /* Resume consumer to allow event consumption */
ff2b03c8 831 lttng_stop_tracing_no_wait(session_name);
434f8068 832 resume_consumer(argv);
ff2b03c8 833 wait_data_pending(session_name);
434f8068 834
a6ae2480
FD
835 nc_status = lttng_notification_channel_unsubscribe(
836 notification_channel, low_condition);
837 ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_OK,
838 "Unsubscribe low condition with pending notification");
839
840 nc_status = lttng_notification_channel_unsubscribe(
841 notification_channel, high_condition);
842 ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_OK,
843 "Unsubscribe high condition with pending notification");
434f8068
JR
844
845end:
846 lttng_notification_channel_destroy(notification_channel);
3780613c
FD
847 lttng_trigger_destroy(low_trigger);
848 lttng_trigger_destroy(high_trigger);
849 lttng_action_destroy(low_action);
850 lttng_action_destroy(high_action);
434f8068 851 lttng_condition_destroy(low_condition);
eff748d0 852 lttng_condition_destroy(high_condition);
434f8068
JR
853}
854
855int main(int argc, const char *argv[])
856{
069fc1c5 857 int test_scenario;
434f8068
JR
858 const char *session_name = NULL;
859 const char *channel_name = NULL;
860 const char *domain_type_string = NULL;
861 enum lttng_domain_type domain_type = LTTNG_DOMAIN_NONE;
862
069fc1c5
FD
863 if (argc < 3) {
864 fail("Missing test scenario and/or domain type argument(s)");
434f8068
JR
865 goto error;
866 }
867
069fc1c5
FD
868 test_scenario = atoi(argv[1]);
869 domain_type_string = argv[2];
434f8068
JR
870
871 if (!strcmp("LTTNG_DOMAIN_UST", domain_type_string)) {
872 domain_type = LTTNG_DOMAIN_UST;
873 }
874 if (!strcmp("LTTNG_DOMAIN_KERNEL", domain_type_string)) {
875 domain_type = LTTNG_DOMAIN_KERNEL;
876 }
877 if (domain_type == LTTNG_DOMAIN_NONE) {
878 fail("Unknown domain type");
879 goto error;
880 }
881
069fc1c5
FD
882 switch (test_scenario) {
883 case 1:
884 {
885 plan_tests(7);
886 /* Test cases that need gen-ust-event testapp. */
887 diag("Test basic notification error paths for domain %s",
888 domain_type_string);
889 test_invalid_channel_subscription(domain_type);
890 break;
891 }
892 case 2:
893 {
894 /* Test cases that need a tracing session enabled. */
895 plan_tests(99);
7676e0d2 896
069fc1c5
FD
897 /*
898 * Argument 7 and upward are named pipe location for consumerd
899 * control.
900 */
901 named_pipe_args_start = 7;
902
903 if (argc < 8) {
904 fail("Missing parameter for tests to run %d", argc);
905 goto error;
906 }
434f8068 907
069fc1c5
FD
908 nb_args = argc;
909
910 session_name = argv[3];
911 channel_name = argv[4];
912 app_pid = (pid_t) atoi(argv[5]);
913 app_state_file = argv[6];
914
915 /*
916 * Test cases are responsible for resuming the app when needed
917 * and making sure it's suspended when returning.
918 */
919 suspend_application();
920
921 test_subscription_twice(session_name, channel_name,
922 domain_type);
923
924 diag("Test trigger for domain %s with buffer_usage_low condition",
925 domain_type_string);
926 test_triggers_buffer_usage_condition(session_name, channel_name,
927 domain_type,
928 LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW);
929
930 diag("Test trigger for domain %s with buffer_usage_high condition",
931 domain_type_string);
932 test_triggers_buffer_usage_condition(session_name, channel_name,
933 domain_type,
934 LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH);
935
936 diag("Test buffer usage notification channel api for domain %s",
937 domain_type_string);
938 test_buffer_usage_notification_channel(session_name, channel_name,
939 domain_type, argv);
940 break;
941 }
942 default:
943 abort();
944 }
90fcb2ec 945
434f8068
JR
946error:
947 return exit_status();
948}
949
This page took 0.122624 seconds and 4 git commands to generate.