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
3 // found in the LICENSE file.
5 #ifndef MOJO_SYSTEM_TEST_UTILS_H_
6 #define MOJO_SYSTEM_TEST_UTILS_H_
10 #include "base/callback_forward.h"
11 #include "base/compiler_specific.h"
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/task_runner.h"
15 #include "base/threading/thread.h"
16 #include "base/time/time.h"
18 namespace tracked_objects
{
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
);
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();
39 // Stopwatch -------------------------------------------------------------------
41 // A simple "stopwatch" for measuring time elapsed from a given starting point.
47 void Start() { start_time_
= base::TimeTicks::Now(); }
49 base::TimeDelta
Elapsed() { return base::TimeTicks::Now() - start_time_
; }
52 base::TimeTicks start_time_
;
54 DISALLOW_COPY_AND_ASSIGN(Stopwatch
);
57 // TestIOThread ----------------------------------------------------------------
61 enum Mode
{ kAutoStart
, kManualStart
};
62 explicit TestIOThread(Mode mode
);
63 // Stops the I/O thread if necessary.
66 // |Start()|/|Stop()| should only be called from the main (creation) thread.
67 // After |Stop()|, |Start()| may be called again to start a new I/O thread.
68 // |Stop()| may be called even when the I/O thread is not started.
72 void PostTask(const tracked_objects::Location
& from_here
,
73 const base::Closure
& task
);
74 void PostTaskAndWait(const tracked_objects::Location
& from_here
,
75 const base::Closure
& task
);
77 base::MessageLoopForIO
* message_loop() {
78 return static_cast<base::MessageLoopForIO
*>(io_thread_
.message_loop());
81 scoped_refptr
<base::TaskRunner
> task_runner() {
82 return message_loop()->message_loop_proxy();
86 base::Thread io_thread_
;
87 bool io_thread_started_
;
89 DISALLOW_COPY_AND_ASSIGN(TestIOThread
);
96 #endif // MOJO_SYSTEM_TEST_UTILS_H_