|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 Tobias Svehagen |
| 3 | + * Copyright (c) 2025 Atym, Inc. |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: Apache-2.0 |
| 6 | + */ |
| 7 | +#include "_main.h" |
| 8 | + |
| 9 | +ZTEST_F(timerfd, test_expire_then_read) |
| 10 | +{ |
| 11 | + struct itimerspec ts = { |
| 12 | + .it_interval = {0, 100 * NSEC_PER_MSEC}, |
| 13 | + .it_value = {0, 100 * NSEC_PER_MSEC}, |
| 14 | + }; |
| 15 | + timerfd_t val; |
| 16 | + int ret; |
| 17 | + |
| 18 | + zassert_ok(timerfd_settime(fixture->fd, 0, &ts, NULL)); |
| 19 | + k_sleep(K_MSEC(550)); |
| 20 | + |
| 21 | + ret = read(fixture->fd, &val, sizeof(val)); |
| 22 | + zassert_true(ret == 8, "read ret %d", ret); |
| 23 | + zassert_true(val == 5, "val == %lld", val); |
| 24 | +} |
| 25 | + |
| 26 | +ZTEST_F(timerfd, test_not_started_shall_not_unblock) |
| 27 | +{ |
| 28 | + short event; |
| 29 | + int ret; |
| 30 | + |
| 31 | + event = POLLIN; |
| 32 | + ret = is_blocked(fixture->fd, &event); |
| 33 | + zassert_equal(ret, 1, "timerfd unblocked by expiry"); |
| 34 | +} |
| 35 | + |
| 36 | +ZTEST_F(timerfd, test_poll_timeout) |
| 37 | +{ |
| 38 | + struct pollfd pfd; |
| 39 | + int ret; |
| 40 | + |
| 41 | + pfd.fd = fixture->fd; |
| 42 | + pfd.events = POLLIN; |
| 43 | + |
| 44 | + ret = poll(&pfd, 1, 500); |
| 45 | + zassert_true(ret == 0, "poll ret %d", ret); |
| 46 | +} |
| 47 | + |
| 48 | +ZTEST_F(timerfd, test_set_poll_event_block) |
| 49 | +{ |
| 50 | + reopen(&fixture->fd, CLOCK_MONOTONIC, 0); |
| 51 | + zassert_ok(ioctl(fixture->fd, TFD_IOC_SET_TICKS, (uint64_t)TESTVAL)); |
| 52 | + timerfd_poll_set_common(fixture->fd); |
| 53 | +} |
| 54 | + |
| 55 | +ZTEST_F(timerfd, test_unset_poll_event_block) |
| 56 | +{ |
| 57 | + timerfd_poll_unset_common(fixture->fd); |
| 58 | +} |
| 59 | + |
| 60 | +K_THREAD_STACK_DEFINE(thread_stack, CONFIG_TEST_STACK_SIZE); |
| 61 | +static struct k_thread thread; |
| 62 | + |
| 63 | +static void thread_timerfd_read_1(void *arg1, void *arg2, void *arg3) |
| 64 | +{ |
| 65 | + uint64_t value; |
| 66 | + struct timerfd_fixture *fixture = arg1; |
| 67 | + |
| 68 | + zassert_equal(read(fixture->fd, &value, sizeof(value)), sizeof(value)); |
| 69 | + zassert_equal(value, 1); |
| 70 | +} |
| 71 | + |
| 72 | +ZTEST_F(timerfd, test_read_then_expire_block) |
| 73 | +{ |
| 74 | + struct itimerspec ts = { |
| 75 | + .it_interval = {0, 100 * NSEC_PER_MSEC}, |
| 76 | + .it_value = {0, 100 * NSEC_PER_MSEC}, |
| 77 | + }; |
| 78 | + |
| 79 | + k_thread_create(&thread, thread_stack, K_THREAD_STACK_SIZEOF(thread_stack), |
| 80 | + thread_timerfd_read_1, fixture, NULL, NULL, 0, 0, K_NO_WAIT); |
| 81 | + |
| 82 | + zassert_ok(timerfd_settime(fixture->fd, 0, &ts, NULL)); |
| 83 | + |
| 84 | + k_thread_join(&thread, K_FOREVER); |
| 85 | +} |
| 86 | + |
| 87 | +static void thread_timerfd_close(void *arg1, void *arg2, void *arg3) |
| 88 | +{ |
| 89 | + struct timerfd_fixture *fixture = arg1; |
| 90 | + |
| 91 | + zassert_ok(close(fixture->fd)); |
| 92 | +} |
| 93 | + |
| 94 | +ZTEST_F(timerfd, test_read_then_close_block) |
| 95 | +{ |
| 96 | + timerfd_t value; |
| 97 | + |
| 98 | + struct itimerspec ts = { |
| 99 | + .it_interval = {1, 0}, |
| 100 | + .it_value = {1, 0}, |
| 101 | + }; |
| 102 | + |
| 103 | + k_thread_create(&thread, thread_stack, K_THREAD_STACK_SIZEOF(thread_stack), |
| 104 | + thread_timerfd_close, fixture, NULL, NULL, 0, 0, K_MSEC(100)); |
| 105 | + |
| 106 | + zassert_ok(timerfd_settime(fixture->fd, 0, &ts, NULL)); |
| 107 | + |
| 108 | + zassert_equal(read(fixture->fd, &value, sizeof(value)), -1); |
| 109 | + |
| 110 | + k_thread_join(&thread, K_FOREVER); |
| 111 | +} |
| 112 | + |
| 113 | +ZTEST_F(timerfd, test_expire_while_pollin) |
| 114 | +{ |
| 115 | + struct itimerspec ts = { |
| 116 | + .it_value = {0, 100 * NSEC_PER_MSEC}, |
| 117 | + }; |
| 118 | + |
| 119 | + struct zsock_pollfd fds[] = { |
| 120 | + { |
| 121 | + .fd = fixture->fd, |
| 122 | + .events = ZSOCK_POLLIN, |
| 123 | + }, |
| 124 | + }; |
| 125 | + timerfd_t value; |
| 126 | + int ret; |
| 127 | + |
| 128 | + zassert_ok(timerfd_settime(fixture->fd, 0, &ts, NULL)); |
| 129 | + |
| 130 | + /* Expect 1 event */ |
| 131 | + ret = zsock_poll(fds, ARRAY_SIZE(fds), 200); |
| 132 | + zassert_equal(ret, 1); |
| 133 | + |
| 134 | + zassert_equal(fds[0].revents, ZSOCK_POLLIN); |
| 135 | + |
| 136 | + /* Check value */ |
| 137 | + zassert_equal(read(fixture->fd, &value, sizeof(value)), sizeof(value)); |
| 138 | + zassert_equal(value, 1); |
| 139 | +} |
0 commit comments