2012-01-16 12:28:10 +00:00
|
|
|
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
2011-10-18 17:54:07 +00:00
|
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
2012-02-01 18:53:40 +00:00
|
|
|
#include "vm/message.h"
|
2017-07-13 15:08:33 +00:00
|
|
|
#include "platform/assert.h"
|
2011-10-18 17:54:07 +00:00
|
|
|
#include "vm/unit_test.h"
|
|
|
|
|
|
|
|
namespace dart {
|
|
|
|
|
2012-01-24 19:31:01 +00:00
|
|
|
static uint8_t* AllocMsg(const char* str) {
|
|
|
|
return reinterpret_cast<uint8_t*>(strdup(str));
|
2011-12-01 18:18:06 +00:00
|
|
|
}
|
|
|
|
|
2011-10-18 17:54:07 +00:00
|
|
|
TEST_CASE(MessageQueue_BasicOperations) {
|
|
|
|
MessageQueue queue;
|
2014-10-27 16:57:21 +00:00
|
|
|
EXPECT(queue.IsEmpty());
|
2015-05-04 20:20:44 +00:00
|
|
|
MessageQueue::Iterator it(&queue);
|
|
|
|
// Queue is empty.
|
|
|
|
EXPECT(!it.HasNext());
|
2011-10-18 17:54:07 +00:00
|
|
|
|
|
|
|
Dart_Port port = 1;
|
|
|
|
|
2012-08-27 20:25:44 +00:00
|
|
|
const char* str1 = "msg1";
|
|
|
|
const char* str2 = "msg2";
|
2014-12-08 22:45:10 +00:00
|
|
|
const char* str3 = "msg3";
|
|
|
|
const char* str4 = "msg4";
|
|
|
|
const char* str5 = "msg5";
|
|
|
|
const char* str6 = "msg6";
|
2012-08-27 20:25:44 +00:00
|
|
|
|
2011-10-18 17:54:07 +00:00
|
|
|
// Add two messages.
|
2019-05-06 21:01:39 +00:00
|
|
|
std::unique_ptr<Message> msg =
|
|
|
|
Message::New(port, AllocMsg(str1), strlen(str1) + 1, nullptr,
|
|
|
|
Message::kNormalPriority);
|
|
|
|
Message* msg1 = msg.get();
|
|
|
|
queue.Enqueue(std::move(msg), false);
|
2015-05-04 20:20:44 +00:00
|
|
|
EXPECT(queue.Length() == 1);
|
2014-10-27 16:57:21 +00:00
|
|
|
EXPECT(!queue.IsEmpty());
|
2015-05-04 20:20:44 +00:00
|
|
|
it.Reset(&queue);
|
|
|
|
EXPECT(it.HasNext());
|
|
|
|
EXPECT(it.Next() == msg1);
|
|
|
|
EXPECT(!it.HasNext());
|
2011-10-18 17:54:07 +00:00
|
|
|
|
2019-05-06 21:01:39 +00:00
|
|
|
msg = Message::New(port, AllocMsg(str2), strlen(str2) + 1, nullptr,
|
|
|
|
Message::kNormalPriority);
|
|
|
|
Message* msg2 = msg.get();
|
|
|
|
queue.Enqueue(std::move(msg), false);
|
2015-05-04 20:20:44 +00:00
|
|
|
EXPECT(queue.Length() == 2);
|
2014-10-27 16:57:21 +00:00
|
|
|
EXPECT(!queue.IsEmpty());
|
2015-05-04 20:20:44 +00:00
|
|
|
it.Reset(&queue);
|
|
|
|
EXPECT(it.HasNext());
|
|
|
|
EXPECT(it.Next() == msg1);
|
|
|
|
EXPECT(it.HasNext());
|
|
|
|
EXPECT(it.Next() == msg2);
|
|
|
|
EXPECT(!it.HasNext());
|
2011-10-18 17:54:07 +00:00
|
|
|
|
2015-05-04 20:20:44 +00:00
|
|
|
// Lookup messages by id.
|
|
|
|
EXPECT(queue.FindMessageById(reinterpret_cast<intptr_t>(msg1)) == msg1);
|
|
|
|
EXPECT(queue.FindMessageById(reinterpret_cast<intptr_t>(msg2)) == msg2);
|
|
|
|
|
|
|
|
// Lookup bad id.
|
|
|
|
EXPECT(queue.FindMessageById(0x1) == NULL);
|
|
|
|
|
|
|
|
// Remove message 1
|
2019-05-06 21:01:39 +00:00
|
|
|
msg = queue.Dequeue();
|
|
|
|
EXPECT(msg != nullptr);
|
2018-02-21 18:57:14 +00:00
|
|
|
EXPECT_STREQ(str1, reinterpret_cast<char*>(msg->snapshot()));
|
2014-10-27 16:57:21 +00:00
|
|
|
EXPECT(!queue.IsEmpty());
|
2011-10-18 17:54:07 +00:00
|
|
|
|
2015-05-04 20:20:44 +00:00
|
|
|
it.Reset(&queue);
|
|
|
|
EXPECT(it.HasNext());
|
|
|
|
EXPECT(it.Next() == msg2);
|
|
|
|
|
|
|
|
// Remove message 2
|
2012-04-19 19:47:27 +00:00
|
|
|
msg = queue.Dequeue();
|
2019-05-06 21:01:39 +00:00
|
|
|
EXPECT(msg != nullptr);
|
2018-02-21 18:57:14 +00:00
|
|
|
EXPECT_STREQ(str2, reinterpret_cast<char*>(msg->snapshot()));
|
2014-10-27 16:57:21 +00:00
|
|
|
EXPECT(queue.IsEmpty());
|
2012-01-24 19:31:01 +00:00
|
|
|
|
2019-05-06 21:01:39 +00:00
|
|
|
msg = Message::New(Message::kIllegalPort, AllocMsg(str3), strlen(str3) + 1,
|
|
|
|
nullptr, Message::kNormalPriority);
|
|
|
|
queue.Enqueue(std::move(msg), true);
|
2014-12-08 22:45:10 +00:00
|
|
|
EXPECT(!queue.IsEmpty());
|
|
|
|
|
2019-05-06 21:01:39 +00:00
|
|
|
msg = Message::New(Message::kIllegalPort, AllocMsg(str4), strlen(str4) + 1,
|
|
|
|
nullptr, Message::kNormalPriority);
|
|
|
|
queue.Enqueue(std::move(msg), true);
|
2014-12-08 22:45:10 +00:00
|
|
|
EXPECT(!queue.IsEmpty());
|
|
|
|
|
2019-05-06 21:01:39 +00:00
|
|
|
msg = Message::New(port, AllocMsg(str5), strlen(str5) + 1, nullptr,
|
|
|
|
Message::kNormalPriority);
|
|
|
|
queue.Enqueue(std::move(msg), false);
|
2014-12-08 22:45:10 +00:00
|
|
|
EXPECT(!queue.IsEmpty());
|
|
|
|
|
2019-05-06 21:01:39 +00:00
|
|
|
msg = Message::New(Message::kIllegalPort, AllocMsg(str6), strlen(str6) + 1,
|
|
|
|
nullptr, Message::kNormalPriority);
|
|
|
|
queue.Enqueue(std::move(msg), true);
|
2014-12-08 22:45:10 +00:00
|
|
|
EXPECT(!queue.IsEmpty());
|
|
|
|
|
|
|
|
msg = queue.Dequeue();
|
2019-05-06 21:01:39 +00:00
|
|
|
EXPECT(msg != nullptr);
|
2018-02-21 18:57:14 +00:00
|
|
|
EXPECT_STREQ(str3, reinterpret_cast<char*>(msg->snapshot()));
|
2014-12-08 22:45:10 +00:00
|
|
|
EXPECT(!queue.IsEmpty());
|
|
|
|
|
|
|
|
msg = queue.Dequeue();
|
2019-05-06 21:01:39 +00:00
|
|
|
EXPECT(msg != nullptr);
|
2018-02-21 18:57:14 +00:00
|
|
|
EXPECT_STREQ(str4, reinterpret_cast<char*>(msg->snapshot()));
|
2014-12-08 22:45:10 +00:00
|
|
|
EXPECT(!queue.IsEmpty());
|
|
|
|
|
|
|
|
msg = queue.Dequeue();
|
2019-05-06 21:01:39 +00:00
|
|
|
EXPECT(msg != nullptr);
|
2018-02-21 18:57:14 +00:00
|
|
|
EXPECT_STREQ(str6, reinterpret_cast<char*>(msg->snapshot()));
|
2014-12-08 22:45:10 +00:00
|
|
|
EXPECT(!queue.IsEmpty());
|
|
|
|
|
|
|
|
msg = queue.Dequeue();
|
2019-05-06 21:01:39 +00:00
|
|
|
EXPECT(msg != nullptr);
|
2018-02-21 18:57:14 +00:00
|
|
|
EXPECT_STREQ(str5, reinterpret_cast<char*>(msg->snapshot()));
|
2014-12-08 22:45:10 +00:00
|
|
|
EXPECT(queue.IsEmpty());
|
2012-01-24 19:31:01 +00:00
|
|
|
}
|
|
|
|
|
2012-12-20 20:03:07 +00:00
|
|
|
TEST_CASE(MessageQueue_Clear) {
|
2011-10-18 17:54:07 +00:00
|
|
|
MessageQueue queue;
|
|
|
|
Dart_Port port1 = 1;
|
|
|
|
Dart_Port port2 = 2;
|
|
|
|
|
2012-08-27 20:25:44 +00:00
|
|
|
const char* str1 = "msg1";
|
|
|
|
const char* str2 = "msg2";
|
|
|
|
|
2011-10-18 17:54:07 +00:00
|
|
|
// Add two messages.
|
2019-05-06 21:01:39 +00:00
|
|
|
std::unique_ptr<Message> msg;
|
|
|
|
msg = Message::New(port1, AllocMsg(str1), strlen(str1) + 1, nullptr,
|
|
|
|
Message::kNormalPriority);
|
|
|
|
queue.Enqueue(std::move(msg), false);
|
|
|
|
msg = Message::New(port2, AllocMsg(str2), strlen(str2) + 1, nullptr,
|
|
|
|
Message::kNormalPriority);
|
|
|
|
queue.Enqueue(std::move(msg), false);
|
2011-10-18 17:54:07 +00:00
|
|
|
|
2014-10-27 16:57:21 +00:00
|
|
|
EXPECT(!queue.IsEmpty());
|
2012-12-20 20:03:07 +00:00
|
|
|
queue.Clear();
|
2014-10-27 16:57:21 +00:00
|
|
|
EXPECT(queue.IsEmpty());
|
2011-10-18 17:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dart
|