| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/public/test/test_browser_thread_bundle.h" | 5 #include "content/public/test/test_browser_thread_bundle.h" |
| 6 | 6 |
| 7 #include "base/logging.h" |
| 7 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 8 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 9 #include "content/browser/browser_thread_impl.h" | 10 #include "content/browser/browser_thread_impl.h" |
| 10 #include "content/public/test/test_browser_thread.h" | 11 #include "content/public/test/test_browser_thread.h" |
| 11 | 12 |
| 12 namespace content { | 13 namespace content { |
| 13 | 14 |
| 14 TestBrowserThreadBundle::TestBrowserThreadBundle() | 15 TestBrowserThreadBundle::TestBrowserThreadBundle() |
| 15 : TestBrowserThreadBundle(DEFAULT) {} | 16 : TestBrowserThreadBundle(DEFAULT) {} |
| 16 | 17 |
| 17 TestBrowserThreadBundle::TestBrowserThreadBundle(int options) | 18 TestBrowserThreadBundle::TestBrowserThreadBundle(int options) |
| 18 : options_(options), threads_started_(false) { | 19 : options_(options), threads_started_(false) { |
| 19 Init(); | 20 Init(); |
| 20 } | 21 } |
| 21 | 22 |
| 22 TestBrowserThreadBundle::~TestBrowserThreadBundle() { | 23 TestBrowserThreadBundle::~TestBrowserThreadBundle() { |
| 23 // To avoid memory leaks, we must ensure that any tasks posted to the blocking | 24 // To avoid memory leaks, we must ensure that any tasks posted to the blocking |
| 24 // pool via PostTaskAndReply are able to reply back to the originating thread. | 25 // pool via PostTaskAndReply are able to reply back to the originating thread. |
| 25 // Thus we must flush the blocking pool while the browser threads still exist. | 26 // Thus we must flush the blocking pool while the browser threads still exist. |
| 26 base::RunLoop().RunUntilIdle(); | 27 base::RunLoop().RunUntilIdle(); |
| 27 BrowserThreadImpl::FlushThreadPoolHelperForTesting(); | 28 BrowserThreadImpl::FlushThreadPoolHelperForTesting(); |
| 28 | 29 |
| 29 // To ensure a clean teardown, each thread's message loop must be flushed | 30 // To ensure a clean teardown, each thread's message loop must be flushed |
| 30 // just before the thread is destroyed. But destroying a fake thread does not | 31 // just before the thread is destroyed. But stopping a fake thread does not |
| 31 // automatically flush the message loop, so we have to do it manually. | 32 // automatically flush the message loop, so we have to do it manually. |
| 32 // See http://crbug.com/247525 for discussion. | 33 // See http://crbug.com/247525 for discussion. |
| 33 base::RunLoop().RunUntilIdle(); | 34 base::RunLoop().RunUntilIdle(); |
| 34 io_thread_.reset(); | 35 io_thread_->Stop(); |
| 35 base::RunLoop().RunUntilIdle(); | 36 base::RunLoop().RunUntilIdle(); |
| 36 cache_thread_.reset(); | 37 cache_thread_->Stop(); |
| 37 base::RunLoop().RunUntilIdle(); | 38 base::RunLoop().RunUntilIdle(); |
| 38 process_launcher_thread_.reset(); | 39 process_launcher_thread_->Stop(); |
| 39 base::RunLoop().RunUntilIdle(); | 40 base::RunLoop().RunUntilIdle(); |
| 40 file_user_blocking_thread_.reset(); | 41 file_user_blocking_thread_->Stop(); |
| 41 base::RunLoop().RunUntilIdle(); | 42 base::RunLoop().RunUntilIdle(); |
| 42 file_thread_.reset(); | 43 file_thread_->Stop(); |
| 43 base::RunLoop().RunUntilIdle(); | 44 base::RunLoop().RunUntilIdle(); |
| 44 db_thread_.reset(); | 45 db_thread_->Stop(); |
| 45 base::RunLoop().RunUntilIdle(); | 46 base::RunLoop().RunUntilIdle(); |
| 46 // This is the point at which we normally shut down the thread pool. So flush | 47 // This is the point at which we normally shut down the thread pool. So flush |
| 47 // it again in case any shutdown tasks have been posted to the pool from the | 48 // it again in case any shutdown tasks have been posted to the pool from the |
| 48 // threads above. | 49 // threads above. |
| 49 BrowserThreadImpl::FlushThreadPoolHelperForTesting(); | 50 BrowserThreadImpl::FlushThreadPoolHelperForTesting(); |
| 50 base::RunLoop().RunUntilIdle(); | 51 base::RunLoop().RunUntilIdle(); |
| 51 ui_thread_.reset(); | 52 ui_thread_->Stop(); |
| 52 base::RunLoop().RunUntilIdle(); | 53 base::RunLoop().RunUntilIdle(); |
| 54 |
| 55 // |message_loop_| needs to explicitly go away before fake threads in order |
| 56 // for DestructionObservers hooked to |message_loop_| to be able to invoke |
| 57 // BrowserThread::CurrentlyOn() -- ref. ~TestBrowserThread(). |
| 58 CHECK(message_loop_->IsIdleForTesting()); |
| 59 message_loop_.reset(); |
| 53 } | 60 } |
| 54 | 61 |
| 55 void TestBrowserThreadBundle::Init() { | 62 void TestBrowserThreadBundle::Init() { |
| 56 // Check for conflicting options can't have two IO threads. | 63 // Check for conflicting options can't have two IO threads. |
| 57 CHECK(!(options_ & IO_MAINLOOP) || !(options_ & REAL_IO_THREAD)); | 64 CHECK(!(options_ & IO_MAINLOOP) || !(options_ & REAL_IO_THREAD)); |
| 58 // There must be a thread to start to use DONT_START_THREADS | 65 // There must be a thread to start to use DONT_START_THREADS |
| 59 CHECK((options_ & ~IO_MAINLOOP) != DONT_START_THREADS); | 66 CHECK((options_ & ~IO_MAINLOOP) != DONT_START_THREADS); |
| 60 | 67 |
| 61 if (options_ & IO_MAINLOOP) { | 68 if (options_ & IO_MAINLOOP) { |
| 62 message_loop_.reset(new base::MessageLoopForIO()); | 69 message_loop_.reset(new base::MessageLoopForIO()); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 if (options_ & REAL_FILE_THREAD) | 115 if (options_ & REAL_FILE_THREAD) |
| 109 file_thread_->Start(); | 116 file_thread_->Start(); |
| 110 | 117 |
| 111 if (options_ & REAL_IO_THREAD) | 118 if (options_ & REAL_IO_THREAD) |
| 112 io_thread_->StartIOThread(); | 119 io_thread_->StartIOThread(); |
| 113 | 120 |
| 114 threads_started_ = true; | 121 threads_started_ = true; |
| 115 } | 122 } |
| 116 | 123 |
| 117 } // namespace content | 124 } // namespace content |
| OLD | NEW |