huangcm
2025-09-01 53d8e046ac1bf2ebe94f671983e3d3be059df91a
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
/*
 * 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/bind_helpers.h>
#include <base/logging.h>
#include <gtest/gtest.h>
#include <future>
 
#include "message_loop_thread.h"
#include "once_timer.h"
 
using bluetooth::common::MessageLoopThread;
using bluetooth::common::OnceTimer;
 
// Allowed error between the expected and actual delay for DoInThreadDelayed().
constexpr uint32_t delay_error_ms = 3;
 
/**
 * Unit tests to verify Task Scheduler.
 */
class OnceTimerTest : 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 CancelTimerAndWait() { timer_->CancelAndWait(); }
 
 protected:
  void SetUp() override {
    ::testing::Test::SetUp();
    counter_ = 0;
    timer_ = new OnceTimer();
    promise_ = new std::promise<void>();
  }
 
  void TearDown() override {
    if (promise_ != nullptr) {
      delete promise_;
      promise_ = nullptr;
    }
    if (timer_ != nullptr) {
      delete timer_;
      timer_ = nullptr;
    }
  }
 
  int counter_;
  OnceTimer* timer_;
  std::promise<void>* promise_;
};
 
TEST_F(OnceTimerTest, initial_is_not_scheduled) {
  ASSERT_FALSE(timer_->IsScheduled());
}
 
TEST_F(OnceTimerTest, schedule_task) {
  std::string name = "test_thread";
  MessageLoopThread message_loop_thread(name);
  message_loop_thread.StartUp();
  auto future = promise_->get_future();
  std::string my_name;
  uint32_t delay_ms = 5;
 
  timer_->Schedule(message_loop_thread.GetWeakPtr(), FROM_HERE,
                   base::BindOnce(&OnceTimerTest::GetName,
                                  base::Unretained(this), &my_name, promise_),
                   base::TimeDelta::FromMilliseconds(delay_ms));
  EXPECT_TRUE(timer_->IsScheduled());
  future.get();
  ASSERT_EQ(name, my_name);
  std::this_thread::sleep_for(std::chrono::milliseconds(delay_error_ms));
  EXPECT_FALSE(timer_->IsScheduled());
}
 
TEST_F(OnceTimerTest, 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(OnceTimerTest, cancel_in_callback_no_deadlock) {
  std::string name = "test_thread";
  MessageLoopThread message_loop_thread(name);
  message_loop_thread.StartUp();
  uint32_t delay_ms = 5;
 
  timer_->Schedule(message_loop_thread.GetWeakPtr(), FROM_HERE,
                   base::BindOnce(&OnceTimerTest::CancelTimerAndWait,
                                  base::Unretained(this)),
                   base::TimeDelta::FromMilliseconds(delay_ms));
}
 
TEST_F(OnceTimerTest, cancel_single_task) {
  std::string name = "test_thread";
  MessageLoopThread message_loop_thread(name);
  message_loop_thread.StartUp();
  uint32_t delay_ms = 100000000;
  timer_->Schedule(
      message_loop_thread.GetWeakPtr(), FROM_HERE,
      base::BindOnce(&OnceTimerTest::ShouldNotHappen, base::Unretained(this)),
      base::TimeDelta::FromMilliseconds(delay_ms));
  std::this_thread::sleep_for(std::chrono::milliseconds(delay_error_ms));
  timer_->CancelAndWait();
}
 
TEST_F(OnceTimerTest, message_loop_thread_down_cancel_task) {
  std::string name = "test_thread";
  {
    MessageLoopThread message_loop_thread(name);
    message_loop_thread.StartUp();
    uint32_t delay_ms = 100000000;
    timer_->Schedule(
        message_loop_thread.GetWeakPtr(), FROM_HERE,
        base::BindOnce(&OnceTimerTest::ShouldNotHappen, base::Unretained(this)),
        base::TimeDelta::FromMilliseconds(delay_ms));
    std::this_thread::sleep_for(std::chrono::milliseconds(delay_error_ms));
  }
}
 
// Verify that if a task is being executed, then cancelling it is no-op
TEST_F(OnceTimerTest, cancel_current_task_no_effect) {
  std::string name = "test_thread";
  MessageLoopThread message_loop_thread(name);
  message_loop_thread.StartUp();
  auto future = promise_->get_future();
  uint32_t delay_ms = 5;
 
  timer_->Schedule(message_loop_thread.GetWeakPtr(), FROM_HERE,
                   base::BindOnce(&OnceTimerTest::SleepAndIncreaseCounter,
                                  base::Unretained(this), promise_, delay_ms),
                   base::TimeDelta::FromMilliseconds(delay_ms));
  EXPECT_TRUE(timer_->IsScheduled());
  future.get();
  timer_->CancelAndWait();
  ASSERT_EQ(counter_, 1);
  EXPECT_FALSE(timer_->IsScheduled());
}
 
TEST_F(OnceTimerTest, reschedule_task_dont_invoke_new_task_early) {
  std::string name = "test_thread";
  MessageLoopThread message_loop_thread(name);
  message_loop_thread.StartUp();
  uint32_t delay_ms = 5;
  timer_->Schedule(message_loop_thread.GetWeakPtr(), FROM_HERE,
                   base::DoNothing(),
                   base::TimeDelta::FromMilliseconds(delay_ms));
  std::this_thread::sleep_for(std::chrono::milliseconds(delay_ms - 2));
  timer_->Schedule(
      message_loop_thread.GetWeakPtr(), FROM_HERE,
      base::BindOnce(&OnceTimerTest::ShouldNotHappen, base::Unretained(this)),
      base::TimeDelta::FromMilliseconds(delay_ms + 1000));
  std::this_thread::sleep_for(std::chrono::milliseconds(delay_ms));
}
 
TEST_F(OnceTimerTest, reschedule_task_when_firing_dont_invoke_new_task_early) {
  std::string name = "test_thread";
  MessageLoopThread message_loop_thread(name);
  message_loop_thread.StartUp();
  uint32_t delay_ms = 5;
  timer_->Schedule(message_loop_thread.GetWeakPtr(), FROM_HERE,
                   base::DoNothing(),
                   base::TimeDelta::FromMilliseconds(delay_ms));
  std::this_thread::sleep_for(std::chrono::milliseconds(delay_ms));
  timer_->Schedule(
      message_loop_thread.GetWeakPtr(), FROM_HERE,
      base::BindOnce(&OnceTimerTest::ShouldNotHappen, base::Unretained(this)),
      base::TimeDelta::FromMilliseconds(delay_ms + 1000));
  std::this_thread::sleep_for(std::chrono::milliseconds(delay_ms));
}
 
TEST_F(OnceTimerTest, reschedule_task_when_firing_must_schedule_new_task) {
  std::string name = "test_thread";
  MessageLoopThread message_loop_thread(name);
  message_loop_thread.StartUp();
  uint32_t delay_ms = 5;
  std::string my_name;
  auto future = promise_->get_future();
  timer_->Schedule(message_loop_thread.GetWeakPtr(), FROM_HERE,
                   base::DoNothing(),
                   base::TimeDelta::FromMilliseconds(delay_ms));
  std::this_thread::sleep_for(std::chrono::milliseconds(delay_ms));
  timer_->Schedule(message_loop_thread.GetWeakPtr(), FROM_HERE,
                   base::BindOnce(&OnceTimerTest::GetName,
                                  base::Unretained(this), &my_name, promise_),
                   base::TimeDelta::FromMilliseconds(delay_ms));
  future.get();
  ASSERT_EQ(name, my_name);
}