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

Side by Side Diff: trunk/src/base/message_loop/message_loop_unittest.cc

Issue 19737005: Revert 212948 "Made MessagePump a non-thread safe class." (Closed) Base URL: svn://svn.chromium.org/chrome/
Patch Set: Created 7 years, 5 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
OLDNEW
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 <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/message_loop.h" 12 #include "base/message_loop/message_loop.h"
13 #include "base/message_loop/message_loop_proxy_impl.h"
14 #include "base/pending_task.h" 13 #include "base/pending_task.h"
15 #include "base/posix/eintr_wrapper.h" 14 #include "base/posix/eintr_wrapper.h"
16 #include "base/run_loop.h" 15 #include "base/run_loop.h"
17 #include "base/synchronization/waitable_event.h" 16 #include "base/synchronization/waitable_event.h"
18 #include "base/thread_task_runner_handle.h" 17 #include "base/thread_task_runner_handle.h"
19 #include "base/threading/platform_thread.h" 18 #include "base/threading/platform_thread.h"
20 #include "base/threading/thread.h" 19 #include "base/threading/thread.h"
21 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
22 21
23 #if defined(OS_WIN) 22 #if defined(OS_WIN)
24 #include "base/message_loop/message_pump_win.h" 23 #include "base/message_loop/message_pump_win.h"
25 #include "base/win/scoped_handle.h" 24 #include "base/win/scoped_handle.h"
26 #endif 25 #endif
27 26
28 namespace base { 27 namespace base {
29 28
29 class MessageLoopLockTest {
30 public:
31 static void LockWaitUnLock(MessageLoop* loop,
32 WaitableEvent* caller_wait,
33 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
30 // TODO(darin): Platform-specific MessageLoop tests should be grouped together 42 // TODO(darin): Platform-specific MessageLoop tests should be grouped together
31 // to avoid chopping this file up with so many #ifdefs. 43 // to avoid chopping this file up with so many #ifdefs.
32 44
33 namespace { 45 namespace {
34 46
35 class Foo : public RefCounted<Foo> { 47 class Foo : public RefCounted<Foo> {
36 public: 48 public:
37 Foo() : test_count_(0) { 49 Foo() : test_count_(0) {
38 } 50 }
39 51
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 114
103 // TryPost with simulated contention. It must fail. We wait for a helper 115 // TryPost with simulated contention. It must fail. We wait for a helper
104 // thread to lock the queue, we TryPost on this thread and finally we 116 // thread to lock the queue, we TryPost on this thread and finally we
105 // signal the helper to unlock and exit. 117 // signal the helper to unlock and exit.
106 WaitableEvent wait(true, false); 118 WaitableEvent wait(true, false);
107 WaitableEvent signal(true, false); 119 WaitableEvent signal(true, false);
108 Thread thread("RunTest_PostTask_helper"); 120 Thread thread("RunTest_PostTask_helper");
109 thread.Start(); 121 thread.Start();
110 thread.message_loop()->PostTask( 122 thread.message_loop()->PostTask(
111 FROM_HERE, 123 FROM_HERE,
112 Bind(&MessageLoop::LockWaitUnLockForTesting, 124 Bind(&MessageLoopLockTest::LockWaitUnLock,
113 base::Unretained(MessageLoop::current()), 125 MessageLoop::current(),
114 &wait, 126 &wait,
115 &signal)); 127 &signal));
116 128
117 wait.Wait(); 129 wait.Wait();
118 EXPECT_FALSE(MessageLoop::current()->TryPostTask(FROM_HERE, Bind( 130 EXPECT_FALSE(MessageLoop::current()->TryPostTask(FROM_HERE, Bind(
119 &Foo::Test2Mixed, foo.get(), a, &d))); 131 &Foo::Test2Mixed, foo.get(), a, &d)));
120 signal.Signal(); 132 signal.Signal();
121 133
122 // 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
123 MessageLoop::current()->PostTask(FROM_HERE, Bind( 135 MessageLoop::current()->PostTask(FROM_HERE, Bind(
124 &MessageLoop::Quit, Unretained(MessageLoop::current()))); 136 &MessageLoop::Quit, Unretained(MessageLoop::current())));
125 137
(...skipping 1750 matching lines...) Expand 10 before | Expand all | Expand 10 after
1876 TEST(MessageLoopTest, WaitForIO) { 1888 TEST(MessageLoopTest, WaitForIO) {
1877 RunTest_WaitForIO(); 1889 RunTest_WaitForIO();
1878 } 1890 }
1879 1891
1880 TEST(MessageLoopTest, HighResolutionTimer) { 1892 TEST(MessageLoopTest, HighResolutionTimer) {
1881 MessageLoop loop; 1893 MessageLoop loop;
1882 1894
1883 const TimeDelta kFastTimer = TimeDelta::FromMilliseconds(5); 1895 const TimeDelta kFastTimer = TimeDelta::FromMilliseconds(5);
1884 const TimeDelta kSlowTimer = TimeDelta::FromMilliseconds(100); 1896 const TimeDelta kSlowTimer = TimeDelta::FromMilliseconds(100);
1885 1897
1886 EXPECT_FALSE(loop.IsHighResolutionTimerEnabledForTesting()); 1898 EXPECT_FALSE(loop.high_resolution_timers_enabled());
1887 1899
1888 // Post a fast task to enable the high resolution timers. 1900 // Post a fast task to enable the high resolution timers.
1889 loop.PostDelayedTask(FROM_HERE, Bind(&PostNTasksThenQuit, 1), 1901 loop.PostDelayedTask(FROM_HERE, Bind(&PostNTasksThenQuit, 1),
1890 kFastTimer); 1902 kFastTimer);
1891 loop.Run(); 1903 loop.Run();
1892 EXPECT_TRUE(loop.IsHighResolutionTimerEnabledForTesting()); 1904 EXPECT_TRUE(loop.high_resolution_timers_enabled());
1893 1905
1894 // Post a slow task and verify high resolution timers 1906 // Post a slow task and verify high resolution timers
1895 // are still enabled. 1907 // are still enabled.
1896 loop.PostDelayedTask(FROM_HERE, Bind(&PostNTasksThenQuit, 1), 1908 loop.PostDelayedTask(FROM_HERE, Bind(&PostNTasksThenQuit, 1),
1897 kSlowTimer); 1909 kSlowTimer);
1898 loop.Run(); 1910 loop.Run();
1899 EXPECT_TRUE(loop.IsHighResolutionTimerEnabledForTesting()); 1911 EXPECT_TRUE(loop.high_resolution_timers_enabled());
1900 1912
1901 // Wait for a while so that high-resolution mode elapses. 1913 // Wait for a while so that high-resolution mode elapses.
1902 PlatformThread::Sleep(TimeDelta::FromMilliseconds( 1914 PlatformThread::Sleep(TimeDelta::FromMilliseconds(
1903 MessageLoop::kHighResolutionTimerModeLeaseTimeMs)); 1915 MessageLoop::kHighResolutionTimerModeLeaseTimeMs));
1904 1916
1905 // Post a slow task to disable the high resolution timers. 1917 // Post a slow task to disable the high resolution timers.
1906 loop.PostDelayedTask(FROM_HERE, Bind(&PostNTasksThenQuit, 1), 1918 loop.PostDelayedTask(FROM_HERE, Bind(&PostNTasksThenQuit, 1),
1907 kSlowTimer); 1919 kSlowTimer);
1908 loop.Run(); 1920 loop.Run();
1909 EXPECT_FALSE(loop.IsHighResolutionTimerEnabledForTesting()); 1921 EXPECT_FALSE(loop.high_resolution_timers_enabled());
1910 } 1922 }
1911 1923
1912 #endif // defined(OS_WIN) 1924 #endif // defined(OS_WIN)
1913 1925
1914 #if defined(OS_POSIX) && !defined(OS_NACL) 1926 #if defined(OS_POSIX) && !defined(OS_NACL)
1915 1927
1916 namespace { 1928 namespace {
1917 1929
1918 class QuitDelegate : public MessageLoopForIO::Watcher { 1930 class QuitDelegate : public MessageLoopForIO::Watcher {
1919 public: 1931 public:
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
2095 // On Linux, the pipe buffer size is 64KiB by default. The bug caused one 2107 // On Linux, the pipe buffer size is 64KiB by default. The bug caused one
2096 // byte accumulated in the pipe per two posts, so we should repeat 128K 2108 // byte accumulated in the pipe per two posts, so we should repeat 128K
2097 // times to reproduce the bug. 2109 // times to reproduce the bug.
2098 const int kNumTimes = 1 << 17; 2110 const int kNumTimes = 1 << 17;
2099 RunTest_RecursivePosts(MessageLoop::TYPE_DEFAULT, kNumTimes); 2111 RunTest_RecursivePosts(MessageLoop::TYPE_DEFAULT, kNumTimes);
2100 RunTest_RecursivePosts(MessageLoop::TYPE_UI, kNumTimes); 2112 RunTest_RecursivePosts(MessageLoop::TYPE_UI, kNumTimes);
2101 RunTest_RecursivePosts(MessageLoop::TYPE_IO, kNumTimes); 2113 RunTest_RecursivePosts(MessageLoop::TYPE_IO, kNumTimes);
2102 } 2114 }
2103 2115
2104 } // namespace base 2116 } // namespace base
OLDNEW
« no previous file with comments | « trunk/src/base/message_loop/message_loop_proxy_impl.cc ('k') | trunk/src/base/message_loop/message_pump.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698