OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/eintr_wrapper.h" | 7 #include "base/eintr_wrapper.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/ref_counted.h" | 10 #include "base/ref_counted.h" |
11 #include "base/task.h" | 11 #include "base/task.h" |
12 #include "base/threading/platform_thread.h" | 12 #include "base/threading/platform_thread.h" |
13 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 #if defined(OS_WIN) | 16 #if defined(OS_WIN) |
17 #include "base/message_pump_win.h" | 17 #include "base/message_pump_win.h" |
18 #include "base/scoped_handle.h" | 18 #include "base/win/scoped_handle.h" |
19 #endif | 19 #endif |
20 #if defined(OS_POSIX) | 20 #if defined(OS_POSIX) |
21 #include "base/message_pump_libevent.h" | 21 #include "base/message_pump_libevent.h" |
22 #endif | 22 #endif |
23 | 23 |
24 using base::PlatformThread; | 24 using base::PlatformThread; |
25 using base::Thread; | 25 using base::Thread; |
26 using base::Time; | 26 using base::Time; |
27 using base::TimeDelta; | 27 using base::TimeDelta; |
28 | 28 |
(...skipping 901 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
930 | 930 |
931 // A side effect of this test is the generation a beep. Sorry. | 931 // A side effect of this test is the generation a beep. Sorry. |
932 void RunTest_RecursiveDenial2(MessageLoop::Type message_loop_type) { | 932 void RunTest_RecursiveDenial2(MessageLoop::Type message_loop_type) { |
933 MessageLoop loop(message_loop_type); | 933 MessageLoop loop(message_loop_type); |
934 | 934 |
935 Thread worker("RecursiveDenial2_worker"); | 935 Thread worker("RecursiveDenial2_worker"); |
936 Thread::Options options; | 936 Thread::Options options; |
937 options.message_loop_type = message_loop_type; | 937 options.message_loop_type = message_loop_type; |
938 ASSERT_EQ(true, worker.StartWithOptions(options)); | 938 ASSERT_EQ(true, worker.StartWithOptions(options)); |
939 TaskList order; | 939 TaskList order; |
940 ScopedHandle event(CreateEvent(NULL, FALSE, FALSE, NULL)); | 940 base::win::ScopedHandle event(CreateEvent(NULL, FALSE, FALSE, NULL)); |
941 worker.message_loop()->PostTask(FROM_HERE, | 941 worker.message_loop()->PostTask(FROM_HERE, |
942 new Recursive2Tasks(MessageLoop::current(), | 942 new Recursive2Tasks(MessageLoop::current(), |
943 event, | 943 event, |
944 true, | 944 true, |
945 &order, | 945 &order, |
946 false)); | 946 false)); |
947 // Let the other thread execute. | 947 // Let the other thread execute. |
948 WaitForSingleObject(event, INFINITE); | 948 WaitForSingleObject(event, INFINITE); |
949 MessageLoop::current()->Run(); | 949 MessageLoop::current()->Run(); |
950 | 950 |
(...skipping 22 matching lines...) Expand all Loading... |
973 // A side effect of this test is the generation a beep. Sorry. This test also | 973 // A side effect of this test is the generation a beep. Sorry. This test also |
974 // needs to process windows messages on the current thread. | 974 // needs to process windows messages on the current thread. |
975 void RunTest_RecursiveSupport2(MessageLoop::Type message_loop_type) { | 975 void RunTest_RecursiveSupport2(MessageLoop::Type message_loop_type) { |
976 MessageLoop loop(message_loop_type); | 976 MessageLoop loop(message_loop_type); |
977 | 977 |
978 Thread worker("RecursiveSupport2_worker"); | 978 Thread worker("RecursiveSupport2_worker"); |
979 Thread::Options options; | 979 Thread::Options options; |
980 options.message_loop_type = message_loop_type; | 980 options.message_loop_type = message_loop_type; |
981 ASSERT_EQ(true, worker.StartWithOptions(options)); | 981 ASSERT_EQ(true, worker.StartWithOptions(options)); |
982 TaskList order; | 982 TaskList order; |
983 ScopedHandle event(CreateEvent(NULL, FALSE, FALSE, NULL)); | 983 base::win::ScopedHandle event(CreateEvent(NULL, FALSE, FALSE, NULL)); |
984 worker.message_loop()->PostTask(FROM_HERE, | 984 worker.message_loop()->PostTask(FROM_HERE, |
985 new Recursive2Tasks(MessageLoop::current(), | 985 new Recursive2Tasks(MessageLoop::current(), |
986 event, | 986 event, |
987 false, | 987 false, |
988 &order, | 988 &order, |
989 true)); | 989 true)); |
990 // Let the other thread execute. | 990 // Let the other thread execute. |
991 WaitForSingleObject(event, INFINITE); | 991 WaitForSingleObject(event, INFINITE); |
992 MessageLoop::current()->Run(); | 992 MessageLoop::current()->Run(); |
993 | 993 |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1180 | 1180 |
1181 void Init(); | 1181 void Init(); |
1182 void WaitForIO(); | 1182 void WaitForIO(); |
1183 OVERLAPPED* context() { return &context_.overlapped; } | 1183 OVERLAPPED* context() { return &context_.overlapped; } |
1184 DWORD size() { return sizeof(buffer_); } | 1184 DWORD size() { return sizeof(buffer_); } |
1185 | 1185 |
1186 private: | 1186 private: |
1187 char buffer_[48]; | 1187 char buffer_[48]; |
1188 MessageLoopForIO::IOContext context_; | 1188 MessageLoopForIO::IOContext context_; |
1189 HANDLE signal_; | 1189 HANDLE signal_; |
1190 ScopedHandle file_; | 1190 base::win::ScopedHandle file_; |
1191 bool wait_; | 1191 bool wait_; |
1192 }; | 1192 }; |
1193 | 1193 |
1194 TestIOHandler::TestIOHandler(const wchar_t* name, HANDLE signal, bool wait) | 1194 TestIOHandler::TestIOHandler(const wchar_t* name, HANDLE signal, bool wait) |
1195 : signal_(signal), wait_(wait) { | 1195 : signal_(signal), wait_(wait) { |
1196 memset(buffer_, 0, sizeof(buffer_)); | 1196 memset(buffer_, 0, sizeof(buffer_)); |
1197 memset(&context_, 0, sizeof(context_)); | 1197 memset(&context_, 0, sizeof(context_)); |
1198 context_.handler = this; | 1198 context_.handler = this; |
1199 | 1199 |
1200 file_.Set(CreateFile(name, GENERIC_READ, 0, NULL, OPEN_EXISTING, | 1200 file_.Set(CreateFile(name, GENERIC_READ, 0, NULL, OPEN_EXISTING, |
(...skipping 27 matching lines...) Expand all Loading... |
1228 explicit IOHandlerTask(TestIOHandler* handler) : handler_(handler) {} | 1228 explicit IOHandlerTask(TestIOHandler* handler) : handler_(handler) {} |
1229 virtual void Run() { | 1229 virtual void Run() { |
1230 handler_->Init(); | 1230 handler_->Init(); |
1231 } | 1231 } |
1232 | 1232 |
1233 private: | 1233 private: |
1234 TestIOHandler* handler_; | 1234 TestIOHandler* handler_; |
1235 }; | 1235 }; |
1236 | 1236 |
1237 void RunTest_IOHandler() { | 1237 void RunTest_IOHandler() { |
1238 ScopedHandle callback_called(CreateEvent(NULL, TRUE, FALSE, NULL)); | 1238 base::win::ScopedHandle callback_called(CreateEvent(NULL, TRUE, FALSE, NULL)); |
1239 ASSERT_TRUE(callback_called.IsValid()); | 1239 ASSERT_TRUE(callback_called.IsValid()); |
1240 | 1240 |
1241 const wchar_t* kPipeName = L"\\\\.\\pipe\\iohandler_pipe"; | 1241 const wchar_t* kPipeName = L"\\\\.\\pipe\\iohandler_pipe"; |
1242 ScopedHandle server(CreateNamedPipe(kPipeName, PIPE_ACCESS_OUTBOUND, 0, 1, | 1242 base::win::ScopedHandle server( |
1243 0, 0, 0, NULL)); | 1243 CreateNamedPipe(kPipeName, PIPE_ACCESS_OUTBOUND, 0, 1, 0, 0, 0, NULL)); |
1244 ASSERT_TRUE(server.IsValid()); | 1244 ASSERT_TRUE(server.IsValid()); |
1245 | 1245 |
1246 Thread thread("IOHandler test"); | 1246 Thread thread("IOHandler test"); |
1247 Thread::Options options; | 1247 Thread::Options options; |
1248 options.message_loop_type = MessageLoop::TYPE_IO; | 1248 options.message_loop_type = MessageLoop::TYPE_IO; |
1249 ASSERT_TRUE(thread.StartWithOptions(options)); | 1249 ASSERT_TRUE(thread.StartWithOptions(options)); |
1250 | 1250 |
1251 MessageLoop* thread_loop = thread.message_loop(); | 1251 MessageLoop* thread_loop = thread.message_loop(); |
1252 ASSERT_TRUE(NULL != thread_loop); | 1252 ASSERT_TRUE(NULL != thread_loop); |
1253 | 1253 |
1254 TestIOHandler handler(kPipeName, callback_called, false); | 1254 TestIOHandler handler(kPipeName, callback_called, false); |
1255 IOHandlerTask* task = new IOHandlerTask(&handler); | 1255 IOHandlerTask* task = new IOHandlerTask(&handler); |
1256 thread_loop->PostTask(FROM_HERE, task); | 1256 thread_loop->PostTask(FROM_HERE, task); |
1257 Sleep(100); // Make sure the thread runs and sleeps for lack of work. | 1257 Sleep(100); // Make sure the thread runs and sleeps for lack of work. |
1258 | 1258 |
1259 const char buffer[] = "Hello there!"; | 1259 const char buffer[] = "Hello there!"; |
1260 DWORD written; | 1260 DWORD written; |
1261 EXPECT_TRUE(WriteFile(server, buffer, sizeof(buffer), &written, NULL)); | 1261 EXPECT_TRUE(WriteFile(server, buffer, sizeof(buffer), &written, NULL)); |
1262 | 1262 |
1263 DWORD result = WaitForSingleObject(callback_called, 1000); | 1263 DWORD result = WaitForSingleObject(callback_called, 1000); |
1264 EXPECT_EQ(WAIT_OBJECT_0, result); | 1264 EXPECT_EQ(WAIT_OBJECT_0, result); |
1265 | 1265 |
1266 thread.Stop(); | 1266 thread.Stop(); |
1267 } | 1267 } |
1268 | 1268 |
1269 void RunTest_WaitForIO() { | 1269 void RunTest_WaitForIO() { |
1270 ScopedHandle callback1_called(CreateEvent(NULL, TRUE, FALSE, NULL)); | 1270 base::win::ScopedHandle callback1_called( |
1271 ScopedHandle callback2_called(CreateEvent(NULL, TRUE, FALSE, NULL)); | 1271 CreateEvent(NULL, TRUE, FALSE, NULL)); |
| 1272 base::win::ScopedHandle callback2_called( |
| 1273 CreateEvent(NULL, TRUE, FALSE, NULL)); |
1272 ASSERT_TRUE(callback1_called.IsValid()); | 1274 ASSERT_TRUE(callback1_called.IsValid()); |
1273 ASSERT_TRUE(callback2_called.IsValid()); | 1275 ASSERT_TRUE(callback2_called.IsValid()); |
1274 | 1276 |
1275 const wchar_t* kPipeName1 = L"\\\\.\\pipe\\iohandler_pipe1"; | 1277 const wchar_t* kPipeName1 = L"\\\\.\\pipe\\iohandler_pipe1"; |
1276 const wchar_t* kPipeName2 = L"\\\\.\\pipe\\iohandler_pipe2"; | 1278 const wchar_t* kPipeName2 = L"\\\\.\\pipe\\iohandler_pipe2"; |
1277 ScopedHandle server1(CreateNamedPipe(kPipeName1, PIPE_ACCESS_OUTBOUND, 0, 1, | 1279 base::win::ScopedHandle server1( |
1278 0, 0, 0, NULL)); | 1280 CreateNamedPipe(kPipeName1, PIPE_ACCESS_OUTBOUND, 0, 1, 0, 0, 0, NULL)); |
1279 ScopedHandle server2(CreateNamedPipe(kPipeName2, PIPE_ACCESS_OUTBOUND, 0, 1, | 1281 base::win::ScopedHandle server2( |
1280 0, 0, 0, NULL)); | 1282 CreateNamedPipe(kPipeName2, PIPE_ACCESS_OUTBOUND, 0, 1, 0, 0, 0, NULL)); |
1281 ASSERT_TRUE(server1.IsValid()); | 1283 ASSERT_TRUE(server1.IsValid()); |
1282 ASSERT_TRUE(server2.IsValid()); | 1284 ASSERT_TRUE(server2.IsValid()); |
1283 | 1285 |
1284 Thread thread("IOHandler test"); | 1286 Thread thread("IOHandler test"); |
1285 Thread::Options options; | 1287 Thread::Options options; |
1286 options.message_loop_type = MessageLoop::TYPE_IO; | 1288 options.message_loop_type = MessageLoop::TYPE_IO; |
1287 ASSERT_TRUE(thread.StartWithOptions(options)); | 1289 ASSERT_TRUE(thread.StartWithOptions(options)); |
1288 | 1290 |
1289 MessageLoop* thread_loop = thread.message_loop(); | 1291 MessageLoop* thread_loop = thread.message_loop(); |
1290 ASSERT_TRUE(NULL != thread_loop); | 1292 ASSERT_TRUE(NULL != thread_loop); |
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1705 loop->PostDelayedTask( | 1707 loop->PostDelayedTask( |
1706 FROM_HERE, | 1708 FROM_HERE, |
1707 new RunAtDestructionTask(&task_destroyed, &destruction_observer_called), | 1709 new RunAtDestructionTask(&task_destroyed, &destruction_observer_called), |
1708 kDelayMS); | 1710 kDelayMS); |
1709 delete loop; | 1711 delete loop; |
1710 EXPECT_TRUE(observer.task_destroyed_before_message_loop()); | 1712 EXPECT_TRUE(observer.task_destroyed_before_message_loop()); |
1711 // The task should have been destroyed when we deleted the loop. | 1713 // The task should have been destroyed when we deleted the loop. |
1712 EXPECT_TRUE(task_destroyed); | 1714 EXPECT_TRUE(task_destroyed); |
1713 EXPECT_TRUE(destruction_observer_called); | 1715 EXPECT_TRUE(destruction_observer_called); |
1714 } | 1716 } |
OLD | NEW |