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. 4 5 #ifndef MOJO_SYSTEM_TEST_UTILS_H_ 6 #define MOJO_SYSTEM_TEST_UTILS_H_ 7 8 #include <stdint.h> 9 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" 17 18 namespace tracked_objects { 19 class Location; 20 } 21 22 namespace mojo { 23 namespace system { 24 namespace test { 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. 42 class Stopwatch { 43 public: Stopwatch()44 Stopwatch() {} ~Stopwatch()45 ~Stopwatch() {} 46 Start()47 void Start() { 48 start_time_ = base::TimeTicks::HighResNow(); 49 } 50 Elapsed()51 base::TimeDelta Elapsed() { 52 return base::TimeTicks::HighResNow() - start_time_; 53 } 54 55 private: 56 base::TimeTicks start_time_; 57 58 DISALLOW_COPY_AND_ASSIGN(Stopwatch); 59 }; 60 61 // TestIOThread ---------------------------------------------------------------- 62 63 class TestIOThread { 64 public: 65 enum Mode { kAutoStart, kManualStart }; 66 explicit TestIOThread(Mode mode); 67 // Stops the I/O thread if necessary. 68 ~TestIOThread(); 69 70 // |Start()|/|Stop()| should only be called from the main (creation) thread. 71 // After |Stop()|, |Start()| may be called again to start a new I/O thread. 72 // |Stop()| may be called even when the I/O thread is not started. 73 void Start(); 74 void Stop(); 75 76 void PostTask(const tracked_objects::Location& from_here, 77 const base::Closure& task); 78 void PostTaskAndWait(const tracked_objects::Location& from_here, 79 const base::Closure& task); 80 message_loop()81 base::MessageLoopForIO* message_loop() { 82 return static_cast<base::MessageLoopForIO*>(io_thread_.message_loop()); 83 } 84 task_runner()85 scoped_refptr<base::TaskRunner> task_runner() { 86 return message_loop()->message_loop_proxy(); 87 } 88 89 private: 90 base::Thread io_thread_; 91 bool io_thread_started_; 92 93 DISALLOW_COPY_AND_ASSIGN(TestIOThread); 94 }; 95 96 } // namespace test 97 } // namespace system 98 } // namespace mojo 99 100 #endif // MOJO_SYSTEM_TEST_UTILS_H_ 101