Index: base/trace_event/memory_dump_manager_unittest.cc |
diff --git a/base/trace_event/memory_dump_manager_unittest.cc b/base/trace_event/memory_dump_manager_unittest.cc |
index fc81ec5767e3c07f3e66626fb8163730b1f1252b..864209f984509f9cdd2c0c2a808f1ae180d0ff42 100644 |
--- a/base/trace_event/memory_dump_manager_unittest.cc |
+++ b/base/trace_event/memory_dump_manager_unittest.cc |
@@ -19,6 +19,7 @@ |
#include "base/test/test_io_thread.h" |
#include "base/test/trace_event_analyzer.h" |
#include "base/threading/platform_thread.h" |
+#include "base/threading/sequenced_task_runner_handle.h" |
#include "base/threading/sequenced_worker_pool.h" |
#include "base/threading/thread.h" |
#include "base/threading/thread_task_runner_handle.h" |
@@ -94,6 +95,20 @@ void OnTraceDataCollected(Closure quit_closure, |
quit_closure.Run(); |
} |
+// Posts |task| to |task_runner| and blocks until it is executed. |
+void PostTaskAndWait(const tracked_objects::Location& from_here, |
+ SequencedTaskRunner* task_runner, |
+ const base::Closure& task) { |
+ base::WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, |
+ WaitableEvent::InitialState::NOT_SIGNALED); |
+ task_runner->PostTask(from_here, task); |
+ task_runner->PostTask( |
+ FROM_HERE, base::Bind(&WaitableEvent::Signal, base::Unretained(&event))); |
+ // The SequencedTaskRunner guarantees that |event| will only be signaled after |
+ // |task| is executed. |
+ event.Wait(); |
+} |
+ |
} // namespace |
// Testing MemoryDumpManagerDelegate which, by default, short-circuits dump |
@@ -688,13 +703,16 @@ TEST_F(MemoryDumpManagerTest, UnregisterDumperFromThreadWhileDumping) { |
// unregister the other one. |
for (const std::unique_ptr<MockMemoryDumpProvider>& mdp : mdps) { |
int other_idx = (mdps.front() == mdp); |
- TestIOThread* other_thread = threads[other_idx].get(); |
+ // TestIOThread's task runner must be obtained from the main thread but can |
+ // then be used from other threads. |
+ scoped_refptr<SingleThreadTaskRunner> other_runner = |
+ threads[other_idx]->task_runner(); |
MockMemoryDumpProvider* other_mdp = mdps[other_idx].get(); |
- auto on_dump = [this, other_thread, other_mdp, &on_memory_dump_call_count]( |
+ auto on_dump = [this, other_runner, other_mdp, &on_memory_dump_call_count]( |
const MemoryDumpArgs& args, ProcessMemoryDump* pmd) { |
- other_thread->PostTaskAndWait( |
- FROM_HERE, base::Bind(&MemoryDumpManager::UnregisterDumpProvider, |
- base::Unretained(&*mdm_), other_mdp)); |
+ PostTaskAndWait(FROM_HERE, other_runner.get(), |
+ base::Bind(&MemoryDumpManager::UnregisterDumpProvider, |
+ base::Unretained(&*mdm_), other_mdp)); |
on_memory_dump_call_count++; |
return true; |
}; |
@@ -739,9 +757,14 @@ TEST_F(MemoryDumpManagerTest, TearDownThreadWhileDumping) { |
for (const std::unique_ptr<MockMemoryDumpProvider>& mdp : mdps) { |
int other_idx = (mdps.front() == mdp); |
TestIOThread* other_thread = threads[other_idx].get(); |
- auto on_dump = [other_thread, &on_memory_dump_call_count]( |
+ // TestIOThread isn't thread-safe and must be stopped on the |main_runner|. |
+ scoped_refptr<SequencedTaskRunner> main_runner = |
+ SequencedTaskRunnerHandle::Get(); |
+ auto on_dump = [other_thread, main_runner, &on_memory_dump_call_count]( |
const MemoryDumpArgs& args, ProcessMemoryDump* pmd) { |
- other_thread->Stop(); |
+ PostTaskAndWait( |
+ FROM_HERE, main_runner.get(), |
+ base::Bind(&TestIOThread::Stop, base::Unretained(other_thread))); |
on_memory_dump_call_count++; |
return true; |
}; |
@@ -1086,8 +1109,8 @@ TEST_F(MemoryDumpManagerTest, UnregisterAndDeleteDumpProviderSoonDuringDump) { |
const MemoryDumpArgs&, ProcessMemoryDump*) -> bool { |
thread_ref = PlatformThread::CurrentRef(); |
TestIOThread thread_for_unregistration(TestIOThread::kAutoStart); |
- thread_for_unregistration.PostTaskAndWait( |
- FROM_HERE, |
+ PostTaskAndWait( |
+ FROM_HERE, thread_for_unregistration.task_runner().get(), |
base::Bind( |
&MemoryDumpManager::UnregisterAndDeleteDumpProviderSoon, |
base::Unretained(MemoryDumpManager::GetInstance()), |