3 * Copyright 2004--2011, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "talk/base/messagequeue.h"
30 #include "talk/base/bind.h"
31 #include "talk/base/gunit.h"
32 #include "talk/base/logging.h"
33 #include "talk/base/thread.h"
34 #include "talk/base/timeutils.h"
35 #include "talk/base/nullsocketserver.h"
37 using namespace talk_base;
39 class MessageQueueTest: public testing::Test, public MessageQueue {
41 bool IsLocked_Worker() {
42 if (!crit_.TryEnter()) {
49 // We have to do this on a worker thread, or else the TryEnter will
50 // succeed, since our critical sections are reentrant.
53 return worker.Invoke<bool>(
54 talk_base::Bind(&MessageQueueTest::IsLocked_Worker, this));
58 struct DeletedLockChecker {
59 DeletedLockChecker(MessageQueueTest* test, bool* was_locked, bool* deleted)
60 : test(test), was_locked(was_locked), deleted(deleted) { }
61 ~DeletedLockChecker() {
63 *was_locked = test->IsLocked();
65 MessageQueueTest* test;
70 static void DelayedPostsWithIdenticalTimesAreProcessedInFifoOrder(
72 EXPECT_TRUE(q != NULL);
73 TimeStamp now = Time();
74 q->PostAt(now, NULL, 3);
75 q->PostAt(now - 2, NULL, 0);
76 q->PostAt(now - 1, NULL, 1);
77 q->PostAt(now, NULL, 4);
78 q->PostAt(now - 1, NULL, 2);
81 for (size_t i=0; i<5; ++i) {
82 memset(&msg, 0, sizeof(msg));
83 EXPECT_TRUE(q->Get(&msg, 0));
84 EXPECT_EQ(i, msg.message_id);
87 EXPECT_FALSE(q->Get(&msg, 0)); // No more messages
90 TEST_F(MessageQueueTest,
91 DelayedPostsWithIdenticalTimesAreProcessedInFifoOrder) {
93 DelayedPostsWithIdenticalTimesAreProcessedInFifoOrder(&q);
94 NullSocketServer nullss;
95 MessageQueue q_nullss(&nullss);
96 DelayedPostsWithIdenticalTimesAreProcessedInFifoOrder(&q_nullss);
99 TEST_F(MessageQueueTest, DisposeNotLocked) {
100 bool was_locked = true;
101 bool deleted = false;
102 DeletedLockChecker* d = new DeletedLockChecker(this, &was_locked, &deleted);
105 EXPECT_FALSE(Get(&msg, 0));
106 EXPECT_TRUE(deleted);
107 EXPECT_FALSE(was_locked);
110 class DeletedMessageHandler : public MessageHandler {
112 explicit DeletedMessageHandler(bool* deleted) : deleted_(deleted) { }
113 ~DeletedMessageHandler() {
116 void OnMessage(Message* msg) { }
121 TEST_F(MessageQueueTest, DiposeHandlerWithPostedMessagePending) {
122 bool deleted = false;
123 DeletedMessageHandler *handler = new DeletedMessageHandler(&deleted);
124 // First, post a dispose.
126 // Now, post a message, which should *not* be returned by Get().
129 EXPECT_FALSE(Get(&msg, 0));
130 EXPECT_TRUE(deleted);
133 struct UnwrapMainThreadScope {
134 UnwrapMainThreadScope() : rewrap_(Thread::Current() != NULL) {
135 if (rewrap_) ThreadManager::Instance()->UnwrapCurrentThread();
137 ~UnwrapMainThreadScope() {
138 if (rewrap_) ThreadManager::Instance()->WrapCurrentThread();
144 TEST(MessageQueueManager, Clear) {
145 UnwrapMainThreadScope s;
146 if (MessageQueueManager::IsInitialized()) {
147 LOG(LS_INFO) << "Unable to run MessageQueueManager::Clear test, since the "
148 << "MessageQueueManager was already initialized by some "
149 << "other test in this run.";
152 bool deleted = false;
153 DeletedMessageHandler* handler = new DeletedMessageHandler(&deleted);
155 EXPECT_TRUE(deleted);
156 EXPECT_FALSE(MessageQueueManager::IsInitialized());