aboutsummaryrefslogtreecommitdiff
path: root/common/repeating_timer_unittest.cc
blob: 7bf4bbc6fd94a44741c7cd2407cefb48b981db7c (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <base/bind.h>
#include <base/logging.h>
#include <gtest/gtest.h>
#include <future>

#include "bind_helpers.h"
#include "message_loop_thread.h"
#include "repeating_timer.h"

using bluetooth::common::MessageLoopThread;
using bluetooth::common::RepeatingTimer;

// Allowed error between the expected and actual delay for DoInThreadDelayed().
constexpr uint32_t delay_error_ms = 100;

/**
 * Unit tests to verify Task Scheduler.
 */
class RepeatingTimerTest : public ::testing::Test {
 public:
  void ShouldNotHappen() { FAIL() << "Should not happen"; }

  void IncreaseTaskCounter(int scheduled_tasks, std::promise<void>* promise) {
    counter_++;
    if (counter_ == scheduled_tasks) {
      promise->set_value();
    }
  }

  void GetName(std::string* name, std::promise<void>* promise) {
    char my_name[256];
    pthread_getname_np(pthread_self(), my_name, sizeof(my_name));
    name->append(my_name);
    promise->set_value();
  }

  void SleepAndIncreaseCounter(std::promise<void>* promise, int sleep_ms) {
    promise->set_value();
    std::this_thread::sleep_for(std::chrono::milliseconds(sleep_ms));
    counter_++;
  }

  void VerifyDelayTimeAndSleep(std::chrono::steady_clock::time_point start_time,
                               int interval_ms, int scheduled_tasks,
                               int task_length_ms,
                               std::promise<void>* promise) {
    auto end_time = std::chrono::steady_clock::now();
    auto actual_delay = std::chrono::duration_cast<std::chrono::milliseconds>(
        end_time - start_time);
    counter_++;
    int64_t scheduled_delay_ms = interval_ms * counter_;
    if (counter_ == scheduled_tasks) {
      promise->set_value();
    }
    ASSERT_NEAR(scheduled_delay_ms, actual_delay.count(), delay_error_ms);
    std::this_thread::sleep_for(std::chrono::milliseconds(task_length_ms));
  }

  void VerifyMultipleDelayedTasks(int scheduled_tasks, int task_length_ms,
                                  int interval_between_tasks_ms) {
    std::string name = "test_thread";
    MessageLoopThread message_loop_thread(name);
    message_loop_thread.StartUp();
    message_loop_thread.EnableRealTimeScheduling();
    auto future = promise_->get_future();
    auto start_time = std::chrono::steady_clock::now();
    timer_->SchedulePeriodic(
        message_loop_thread.GetWeakPtr(), FROM_HERE,
        base::BindRepeating(&RepeatingTimerTest::VerifyDelayTimeAndSleep,
                            base::Unretained(this), start_time,
                            interval_between_tasks_ms, scheduled_tasks,
                            task_length_ms, promise_),
        base::TimeDelta::FromMilliseconds(interval_between_tasks_ms));
    future.get();
    timer_->CancelAndWait();
  }

  void CancelRepeatingTimerAndWait() { timer_->CancelAndWait(); }

 protected:
  void SetUp() override {
    ::testing::Test::SetUp();
    counter_ = 0;
    timer_ = new RepeatingTimer();
    promise_ = new std::promise<void>();
  }

  void TearDown() override {
    if (promise_ != nullptr) {
      delete promise_;
      promise_ = nullptr;
    }
    if (timer_ != nullptr) {
      delete timer_;
      timer_ = nullptr;
    }
  }

  int counter_;
  RepeatingTimer* timer_;
  std::promise<void>* promise_;
};

TEST_F(RepeatingTimerTest, initial_is_not_scheduled) {
  ASSERT_FALSE(timer_->IsScheduled());
}

TEST_F(RepeatingTimerTest, cancel_without_scheduling) {
  std::string name = "test_thread";
  MessageLoopThread message_loop_thread(name);
  message_loop_thread.StartUp();

  EXPECT_FALSE(timer_->IsScheduled());
  timer_->CancelAndWait();
  EXPECT_FALSE(timer_->IsScheduled());
}

TEST_F(RepeatingTimerTest, periodic_run) {
  std::string name = "test_thread";
  MessageLoopThread message_loop_thread(name);
  message_loop_thread.StartUp();
  auto future = promise_->get_future();
  uint32_t delay_ms = 5;
  int num_tasks = 200;

  timer_->SchedulePeriodic(
      message_loop_thread.GetWeakPtr(), FROM_HERE,
      base::BindRepeating(&RepeatingTimerTest::IncreaseTaskCounter,
                          base::Unretained(this), num_tasks, promise_),
      base::TimeDelta::FromMilliseconds(delay_ms));
  future.get();
  ASSERT_GE(counter_, num_tasks);
  timer_->CancelAndWait();
}

TEST_F(RepeatingTimerTest, schedule_periodic_task_zero_interval) {
  std::string name = "test_thread";
  MessageLoopThread message_loop_thread(name);
  message_loop_thread.StartUp();
  uint32_t interval_ms = 0;

  ASSERT_FALSE(timer_->SchedulePeriodic(
      message_loop_thread.GetWeakPtr(), FROM_HERE,
      base::BindRepeating(&RepeatingTimerTest::ShouldNotHappen,
                          base::Unretained(this)),
      base::TimeDelta::FromMilliseconds(interval_ms)));
  std::this_thread::sleep_for(std::chrono::milliseconds(delay_error_ms));
}

// Verify that deleting the timer without cancelling it will cancel the task
TEST_F(RepeatingTimerTest, periodic_delete_without_cancel) {
  std::string name = "test_thread";
  MessageLoopThread message_loop_thread(name);
  message_loop_thread.StartUp();
  uint32_t delay_ms = 5;
  timer_->SchedulePeriodic(
      message_loop_thread.GetWeakPtr(), FROM_HERE,
      base::BindRepeating(&RepeatingTimerTest::ShouldNotHappen,
                          base::Unretained(this)),
      base::TimeDelta::FromMilliseconds(delay_ms));
  delete timer_;
  timer_ = nullptr;
  std::this_thread::sleep_for(std::chrono::milliseconds(delay_error_ms));
}

TEST_F(RepeatingTimerTest, cancel_single_task_near_fire_no_race_condition) {
  std::string name = "test_thread";
  MessageLoopThread message_loop_thread(name);
  message_loop_thread.StartUp();
  uint32_t delay_ms = 5;
  timer_->SchedulePeriodic(message_loop_thread.GetWeakPtr(), FROM_HERE,
                           base::DoNothing(),
                           base::TimeDelta::FromMilliseconds(delay_ms));
  std::this_thread::sleep_for(std::chrono::milliseconds(delay_ms));
  timer_->CancelAndWait();
}

TEST_F(RepeatingTimerTest, cancel_periodic_task) {
  std::string name = "test_thread";
  MessageLoopThread message_loop_thread(name);
  message_loop_thread.StartUp();
  uint32_t delay_ms = 5;
  int num_tasks = 5;
  auto future = promise_->get_future();

  timer_->SchedulePeriodic(
      message_loop_thread.GetWeakPtr(), FROM_HERE,
      base::BindRepeating(&RepeatingTimerTest::IncreaseTaskCounter,
                          base::Unretained(this), num_tasks, promise_),
      base::TimeDelta::FromMilliseconds(delay_ms));
  future.wait();
  timer_->CancelAndWait();
  std::this_thread::sleep_for(
      std::chrono::milliseconds(delay_ms + delay_error_ms));
  int counter = counter_;
  std::this_thread::sleep_for(
      std::chrono::milliseconds(delay_ms + delay_error_ms));
  ASSERT_EQ(counter, counter_);
}

// Schedule 10 short periodic tasks with interval 1 ms between each; verify the
// functionality
TEST_F(RepeatingTimerTest, schedule_multiple_delayed_tasks) {
  VerifyMultipleDelayedTasks(10, 0, 1);
}

// Schedule 10 periodic tasks with interval 2 ms between each and each takes 1
// ms; verify the functionality
TEST_F(RepeatingTimerTest, schedule_multiple_delayed_slow_tasks) {
  VerifyMultipleDelayedTasks(10, 1, 2);
}

TEST_F(RepeatingTimerTest,
       message_loop_thread_down_cancel_scheduled_periodic_task) {
  std::string name = "test_thread";
  MessageLoopThread message_loop_thread(name);
  message_loop_thread.StartUp();
  std::string my_name;
  auto future = promise_->get_future();
  uint32_t delay_ms = 5;
  int num_tasks = 5;

  timer_->SchedulePeriodic(
      message_loop_thread.GetWeakPtr(), FROM_HERE,
      base::BindRepeating(&RepeatingTimerTest::IncreaseTaskCounter,
                          base::Unretained(this), num_tasks, promise_),
      base::TimeDelta::FromMilliseconds(delay_ms));
  future.wait();
  message_loop_thread.ShutDown();
  std::this_thread::sleep_for(
      std::chrono::milliseconds(delay_ms + delay_error_ms));
  int counter = counter_;
  std::this_thread::sleep_for(
      std::chrono::milliseconds(delay_ms + delay_error_ms));
  ASSERT_EQ(counter, counter_);
}