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/logging.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
11 #include "base/task_scheduler/task_scheduler.h" | 11 #include "base/task_scheduler/task_scheduler.h" |
12 #include "base/test/scoped_async_task_scheduler.h" | 12 #include "base/test/scoped_async_task_scheduler.h" |
13 #include "content/browser/browser_thread_impl.h" | 13 #include "content/browser/browser_thread_impl.h" |
14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
15 #include "content/public/test/test_browser_thread.h" | 15 #include "content/public/test/test_browser_thread.h" |
| 16 #include "content/public/test/test_utils.h" |
16 | 17 |
17 namespace content { | 18 namespace content { |
18 | 19 |
19 TestBrowserThreadBundle::TestBrowserThreadBundle() | 20 TestBrowserThreadBundle::TestBrowserThreadBundle() |
20 : TestBrowserThreadBundle(DEFAULT) {} | 21 : TestBrowserThreadBundle(DEFAULT) {} |
21 | 22 |
22 TestBrowserThreadBundle::TestBrowserThreadBundle(int options) | 23 TestBrowserThreadBundle::TestBrowserThreadBundle(int options) |
23 : options_(options), threads_created_(false) { | 24 : options_(options), threads_created_(false) { |
24 Init(); | 25 Init(); |
25 } | 26 } |
(...skipping 25 matching lines...) Expand all Loading... |
51 db_thread_->Stop(); | 52 db_thread_->Stop(); |
52 base::RunLoop().RunUntilIdle(); | 53 base::RunLoop().RunUntilIdle(); |
53 // This is the point at which we normally shut down the thread pool. So flush | 54 // This is the point at which we normally shut down the thread pool. So flush |
54 // it again in case any shutdown tasks have been posted to the pool from the | 55 // it again in case any shutdown tasks have been posted to the pool from the |
55 // threads above. | 56 // threads above. |
56 BrowserThreadImpl::FlushThreadPoolHelperForTesting(); | 57 BrowserThreadImpl::FlushThreadPoolHelperForTesting(); |
57 base::RunLoop().RunUntilIdle(); | 58 base::RunLoop().RunUntilIdle(); |
58 ui_thread_->Stop(); | 59 ui_thread_->Stop(); |
59 base::RunLoop().RunUntilIdle(); | 60 base::RunLoop().RunUntilIdle(); |
60 | 61 |
| 62 // This is required to ensure we run all remaining tasks in an atomic step |
| 63 // (instead of ~ScopedAsyncTaskScheduler() followed by another |
| 64 // RunLoop().RunUntilIdle()). Otherwise If a pending task in |
| 65 // |scoped_async_task_scheduler_| posts to |message_loop_|, that task can then |
| 66 // post back to |scoped_async_task_scheduler_| after the former was destroyed. |
| 67 // This is a bit different than production where the main thread is not |
| 68 // flushed after it's done running but this approach is preferred in unit |
| 69 // tests as running more tasks can merely uncover more issues (e.g. if a bad |
| 70 // tasks is posted but never blocked upon it could make a test flaky whereas |
| 71 // by flushing we guarantee it will blow up). |
| 72 RunAllBlockingPoolTasksUntilIdle(); |
| 73 |
61 scoped_async_task_scheduler_.reset(); | 74 scoped_async_task_scheduler_.reset(); |
62 | |
63 base::RunLoop().RunUntilIdle(); | |
64 CHECK(base::MessageLoop::current()->IsIdleForTesting()); | 75 CHECK(base::MessageLoop::current()->IsIdleForTesting()); |
65 | 76 |
66 // |message_loop_| needs to explicitly go away before fake threads in order | 77 // |message_loop_| needs to explicitly go away before fake threads in order |
67 // for DestructionObservers hooked to |message_loop_| to be able to invoke | 78 // for DestructionObservers hooked to |message_loop_| to be able to invoke |
68 // BrowserThread::CurrentlyOn() -- ref. ~TestBrowserThread(). | 79 // BrowserThread::CurrentlyOn() -- ref. ~TestBrowserThread(). |
69 message_loop_.reset(); | 80 message_loop_.reset(); |
70 } | 81 } |
71 | 82 |
72 void TestBrowserThreadBundle::Init() { | 83 void TestBrowserThreadBundle::Init() { |
73 // Check that the UI thread hasn't already been initialized. This will fail if | 84 // Check that the UI thread hasn't already been initialized. This will fail if |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 io_thread_->StartIOThread(); | 151 io_thread_->StartIOThread(); |
141 } else { | 152 } else { |
142 io_thread_ = base::MakeUnique<TestBrowserThread>( | 153 io_thread_ = base::MakeUnique<TestBrowserThread>( |
143 BrowserThread::IO, base::MessageLoop::current()); | 154 BrowserThread::IO, base::MessageLoop::current()); |
144 } | 155 } |
145 | 156 |
146 threads_created_ = true; | 157 threads_created_ = true; |
147 } | 158 } |
148 | 159 |
149 } // namespace content | 160 } // namespace content |
OLD | NEW |