diff options
author | darin@google.com <darin@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-09 05:15:56 +0000 |
---|---|---|
committer | darin@google.com <darin@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-09 05:15:56 +0000 |
commit | 166020bd7aade44caeaefd67422b6162172a8956 (patch) | |
tree | 8efb28c81b85111a792a82c28d69f0fd0cdc8c10 /base/message_loop_unittest.cc | |
parent | 61198eeba940f70a9ecaa36faa9e5b84555011ea (diff) | |
download | chromium_src-166020bd7aade44caeaefd67422b6162172a8956.zip chromium_src-166020bd7aade44caeaefd67422b6162172a8956.tar.gz chromium_src-166020bd7aade44caeaefd67422b6162172a8956.tar.bz2 |
Delete pending tasks that have not run. To do this properly, I found that I cannot clear MessageLoop::current() until all of the tasks have been deleted.
I wrote a loop to make sure that the queues are all empty. This could be abused, obviously. Another approach would be to CHECK that the second loop doesn't do anything. Thoughts?
R=jar
Review URL: http://codereview.chromium.org/1829
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@1891 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/message_loop_unittest.cc')
-rw-r--r-- | base/message_loop_unittest.cc | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/base/message_loop_unittest.cc b/base/message_loop_unittest.cc index b36090a6d..e79c576 100644 --- a/base/message_loop_unittest.cc +++ b/base/message_loop_unittest.cc @@ -290,6 +290,52 @@ void RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::Type message_loop_type) EXPECT_TRUE(run_time2 > run_time1); } +class RecordDeletionTask : public Task { + public: + RecordDeletionTask(Task* post_on_delete, bool* was_deleted) + : post_on_delete_(post_on_delete), was_deleted_(was_deleted) { + } + ~RecordDeletionTask() { + *was_deleted_ = true; + if (post_on_delete_) + MessageLoop::current()->PostTask(FROM_HERE, post_on_delete_); + } + virtual void Run() {} + private: + Task* post_on_delete_; + bool* was_deleted_; +}; + +void RunTest_EnsureTaskDeletion(MessageLoop::Type message_loop_type) { + bool a_was_deleted = false; + bool b_was_deleted = false; + { + MessageLoop loop(message_loop_type); + loop.PostTask( + FROM_HERE, new RecordDeletionTask(NULL, &a_was_deleted)); + loop.PostDelayedTask( + FROM_HERE, new RecordDeletionTask(NULL, &b_was_deleted), 1000); + } + EXPECT_TRUE(a_was_deleted); + EXPECT_TRUE(b_was_deleted); +} + +void RunTest_EnsureTaskDeletion_Chain(MessageLoop::Type message_loop_type) { + bool a_was_deleted = false; + bool b_was_deleted = false; + bool c_was_deleted = false; + { + MessageLoop loop(message_loop_type); + RecordDeletionTask* a = new RecordDeletionTask(NULL, &a_was_deleted); + RecordDeletionTask* b = new RecordDeletionTask(a, &b_was_deleted); + RecordDeletionTask* c = new RecordDeletionTask(b, &c_was_deleted); + loop.PostTask(FROM_HERE, c); + } + EXPECT_TRUE(a_was_deleted); + EXPECT_TRUE(b_was_deleted); + EXPECT_TRUE(c_was_deleted); +} + class NestingTest : public Task { public: explicit NestingTest(int* depth) : depth_(depth) { @@ -1049,6 +1095,18 @@ TEST(MessageLoopTest, PostDelayedTask_InPostOrder_3) { RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_IO); } +TEST(MessageLoopTest, EnsureTaskDeletion) { + RunTest_EnsureTaskDeletion(MessageLoop::TYPE_DEFAULT); + RunTest_EnsureTaskDeletion(MessageLoop::TYPE_UI); + RunTest_EnsureTaskDeletion(MessageLoop::TYPE_IO); +} + +TEST(MessageLoopTest, EnsureTaskDeletion_Chain) { + RunTest_EnsureTaskDeletion_Chain(MessageLoop::TYPE_DEFAULT); + RunTest_EnsureTaskDeletion_Chain(MessageLoop::TYPE_UI); + RunTest_EnsureTaskDeletion_Chain(MessageLoop::TYPE_IO); +} + #if defined(OS_WIN) TEST(MessageLoopTest, Crasher) { RunTest_Crasher(MessageLoop::TYPE_DEFAULT); |