Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(124)

Side by Side Diff: base/message_loop_unittest.cc

Issue 14387002: Adding TryPostTask to the message loop (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: change comment Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/message_loop.cc ('k') | base/synchronization/lock.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <vector> 5 #include <vector>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/compiler_specific.h" 9 #include "base/compiler_specific.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
12 #include "base/message_loop.h" 12 #include "base/message_loop.h"
13 #include "base/pending_task.h" 13 #include "base/pending_task.h"
14 #include "base/posix/eintr_wrapper.h" 14 #include "base/posix/eintr_wrapper.h"
15 #include "base/run_loop.h" 15 #include "base/run_loop.h"
16 #include "base/synchronization/waitable_event.h"
16 #include "base/thread_task_runner_handle.h" 17 #include "base/thread_task_runner_handle.h"
17 #include "base/threading/platform_thread.h" 18 #include "base/threading/platform_thread.h"
18 #include "base/threading/thread.h" 19 #include "base/threading/thread.h"
19 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
20 21
21 #if defined(OS_WIN) 22 #if defined(OS_WIN)
22 #include "base/message_pump_win.h" 23 #include "base/message_pump_win.h"
23 #include "base/win/scoped_handle.h" 24 #include "base/win/scoped_handle.h"
24 #endif 25 #endif
25 26
26 namespace base { 27 namespace base {
27 28
29 class MessageLoopLockTest {
30 public:
31 static void LockWaitUnLock(MessageLoop* loop,
32 base::WaitableEvent* caller_wait,
33 base::WaitableEvent* caller_signal) {
34
35 loop->incoming_queue_lock_.Acquire();
36 caller_wait->Signal();
37 caller_signal->Wait();
38 loop->incoming_queue_lock_.Release();
39 }
40 };
41
28 // TODO(darin): Platform-specific MessageLoop tests should be grouped together 42 // TODO(darin): Platform-specific MessageLoop tests should be grouped together
29 // to avoid chopping this file up with so many #ifdefs. 43 // to avoid chopping this file up with so many #ifdefs.
30 44
31 namespace { 45 namespace {
32 46
33 class Foo : public RefCounted<Foo> { 47 class Foo : public RefCounted<Foo> {
34 public: 48 public:
35 Foo() : test_count_(0) { 49 Foo() : test_count_(0) {
36 } 50 }
37 51
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 MessageLoop::current()->PostTask(FROM_HERE, Bind( 100 MessageLoop::current()->PostTask(FROM_HERE, Bind(
87 &Foo::Test0, foo.get())); 101 &Foo::Test0, foo.get()));
88 MessageLoop::current()->PostTask(FROM_HERE, Bind( 102 MessageLoop::current()->PostTask(FROM_HERE, Bind(
89 &Foo::Test1ConstRef, foo.get(), a)); 103 &Foo::Test1ConstRef, foo.get(), a));
90 MessageLoop::current()->PostTask(FROM_HERE, Bind( 104 MessageLoop::current()->PostTask(FROM_HERE, Bind(
91 &Foo::Test1Ptr, foo.get(), &b)); 105 &Foo::Test1Ptr, foo.get(), &b));
92 MessageLoop::current()->PostTask(FROM_HERE, Bind( 106 MessageLoop::current()->PostTask(FROM_HERE, Bind(
93 &Foo::Test1Int, foo.get(), 100)); 107 &Foo::Test1Int, foo.get(), 100));
94 MessageLoop::current()->PostTask(FROM_HERE, Bind( 108 MessageLoop::current()->PostTask(FROM_HERE, Bind(
95 &Foo::Test2Ptr, foo.get(), &a, &c)); 109 &Foo::Test2Ptr, foo.get(), &a, &c));
96 MessageLoop::current()->PostTask(FROM_HERE, Bind( 110
97 &Foo::Test2Mixed, foo.get(), a, &d)); 111 // TryPost with no contention. It must succeed.
112 EXPECT_TRUE(MessageLoop::current()->TryPostTask(FROM_HERE, Bind(
113 &Foo::Test2Mixed, foo.get(), a, &d)));
114
115 // TryPost with simulated contention. It must fail. We wait for a helper
116 // thread to lock the queue, we TryPost on this thread and finally we
117 // signal the helper to unlock and exit.
118 WaitableEvent wait(true, false);
119 WaitableEvent signal(true, false);
120 Thread thread("RunTest_PostTask_helper");
121 thread.Start();
122 thread.message_loop()->PostTask(
123 FROM_HERE,
124 base::Bind(&MessageLoopLockTest::LockWaitUnLock,
125 MessageLoop::current(),
126 &wait,
127 &signal));
128
129 wait.Wait();
130 EXPECT_FALSE(MessageLoop::current()->TryPostTask(FROM_HERE, Bind(
131 &Foo::Test2Mixed, foo.get(), a, &d)));
132 signal.Signal();
98 133
99 // After all tests, post a message that will shut down the message loop 134 // After all tests, post a message that will shut down the message loop
100 MessageLoop::current()->PostTask(FROM_HERE, Bind( 135 MessageLoop::current()->PostTask(FROM_HERE, Bind(
101 &MessageLoop::Quit, Unretained(MessageLoop::current()))); 136 &MessageLoop::Quit, Unretained(MessageLoop::current())));
102 137
103 // Now kick things off 138 // Now kick things off
104 MessageLoop::current()->Run(); 139 MessageLoop::current()->Run();
105 140
106 EXPECT_EQ(foo->test_count(), 105); 141 EXPECT_EQ(foo->test_count(), 105);
107 EXPECT_EQ(foo->result(), "abacad"); 142 EXPECT_EQ(foo->result(), "abacad");
(...skipping 1965 matching lines...) Expand 10 before | Expand all | Expand 10 after
2073 // On Linux, the pipe buffer size is 64KiB by default. The bug caused one 2108 // On Linux, the pipe buffer size is 64KiB by default. The bug caused one
2074 // byte accumulated in the pipe per two posts, so we should repeat 128K 2109 // byte accumulated in the pipe per two posts, so we should repeat 128K
2075 // times to reproduce the bug. 2110 // times to reproduce the bug.
2076 const int kNumTimes = 1 << 17; 2111 const int kNumTimes = 1 << 17;
2077 RunTest_RecursivePosts(MessageLoop::TYPE_DEFAULT, kNumTimes); 2112 RunTest_RecursivePosts(MessageLoop::TYPE_DEFAULT, kNumTimes);
2078 RunTest_RecursivePosts(MessageLoop::TYPE_UI, kNumTimes); 2113 RunTest_RecursivePosts(MessageLoop::TYPE_UI, kNumTimes);
2079 RunTest_RecursivePosts(MessageLoop::TYPE_IO, kNumTimes); 2114 RunTest_RecursivePosts(MessageLoop::TYPE_IO, kNumTimes);
2080 } 2115 }
2081 2116
2082 } // namespace base 2117 } // namespace base
OLDNEW
« no previous file with comments | « base/message_loop.cc ('k') | base/synchronization/lock.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698