blob: 49eb03fa71c837ab5e52c090a5a19147aa7c96c2 [file] [log] [blame]
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// TestWebThreadBundle is a convenience class for creating a set of
// TestWebThreads, a blocking pool, and a task scheduler in unit tests. For
// most tests, it is sufficient to just instantiate the TestWebThreadBundle as a
// member variable. It is a good idea to put the TestWebThreadBundle as the
// first member variable in test classes, so it is destroyed last, and the test
// threads always exist from the perspective of other classes.
// By default, all of the created TestWebThreads will be backed by a single
// shared MessageLoop. If a test truly needs separate threads, it can do so by
// passing the appropriate combination of option values during the
// TestWebThreadBundle construction.
// To synchronously run tasks posted to TestWebThreads that use the shared
// MessageLoop, call RunLoop::Run/RunUntilIdle() on the thread where the
// TestWebThreadBundle lives. The destructor of TestWebThreadBundle runs
// remaining TestWebThreads tasks, remaining blocking pool tasks, and remaining
// BLOCK_SHUTDOWN task scheduler tasks.
// Some tests using the IO thread expect a MessageLoopForIO. Passing
// IO_MAINLOOP will use a MessageLoopForIO for the main MessageLoop.
// Most of the time, this avoids needing to use a REAL_IO_THREAD.
#include <memory>
#include "base/macros.h"
namespace base {
class MessageLoop;
namespace test {
class ScopedAsyncTaskScheduler;
} // namespace test
} // namespace base
namespace web {
class TestWebThread;
class TestWebThreadBundle {
// Used to specify the type of MessageLoop that backs the UI thread, and
// which of the named WebThreads should be backed by a real
// threads. The UI thread is always the main thread in a unit test.
enum Options {
IO_MAINLOOP = 1 << 0,
REAL_DB_THREAD = 1 << 1,
REAL_IO_THREAD = 1 << 3,
explicit TestWebThreadBundle(int options);
void Init(int options);
std::unique_ptr<base::MessageLoop> message_loop_;
std::unique_ptr<TestWebThread> ui_thread_;
std::unique_ptr<TestWebThread> db_thread_;
std::unique_ptr<TestWebThread> file_thread_;
std::unique_ptr<TestWebThread> file_user_blocking_thread_;
std::unique_ptr<TestWebThread> cache_thread_;
std::unique_ptr<TestWebThread> io_thread_;
} // namespace web