1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <osTest.h>
32 #include <unistd.h>
33 #include <signal.h>
34 #include <time.h>
35 #include <sys/time.h>
36 #include <inttypes.h>
37 #include "lt_timer_test.h"
38
39 #define SIG SIGALRM
40 #define CLOCKID CLOCK_REALTIME
41
SetTimerTest(void)42 static int SetTimerTest(void)
43 {
44 int ret = 0;
45 int sig = 0;
46 int failed = 0;
47 timer_t timerid;
48 sigset_t set, oldSet;
49 struct sigevent sev;
50
51 ret = sigemptyset(&set);
52 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
53
54 ret = sigaddset(&set, SIG);
55 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
56
57 ret = sigprocmask(SIG_BLOCK, &set, &oldSet);
58 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
59
60 /* Create the timer */
61 sev.sigev_notify = SIGEV_SIGNAL;
62 sev.sigev_signo = SIG;
63 sev.sigev_value.sival_ptr = &timerid;
64 ret = timer_create(CLOCKID, &sev, &timerid);
65 LogPrintln("timer_create %p: %d", timerid, ret);
66 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
67
68 struct timespec testcases[] = {
69 {0, 30000000},
70 {1, 0},
71 {1, 5},
72 {1, 5000},
73 {1, 30000000},
74 {2, 0},
75 }, zero = {0, 0};
76
77 for (int i = 0; i < sizeof(testcases) / sizeof(testcases[0]); ++i) {
78 struct timespec start, end;
79 struct itimerspec its;
80 int64_t expected, escaped;
81
82 its.it_interval = zero;
83 its.it_value = testcases[i];
84
85 ret = clock_gettime(CLOCKID, &start);
86 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
87
88 ret = timer_settime(timerid, 0, &its, nullptr);
89 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
90
91 ret = sigwait(&set, &sig);
92 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
93
94 ret = clock_gettime(CLOCKID, &end);
95 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
96
97 expected = its.it_value.tv_sec * static_cast<int64_t>(1e9) + its.it_value.tv_nsec;
98 escaped = end.tv_sec * static_cast<int64_t>(1e9) + end.tv_nsec - start.tv_sec * static_cast<int64_t>(1e9) -
99 start.tv_nsec;
100
101 failed += (escaped < expected || (escaped - expected) >= 20000000); // 20000000, 2 ticks.
102 }
103
104 ret = timer_delete(timerid);
105 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
106
107 ret = sigprocmask(SIG_SETMASK, &oldSet, nullptr);
108 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
109
110 ICUNIT_ASSERT_EQUAL(failed, 0, failed);
111
112 return 0;
113 }
114
TimerTest003(void)115 void TimerTest003(void)
116 {
117 TEST_ADD_CASE(__FUNCTION__, SetTimerTest, TEST_POSIX, TEST_SWTMR, TEST_LEVEL0, TEST_FUNCTION);
118 }
119