OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <math.h> | 5 #include <math.h> |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
12 #include "base/message_loop/message_loop_proxy.h" | 12 #include "base/message_loop/message_loop_proxy.h" |
| 13 #include "base/run_loop.h" |
13 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
14 #include "base/strings/string_split.h" | 15 #include "base/strings/string_split.h" |
15 #include "base/strings/string_tokenizer.h" | 16 #include "base/strings/string_tokenizer.h" |
16 #include "base/synchronization/condition_variable.h" | 17 #include "base/synchronization/condition_variable.h" |
17 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
18 #include "base/threading/platform_thread.h" | 19 #include "base/threading/platform_thread.h" |
19 #include "base/time/time.h" | 20 #include "base/time/time.h" |
20 #include "build/build_config.h" | 21 #include "build/build_config.h" |
21 #include "chrome/browser/metrics/thread_watcher.h" | 22 #include "chrome/browser/metrics/thread_watcher.h" |
22 #include "chrome/common/chrome_switches.h" | 23 #include "chrome/common/chrome_switches.h" |
(...skipping 599 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
622 base::Bind(&ThreadWatcher::DeActivateThreadWatching, | 623 base::Bind(&ThreadWatcher::DeActivateThreadWatching, |
623 base::Unretained(io_watcher_))); | 624 base::Unretained(io_watcher_))); |
624 WatchDogThread::PostTask( | 625 WatchDogThread::PostTask( |
625 FROM_HERE, | 626 FROM_HERE, |
626 base::Bind(&ThreadWatcher::DeActivateThreadWatching, | 627 base::Bind(&ThreadWatcher::DeActivateThreadWatching, |
627 base::Unretained(db_watcher_))); | 628 base::Unretained(db_watcher_))); |
628 | 629 |
629 // Wait for the io_watcher_'s VeryLongMethod to finish. | 630 // Wait for the io_watcher_'s VeryLongMethod to finish. |
630 io_watcher_->WaitForWaitStateChange(kUnresponsiveTime * 10, ALL_DONE); | 631 io_watcher_->WaitForWaitStateChange(kUnresponsiveTime * 10, ALL_DONE); |
631 } | 632 } |
| 633 |
| 634 TEST(ThreadWatcherListTest, Restart) { |
| 635 ThreadWatcherList::g_initialize_delay_seconds = 1; |
| 636 |
| 637 base::MessageLoopForUI message_loop_for_ui; |
| 638 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop_for_ui); |
| 639 |
| 640 scoped_ptr<WatchDogThread> watchdog_thread_(new WatchDogThread()); |
| 641 watchdog_thread_->Start(); |
| 642 |
| 643 EXPECT_FALSE(ThreadWatcherList::g_thread_watcher_list_); |
| 644 |
| 645 // See http://crbug.com/347887. |
| 646 // StartWatchingAll() will PostDelayedTask to create g_thread_watcher_list_, |
| 647 // whilst StopWatchingAll() will just PostTask to destroy it. |
| 648 // Ensure that when Stop is called, Start will NOT create |
| 649 // g_thread_watcher_list_ later on. |
| 650 ThreadWatcherList::StartWatchingAll(*CommandLine::ForCurrentProcess()); |
| 651 ThreadWatcherList::StopWatchingAll(); |
| 652 message_loop_for_ui.PostDelayedTask( |
| 653 FROM_HERE, |
| 654 message_loop_for_ui.QuitClosure(), |
| 655 base::TimeDelta::FromSeconds( |
| 656 ThreadWatcherList::g_initialize_delay_seconds)); |
| 657 message_loop_for_ui.Run(); |
| 658 EXPECT_FALSE(ThreadWatcherList::g_thread_watcher_list_); |
| 659 EXPECT_TRUE(ThreadWatcherList::g_stopped_); |
| 660 |
| 661 // Proceed with just |StartWatchingAll| and ensure it'll be started. |
| 662 ThreadWatcherList::StartWatchingAll(*CommandLine::ForCurrentProcess()); |
| 663 message_loop_for_ui.PostDelayedTask( |
| 664 FROM_HERE, |
| 665 message_loop_for_ui.QuitClosure(), |
| 666 base::TimeDelta::FromSeconds( |
| 667 ThreadWatcherList::g_initialize_delay_seconds + 1)); |
| 668 message_loop_for_ui.Run(); |
| 669 EXPECT_TRUE(ThreadWatcherList::g_thread_watcher_list_); |
| 670 EXPECT_FALSE(ThreadWatcherList::g_stopped_); |
| 671 |
| 672 // Finally, StopWatchingAll() must stop. |
| 673 ThreadWatcherList::StopWatchingAll(); |
| 674 message_loop_for_ui.PostDelayedTask( |
| 675 FROM_HERE, |
| 676 message_loop_for_ui.QuitClosure(), |
| 677 base::TimeDelta::FromSeconds( |
| 678 ThreadWatcherList::g_initialize_delay_seconds)); |
| 679 message_loop_for_ui.Run(); |
| 680 EXPECT_FALSE(ThreadWatcherList::g_thread_watcher_list_); |
| 681 EXPECT_TRUE(ThreadWatcherList::g_stopped_); |
| 682 } |
OLD | NEW |