OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 MOJO_SYSTEM_TEST_UTILS_H_ | 5 #ifndef MOJO_SYSTEM_TEST_UTILS_H_ |
6 #define MOJO_SYSTEM_TEST_UTILS_H_ | 6 #define MOJO_SYSTEM_TEST_UTILS_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "base/callback_forward.h" | 10 #include "base/callback_forward.h" |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/task_runner.h" | 14 #include "base/task_runner.h" |
15 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
17 | 17 |
18 namespace tracked_objects { | 18 namespace tracked_objects { |
19 class Location; | 19 class Location; |
20 } | 20 } |
21 | 21 |
22 namespace mojo { | 22 namespace mojo { |
23 namespace system { | 23 namespace system { |
24 namespace test { | 24 namespace test { |
25 | 25 |
| 26 // Posts the given task (to the given task runner) and waits for it to complete. |
| 27 // (Note: Doesn't spin the current thread's message loop, so if you're careless |
| 28 // this could easily deadlock.) |
| 29 void PostTaskAndWait(scoped_refptr<base::TaskRunner> task_runner, |
| 30 const tracked_objects::Location& from_here, |
| 31 const base::Closure& task); |
| 32 |
| 33 // A timeout smaller than |TestTimeouts::tiny_timeout()|. Warning: This may lead |
| 34 // to flakiness, but this is unavoidable if, e.g., you're trying to ensure that |
| 35 // functions with timeouts are reasonably accurate. We want this to be as small |
| 36 // as possible without causing too much flakiness. |
| 37 base::TimeDelta EpsilonTimeout(); |
| 38 |
| 39 // Stopwatch ------------------------------------------------------------------- |
| 40 |
| 41 // A simple "stopwatch" for measuring time elapsed from a given starting point. |
26 class Stopwatch { | 42 class Stopwatch { |
27 public: | 43 public: |
28 Stopwatch() {} | 44 Stopwatch() {} |
29 ~Stopwatch() {} | 45 ~Stopwatch() {} |
30 | 46 |
31 void Start() { | 47 void Start() { |
32 start_time_ = base::TimeTicks::HighResNow(); | 48 start_time_ = base::TimeTicks::HighResNow(); |
33 } | 49 } |
34 | 50 |
35 int64_t Elapsed() { | 51 base::TimeDelta Elapsed() { |
36 return (base::TimeTicks::HighResNow() - start_time_).InMicroseconds(); | 52 return base::TimeTicks::HighResNow() - start_time_; |
37 } | 53 } |
38 | 54 |
39 private: | 55 private: |
40 base::TimeTicks start_time_; | 56 base::TimeTicks start_time_; |
41 | 57 |
42 DISALLOW_COPY_AND_ASSIGN(Stopwatch); | 58 DISALLOW_COPY_AND_ASSIGN(Stopwatch); |
43 }; | 59 }; |
44 | 60 |
45 // Posts the given task (to the given task runner) and waits for it to complete. | |
46 // (Note: Doesn't spin the current thread's message loop, so if you're careless | |
47 // this could easily deadlock.) | |
48 void PostTaskAndWait(scoped_refptr<base::TaskRunner> task_runner, | |
49 const tracked_objects::Location& from_here, | |
50 const base::Closure& task); | |
51 | |
52 // TestIOThread ---------------------------------------------------------------- | 61 // TestIOThread ---------------------------------------------------------------- |
53 | 62 |
54 class TestIOThread { | 63 class TestIOThread { |
55 public: | 64 public: |
56 enum Mode { kAutoStart, kManualStart }; | 65 enum Mode { kAutoStart, kManualStart }; |
57 explicit TestIOThread(Mode mode); | 66 explicit TestIOThread(Mode mode); |
58 // Stops the I/O thread if necessary. | 67 // Stops the I/O thread if necessary. |
59 ~TestIOThread(); | 68 ~TestIOThread(); |
60 | 69 |
61 // |Start()|/|Stop()| should only be called from the main (creation) thread. | 70 // |Start()|/|Stop()| should only be called from the main (creation) thread. |
(...skipping 20 matching lines...) Expand all Loading... |
82 bool io_thread_started_; | 91 bool io_thread_started_; |
83 | 92 |
84 DISALLOW_COPY_AND_ASSIGN(TestIOThread); | 93 DISALLOW_COPY_AND_ASSIGN(TestIOThread); |
85 }; | 94 }; |
86 | 95 |
87 } // namespace test | 96 } // namespace test |
88 } // namespace system | 97 } // namespace system |
89 } // namespace mojo | 98 } // namespace mojo |
90 | 99 |
91 #endif // MOJO_SYSTEM_TEST_UTILS_H_ | 100 #endif // MOJO_SYSTEM_TEST_UTILS_H_ |
OLD | NEW |