1 | // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 | // for details. All rights reserved. Use of this source code is governed by a |
3 | // BSD-style license that can be found in the LICENSE file. |
4 | |
5 | #include "vm/message.h" |
6 | #include "platform/assert.h" |
7 | #include "vm/unit_test.h" |
8 | |
9 | namespace dart { |
10 | |
11 | static uint8_t* AllocMsg(const char* str) { |
12 | return reinterpret_cast<uint8_t*>(Utils::StrDup(str)); |
13 | } |
14 | |
15 | TEST_CASE(MessageQueue_BasicOperations) { |
16 | MessageQueue queue; |
17 | EXPECT(queue.IsEmpty()); |
18 | MessageQueue::Iterator it(&queue); |
19 | // Queue is empty. |
20 | EXPECT(!it.HasNext()); |
21 | |
22 | Dart_Port port = 1; |
23 | |
24 | const char* str1 = "msg1" ; |
25 | const char* str2 = "msg2" ; |
26 | const char* str3 = "msg3" ; |
27 | const char* str4 = "msg4" ; |
28 | const char* str5 = "msg5" ; |
29 | const char* str6 = "msg6" ; |
30 | |
31 | // Add two messages. |
32 | std::unique_ptr<Message> msg = |
33 | Message::New(port, AllocMsg(str1), strlen(str1) + 1, nullptr, |
34 | Message::kNormalPriority); |
35 | Message* msg1 = msg.get(); |
36 | queue.Enqueue(std::move(msg), false); |
37 | EXPECT(queue.Length() == 1); |
38 | EXPECT(!queue.IsEmpty()); |
39 | it.Reset(&queue); |
40 | EXPECT(it.HasNext()); |
41 | EXPECT(it.Next() == msg1); |
42 | EXPECT(!it.HasNext()); |
43 | |
44 | msg = Message::New(port, AllocMsg(str2), strlen(str2) + 1, nullptr, |
45 | Message::kNormalPriority); |
46 | Message* msg2 = msg.get(); |
47 | queue.Enqueue(std::move(msg), false); |
48 | EXPECT(queue.Length() == 2); |
49 | EXPECT(!queue.IsEmpty()); |
50 | it.Reset(&queue); |
51 | EXPECT(it.HasNext()); |
52 | EXPECT(it.Next() == msg1); |
53 | EXPECT(it.HasNext()); |
54 | EXPECT(it.Next() == msg2); |
55 | EXPECT(!it.HasNext()); |
56 | |
57 | // Lookup messages by id. |
58 | EXPECT(queue.FindMessageById(reinterpret_cast<intptr_t>(msg1)) == msg1); |
59 | EXPECT(queue.FindMessageById(reinterpret_cast<intptr_t>(msg2)) == msg2); |
60 | |
61 | // Lookup bad id. |
62 | EXPECT(queue.FindMessageById(0x1) == NULL); |
63 | |
64 | // Remove message 1 |
65 | msg = queue.Dequeue(); |
66 | EXPECT(msg != nullptr); |
67 | EXPECT_STREQ(str1, reinterpret_cast<char*>(msg->snapshot())); |
68 | EXPECT(!queue.IsEmpty()); |
69 | |
70 | it.Reset(&queue); |
71 | EXPECT(it.HasNext()); |
72 | EXPECT(it.Next() == msg2); |
73 | |
74 | // Remove message 2 |
75 | msg = queue.Dequeue(); |
76 | EXPECT(msg != nullptr); |
77 | EXPECT_STREQ(str2, reinterpret_cast<char*>(msg->snapshot())); |
78 | EXPECT(queue.IsEmpty()); |
79 | |
80 | msg = Message::New(Message::kIllegalPort, AllocMsg(str3), strlen(str3) + 1, |
81 | nullptr, Message::kNormalPriority); |
82 | queue.Enqueue(std::move(msg), true); |
83 | EXPECT(!queue.IsEmpty()); |
84 | |
85 | msg = Message::New(Message::kIllegalPort, AllocMsg(str4), strlen(str4) + 1, |
86 | nullptr, Message::kNormalPriority); |
87 | queue.Enqueue(std::move(msg), true); |
88 | EXPECT(!queue.IsEmpty()); |
89 | |
90 | msg = Message::New(port, AllocMsg(str5), strlen(str5) + 1, nullptr, |
91 | Message::kNormalPriority); |
92 | queue.Enqueue(std::move(msg), false); |
93 | EXPECT(!queue.IsEmpty()); |
94 | |
95 | msg = Message::New(Message::kIllegalPort, AllocMsg(str6), strlen(str6) + 1, |
96 | nullptr, Message::kNormalPriority); |
97 | queue.Enqueue(std::move(msg), true); |
98 | EXPECT(!queue.IsEmpty()); |
99 | |
100 | msg = queue.Dequeue(); |
101 | EXPECT(msg != nullptr); |
102 | EXPECT_STREQ(str3, reinterpret_cast<char*>(msg->snapshot())); |
103 | EXPECT(!queue.IsEmpty()); |
104 | |
105 | msg = queue.Dequeue(); |
106 | EXPECT(msg != nullptr); |
107 | EXPECT_STREQ(str4, reinterpret_cast<char*>(msg->snapshot())); |
108 | EXPECT(!queue.IsEmpty()); |
109 | |
110 | msg = queue.Dequeue(); |
111 | EXPECT(msg != nullptr); |
112 | EXPECT_STREQ(str6, reinterpret_cast<char*>(msg->snapshot())); |
113 | EXPECT(!queue.IsEmpty()); |
114 | |
115 | msg = queue.Dequeue(); |
116 | EXPECT(msg != nullptr); |
117 | EXPECT_STREQ(str5, reinterpret_cast<char*>(msg->snapshot())); |
118 | EXPECT(queue.IsEmpty()); |
119 | } |
120 | |
121 | TEST_CASE(MessageQueue_Clear) { |
122 | MessageQueue queue; |
123 | Dart_Port port1 = 1; |
124 | Dart_Port port2 = 2; |
125 | |
126 | const char* str1 = "msg1" ; |
127 | const char* str2 = "msg2" ; |
128 | |
129 | // Add two messages. |
130 | std::unique_ptr<Message> msg; |
131 | msg = Message::New(port1, AllocMsg(str1), strlen(str1) + 1, nullptr, |
132 | Message::kNormalPriority); |
133 | queue.Enqueue(std::move(msg), false); |
134 | msg = Message::New(port2, AllocMsg(str2), strlen(str2) + 1, nullptr, |
135 | Message::kNormalPriority); |
136 | queue.Enqueue(std::move(msg), false); |
137 | |
138 | EXPECT(!queue.IsEmpty()); |
139 | queue.Clear(); |
140 | EXPECT(queue.IsEmpty()); |
141 | } |
142 | |
143 | } // namespace dart |
144 | |