OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/task_scheduler/test_task_factory.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/bind_helpers.h" | |
9 #include "base/location.h" | |
10 #include "base/synchronization/waitable_event.h" | |
11 #include "testing/gtest/include/gtest/gtest.h" | |
12 | |
13 namespace base { | |
14 namespace internal { | |
15 | |
16 TestTaskFactory::TestTaskFactory(scoped_refptr<TaskRunner> task_runner, | |
17 ExecutionMode execution_mode) | |
18 : cv_(&lock_), | |
19 task_runner_(std::move(task_runner)), | |
20 execution_mode_(execution_mode) { | |
21 // Detach |thread_checker_| from the current thread. It will be attached to | |
22 // the first thread that calls ThreadCheckerImpl::CalledOnValidThread(). | |
23 thread_checker_.DetachFromThread(); | |
24 } | |
25 | |
26 TestTaskFactory::~TestTaskFactory() { | |
27 WaitForAllTasksToRun(); | |
28 } | |
29 | |
30 void TestTaskFactory::PostTask(PostNestedTask post_nested_task, | |
31 WaitableEvent* event) { | |
32 AutoLock auto_lock(lock_); | |
33 EXPECT_TRUE(task_runner_->PostTask( | |
robliao
2016/04/21 01:38:17
Given that this is a utility, EXPECT_TRUE is proba
fdoray
2016/04/22 15:54:03
Moved the EXPECT_TRUE to callers.
Do you suggest
robliao
2016/04/22 23:04:19
The overarching question here is would consumers o
fdoray
2016/04/25 14:25:01
I added comments in the header file to document th
| |
34 FROM_HERE, | |
35 Bind(&TestTaskFactory::RunTaskCallback, Unretained(this), | |
36 num_posted_tasks_++, post_nested_task, Unretained(event)))); | |
37 } | |
38 | |
39 void TestTaskFactory::WaitForAllTasksToRun() const { | |
40 AutoLock auto_lock(lock_); | |
41 while (ran_tasks_.size() < num_posted_tasks_) | |
42 cv_.Wait(); | |
43 } | |
44 | |
45 void TestTaskFactory::RunTaskCallback(size_t task_index, | |
46 PostNestedTask post_nested_task, | |
47 WaitableEvent* event) { | |
48 if (post_nested_task == PostNestedTask::YES) | |
49 PostTask(PostNestedTask::NO, nullptr); | |
50 | |
51 EXPECT_TRUE(task_runner_->RunsTasksOnCurrentThread()); | |
52 | |
53 { | |
54 AutoLock auto_lock(lock_); | |
55 | |
56 if (task_index >= num_posted_tasks_) | |
57 ADD_FAILURE() << "A task with an invalid index ran."; | |
58 | |
59 if ((execution_mode_ == ExecutionMode::SINGLE_THREADED || | |
60 execution_mode_ == ExecutionMode::SEQUENCED) && | |
61 task_index != ran_tasks_.size()) { | |
62 ADD_FAILURE() << "A task didn't run in the expected order."; | |
63 } | |
64 | |
65 if (execution_mode_ == ExecutionMode::SINGLE_THREADED) | |
66 EXPECT_TRUE(thread_checker_.CalledOnValidThread()); | |
67 | |
68 if (ran_tasks_.find(task_index) != ran_tasks_.end()) | |
69 ADD_FAILURE() << "A task ran more than once."; | |
70 ran_tasks_.insert(task_index); | |
71 | |
72 cv_.Signal(); | |
73 } | |
74 | |
75 if (event) | |
76 event->Wait(); | |
77 } | |
78 | |
79 } // namespace internal | |
80 } // namespace base | |
OLD | NEW |