thread_test_helper.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright (c) 2011 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. #ifndef BASE_TEST_THREAD_TEST_HELPER_H_
  5. #define BASE_TEST_THREAD_TEST_HELPER_H_
  6. #include "base/compiler_specific.h"
  7. #include "base/macros.h"
  8. #include "base/memory/ref_counted.h"
  9. #include "base/sequenced_task_runner.h"
  10. #include "base/synchronization/waitable_event.h"
  11. namespace base {
  12. // Helper class that executes code on a given target sequence/thread while
  13. // blocking on the invoking sequence/thread. To use, derive from this class and
  14. // overwrite RunTest. An alternative use of this class is to use it directly. It
  15. // will then block until all pending tasks on a given sequence/thread have been
  16. // executed.
  17. class ThreadTestHelper : public RefCountedThreadSafe<ThreadTestHelper> {
  18. public:
  19. explicit ThreadTestHelper(scoped_refptr<SequencedTaskRunner> target_sequence);
  20. // True if RunTest() was successfully executed on the target sequence.
  21. bool Run() WARN_UNUSED_RESULT;
  22. virtual void RunTest();
  23. protected:
  24. friend class RefCountedThreadSafe<ThreadTestHelper>;
  25. virtual ~ThreadTestHelper();
  26. // Use this method to store the result of RunTest().
  27. void set_test_result(bool test_result) { test_result_ = test_result; }
  28. private:
  29. void RunOnSequence();
  30. bool test_result_;
  31. scoped_refptr<SequencedTaskRunner> target_sequence_;
  32. WaitableEvent done_event_;
  33. DISALLOW_COPY_AND_ASSIGN(ThreadTestHelper);
  34. };
  35. } // namespace base
  36. #endif // BASE_TEST_THREAD_TEST_HELPER_H_