summaryrefslogtreecommitdiff
path: root/cras/src/tests/polled_interval_checker_unittest.cc
blob: a4aff09c3a87bd1ea47f2f8413c41a64558a3d3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2018 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <gtest/gtest.h>
#include <stdio.h>

extern "C" {
#include "polled_interval_checker.h"
}

static const int INTERVAL_DURATION = 5;

static struct timespec time_now;

static struct polled_interval* create_interval() {
  struct polled_interval* interval =
      pic_polled_interval_create(INTERVAL_DURATION);
  EXPECT_NE(interval, static_cast<struct polled_interval*>(NULL));
  return interval;
}

TEST(PolledIntervalCheckerTest, CreateDestroy) {
  // Create an interval, checks it is non-null.
  struct polled_interval* interval = create_interval();

  pic_polled_interval_destroy(&interval);

  // Check it's been set to null.
  EXPECT_EQ(interval, static_cast<struct polled_interval*>(NULL));
}

TEST(PolledIntervalCheckerTest, BasicFlow) {
  // Set initial time.
  time_now.tv_sec = 1000;
  time_now.tv_nsec = 0;
  pic_update_current_time();

  // Create interval starting at initial time.
  struct polled_interval* interval = create_interval();

  // Check it hasn't elapsed.
  EXPECT_FALSE(pic_interval_elapsed(interval));

  // Increment time by less than the interval duration.
  time_now.tv_sec += INTERVAL_DURATION / 2;
  pic_update_current_time();

  // Check the interval hasn't elapsed yet.
  EXPECT_FALSE(pic_interval_elapsed(interval));

  // Increment time past the duration of the interval.
  time_now.tv_sec += INTERVAL_DURATION;

  // We haven't updated the current time, check the interval hasn't
  // elapsed (that it isn't calling clock_gettime without us asking it to).
  EXPECT_FALSE(pic_interval_elapsed(interval));

  // Update time, check the interval has elapsed.
  pic_update_current_time();
  EXPECT_TRUE(pic_interval_elapsed(interval));
  pic_polled_interval_destroy(&interval);
}

TEST(PolledIntervalCheckerTest, DoesNotResetAutomatically) {
  // Set initial time.
  time_now.tv_sec = 1000;
  time_now.tv_nsec = 0;
  pic_update_current_time();

  struct polled_interval* interval = create_interval();

  // Initial check.
  EXPECT_FALSE(pic_interval_elapsed(interval));

  // Increment time so the interval elapses.
  time_now.tv_sec += INTERVAL_DURATION;
  pic_update_current_time();

  // Check the interval has elapsed.
  EXPECT_TRUE(pic_interval_elapsed(interval));

  // Increment time further.
  time_now.tv_sec += INTERVAL_DURATION * 2;
  pic_update_current_time();

  // Check the interval has still elapsed.
  EXPECT_TRUE(pic_interval_elapsed(interval));

  // Check repeated calls return true.
  EXPECT_TRUE(pic_interval_elapsed(interval));
  pic_polled_interval_destroy(&interval);
}

TEST(PolledIntervalCheckerTest, Reset) {
  // Set initial time.
  time_now.tv_sec = 1000;
  time_now.tv_nsec = 0;
  pic_update_current_time();

  struct polled_interval* interval = create_interval();

  // Initial check.
  EXPECT_FALSE(pic_interval_elapsed(interval));

  // Increment time so the interval elapses.
  time_now.tv_sec += INTERVAL_DURATION;
  pic_update_current_time();

  // Check the interval has elapsed.
  EXPECT_TRUE(pic_interval_elapsed(interval));

  // Increment time further.
  time_now.tv_sec += INTERVAL_DURATION * 2;
  pic_update_current_time();

  // Check the interval has still elapsed.
  EXPECT_TRUE(pic_interval_elapsed(interval));

  // Reset the interval.
  pic_interval_reset(interval);

  // Check it's been reset.
  EXPECT_FALSE(pic_interval_elapsed(interval));

  // Increment time to just before it should elapse again.
  time_now.tv_sec += INTERVAL_DURATION - 1;
  pic_update_current_time();

  // Check it still has not elapsed.
  EXPECT_FALSE(pic_interval_elapsed(interval));

  // Increment time to one duration after we reset it.
  time_now.tv_sec += 1;
  pic_update_current_time();

  // Check the interval has elapsed now.
  EXPECT_TRUE(pic_interval_elapsed(interval));
  pic_polled_interval_destroy(&interval);
}

/* Stubs */
extern "C" {

int clock_gettime(clockid_t clk_id, struct timespec* tp) {
  *tp = time_now;
  return 0;
}

}  // extern "C"

int main(int argc, char** argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}