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