summaryrefslogtreecommitdiff
path: root/cras/src/tests/cras_tm_unittest.cc
blob: 3f5521ebe69f8d125d3a2bae8876ce1b7f935c8b (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright (c) 2012 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 "cras_tm.h"
#include "cras_types.h"
}

namespace {

class TimerTestSuite : public testing::Test {
 protected:
  virtual void SetUp() {
    tm_ = cras_tm_init();
    ASSERT_TRUE(tm_);
  }

  virtual void TearDown() { cras_tm_deinit(tm_); }

  struct cras_tm* tm_;
};

static struct timespec time_now;
static unsigned int test_cb_called;
static unsigned int test_cb2_called;

void test_cb(struct cras_timer* t, void* data) {
  test_cb_called++;
}

void test_cb2(struct cras_timer* t, void* data) {
  test_cb2_called++;
}

TEST_F(TimerTestSuite, InitNoTimers) {
  struct timespec ts;
  int timers_active;

  timers_active = cras_tm_get_next_timeout(tm_, &ts);
  EXPECT_FALSE(timers_active);
}

TEST_F(TimerTestSuite, AddTimer) {
  struct cras_timer* t;

  t = cras_tm_create_timer(tm_, 10, test_cb, this);
  EXPECT_TRUE(t);
}

TEST_F(TimerTestSuite, AddLongTimer) {
  struct timespec ts;
  struct cras_timer* t;
  int timers_active;

  time_now.tv_sec = 0;
  time_now.tv_nsec = 0;
  t = cras_tm_create_timer(tm_, 10000, test_cb, this);
  EXPECT_TRUE(t);

  timers_active = cras_tm_get_next_timeout(tm_, &ts);
  ASSERT_TRUE(timers_active);
  EXPECT_EQ(10, ts.tv_sec);
  EXPECT_EQ(0, ts.tv_nsec);

  // All timers already fired.
  time_now.tv_sec = 12;
  time_now.tv_nsec = 0;
  timers_active = cras_tm_get_next_timeout(tm_, &ts);
  ASSERT_TRUE(timers_active);
  EXPECT_EQ(0, ts.tv_sec);
  EXPECT_EQ(0, ts.tv_nsec);

  cras_tm_cancel_timer(tm_, t);
  timers_active = cras_tm_get_next_timeout(tm_, &ts);
  EXPECT_FALSE(timers_active);
}

TEST_F(TimerTestSuite, AddRemoveTimer) {
  struct timespec ts;
  struct cras_timer* t;
  int timers_active;

  time_now.tv_sec = 0;
  time_now.tv_nsec = 0;
  t = cras_tm_create_timer(tm_, 10, test_cb, this);
  EXPECT_TRUE(t);

  timers_active = cras_tm_get_next_timeout(tm_, &ts);
  ASSERT_TRUE(timers_active);
  EXPECT_EQ(0, ts.tv_sec);
  EXPECT_EQ(10 * 1000000, ts.tv_nsec);

  // All timers already fired.
  time_now.tv_sec = 1;
  time_now.tv_nsec = 0;
  timers_active = cras_tm_get_next_timeout(tm_, &ts);
  ASSERT_TRUE(timers_active);
  EXPECT_EQ(0, ts.tv_sec);
  EXPECT_EQ(0, ts.tv_nsec);

  cras_tm_cancel_timer(tm_, t);
  timers_active = cras_tm_get_next_timeout(tm_, &ts);
  EXPECT_FALSE(timers_active);
}

TEST_F(TimerTestSuite, AddTwoTimers) {
  struct timespec ts;
  struct cras_timer *t1, *t2;
  int timers_active;
  static const unsigned int t1_to = 10;
  static const unsigned int t2_offset = 5;
  static const unsigned int t2_to = 7;

  time_now.tv_sec = 0;
  time_now.tv_nsec = 0;
  t1 = cras_tm_create_timer(tm_, t1_to, test_cb, this);
  ASSERT_TRUE(t1);

  time_now.tv_sec = 0;
  time_now.tv_nsec = t2_offset;
  t2 = cras_tm_create_timer(tm_, t2_to, test_cb2, this);
  ASSERT_TRUE(t2);

  /* Check That the right calls are made at the right times. */
  test_cb_called = 0;
  test_cb2_called = 0;
  time_now.tv_sec = 0;
  time_now.tv_nsec = t2_to * 1000000 + t2_offset;
  cras_tm_call_callbacks(tm_);
  EXPECT_EQ(0, test_cb_called);
  EXPECT_EQ(1, test_cb2_called);
  timers_active = cras_tm_get_next_timeout(tm_, &ts);
  ASSERT_TRUE(timers_active);

  time_now.tv_sec = 0;
  time_now.tv_nsec = t2_offset;
  t2 = cras_tm_create_timer(tm_, t2_to, test_cb2, this);
  ASSERT_TRUE(t2);

  test_cb_called = 0;
  test_cb2_called = 0;
  time_now.tv_sec = 0;
  time_now.tv_nsec = t1_to * 1000000;
  cras_tm_call_callbacks(tm_);
  EXPECT_EQ(1, test_cb_called);
  EXPECT_EQ(1, test_cb2_called);
  timers_active = cras_tm_get_next_timeout(tm_, &ts);
  EXPECT_FALSE(timers_active);

  time_now.tv_sec = 0;
  time_now.tv_nsec = 0;
  t1 = cras_tm_create_timer(tm_, t1_to, test_cb, this);
  ASSERT_TRUE(t1);

  time_now.tv_sec = 0;
  time_now.tv_nsec = t2_offset;
  t2 = cras_tm_create_timer(tm_, t2_to, test_cb2, this);
  ASSERT_TRUE(t2);

  /* Timeout values returned are correct. */
  time_now.tv_sec = 0;
  time_now.tv_nsec = 50;
  timers_active = cras_tm_get_next_timeout(tm_, &ts);
  ASSERT_TRUE(timers_active);
  EXPECT_EQ(0, ts.tv_sec);
  EXPECT_EQ(t2_to * 1000000 + t2_offset - time_now.tv_nsec, ts.tv_nsec);

  cras_tm_cancel_timer(tm_, t2);

  time_now.tv_sec = 0;
  time_now.tv_nsec = 60;
  timers_active = cras_tm_get_next_timeout(tm_, &ts);
  ASSERT_TRUE(timers_active);
  EXPECT_EQ(0, ts.tv_sec);
  EXPECT_EQ(t1_to * 1000000 - time_now.tv_nsec, ts.tv_nsec);
  cras_tm_cancel_timer(tm_, t1);
}

/* Stubs */
extern "C" {

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

}  // extern "C"

}  //  namespace

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