OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/base/messagequeue.h" | 11 #include "webrtc/base/messagequeue.h" |
12 | 12 |
| 13 #include <functional> |
| 14 |
| 15 #include "webrtc/base/atomicops.h" |
13 #include "webrtc/base/bind.h" | 16 #include "webrtc/base/bind.h" |
| 17 #include "webrtc/base/event.h" |
14 #include "webrtc/base/gunit.h" | 18 #include "webrtc/base/gunit.h" |
15 #include "webrtc/base/logging.h" | 19 #include "webrtc/base/logging.h" |
16 #include "webrtc/base/thread.h" | 20 #include "webrtc/base/thread.h" |
17 #include "webrtc/base/timeutils.h" | 21 #include "webrtc/base/timeutils.h" |
18 #include "webrtc/base/nullsocketserver.h" | 22 #include "webrtc/base/nullsocketserver.h" |
19 | 23 |
20 using namespace rtc; | 24 using namespace rtc; |
21 | 25 |
22 class MessageQueueTest: public testing::Test, public MessageQueue { | 26 class MessageQueueTest: public testing::Test, public MessageQueue { |
23 public: | 27 public: |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 << "MessageQueueManager was already initialized by some " | 137 << "MessageQueueManager was already initialized by some " |
134 << "other test in this run."; | 138 << "other test in this run."; |
135 return; | 139 return; |
136 } | 140 } |
137 bool deleted = false; | 141 bool deleted = false; |
138 DeletedMessageHandler* handler = new DeletedMessageHandler(&deleted); | 142 DeletedMessageHandler* handler = new DeletedMessageHandler(&deleted); |
139 delete handler; | 143 delete handler; |
140 EXPECT_TRUE(deleted); | 144 EXPECT_TRUE(deleted); |
141 EXPECT_FALSE(MessageQueueManager::IsInitialized()); | 145 EXPECT_FALSE(MessageQueueManager::IsInitialized()); |
142 } | 146 } |
| 147 |
| 148 // Ensure that ProcessAllMessageQueues does its essential function; process |
| 149 // all messages (both delayed and non delayed) up until the current time, on |
| 150 // all registered message queues. |
| 151 TEST(MessageQueueManager, ProcessAllMessageQueues) { |
| 152 Event entered_process_all_message_queues(true, false); |
| 153 Thread a; |
| 154 Thread b; |
| 155 a.Start(); |
| 156 b.Start(); |
| 157 |
| 158 volatile int messages_processed = 0; |
| 159 FunctorMessageHandler<void, std::function<void()>> incrementer( |
| 160 [&messages_processed, &entered_process_all_message_queues] { |
| 161 // Wait for event as a means to ensure Increment doesn't occur outside |
| 162 // of ProcessAllMessageQueues. The event is set by a message posted to |
| 163 // the main thread, which is guaranteed to be handled inside |
| 164 // ProcessAllMessageQueues. |
| 165 entered_process_all_message_queues.Wait(Event::kForever); |
| 166 AtomicOps::Increment(&messages_processed); |
| 167 }); |
| 168 FunctorMessageHandler<void, std::function<void()>> event_signaler( |
| 169 [&entered_process_all_message_queues] { |
| 170 entered_process_all_message_queues.Set(); |
| 171 }); |
| 172 |
| 173 // Post messages (both delayed and non delayed) to both threads. |
| 174 a.Post(RTC_FROM_HERE, &incrementer); |
| 175 b.Post(RTC_FROM_HERE, &incrementer); |
| 176 a.PostDelayed(RTC_FROM_HERE, 0, &incrementer); |
| 177 b.PostDelayed(RTC_FROM_HERE, 0, &incrementer); |
| 178 rtc::Thread::Current()->Post(RTC_FROM_HERE, &event_signaler); |
| 179 |
| 180 MessageQueueManager::ProcessAllMessageQueues(); |
| 181 EXPECT_EQ(4, AtomicOps::AcquireLoad(&messages_processed)); |
| 182 } |
| 183 |
| 184 // Test that ProcessAllMessageQueues doesn't hang if a thread is quitting. |
| 185 TEST(MessageQueueManager, ProcessAllMessageQueuesWithQuittingThread) { |
| 186 Thread t; |
| 187 t.Start(); |
| 188 t.Quit(); |
| 189 MessageQueueManager::ProcessAllMessageQueues(); |
| 190 } |
| 191 |
| 192 // Test that ProcessAllMessageQueues doesn't hang if a queue clears its |
| 193 // messages. |
| 194 TEST(MessageQueueManager, ProcessAllMessageQueuesWithClearedQueue) { |
| 195 Event entered_process_all_message_queues(true, false); |
| 196 Thread t; |
| 197 t.Start(); |
| 198 |
| 199 FunctorMessageHandler<void, std::function<void()>> clearer( |
| 200 [&entered_process_all_message_queues] { |
| 201 // Wait for event as a means to ensure Clear doesn't occur outside of |
| 202 // ProcessAllMessageQueues. The event is set by a message posted to the |
| 203 // main thread, which is guaranteed to be handled inside |
| 204 // ProcessAllMessageQueues. |
| 205 entered_process_all_message_queues.Wait(Event::kForever); |
| 206 rtc::Thread::Current()->Clear(nullptr); |
| 207 }); |
| 208 FunctorMessageHandler<void, std::function<void()>> event_signaler( |
| 209 [&entered_process_all_message_queues] { |
| 210 entered_process_all_message_queues.Set(); |
| 211 }); |
| 212 |
| 213 // Post messages (both delayed and non delayed) to both threads. |
| 214 t.Post(RTC_FROM_HERE, &clearer); |
| 215 rtc::Thread::Current()->Post(RTC_FROM_HERE, &event_signaler); |
| 216 MessageQueueManager::ProcessAllMessageQueues(); |
| 217 } |
OLD | NEW |