|
37 | 37 | #include <stdint.h> |
38 | 38 | #include <stdio.h> |
39 | 39 | #include <stdlib.h> |
| 40 | +#include <pthread.h> |
40 | 41 | #include <string.h> |
| 42 | +#include <time.h> |
41 | 43 | #include <unistd.h> |
42 | 44 | #include <sys/boardctl.h> |
43 | 45 | #include <sys/param.h> |
@@ -532,3 +534,130 @@ void test_nxinit_action_builtin_pmconfig(FAR void **state) |
532 | 534 | skip(); |
533 | 535 | #endif |
534 | 536 | } |
| 537 | + |
| 538 | +#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_TMPFS) |
| 539 | + |
| 540 | +/* Create the file of the "late" case of test_nxinit_action_builtin_wait |
| 541 | + * while the test is blocked in "wait". |
| 542 | + */ |
| 543 | + |
| 544 | +static void *wait_create_late_file(FAR void *arg) |
| 545 | +{ |
| 546 | + int fd; |
| 547 | + |
| 548 | + UNUSED(arg); |
| 549 | + usleep(200 * 1000); |
| 550 | + fd = open(TEST_MNT "/wait_late", O_CREAT | O_WRONLY, 0644); |
| 551 | + if (fd >= 0) |
| 552 | + { |
| 553 | + close(fd); |
| 554 | + } |
| 555 | + |
| 556 | + return NULL; |
| 557 | +} |
| 558 | + |
| 559 | +#endif |
| 560 | + |
| 561 | +/**************************************************************************** |
| 562 | + * Name: test_nxinit_action_builtin_wait |
| 563 | + * |
| 564 | + * Description: |
| 565 | + * "wait" polls for the file and returns when it exists. A missing |
| 566 | + * file only warns and returns 0 once the optional timeout (seconds, |
| 567 | + * default 5) is reached, and a bad timeout fails with -EINVAL. |
| 568 | + ****************************************************************************/ |
| 569 | + |
| 570 | +void test_nxinit_action_builtin_wait(FAR void **state) |
| 571 | +{ |
| 572 | +#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_TMPFS) |
| 573 | + FAR char *mnt[] = |
| 574 | + { |
| 575 | + "mount", "tmpfs", "none", TEST_MNT, NULL |
| 576 | + }; |
| 577 | + |
| 578 | + FAR char *umnt[] = |
| 579 | + { |
| 580 | + "umount", TEST_MNT, NULL |
| 581 | + }; |
| 582 | + |
| 583 | + FAR char *found[] = |
| 584 | + { |
| 585 | + "wait", TEST_MNT "/wait_file", NULL |
| 586 | + }; |
| 587 | + |
| 588 | + FAR char *missing[] = |
| 589 | + { |
| 590 | + "wait", TEST_MNT "/no_such_file", "0.1", NULL |
| 591 | + }; |
| 592 | + |
| 593 | + FAR char *late[] = |
| 594 | + { |
| 595 | + "wait", TEST_MNT "/wait_late", NULL |
| 596 | + }; |
| 597 | + |
| 598 | + FAR char *badnum[] = |
| 599 | + { |
| 600 | + "wait", TEST_MNT "/no_such_file", "bogus", NULL |
| 601 | + }; |
| 602 | + |
| 603 | + FAR char *negative[] = |
| 604 | + { |
| 605 | + "wait", TEST_MNT "/no_such_file", "-1", NULL |
| 606 | + }; |
| 607 | + |
| 608 | + struct action_manager_s am; |
| 609 | + struct timespec start; |
| 610 | + struct timespec end; |
| 611 | + pthread_t tid; |
| 612 | + long waited; |
| 613 | + int fd; |
| 614 | + |
| 615 | + action_manager_init(&am); |
| 616 | + assert_int_equal(init_builtin_run(&am, 4, mnt), 0); |
| 617 | + |
| 618 | + /* A missing file waits for the timeout, then warns and returns 0 */ |
| 619 | + |
| 620 | + clock_gettime(CLOCK_MONOTONIC, &start); |
| 621 | + assert_int_equal(init_builtin_run(&am, 3, missing), 0); |
| 622 | + clock_gettime(CLOCK_MONOTONIC, &end); |
| 623 | + waited = (end.tv_sec - start.tv_sec) * 1000 + |
| 624 | + (end.tv_nsec - start.tv_nsec) / 1000000; |
| 625 | + assert_true(waited >= 90); |
| 626 | + |
| 627 | + /* A file that shows up while waiting ends the wait */ |
| 628 | + |
| 629 | + clock_gettime(CLOCK_MONOTONIC, &start); |
| 630 | + assert_int_equal(pthread_create(&tid, NULL, wait_create_late_file, |
| 631 | + NULL), 0); |
| 632 | + assert_int_equal(init_builtin_run(&am, 2, late), 0); |
| 633 | + assert_int_equal(pthread_join(tid, NULL), 0); |
| 634 | + clock_gettime(CLOCK_MONOTONIC, &end); |
| 635 | + waited = (end.tv_sec - start.tv_sec) * 1000 + |
| 636 | + (end.tv_nsec - start.tv_nsec) / 1000000; |
| 637 | + assert_true(waited >= 150 && waited < 3000); |
| 638 | + assert_int_equal(unlink(TEST_MNT "/wait_late"), 0); |
| 639 | + |
| 640 | + /* A bad or negative timeout fails right away */ |
| 641 | + |
| 642 | + assert_int_equal(init_builtin_run(&am, 3, badnum), -EINVAL); |
| 643 | + assert_int_equal(init_builtin_run(&am, 3, negative), -EINVAL); |
| 644 | + |
| 645 | + /* An existing file returns 0 right away */ |
| 646 | + |
| 647 | + fd = open(TEST_MNT "/wait_file", O_CREAT | O_WRONLY, 0644); |
| 648 | + assert_true(fd >= 0); |
| 649 | + close(fd); |
| 650 | + clock_gettime(CLOCK_MONOTONIC, &start); |
| 651 | + assert_int_equal(init_builtin_run(&am, 2, found), 0); |
| 652 | + clock_gettime(CLOCK_MONOTONIC, &end); |
| 653 | + waited = (end.tv_sec - start.tv_sec) * 1000 + |
| 654 | + (end.tv_nsec - start.tv_nsec) / 1000000; |
| 655 | + assert_true(waited < 1000); |
| 656 | + assert_int_equal(unlink(TEST_MNT "/wait_file"), 0); |
| 657 | + |
| 658 | + assert_int_equal(init_builtin_run(&am, 2, umnt), 0); |
| 659 | + action_manager_free_all(&am); |
| 660 | +#else |
| 661 | + skip(); |
| 662 | +#endif |
| 663 | +} |
0 commit comments