File: task_runner_unittest.cc

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (286 lines) | stat: -rw-r--r-- 8,400 bytes parent folder | download | duplicates (10)
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright 2019 The Chromium 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 "platform/impl/task_runner.h"

#include <unistd.h>

#include <atomic>
#include <chrono>
#include <string>
#include <thread>

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "platform/api/time.h"
#include "platform/test/fake_clock.h"
#include "util/chrono_helpers.h"
namespace openscreen {
namespace {

using ::testing::_;

const auto kTaskRunnerSleepTime = milliseconds(1);
constexpr Clock::duration kWaitTimeout = milliseconds(1000);

void WaitUntilCondition(std::function<bool()> predicate) {
  while (!predicate()) {
    std::this_thread::sleep_for(kTaskRunnerSleepTime);
  }
}

class FakeTaskWaiter final : public TaskRunnerImpl::TaskWaiter {
 public:
  explicit FakeTaskWaiter(ClockNowFunctionPtr now_function)
      : now_function_(now_function) {}
  ~FakeTaskWaiter() override = default;

  Error WaitForTaskToBePosted(Clock::duration timeout) override {
    Clock::time_point start = now_function_();
    waiting_.store(true);
    while (!has_event_.load() && (now_function_() - start) < timeout) {
      EXPECT_EQ(usleep(100 /* microseconds */), 0);
    }
    waiting_.store(false);
    has_event_.store(false);
    return Error::None();
  }

  void OnTaskPosted() override { has_event_.store(true); }

  void WakeUpAndStop() {
    OnTaskPosted();
    task_runner_->RequestStopSoon();
  }

  bool IsWaiting() const { return waiting_.load(); }

  void SetTaskRunner(TaskRunnerImpl* task_runner) {
    task_runner_ = task_runner;
  }

 private:
  const ClockNowFunctionPtr now_function_;
  TaskRunnerImpl* task_runner_;
  std::atomic<bool> has_event_{false};
  std::atomic<bool> waiting_{false};
};

class TaskRunnerWithWaiterFactory {
 public:
  static std::unique_ptr<TaskRunnerImpl> Create(
      ClockNowFunctionPtr now_function) {
    fake_waiter = std::make_unique<FakeTaskWaiter>(now_function);
    auto runner = std::make_unique<TaskRunnerImpl>(
        now_function, fake_waiter.get(), std::chrono::hours(1));
    fake_waiter->SetTaskRunner(runner.get());
    return runner;
  }

  static std::unique_ptr<FakeTaskWaiter> fake_waiter;
};

// static
std::unique_ptr<FakeTaskWaiter> TaskRunnerWithWaiterFactory::fake_waiter;

}  // anonymous namespace

TEST(TaskRunnerImplTest, TaskRunnerExecutesTaskAndStops) {
  FakeClock fake_clock{Clock::time_point(milliseconds(1337))};
  TaskRunnerImpl runner(&fake_clock.now);

  std::string ran_tasks = "";
  runner.PostTask([&ran_tasks] { ran_tasks += "1"; });
  runner.RequestStopSoon();

  runner.RunUntilStopped();
  EXPECT_EQ(ran_tasks, "1");
}

TEST(TaskRunnerImplTest, TaskRunnerRunsDelayedTasksInOrder) {
  FakeClock fake_clock{Clock::time_point(milliseconds(1337))};
  TaskRunnerImpl runner(&fake_clock.now);

  std::thread t([&runner] { runner.RunUntilStopped(); });

  std::string ran_tasks = "";

  const auto kDelayTime = milliseconds(5);
  const auto task_one = [&ran_tasks] { ran_tasks += "1"; };
  runner.PostTaskWithDelay(task_one, kDelayTime);

  const auto task_two = [&ran_tasks] { ran_tasks += "2"; };
  runner.PostTaskWithDelay(task_two, kDelayTime * 2);

  EXPECT_EQ(ran_tasks, "");
  fake_clock.Advance(kDelayTime);
  WaitUntilCondition([&ran_tasks] { return ran_tasks == "1"; });
  EXPECT_EQ(ran_tasks, "1");

  fake_clock.Advance(kDelayTime);
  WaitUntilCondition([&ran_tasks] { return ran_tasks == "12"; });
  EXPECT_EQ(ran_tasks, "12");

  runner.RequestStopSoon();
  t.join();
}

TEST(TaskRunnerImplTest, SingleThreadedTaskRunnerRunsSequentially) {
  FakeClock fake_clock{Clock::time_point(milliseconds(1337))};
  TaskRunnerImpl runner(&fake_clock.now);

  std::string ran_tasks;
  const auto task_one = [&ran_tasks] { ran_tasks += "1"; };
  const auto task_two = [&ran_tasks] { ran_tasks += "2"; };
  const auto task_three = [&ran_tasks] { ran_tasks += "3"; };
  const auto task_four = [&ran_tasks] { ran_tasks += "4"; };
  const auto task_five = [&ran_tasks] { ran_tasks += "5"; };

  runner.PostTask(task_one);
  runner.PostTask(task_two);
  runner.PostTask(task_three);
  runner.PostTask(task_four);
  runner.PostTask(task_five);
  runner.RequestStopSoon();
  EXPECT_EQ(ran_tasks, "");

  runner.RunUntilStopped();
  EXPECT_EQ(ran_tasks, "12345");
}

TEST(TaskRunnerImplTest, RunsAllImmediateTasksBeforeStopping) {
  FakeClock fake_clock{Clock::time_point(milliseconds(1337))};
  TaskRunnerImpl runner(&fake_clock.now);

  std::string result;
  runner.PostTask([&] {
    result += "Alice";

    // Post a task that runs just before the quit task.
    runner.PostTask([&] {
      result += " says goodbye";

      // These tasks will enter the queue after the quit task *and* after the
      // main loop breaks. They will be executed by the flushing phase.
      runner.PostTask([&] {
        result += " and is not";
        runner.PostTask([&] { result += " forgotten."; });
      });
    });

    // Post the quit task.
    runner.RequestStopSoon();
  });

  EXPECT_EQ(result, "");
  runner.RunUntilStopped();
  // All posted tasks will execute because RequestStopSoon() guarantees all
  // immediately-runnable tasks will run before exiting, even if new
  // immediately-runnable tasks are posted in the meantime.
  EXPECT_EQ(result, "Alice says goodbye and is not forgotten.");
}

TEST(TaskRunnerImplTest, TaskRunnerIsStableWithLotsOfTasks) {
  FakeClock fake_clock{Clock::time_point(milliseconds(1337))};
  TaskRunnerImpl runner(&fake_clock.now);

  const int kNumberOfTasks = 500;
  std::string expected_ran_tasks;
  expected_ran_tasks.append(kNumberOfTasks, '1');

  std::string ran_tasks;
  for (int i = 0; i < kNumberOfTasks; ++i) {
    const auto task = [&ran_tasks] { ran_tasks += "1"; };
    runner.PostTask(task);
  }

  runner.RequestStopSoon();
  runner.RunUntilStopped();
  EXPECT_EQ(ran_tasks, expected_ran_tasks);
}

TEST(TaskRunnerImplTest, TaskRunnerDelayedTasksDontBlockImmediateTasks) {
  TaskRunnerImpl runner(Clock::now);

  std::string ran_tasks;
  const auto task = [&ran_tasks] { ran_tasks += "1"; };
  const auto delayed_task = [&ran_tasks] { ran_tasks += "A"; };

  runner.PostTaskWithDelay(delayed_task, milliseconds(10000));
  runner.PostTask(task);

  runner.RequestStopSoon();
  runner.RunUntilStopped();
  // The immediate task should have run, even though the delayed task
  // was added first.

  EXPECT_EQ(ran_tasks, "1");
}

TEST(TaskRunnerImplTest, TaskRunnerUsesEventWaiter) {
  std::unique_ptr<TaskRunnerImpl> runner =
      TaskRunnerWithWaiterFactory::Create(Clock::now);

  std::atomic<int> x{0};
  std::thread t([&runner, &x] {
    runner.get()->RunUntilStopped();
    x = 1;
  });

  const Clock::time_point start1 = Clock::now();
  FakeTaskWaiter* fake_waiter = TaskRunnerWithWaiterFactory::fake_waiter.get();
  while ((Clock::now() - start1) < kWaitTimeout && !fake_waiter->IsWaiting()) {
    std::this_thread::sleep_for(kTaskRunnerSleepTime);
  }
  ASSERT_TRUE(fake_waiter->IsWaiting());

  fake_waiter->WakeUpAndStop();
  const Clock::time_point start2 = Clock::now();
  while ((Clock::now() - start2) < kWaitTimeout && x == 0) {
    std::this_thread::sleep_for(kTaskRunnerSleepTime);
  }
  ASSERT_EQ(x, 1);
  ASSERT_FALSE(fake_waiter->IsWaiting());
  t.join();
}

TEST(TaskRunnerImplTest, WakesEventWaiterOnPostTask) {
  std::unique_ptr<TaskRunnerImpl> runner =
      TaskRunnerWithWaiterFactory::Create(Clock::now);

  std::atomic<int> x{0};
  std::thread t([&runner] { runner.get()->RunUntilStopped(); });

  const Clock::time_point start1 = Clock::now();
  FakeTaskWaiter* fake_waiter = TaskRunnerWithWaiterFactory::fake_waiter.get();
  while ((Clock::now() - start1) < kWaitTimeout && !fake_waiter->IsWaiting()) {
    std::this_thread::sleep_for(kTaskRunnerSleepTime);
  }
  ASSERT_TRUE(fake_waiter->IsWaiting());

  runner->PostTask([&x]() { x = 1; });
  const Clock::time_point start2 = Clock::now();
  while ((Clock::now() - start2) < kWaitTimeout && x == 0) {
    std::this_thread::sleep_for(kTaskRunnerSleepTime);
  }
  ASSERT_EQ(x, 1);

  fake_waiter->WakeUpAndStop();
  t.join();
}

class RepeatedClass {
 public:
  MOCK_METHOD0(Repeat, absl::optional<Clock::duration>());

  absl::optional<Clock::duration> DoCall() {
    auto result = Repeat();
    execution_count++;
    return result;
  }

  std::atomic<int> execution_count{0};
};

}  // namespace openscreen