123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- #ifndef BASE_TEST_TASK_ENVIRONMENT_H_
- #define BASE_TEST_TASK_ENVIRONMENT_H_
- #include <memory>
- #include "base/compiler_specific.h"
- #include "base/macros.h"
- #include "base/memory/ref_counted.h"
- #include "base/observer_list.h"
- #include "base/single_thread_task_runner.h"
- #include "base/task/lazy_thread_pool_task_runner.h"
- #include "base/task/sequence_manager/sequence_manager.h"
- #include "base/test/scoped_run_loop_timeout.h"
- #include "base/threading/thread_checker.h"
- #include "base/time/time.h"
- #include "base/traits_bag.h"
- #include "build/build_config.h"
- namespace base {
- class Clock;
- class FileDescriptorWatcher;
- class SimpleTaskExecutor;
- class TickClock;
- namespace subtle {
- class ScopedTimeClockOverrides;
- }
- namespace test {
- class TaskEnvironment {
- protected:
-
-
-
-
-
- struct SubclassCreatesDefaultTaskRunner {};
- public:
- enum class TimeSource {
-
- SYSTEM_TIME,
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- MOCK_TIME,
- DEFAULT = SYSTEM_TIME
- };
-
-
-
-
- enum class MainThreadType {
-
- DEFAULT,
-
- UI,
-
-
- IO,
- };
-
-
- enum class ThreadPoolExecutionMode {
-
-
-
-
- QUEUED,
-
-
-
-
- ASYNC,
- DEFAULT = ASYNC
- };
- enum class ThreadingMode {
-
-
- MULTIPLE_THREADS,
-
-
-
- MAIN_THREAD_ONLY,
- DEFAULT = MULTIPLE_THREADS
- };
-
-
- enum class ThreadPoolCOMEnvironment {
-
- NONE,
-
- COM_MTA,
-
-
-
-
-
-
-
-
-
-
- DEFAULT = COM_MTA,
- };
-
- struct ValidTraits {
- ValidTraits(TimeSource);
- ValidTraits(MainThreadType);
- ValidTraits(ThreadPoolExecutionMode);
- ValidTraits(SubclassCreatesDefaultTaskRunner);
- ValidTraits(ThreadingMode);
- ValidTraits(ThreadPoolCOMEnvironment);
- };
-
-
- template <typename... TaskEnvironmentTraits,
- class CheckArgumentsAreValid = std::enable_if_t<
- trait_helpers::AreValidTraits<ValidTraits,
- TaskEnvironmentTraits...>::value>>
- NOINLINE explicit TaskEnvironment(TaskEnvironmentTraits... traits)
- : TaskEnvironment(
- trait_helpers::GetEnum<TimeSource, TimeSource::DEFAULT>(traits...),
- trait_helpers::GetEnum<MainThreadType, MainThreadType::DEFAULT>(
- traits...),
- trait_helpers::GetEnum<ThreadPoolExecutionMode,
- ThreadPoolExecutionMode::DEFAULT>(traits...),
- trait_helpers::GetEnum<ThreadingMode, ThreadingMode::DEFAULT>(
- traits...),
- trait_helpers::GetEnum<ThreadPoolCOMEnvironment,
- ThreadPoolCOMEnvironment::DEFAULT>(
- traits...),
- trait_helpers::HasTrait<SubclassCreatesDefaultTaskRunner,
- TaskEnvironmentTraits...>(),
- trait_helpers::NotATraitTag()) {}
-
-
- virtual ~TaskEnvironment();
-
- scoped_refptr<base::SingleThreadTaskRunner> GetMainThreadTaskRunner();
-
-
- bool MainThreadIsIdle() const;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- void RunUntilIdle();
-
-
-
-
-
-
-
- void FastForwardBy(TimeDelta delta);
-
-
-
-
-
- void FastForwardUntilNoTasksRemain();
-
-
-
-
-
-
-
-
-
-
-
-
-
- void AdvanceClock(TimeDelta delta);
-
-
- const TickClock* GetMockTickClock() const;
- std::unique_ptr<TickClock> DeprecatedGetMockTickClock();
-
-
-
-
-
-
- const Clock* GetMockClock() const;
-
-
-
-
-
- base::TimeTicks NowTicks() const;
-
-
-
-
- size_t GetPendingMainThreadTaskCount() const;
-
-
-
- TimeDelta NextMainThreadPendingTaskDelay() const;
-
-
-
- bool NextTaskIsDelayed() const;
-
-
- void DescribePendingMainThreadTasks() const;
- class DestructionObserver : public CheckedObserver {
- public:
- DestructionObserver() = default;
- ~DestructionObserver() override = default;
- DestructionObserver(const DestructionObserver&) = delete;
- DestructionObserver& operator=(const DestructionObserver&) = delete;
- virtual void WillDestroyCurrentTaskEnvironment() = 0;
- };
-
-
-
- static void AddDestructionObserver(DestructionObserver* observer);
- static void RemoveDestructionObserver(DestructionObserver* observer);
-
-
-
-
-
-
- static constexpr int kNumForegroundThreadPoolThreads = 4;
- protected:
- explicit TaskEnvironment(TaskEnvironment&& other);
- constexpr MainThreadType main_thread_type() const {
- return main_thread_type_;
- }
- constexpr ThreadPoolExecutionMode thread_pool_execution_mode() const {
- return thread_pool_execution_mode_;
- }
-
- sequence_manager::TimeDomain* GetTimeDomain() const;
- sequence_manager::SequenceManager* sequence_manager() const;
- void DeferredInitFromSubclass(
- scoped_refptr<base::SingleThreadTaskRunner> task_runner);
-
- void NotifyDestructionObserversAndReleaseSequenceManager();
- private:
- class TestTaskTracker;
- class MockTimeDomain;
- void InitializeThreadPool();
- void DestroyThreadPool();
- void CompleteInitialization();
-
-
- TaskEnvironment(TimeSource time_source,
- MainThreadType main_thread_type,
- ThreadPoolExecutionMode thread_pool_execution_mode,
- ThreadingMode threading_mode,
- ThreadPoolCOMEnvironment thread_pool_com_environment,
- bool subclass_creates_default_taskrunner,
- trait_helpers::NotATraitTag tag);
- const MainThreadType main_thread_type_;
- const ThreadPoolExecutionMode thread_pool_execution_mode_;
- const ThreadingMode threading_mode_;
- const ThreadPoolCOMEnvironment thread_pool_com_environment_;
- const bool subclass_creates_default_taskrunner_;
- std::unique_ptr<sequence_manager::SequenceManager> sequence_manager_;
-
-
- std::unique_ptr<MockTimeDomain> mock_time_domain_;
-
-
- std::unique_ptr<subtle::ScopedTimeClockOverrides> time_overrides_;
- scoped_refptr<sequence_manager::TaskQueue> task_queue_;
- scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
-
- std::unique_ptr<Clock> mock_clock_;
- #if defined(OS_POSIX) || defined(OS_FUCHSIA)
-
- std::unique_ptr<FileDescriptorWatcher> file_descriptor_watcher_;
- #endif
-
- TestTaskTracker* task_tracker_ = nullptr;
-
- std::unique_ptr<internal::ScopedLazyTaskRunnerListForTesting>
- scoped_lazy_task_runner_list_for_testing_;
-
- std::unique_ptr<ScopedRunLoopTimeout> run_loop_timeout_;
- std::unique_ptr<bool> owns_instance_ = std::make_unique<bool>(true);
-
- std::unique_ptr<SimpleTaskExecutor> simple_task_executor_;
-
-
-
- THREAD_CHECKER(main_thread_checker_);
- DISALLOW_COPY_AND_ASSIGN(TaskEnvironment);
- };
- class SingleThreadTaskEnvironment : public TaskEnvironment {
- public:
- template <class... ArgTypes>
- SingleThreadTaskEnvironment(ArgTypes... args)
- : TaskEnvironment(ThreadingMode::MAIN_THREAD_ONLY, args...) {}
- };
- }
- }
- #endif
|