1 // Copyright 2018 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <stdio.h>
6 #include <gtest/gtest.h>
7
8 extern "C" {
9 #include "polled_interval_checker.h"
10 }
11
12 static const int INTERVAL_DURATION = 5;
13
14 static struct timespec time_now;
15
create_interval()16 static struct polled_interval* create_interval() {
17 struct polled_interval *interval =
18 pic_polled_interval_create(INTERVAL_DURATION);
19 EXPECT_NE(interval, static_cast<struct polled_interval*>(NULL));
20 return interval;
21 }
22
TEST(PolledIntervalCheckerTest,CreateDestroy)23 TEST(PolledIntervalCheckerTest, CreateDestroy) {
24 // Create an interval, checks it is non-null.
25 struct polled_interval *interval = create_interval();
26
27 pic_polled_interval_destroy(&interval);
28
29 // Check it's been set to null.
30 EXPECT_EQ(interval, static_cast<struct polled_interval*>(NULL));
31 }
32
TEST(PolledIntervalCheckerTest,BasicFlow)33 TEST(PolledIntervalCheckerTest, BasicFlow) {
34 // Set initial time.
35 time_now.tv_sec = 1000;
36 time_now.tv_nsec = 0;
37 pic_update_current_time();
38
39 // Create interval starting at initial time.
40 struct polled_interval *interval = create_interval();
41
42 // Check it hasn't elapsed.
43 EXPECT_FALSE(pic_interval_elapsed(interval));
44
45 // Increment time by less than the interval duration.
46 time_now.tv_sec += INTERVAL_DURATION / 2;
47 pic_update_current_time();
48
49 // Check the interval hasn't elapsed yet.
50 EXPECT_FALSE(pic_interval_elapsed(interval));
51
52 // Increment time past the duration of the interval.
53 time_now.tv_sec += INTERVAL_DURATION;
54
55 // We haven't updated the current time, check the interval hasn't
56 // elapsed (that it isn't calling clock_gettime without us asking it to).
57 EXPECT_FALSE(pic_interval_elapsed(interval));
58
59 // Update time, check the interval has elapsed.
60 pic_update_current_time();
61 EXPECT_TRUE(pic_interval_elapsed(interval));
62 pic_polled_interval_destroy(&interval);
63 }
64
TEST(PolledIntervalCheckerTest,DoesNotResetAutomatically)65 TEST(PolledIntervalCheckerTest, DoesNotResetAutomatically) {
66 // Set initial time.
67 time_now.tv_sec = 1000;
68 time_now.tv_nsec = 0;
69 pic_update_current_time();
70
71 struct polled_interval *interval = create_interval();
72
73 // Sanity check.
74 EXPECT_FALSE(pic_interval_elapsed(interval));
75
76 // Increment time so the interval elapses.
77 time_now.tv_sec += INTERVAL_DURATION;
78 pic_update_current_time();
79
80 // Check the interval has elapsed.
81 EXPECT_TRUE(pic_interval_elapsed(interval));
82
83 // Increment time further.
84 time_now.tv_sec += INTERVAL_DURATION * 2;
85 pic_update_current_time();
86
87 // Check the interval has still elapsed.
88 EXPECT_TRUE(pic_interval_elapsed(interval));
89
90 // Check repeated calls return true.
91 EXPECT_TRUE(pic_interval_elapsed(interval));
92 pic_polled_interval_destroy(&interval);
93 }
94
TEST(PolledIntervalCheckerTest,Reset)95 TEST(PolledIntervalCheckerTest, Reset) {
96 // Set initial time.
97 time_now.tv_sec = 1000;
98 time_now.tv_nsec = 0;
99 pic_update_current_time();
100
101 struct polled_interval *interval = create_interval();
102
103 // Sanity check.
104 EXPECT_FALSE(pic_interval_elapsed(interval));
105
106 // Increment time so the interval elapses.
107 time_now.tv_sec += INTERVAL_DURATION;
108 pic_update_current_time();
109
110 // Check the interval has elapsed.
111 EXPECT_TRUE(pic_interval_elapsed(interval));
112
113 // Increment time further.
114 time_now.tv_sec += INTERVAL_DURATION * 2;
115 pic_update_current_time();
116
117 // Check the interval has still elapsed.
118 EXPECT_TRUE(pic_interval_elapsed(interval));
119
120 // Reset the interval.
121 pic_interval_reset(interval);
122
123 // Check it's been reset.
124 EXPECT_FALSE(pic_interval_elapsed(interval));
125
126 // Increment time to just before it should elapse again.
127 time_now.tv_sec += INTERVAL_DURATION - 1;
128 pic_update_current_time();
129
130 // Check it still has not elapsed.
131 EXPECT_FALSE(pic_interval_elapsed(interval));
132
133 // Increment time to one duration after we reset it.
134 time_now.tv_sec += 1;
135 pic_update_current_time();
136
137 // Check the interval has elapsed now.
138 EXPECT_TRUE(pic_interval_elapsed(interval));
139 pic_polled_interval_destroy(&interval);
140 }
141
142 /* Stubs */
143 extern "C" {
144
clock_gettime(clockid_t clk_id,struct timespec * tp)145 int clock_gettime(clockid_t clk_id, struct timespec *tp) {
146 *tp = time_now;
147 return 0;
148 }
149
150 } // extern "C"
151
main(int argc,char ** argv)152 int main(int argc, char **argv) {
153 ::testing::InitGoogleTest(&argc, argv);
154 return RUN_ALL_TESTS();
155 }
156