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/message_handler.h" | 5 #include "vm/message_handler.h" |
6 #include "vm/port.h" | 6 #include "vm/port.h" |
7 #include "vm/unit_test.h" | 7 #include "vm/unit_test.h" |
8 | 8 |
9 namespace dart { | 9 namespace dart { |
10 | 10 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 return (reinterpret_cast<TestMessageHandler*>(data))->End(); | 116 return (reinterpret_cast<TestMessageHandler*>(data))->End(); |
117 } | 117 } |
118 | 118 |
119 | 119 |
120 UNIT_TEST_CASE(MessageHandler_PostMessage) { | 120 UNIT_TEST_CASE(MessageHandler_PostMessage) { |
121 TestMessageHandler handler; | 121 TestMessageHandler handler; |
122 MessageHandlerTestPeer handler_peer(&handler); | 122 MessageHandlerTestPeer handler_peer(&handler); |
123 EXPECT_EQ(0, handler.notify_count()); | 123 EXPECT_EQ(0, handler.notify_count()); |
124 | 124 |
125 // Post a message. | 125 // Post a message. |
126 Message* message = new Message(0, 0, NULL, 0, Message::kNormalPriority); | 126 Message* message = new Message(0, NULL, 0, Message::kNormalPriority); |
127 handler_peer.PostMessage(message); | 127 handler_peer.PostMessage(message); |
128 | 128 |
129 // The notify callback is called. | 129 // The notify callback is called. |
130 EXPECT_EQ(1, handler.notify_count()); | 130 EXPECT_EQ(1, handler.notify_count()); |
131 | 131 |
132 // The message has been added to the correct queue. | 132 // The message has been added to the correct queue. |
133 EXPECT(message == handler_peer.queue()->Dequeue()); | 133 EXPECT(message == handler_peer.queue()->Dequeue()); |
134 EXPECT(NULL == handler_peer.oob_queue()->Dequeue()); | 134 EXPECT(NULL == handler_peer.oob_queue()->Dequeue()); |
135 delete message; | 135 delete message; |
136 | 136 |
137 // Post an oob message. | 137 // Post an oob message. |
138 message = new Message(0, 0, NULL, 0, Message::kOOBPriority); | 138 message = new Message(0, NULL, 0, Message::kOOBPriority); |
139 handler_peer.PostMessage(message); | 139 handler_peer.PostMessage(message); |
140 | 140 |
141 // The notify callback is called. | 141 // The notify callback is called. |
142 EXPECT_EQ(2, handler.notify_count()); | 142 EXPECT_EQ(2, handler.notify_count()); |
143 | 143 |
144 // The message has been added to the correct queue. | 144 // The message has been added to the correct queue. |
145 EXPECT(message == handler_peer.oob_queue()->Dequeue()); | 145 EXPECT(message == handler_peer.oob_queue()->Dequeue()); |
146 EXPECT(NULL == handler_peer.queue()->Dequeue()); | 146 EXPECT(NULL == handler_peer.queue()->Dequeue()); |
147 delete message; | 147 delete message; |
148 } | 148 } |
149 | 149 |
150 | 150 |
151 UNIT_TEST_CASE(MessageHandler_ClosePort) { | 151 UNIT_TEST_CASE(MessageHandler_ClosePort) { |
152 TestMessageHandler handler; | 152 TestMessageHandler handler; |
153 MessageHandlerTestPeer handler_peer(&handler); | 153 MessageHandlerTestPeer handler_peer(&handler); |
154 Message* message1 = new Message(1, 0, NULL, 0, Message::kNormalPriority); | 154 Message* message1 = new Message(1, NULL, 0, Message::kNormalPriority); |
155 handler_peer.PostMessage(message1); | 155 handler_peer.PostMessage(message1); |
156 Message* message2 = new Message(2, 0, NULL, 0, Message::kNormalPriority); | 156 Message* message2 = new Message(2, NULL, 0, Message::kNormalPriority); |
157 handler_peer.PostMessage(message2); | 157 handler_peer.PostMessage(message2); |
158 | 158 |
159 handler_peer.ClosePort(1); | 159 handler_peer.ClosePort(1); |
160 | 160 |
161 // Closing the port does not drop the messages from the queue. | 161 // Closing the port does not drop the messages from the queue. |
162 EXPECT(message1 == handler_peer.queue()->Dequeue()); | 162 EXPECT(message1 == handler_peer.queue()->Dequeue()); |
163 EXPECT(message2 == handler_peer.queue()->Dequeue()); | 163 EXPECT(message2 == handler_peer.queue()->Dequeue()); |
164 delete message1; | 164 delete message1; |
165 delete message2; | 165 delete message2; |
166 } | 166 } |
167 | 167 |
168 | 168 |
169 UNIT_TEST_CASE(MessageHandler_CloseAllPorts) { | 169 UNIT_TEST_CASE(MessageHandler_CloseAllPorts) { |
170 TestMessageHandler handler; | 170 TestMessageHandler handler; |
171 MessageHandlerTestPeer handler_peer(&handler); | 171 MessageHandlerTestPeer handler_peer(&handler); |
172 Message* message1 = new Message(1, 0, NULL, 0, Message::kNormalPriority); | 172 Message* message1 = new Message(1, NULL, 0, Message::kNormalPriority); |
173 handler_peer.PostMessage(message1); | 173 handler_peer.PostMessage(message1); |
174 Message* message2 = new Message(2, 0, NULL, 0, Message::kNormalPriority); | 174 Message* message2 = new Message(2, NULL, 0, Message::kNormalPriority); |
175 handler_peer.PostMessage(message2); | 175 handler_peer.PostMessage(message2); |
176 | 176 |
177 handler_peer.CloseAllPorts(); | 177 handler_peer.CloseAllPorts(); |
178 | 178 |
179 // All messages are dropped from the queue. | 179 // All messages are dropped from the queue. |
180 EXPECT(NULL == handler_peer.queue()->Dequeue()); | 180 EXPECT(NULL == handler_peer.queue()->Dequeue()); |
181 } | 181 } |
182 | 182 |
183 | 183 |
184 UNIT_TEST_CASE(MessageHandler_HandleNextMessage) { | 184 UNIT_TEST_CASE(MessageHandler_HandleNextMessage) { |
185 TestMessageHandler handler; | 185 TestMessageHandler handler; |
186 MessageHandlerTestPeer handler_peer(&handler); | 186 MessageHandlerTestPeer handler_peer(&handler); |
187 Dart_Port port1 = PortMap::CreatePort(&handler); | 187 Dart_Port port1 = PortMap::CreatePort(&handler); |
188 Dart_Port port2 = PortMap::CreatePort(&handler); | 188 Dart_Port port2 = PortMap::CreatePort(&handler); |
189 Dart_Port port3 = PortMap::CreatePort(&handler); | 189 Dart_Port port3 = PortMap::CreatePort(&handler); |
190 Message* message1 = new Message(port1, 0, NULL, 0, Message::kNormalPriority); | 190 Message* message1 = new Message(port1, NULL, 0, Message::kNormalPriority); |
191 handler_peer.PostMessage(message1); | 191 handler_peer.PostMessage(message1); |
192 Message* oob_message1 = new Message(port2, 0, NULL, 0, Message::kOOBPriority); | 192 Message* oob_message1 = new Message(port2, NULL, 0, Message::kOOBPriority); |
193 handler_peer.PostMessage(oob_message1); | 193 handler_peer.PostMessage(oob_message1); |
194 Message* message2 = new Message(port2, 0, NULL, 0, Message::kNormalPriority); | 194 Message* message2 = new Message(port2, NULL, 0, Message::kNormalPriority); |
195 handler_peer.PostMessage(message2); | 195 handler_peer.PostMessage(message2); |
196 Message* oob_message2 = new Message(port3, 0, NULL, 0, Message::kOOBPriority); | 196 Message* oob_message2 = new Message(port3, NULL, 0, Message::kOOBPriority); |
197 handler_peer.PostMessage(oob_message2); | 197 handler_peer.PostMessage(oob_message2); |
198 | 198 |
199 // We handle both oob messages and a single normal message. | 199 // We handle both oob messages and a single normal message. |
200 EXPECT(handler.HandleNextMessage()); | 200 EXPECT(handler.HandleNextMessage()); |
201 EXPECT_EQ(3, handler.message_count()); | 201 EXPECT_EQ(3, handler.message_count()); |
202 Dart_Port* ports = handler.port_buffer(); | 202 Dart_Port* ports = handler.port_buffer(); |
203 EXPECT_EQ(port2, ports[0]); | 203 EXPECT_EQ(port2, ports[0]); |
204 EXPECT_EQ(port3, ports[1]); | 204 EXPECT_EQ(port3, ports[1]); |
205 EXPECT_EQ(port1, ports[2]); | 205 EXPECT_EQ(port1, ports[2]); |
206 PortMap::ClosePorts(&handler); | 206 PortMap::ClosePorts(&handler); |
207 } | 207 } |
208 | 208 |
209 | 209 |
210 UNIT_TEST_CASE(MessageHandler_HandleOOBMessages) { | 210 UNIT_TEST_CASE(MessageHandler_HandleOOBMessages) { |
211 TestMessageHandler handler; | 211 TestMessageHandler handler; |
212 MessageHandlerTestPeer handler_peer(&handler); | 212 MessageHandlerTestPeer handler_peer(&handler); |
213 Dart_Port port1 = PortMap::CreatePort(&handler); | 213 Dart_Port port1 = PortMap::CreatePort(&handler); |
214 Dart_Port port2 = PortMap::CreatePort(&handler); | 214 Dart_Port port2 = PortMap::CreatePort(&handler); |
215 Dart_Port port3 = PortMap::CreatePort(&handler); | 215 Dart_Port port3 = PortMap::CreatePort(&handler); |
216 Dart_Port port4 = PortMap::CreatePort(&handler); | 216 Dart_Port port4 = PortMap::CreatePort(&handler); |
217 Message* message1 = new Message(port1, 0, NULL, 0, Message::kNormalPriority); | 217 Message* message1 = new Message(port1, NULL, 0, Message::kNormalPriority); |
218 handler_peer.PostMessage(message1); | 218 handler_peer.PostMessage(message1); |
219 Message* message2 = new Message(port2, 0, NULL, 0, Message::kNormalPriority); | 219 Message* message2 = new Message(port2, NULL, 0, Message::kNormalPriority); |
220 handler_peer.PostMessage(message2); | 220 handler_peer.PostMessage(message2); |
221 Message* oob_message1 = new Message(port3, 0, NULL, 0, Message::kOOBPriority); | 221 Message* oob_message1 = new Message(port3, NULL, 0, Message::kOOBPriority); |
222 handler_peer.PostMessage(oob_message1); | 222 handler_peer.PostMessage(oob_message1); |
223 Message* oob_message2 = new Message(port4, 0, NULL, 0, Message::kOOBPriority); | 223 Message* oob_message2 = new Message(port4, NULL, 0, Message::kOOBPriority); |
224 handler_peer.PostMessage(oob_message2); | 224 handler_peer.PostMessage(oob_message2); |
225 | 225 |
226 // We handle both oob messages but no normal messages. | 226 // We handle both oob messages but no normal messages. |
227 EXPECT(handler.HandleOOBMessages()); | 227 EXPECT(handler.HandleOOBMessages()); |
228 EXPECT_EQ(2, handler.message_count()); | 228 EXPECT_EQ(2, handler.message_count()); |
229 Dart_Port* ports = handler.port_buffer(); | 229 Dart_Port* ports = handler.port_buffer(); |
230 EXPECT_EQ(port3, ports[0]); | 230 EXPECT_EQ(port3, ports[0]); |
231 EXPECT_EQ(port4, ports[1]); | 231 EXPECT_EQ(port4, ports[1]); |
232 handler_peer.CloseAllPorts(); | 232 handler_peer.CloseAllPorts(); |
233 } | 233 } |
234 | 234 |
235 | 235 |
236 struct ThreadStartInfo { | 236 struct ThreadStartInfo { |
237 MessageHandler* handler; | 237 MessageHandler* handler; |
238 Dart_Port* ports; | 238 Dart_Port* ports; |
239 int count; | 239 int count; |
240 }; | 240 }; |
241 | 241 |
242 | 242 |
243 static void SendMessages(uword param) { | 243 static void SendMessages(uword param) { |
244 ThreadStartInfo* info = reinterpret_cast<ThreadStartInfo*>(param); | 244 ThreadStartInfo* info = reinterpret_cast<ThreadStartInfo*>(param); |
245 MessageHandler* handler = info->handler; | 245 MessageHandler* handler = info->handler; |
246 MessageHandlerTestPeer handler_peer(handler); | 246 MessageHandlerTestPeer handler_peer(handler); |
247 for (int i = 0; i < info->count; i++) { | 247 for (int i = 0; i < info->count; i++) { |
248 Message* message = | 248 Message* message = |
249 new Message(info->ports[i], 0, NULL, 0, Message::kNormalPriority); | 249 new Message(info->ports[i], NULL, 0, Message::kNormalPriority); |
250 handler_peer.PostMessage(message); | 250 handler_peer.PostMessage(message); |
251 } | 251 } |
252 } | 252 } |
253 | 253 |
254 | 254 |
255 UNIT_TEST_CASE(MessageHandler_Run) { | 255 UNIT_TEST_CASE(MessageHandler_Run) { |
256 ThreadPool pool; | 256 ThreadPool pool; |
257 TestMessageHandler handler; | 257 TestMessageHandler handler; |
258 MessageHandlerTestPeer handler_peer(&handler); | 258 MessageHandlerTestPeer handler_peer(&handler); |
259 int sleep = 0; | 259 int sleep = 0; |
260 const int kMaxSleep = 20 * 1000; // 20 seconds. | 260 const int kMaxSleep = 20 * 1000; // 20 seconds. |
261 | 261 |
262 EXPECT(!handler.HasLivePorts()); | 262 EXPECT(!handler.HasLivePorts()); |
263 handler_peer.increment_live_ports(); | 263 handler_peer.increment_live_ports(); |
264 | 264 |
265 handler.Run(&pool, | 265 handler.Run(&pool, |
266 TestStartFunction, | 266 TestStartFunction, |
267 TestEndFunction, | 267 TestEndFunction, |
268 reinterpret_cast<uword>(&handler)); | 268 reinterpret_cast<uword>(&handler)); |
269 Dart_Port port = PortMap::CreatePort(&handler); | 269 Dart_Port port = PortMap::CreatePort(&handler); |
270 Message* message = new Message(port, 0, NULL, 0, Message::kNormalPriority); | 270 Message* message = new Message(port, NULL, 0, Message::kNormalPriority); |
271 handler_peer.PostMessage(message); | 271 handler_peer.PostMessage(message); |
272 | 272 |
273 // Wait for the first message to be handled. | 273 // Wait for the first message to be handled. |
274 while (sleep < kMaxSleep && handler.message_count() < 1) { | 274 while (sleep < kMaxSleep && handler.message_count() < 1) { |
275 OS::Sleep(10); | 275 OS::Sleep(10); |
276 sleep += 10; | 276 sleep += 10; |
277 } | 277 } |
278 EXPECT_EQ(1, handler.message_count()); | 278 EXPECT_EQ(1, handler.message_count()); |
279 EXPECT(handler.start_called()); | 279 EXPECT(handler.start_called()); |
280 EXPECT(!handler.end_called()); | 280 EXPECT(!handler.end_called()); |
(...skipping 21 matching lines...) Expand all Loading... |
302 EXPECT_EQ(port, handler_ports[0]); | 302 EXPECT_EQ(port, handler_ports[0]); |
303 for (int i = 1; i < 11; i++) { | 303 for (int i = 1; i < 11; i++) { |
304 EXPECT_EQ(ports[i - 1], handler_ports[i]); | 304 EXPECT_EQ(ports[i - 1], handler_ports[i]); |
305 } | 305 } |
306 handler_peer.decrement_live_ports(); | 306 handler_peer.decrement_live_ports(); |
307 EXPECT(!handler.HasLivePorts()); | 307 EXPECT(!handler.HasLivePorts()); |
308 PortMap::ClosePorts(&handler); | 308 PortMap::ClosePorts(&handler); |
309 } | 309 } |
310 | 310 |
311 } // namespace dart | 311 } // namespace dart |
OLD | NEW |