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

Side by Side Diff: webrtc/base/messagequeue_unittest.cc

Issue 2877023002: Move webrtc/{base => rtc_base} (Closed)
Patch Set: update presubmit.py and DEPS include rules Created 3 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
« no previous file with comments | « webrtc/base/messagequeue.cc ('k') | webrtc/base/mod_ops.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "webrtc/base/messagequeue.h"
12
13 #include <functional>
14
15 #include "webrtc/base/atomicops.h"
16 #include "webrtc/base/bind.h"
17 #include "webrtc/base/event.h"
18 #include "webrtc/base/gunit.h"
19 #include "webrtc/base/logging.h"
20 #include "webrtc/base/thread.h"
21 #include "webrtc/base/timeutils.h"
22 #include "webrtc/base/nullsocketserver.h"
23
24 using namespace rtc;
25
26 class MessageQueueTest: public testing::Test, public MessageQueue {
27 public:
28 MessageQueueTest() : MessageQueue(SocketServer::CreateDefault(), true) {}
29 bool IsLocked_Worker() {
30 if (!crit_.TryEnter()) {
31 return true;
32 }
33 crit_.Leave();
34 return false;
35 }
36 bool IsLocked() {
37 // We have to do this on a worker thread, or else the TryEnter will
38 // succeed, since our critical sections are reentrant.
39 Thread worker;
40 worker.Start();
41 return worker.Invoke<bool>(
42 RTC_FROM_HERE, rtc::Bind(&MessageQueueTest::IsLocked_Worker, this));
43 }
44 };
45
46 struct DeletedLockChecker {
47 DeletedLockChecker(MessageQueueTest* test, bool* was_locked, bool* deleted)
48 : test(test), was_locked(was_locked), deleted(deleted) { }
49 ~DeletedLockChecker() {
50 *deleted = true;
51 *was_locked = test->IsLocked();
52 }
53 MessageQueueTest* test;
54 bool* was_locked;
55 bool* deleted;
56 };
57
58 static void DelayedPostsWithIdenticalTimesAreProcessedInFifoOrder(
59 MessageQueue* q) {
60 EXPECT_TRUE(q != nullptr);
61 int64_t now = TimeMillis();
62 q->PostAt(RTC_FROM_HERE, now, nullptr, 3);
63 q->PostAt(RTC_FROM_HERE, now - 2, nullptr, 0);
64 q->PostAt(RTC_FROM_HERE, now - 1, nullptr, 1);
65 q->PostAt(RTC_FROM_HERE, now, nullptr, 4);
66 q->PostAt(RTC_FROM_HERE, now - 1, nullptr, 2);
67
68 Message msg;
69 for (size_t i=0; i<5; ++i) {
70 memset(&msg, 0, sizeof(msg));
71 EXPECT_TRUE(q->Get(&msg, 0));
72 EXPECT_EQ(i, msg.message_id);
73 }
74
75 EXPECT_FALSE(q->Get(&msg, 0)); // No more messages
76 }
77
78 TEST_F(MessageQueueTest,
79 DelayedPostsWithIdenticalTimesAreProcessedInFifoOrder) {
80 MessageQueue q(SocketServer::CreateDefault(), true);
81 DelayedPostsWithIdenticalTimesAreProcessedInFifoOrder(&q);
82
83 NullSocketServer nullss;
84 MessageQueue q_nullss(&nullss, true);
85 DelayedPostsWithIdenticalTimesAreProcessedInFifoOrder(&q_nullss);
86 }
87
88 TEST_F(MessageQueueTest, DisposeNotLocked) {
89 bool was_locked = true;
90 bool deleted = false;
91 DeletedLockChecker* d = new DeletedLockChecker(this, &was_locked, &deleted);
92 Dispose(d);
93 Message msg;
94 EXPECT_FALSE(Get(&msg, 0));
95 EXPECT_TRUE(deleted);
96 EXPECT_FALSE(was_locked);
97 }
98
99 class DeletedMessageHandler : public MessageHandler {
100 public:
101 explicit DeletedMessageHandler(bool* deleted) : deleted_(deleted) { }
102 ~DeletedMessageHandler() {
103 *deleted_ = true;
104 }
105 void OnMessage(Message* msg) { }
106 private:
107 bool* deleted_;
108 };
109
110 TEST_F(MessageQueueTest, DiposeHandlerWithPostedMessagePending) {
111 bool deleted = false;
112 DeletedMessageHandler *handler = new DeletedMessageHandler(&deleted);
113 // First, post a dispose.
114 Dispose(handler);
115 // Now, post a message, which should *not* be returned by Get().
116 Post(RTC_FROM_HERE, handler, 1);
117 Message msg;
118 EXPECT_FALSE(Get(&msg, 0));
119 EXPECT_TRUE(deleted);
120 }
121
122 struct UnwrapMainThreadScope {
123 UnwrapMainThreadScope() : rewrap_(Thread::Current() != nullptr) {
124 if (rewrap_) ThreadManager::Instance()->UnwrapCurrentThread();
125 }
126 ~UnwrapMainThreadScope() {
127 if (rewrap_) ThreadManager::Instance()->WrapCurrentThread();
128 }
129 private:
130 bool rewrap_;
131 };
132
133 TEST(MessageQueueManager, Clear) {
134 UnwrapMainThreadScope s;
135 if (MessageQueueManager::IsInitialized()) {
136 LOG(LS_INFO) << "Unable to run MessageQueueManager::Clear test, since the "
137 << "MessageQueueManager was already initialized by some "
138 << "other test in this run.";
139 return;
140 }
141 bool deleted = false;
142 DeletedMessageHandler* handler = new DeletedMessageHandler(&deleted);
143 delete handler;
144 EXPECT_TRUE(deleted);
145 EXPECT_FALSE(MessageQueueManager::IsInitialized());
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 }
OLDNEW
« no previous file with comments | « webrtc/base/messagequeue.cc ('k') | webrtc/base/mod_ops.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698