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 "vm/port.h" |
5 #include "platform/assert.h" | 6 #include "platform/assert.h" |
6 #include "vm/lockers.h" | 7 #include "vm/lockers.h" |
7 #include "vm/message_handler.h" | 8 #include "vm/message_handler.h" |
8 #include "vm/os.h" | 9 #include "vm/os.h" |
9 #include "vm/port.h" | |
10 #include "vm/unit_test.h" | 10 #include "vm/unit_test.h" |
11 | 11 |
12 namespace dart { | 12 namespace dart { |
13 | 13 |
14 // Provides private access to PortMap for testing. | 14 // Provides private access to PortMap for testing. |
15 class PortMapTestPeer { | 15 class PortMapTestPeer { |
16 public: | 16 public: |
17 static bool IsActivePort(Dart_Port port) { | 17 static bool IsActivePort(Dart_Port port) { |
18 MutexLocker ml(PortMap::mutex_); | 18 MutexLocker ml(PortMap::mutex_); |
19 return (PortMap::FindPort(port) >= 0); | 19 return (PortMap::FindPort(port) >= 0); |
20 } | 20 } |
21 | 21 |
22 static bool IsLivePort(Dart_Port port) { | 22 static bool IsLivePort(Dart_Port port) { |
23 MutexLocker ml(PortMap::mutex_); | 23 MutexLocker ml(PortMap::mutex_); |
24 intptr_t index = PortMap::FindPort(port); | 24 intptr_t index = PortMap::FindPort(port); |
25 if (index < 0) { | 25 if (index < 0) { |
26 return false; | 26 return false; |
27 } | 27 } |
28 return PortMap::map_[index].state == PortMap::kLivePort; | 28 return PortMap::map_[index].state == PortMap::kLivePort; |
29 } | 29 } |
30 }; | 30 }; |
31 | 31 |
32 | |
33 class PortTestMessageHandler : public MessageHandler { | 32 class PortTestMessageHandler : public MessageHandler { |
34 public: | 33 public: |
35 PortTestMessageHandler() : notify_count(0) {} | 34 PortTestMessageHandler() : notify_count(0) {} |
36 | 35 |
37 void MessageNotify(Message::Priority priority) { notify_count++; } | 36 void MessageNotify(Message::Priority priority) { notify_count++; } |
38 | 37 |
39 MessageStatus HandleMessage(Message* message) { return kOK; } | 38 MessageStatus HandleMessage(Message* message) { return kOK; } |
40 | 39 |
41 int notify_count; | 40 int notify_count; |
42 }; | 41 }; |
43 | 42 |
44 | |
45 TEST_CASE(PortMap_CreateAndCloseOnePort) { | 43 TEST_CASE(PortMap_CreateAndCloseOnePort) { |
46 PortTestMessageHandler handler; | 44 PortTestMessageHandler handler; |
47 Dart_Port port = PortMap::CreatePort(&handler); | 45 Dart_Port port = PortMap::CreatePort(&handler); |
48 EXPECT_NE(0, port); | 46 EXPECT_NE(0, port); |
49 EXPECT(PortMapTestPeer::IsActivePort(port)); | 47 EXPECT(PortMapTestPeer::IsActivePort(port)); |
50 | 48 |
51 PortMap::ClosePort(port); | 49 PortMap::ClosePort(port); |
52 EXPECT(!PortMapTestPeer::IsActivePort(port)); | 50 EXPECT(!PortMapTestPeer::IsActivePort(port)); |
53 } | 51 } |
54 | 52 |
55 | |
56 TEST_CASE(PortMap_CreateAndCloseTwoPorts) { | 53 TEST_CASE(PortMap_CreateAndCloseTwoPorts) { |
57 PortTestMessageHandler handler; | 54 PortTestMessageHandler handler; |
58 Dart_Port port1 = PortMap::CreatePort(&handler); | 55 Dart_Port port1 = PortMap::CreatePort(&handler); |
59 Dart_Port port2 = PortMap::CreatePort(&handler); | 56 Dart_Port port2 = PortMap::CreatePort(&handler); |
60 EXPECT(PortMapTestPeer::IsActivePort(port1)); | 57 EXPECT(PortMapTestPeer::IsActivePort(port1)); |
61 EXPECT(PortMapTestPeer::IsActivePort(port2)); | 58 EXPECT(PortMapTestPeer::IsActivePort(port2)); |
62 | 59 |
63 // Uniqueness. | 60 // Uniqueness. |
64 EXPECT_NE(port1, port2); | 61 EXPECT_NE(port1, port2); |
65 | 62 |
66 PortMap::ClosePort(port1); | 63 PortMap::ClosePort(port1); |
67 EXPECT(!PortMapTestPeer::IsActivePort(port1)); | 64 EXPECT(!PortMapTestPeer::IsActivePort(port1)); |
68 EXPECT(PortMapTestPeer::IsActivePort(port2)); | 65 EXPECT(PortMapTestPeer::IsActivePort(port2)); |
69 | 66 |
70 PortMap::ClosePort(port2); | 67 PortMap::ClosePort(port2); |
71 EXPECT(!PortMapTestPeer::IsActivePort(port1)); | 68 EXPECT(!PortMapTestPeer::IsActivePort(port1)); |
72 EXPECT(!PortMapTestPeer::IsActivePort(port2)); | 69 EXPECT(!PortMapTestPeer::IsActivePort(port2)); |
73 } | 70 } |
74 | 71 |
75 | |
76 TEST_CASE(PortMap_ClosePorts) { | 72 TEST_CASE(PortMap_ClosePorts) { |
77 PortTestMessageHandler handler; | 73 PortTestMessageHandler handler; |
78 Dart_Port port1 = PortMap::CreatePort(&handler); | 74 Dart_Port port1 = PortMap::CreatePort(&handler); |
79 Dart_Port port2 = PortMap::CreatePort(&handler); | 75 Dart_Port port2 = PortMap::CreatePort(&handler); |
80 EXPECT(PortMapTestPeer::IsActivePort(port1)); | 76 EXPECT(PortMapTestPeer::IsActivePort(port1)); |
81 EXPECT(PortMapTestPeer::IsActivePort(port2)); | 77 EXPECT(PortMapTestPeer::IsActivePort(port2)); |
82 | 78 |
83 // Close all ports at once. | 79 // Close all ports at once. |
84 PortMap::ClosePorts(&handler); | 80 PortMap::ClosePorts(&handler); |
85 EXPECT(!PortMapTestPeer::IsActivePort(port1)); | 81 EXPECT(!PortMapTestPeer::IsActivePort(port1)); |
86 EXPECT(!PortMapTestPeer::IsActivePort(port2)); | 82 EXPECT(!PortMapTestPeer::IsActivePort(port2)); |
87 } | 83 } |
88 | 84 |
89 | |
90 TEST_CASE(PortMap_CreateManyPorts) { | 85 TEST_CASE(PortMap_CreateManyPorts) { |
91 PortTestMessageHandler handler; | 86 PortTestMessageHandler handler; |
92 for (int i = 0; i < 32; i++) { | 87 for (int i = 0; i < 32; i++) { |
93 Dart_Port port = PortMap::CreatePort(&handler); | 88 Dart_Port port = PortMap::CreatePort(&handler); |
94 EXPECT(PortMapTestPeer::IsActivePort(port)); | 89 EXPECT(PortMapTestPeer::IsActivePort(port)); |
95 PortMap::ClosePort(port); | 90 PortMap::ClosePort(port); |
96 EXPECT(!PortMapTestPeer::IsActivePort(port)); | 91 EXPECT(!PortMapTestPeer::IsActivePort(port)); |
97 } | 92 } |
98 } | 93 } |
99 | 94 |
100 | |
101 TEST_CASE(PortMap_SetPortState) { | 95 TEST_CASE(PortMap_SetPortState) { |
102 PortTestMessageHandler handler; | 96 PortTestMessageHandler handler; |
103 | 97 |
104 // Regular port. | 98 // Regular port. |
105 Dart_Port port = PortMap::CreatePort(&handler); | 99 Dart_Port port = PortMap::CreatePort(&handler); |
106 EXPECT_NE(0, port); | 100 EXPECT_NE(0, port); |
107 EXPECT(PortMapTestPeer::IsActivePort(port)); | 101 EXPECT(PortMapTestPeer::IsActivePort(port)); |
108 EXPECT(!PortMapTestPeer::IsLivePort(port)); | 102 EXPECT(!PortMapTestPeer::IsLivePort(port)); |
109 | 103 |
110 PortMap::SetPortState(port, PortMap::kLivePort); | 104 PortMap::SetPortState(port, PortMap::kLivePort); |
(...skipping 12 matching lines...) Expand all Loading... |
123 | 117 |
124 PortMap::SetPortState(port, PortMap::kControlPort); | 118 PortMap::SetPortState(port, PortMap::kControlPort); |
125 EXPECT(PortMapTestPeer::IsActivePort(port)); | 119 EXPECT(PortMapTestPeer::IsActivePort(port)); |
126 EXPECT(!PortMapTestPeer::IsLivePort(port)); | 120 EXPECT(!PortMapTestPeer::IsLivePort(port)); |
127 | 121 |
128 PortMap::ClosePort(port); | 122 PortMap::ClosePort(port); |
129 EXPECT(!PortMapTestPeer::IsActivePort(port)); | 123 EXPECT(!PortMapTestPeer::IsActivePort(port)); |
130 EXPECT(!PortMapTestPeer::IsLivePort(port)); | 124 EXPECT(!PortMapTestPeer::IsLivePort(port)); |
131 } | 125 } |
132 | 126 |
133 | |
134 TEST_CASE(PortMap_PostMessage) { | 127 TEST_CASE(PortMap_PostMessage) { |
135 PortTestMessageHandler handler; | 128 PortTestMessageHandler handler; |
136 Dart_Port port = PortMap::CreatePort(&handler); | 129 Dart_Port port = PortMap::CreatePort(&handler); |
137 EXPECT_EQ(0, handler.notify_count); | 130 EXPECT_EQ(0, handler.notify_count); |
138 | 131 |
139 const char* message = "msg"; | 132 const char* message = "msg"; |
140 intptr_t message_len = strlen(message) + 1; | 133 intptr_t message_len = strlen(message) + 1; |
141 | 134 |
142 EXPECT(PortMap::PostMessage( | 135 EXPECT(PortMap::PostMessage( |
143 new Message(port, reinterpret_cast<uint8_t*>(strdup(message)), | 136 new Message(port, reinterpret_cast<uint8_t*>(strdup(message)), |
144 message_len, Message::kNormalPriority))); | 137 message_len, Message::kNormalPriority))); |
145 | 138 |
146 // Check that the message notify callback was called. | 139 // Check that the message notify callback was called. |
147 EXPECT_EQ(1, handler.notify_count); | 140 EXPECT_EQ(1, handler.notify_count); |
148 PortMap::ClosePorts(&handler); | 141 PortMap::ClosePorts(&handler); |
149 } | 142 } |
150 | 143 |
151 | |
152 TEST_CASE(PortMap_PostIntegerMessage) { | 144 TEST_CASE(PortMap_PostIntegerMessage) { |
153 PortTestMessageHandler handler; | 145 PortTestMessageHandler handler; |
154 Dart_Port port = PortMap::CreatePort(&handler); | 146 Dart_Port port = PortMap::CreatePort(&handler); |
155 EXPECT_EQ(0, handler.notify_count); | 147 EXPECT_EQ(0, handler.notify_count); |
156 | 148 |
157 EXPECT(PortMap::PostMessage( | 149 EXPECT(PortMap::PostMessage( |
158 new Message(port, Smi::New(42), Message::kNormalPriority))); | 150 new Message(port, Smi::New(42), Message::kNormalPriority))); |
159 | 151 |
160 // Check that the message notify callback was called. | 152 // Check that the message notify callback was called. |
161 EXPECT_EQ(1, handler.notify_count); | 153 EXPECT_EQ(1, handler.notify_count); |
162 PortMap::ClosePorts(&handler); | 154 PortMap::ClosePorts(&handler); |
163 } | 155 } |
164 | 156 |
165 | |
166 TEST_CASE(PortMap_PostNullMessage) { | 157 TEST_CASE(PortMap_PostNullMessage) { |
167 PortTestMessageHandler handler; | 158 PortTestMessageHandler handler; |
168 Dart_Port port = PortMap::CreatePort(&handler); | 159 Dart_Port port = PortMap::CreatePort(&handler); |
169 EXPECT_EQ(0, handler.notify_count); | 160 EXPECT_EQ(0, handler.notify_count); |
170 | 161 |
171 EXPECT(PortMap::PostMessage( | 162 EXPECT(PortMap::PostMessage( |
172 new Message(port, Object::null(), Message::kNormalPriority))); | 163 new Message(port, Object::null(), Message::kNormalPriority))); |
173 | 164 |
174 // Check that the message notify callback was called. | 165 // Check that the message notify callback was called. |
175 EXPECT_EQ(1, handler.notify_count); | 166 EXPECT_EQ(1, handler.notify_count); |
176 PortMap::ClosePorts(&handler); | 167 PortMap::ClosePorts(&handler); |
177 } | 168 } |
178 | 169 |
179 | |
180 TEST_CASE(PortMap_PostMessageClosedPort) { | 170 TEST_CASE(PortMap_PostMessageClosedPort) { |
181 // Create a port id and make it invalid. | 171 // Create a port id and make it invalid. |
182 PortTestMessageHandler handler; | 172 PortTestMessageHandler handler; |
183 Dart_Port port = PortMap::CreatePort(&handler); | 173 Dart_Port port = PortMap::CreatePort(&handler); |
184 PortMap::ClosePort(port); | 174 PortMap::ClosePort(port); |
185 | 175 |
186 const char* message = "msg"; | 176 const char* message = "msg"; |
187 intptr_t message_len = strlen(message) + 1; | 177 intptr_t message_len = strlen(message) + 1; |
188 | 178 |
189 EXPECT(!PortMap::PostMessage( | 179 EXPECT(!PortMap::PostMessage( |
190 new Message(port, reinterpret_cast<uint8_t*>(strdup(message)), | 180 new Message(port, reinterpret_cast<uint8_t*>(strdup(message)), |
191 message_len, Message::kNormalPriority))); | 181 message_len, Message::kNormalPriority))); |
192 } | 182 } |
193 | 183 |
194 } // namespace dart | 184 } // namespace dart |
OLD | NEW |