OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "platform/assert.h" | 5 #include "platform/assert.h" |
6 #include "vm/message.h" | 6 #include "vm/message.h" |
7 #include "vm/unit_test.h" | 7 #include "vm/unit_test.h" |
8 | 8 |
9 namespace dart { | 9 namespace dart { |
10 | 10 |
11 | 11 |
12 // Provide access to private members of MessageQueue for testing. | |
13 class MessageQueueTestPeer { | |
14 public: | |
15 explicit MessageQueueTestPeer(MessageQueue* queue) : queue_(queue) {} | |
16 | |
17 bool HasMessage() const { | |
18 // We don't really need to grab the monitor during the unit test, | |
19 // but it doesn't hurt. | |
20 bool result = (queue_->head_ != NULL); | |
21 return result; | |
22 } | |
23 | |
24 private: | |
25 MessageQueue* queue_; | |
26 | |
27 DISALLOW_COPY_AND_ASSIGN(MessageQueueTestPeer); | |
28 }; | |
29 | |
30 | |
31 static uint8_t* AllocMsg(const char* str) { | 12 static uint8_t* AllocMsg(const char* str) { |
32 return reinterpret_cast<uint8_t*>(strdup(str)); | 13 return reinterpret_cast<uint8_t*>(strdup(str)); |
33 } | 14 } |
34 | 15 |
35 | 16 |
36 TEST_CASE(MessageQueue_BasicOperations) { | 17 TEST_CASE(MessageQueue_BasicOperations) { |
37 MessageQueue queue; | 18 MessageQueue queue; |
38 MessageQueueTestPeer queue_peer(&queue); | 19 EXPECT(queue.IsEmpty()); |
39 EXPECT(!queue_peer.HasMessage()); | |
40 | 20 |
41 Dart_Port port = 1; | 21 Dart_Port port = 1; |
42 | 22 |
43 const char* str1 = "msg1"; | 23 const char* str1 = "msg1"; |
44 const char* str2 = "msg2"; | 24 const char* str2 = "msg2"; |
45 | 25 |
46 // Add two messages. | 26 // Add two messages. |
47 Message* msg1 = | 27 Message* msg1 = |
48 new Message(port, AllocMsg(str1), strlen(str1) + 1, | 28 new Message(port, AllocMsg(str1), strlen(str1) + 1, |
49 Message::kNormalPriority); | 29 Message::kNormalPriority); |
50 queue.Enqueue(msg1); | 30 queue.Enqueue(msg1); |
51 EXPECT(queue_peer.HasMessage()); | 31 EXPECT(!queue.IsEmpty()); |
52 | 32 |
53 Message* msg2 = | 33 Message* msg2 = |
54 new Message(port, AllocMsg(str2), strlen(str2) + 1, | 34 new Message(port, AllocMsg(str2), strlen(str2) + 1, |
55 Message::kNormalPriority); | 35 Message::kNormalPriority); |
56 | 36 |
57 queue.Enqueue(msg2); | 37 queue.Enqueue(msg2); |
58 EXPECT(queue_peer.HasMessage()); | 38 EXPECT(!queue.IsEmpty()); |
59 | 39 |
60 // Remove two messages. | 40 // Remove two messages. |
61 Message* msg = queue.Dequeue(); | 41 Message* msg = queue.Dequeue(); |
62 EXPECT(msg != NULL); | 42 EXPECT(msg != NULL); |
63 EXPECT_STREQ(str1, reinterpret_cast<char*>(msg->data())); | 43 EXPECT_STREQ(str1, reinterpret_cast<char*>(msg->data())); |
64 EXPECT(queue_peer.HasMessage()); | 44 EXPECT(!queue.IsEmpty()); |
65 | 45 |
66 msg = queue.Dequeue(); | 46 msg = queue.Dequeue(); |
67 EXPECT(msg != NULL); | 47 EXPECT(msg != NULL); |
68 EXPECT_STREQ(str2, reinterpret_cast<char*>(msg->data())); | 48 EXPECT_STREQ(str2, reinterpret_cast<char*>(msg->data())); |
69 EXPECT(!queue_peer.HasMessage()); | 49 EXPECT(queue.IsEmpty()); |
70 | 50 |
71 delete msg1; | 51 delete msg1; |
72 delete msg2; | 52 delete msg2; |
73 } | 53 } |
74 | 54 |
75 | 55 |
76 TEST_CASE(MessageQueue_Clear) { | 56 TEST_CASE(MessageQueue_Clear) { |
77 MessageQueue queue; | 57 MessageQueue queue; |
78 MessageQueueTestPeer queue_peer(&queue); | |
79 Dart_Port port1 = 1; | 58 Dart_Port port1 = 1; |
80 Dart_Port port2 = 2; | 59 Dart_Port port2 = 2; |
81 | 60 |
82 const char* str1 = "msg1"; | 61 const char* str1 = "msg1"; |
83 const char* str2 = "msg2"; | 62 const char* str2 = "msg2"; |
84 | 63 |
85 // Add two messages. | 64 // Add two messages. |
86 Message* msg1 = | 65 Message* msg1 = |
87 new Message(port1, AllocMsg(str1), strlen(str1) + 1, | 66 new Message(port1, AllocMsg(str1), strlen(str1) + 1, |
88 Message::kNormalPriority); | 67 Message::kNormalPriority); |
89 queue.Enqueue(msg1); | 68 queue.Enqueue(msg1); |
90 Message* msg2 = | 69 Message* msg2 = |
91 new Message(port2, AllocMsg(str2), strlen(str2) + 1, | 70 new Message(port2, AllocMsg(str2), strlen(str2) + 1, |
92 Message::kNormalPriority); | 71 Message::kNormalPriority); |
93 queue.Enqueue(msg2); | 72 queue.Enqueue(msg2); |
94 | 73 |
95 EXPECT(queue_peer.HasMessage()); | 74 EXPECT(!queue.IsEmpty()); |
96 queue.Clear(); | 75 queue.Clear(); |
97 EXPECT(!queue_peer.HasMessage()); | 76 EXPECT(queue.IsEmpty()); |
98 | 77 |
99 // msg1 and msg2 already delete by FlushAll. | 78 // msg1 and msg2 already delete by FlushAll. |
100 } | 79 } |
101 | 80 |
102 } // namespace dart | 81 } // namespace dart |
OLD | NEW |