Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(140)

Side by Side Diff: base/test/test_simple_task_runner.h

Issue 2296923003: Make TestSimpleTaskRunner thread-safe. (Closed)
Patch Set: fix asan error Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | base/test/test_simple_task_runner.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_TEST_TEST_SIMPLE_TASK_RUNNER_H_ 5 #ifndef BASE_TEST_TEST_SIMPLE_TASK_RUNNER_H_
6 #define BASE_TEST_TEST_SIMPLE_TASK_RUNNER_H_ 6 #define BASE_TEST_TEST_SIMPLE_TASK_RUNNER_H_
7 7
8 #include <deque> 8 #include <deque>
9 9
10 #include "base/compiler_specific.h" 10 #include "base/compiler_specific.h"
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/single_thread_task_runner.h" 12 #include "base/single_thread_task_runner.h"
13 #include "base/synchronization/lock.h"
13 #include "base/test/test_pending_task.h" 14 #include "base/test/test_pending_task.h"
14 #include "base/threading/thread_checker.h" 15 #include "base/threading/platform_thread.h"
15 16
16 namespace base { 17 namespace base {
17 18
18 class TimeDelta; 19 class TimeDelta;
19 20
20 // TestSimpleTaskRunner is a simple TaskRunner implementation that can 21 // TestSimpleTaskRunner is a simple TaskRunner implementation that can
21 // be used for testing. It implements SingleThreadTaskRunner as that 22 // be used for testing. It implements SingleThreadTaskRunner as that
22 // interface implements SequencedTaskRunner, which in turn implements 23 // interface implements SequencedTaskRunner, which in turn implements
23 // TaskRunner, so TestSimpleTaskRunner can be passed in to a function 24 // TaskRunner, so TestSimpleTaskRunner can be passed in to a function
24 // that accepts any *TaskRunner object. 25 // that accepts any *TaskRunner object.
25 // 26 //
26 // TestSimpleTaskRunner has the following properties which make it simple: 27 // TestSimpleTaskRunner has the following properties which make it simple:
27 // 28 //
28 // - It is non-thread safe; all member functions must be called on
29 // the same thread.
30 // - Tasks are simply stored in a queue in FIFO order, ignoring delay 29 // - Tasks are simply stored in a queue in FIFO order, ignoring delay
31 // and nestability. 30 // and nestability.
32 // - Tasks aren't guaranteed to be destroyed immediately after 31 // - Tasks aren't guaranteed to be destroyed immediately after
33 // they're run. 32 // they're run.
34 // 33 //
35 // However, TestSimpleTaskRunner allows for reentrancy, in that it 34 // However, TestSimpleTaskRunner allows for reentrancy, in that it
36 // handles the running of tasks that in turn call back into itself 35 // handles the running of tasks that in turn call back into itself
37 // (e.g., to post more tasks). 36 // (e.g., to post more tasks).
38 // 37 //
39 // If you need more complicated properties, consider using this class
40 // as a template for writing a test TaskRunner implementation using
41 // TestPendingTask.
42 //
43 // Note that, like any TaskRunner, TestSimpleTaskRunner is 38 // Note that, like any TaskRunner, TestSimpleTaskRunner is
44 // ref-counted. 39 // ref-counted.
45 class TestSimpleTaskRunner : public SingleThreadTaskRunner { 40 class TestSimpleTaskRunner : public SingleThreadTaskRunner {
46 public: 41 public:
47 TestSimpleTaskRunner(); 42 TestSimpleTaskRunner();
48 43
49 // SingleThreadTaskRunner implementation. 44 // SingleThreadTaskRunner implementation.
50 bool PostDelayedTask(const tracked_objects::Location& from_here, 45 bool PostDelayedTask(const tracked_objects::Location& from_here,
51 const Closure& task, 46 const Closure& task,
52 TimeDelta delay) override; 47 TimeDelta delay) override;
53 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, 48 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
54 const Closure& task, 49 const Closure& task,
55 TimeDelta delay) override; 50 TimeDelta delay) override;
56 51
57 bool RunsTasksOnCurrentThread() const override; 52 bool RunsTasksOnCurrentThread() const override;
58 53
59 const std::deque<TestPendingTask>& GetPendingTasks() const; 54 std::deque<TestPendingTask> GetPendingTasks() const;
55 size_t NumPendingTasks() const;
60 bool HasPendingTask() const; 56 bool HasPendingTask() const;
61 base::TimeDelta NextPendingTaskDelay() const; 57 base::TimeDelta NextPendingTaskDelay() const;
62 58
63 // Clears the queue of pending tasks without running them. 59 // Clears the queue of pending tasks without running them.
64 void ClearPendingTasks(); 60 void ClearPendingTasks();
65 61
66 // Runs each current pending task in order and clears the queue. 62 // Runs each current pending task in order and clears the queue. Tasks posted
67 // Any tasks posted by the tasks are not run. 63 // by the tasks that run within this call do not run within this call. Can
68 virtual void RunPendingTasks(); 64 // only be called on the thread that created this TestSimpleTaskRunner.
65 void RunPendingTasks();
69 66
70 // Runs pending tasks until the queue is empty. 67 // Runs pending tasks until the queue is empty. Can only be called on the
68 // thread that created this TestSimpleTaskRunner.
71 void RunUntilIdle(); 69 void RunUntilIdle();
72 70
73 protected: 71 protected:
74 ~TestSimpleTaskRunner() override; 72 ~TestSimpleTaskRunner() override;
danakj 2016/09/01 21:22:18 nit: idk why this is protected.. maybe make this p
fdoray 2016/09/02 17:39:16 TestSimpleTaskRunner is ref-counted. Having a prot
danakj 2016/09/02 20:00:07 Oh I missed that since it's inheriting that. Thank
75 73
74 private:
75 // Thread on which this was instantiated.
76 const PlatformThreadRef thread_ref_ = PlatformThread::CurrentRef();
77
78 // Synchronizes access to |pending_tasks_|.
79 mutable Lock lock_;
80
76 std::deque<TestPendingTask> pending_tasks_; 81 std::deque<TestPendingTask> pending_tasks_;
77 ThreadChecker thread_checker_;
78 82
79 private:
80 DISALLOW_COPY_AND_ASSIGN(TestSimpleTaskRunner); 83 DISALLOW_COPY_AND_ASSIGN(TestSimpleTaskRunner);
81 }; 84 };
82 85
83 } // namespace base 86 } // namespace base
84 87
85 #endif // BASE_TEST_TEST_SIMPLE_TASK_RUNNER_H_ 88 #endif // BASE_TEST_TEST_SIMPLE_TASK_RUNNER_H_
OLDNEW
« no previous file with comments | « no previous file | base/test/test_simple_task_runner.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698