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 21 matching lines...) Expand all Loading... |
32 | 32 |
33 class TestMessageHandler : public MessageHandler { | 33 class TestMessageHandler : public MessageHandler { |
34 public: | 34 public: |
35 TestMessageHandler() | 35 TestMessageHandler() |
36 : port_buffer_(NULL), | 36 : port_buffer_(NULL), |
37 port_buffer_size_(0), | 37 port_buffer_size_(0), |
38 notify_count_(0), | 38 notify_count_(0), |
39 message_count_(0), | 39 message_count_(0), |
40 start_called_(false), | 40 start_called_(false), |
41 end_called_(false), | 41 end_called_(false), |
42 result_(true) { | 42 results_(NULL) { |
43 } | 43 } |
44 | 44 |
45 ~TestMessageHandler() { | 45 ~TestMessageHandler() { |
46 delete[] port_buffer_; | 46 delete[] port_buffer_; |
47 } | 47 } |
48 | 48 |
49 void MessageNotify(Message::Priority priority) { | 49 void MessageNotify(Message::Priority priority) { |
50 notify_count_++; | 50 notify_count_++; |
51 } | 51 } |
52 | 52 |
53 bool HandleMessage(Message* message) { | 53 MessageStatus HandleMessage(Message* message) { |
54 // For testing purposes, keep a list of the ports | 54 // For testing purposes, keep a list of the ports |
55 // for all messages we receive. | 55 // for all messages we receive. |
56 AddPortToBuffer(message->dest_port()); | 56 AddPortToBuffer(message->dest_port()); |
57 delete message; | 57 delete message; |
58 message_count_++; | 58 message_count_++; |
59 return result_; | 59 MessageStatus status = kOK; |
| 60 if (results_ != NULL) { |
| 61 status = results_[0]; |
| 62 results_++; |
| 63 } |
| 64 return status; |
60 } | 65 } |
61 | 66 |
62 bool Start() { | 67 MessageStatus Start() { |
63 start_called_ = true; | 68 start_called_ = true; |
64 return true; | 69 return kOK; |
65 } | 70 } |
66 | 71 |
67 void End() { | 72 void End() { |
68 end_called_ = true; | 73 end_called_ = true; |
69 AddPortToBuffer(-2); | 74 AddPortToBuffer(-2); |
70 } | 75 } |
71 | 76 |
72 Dart_Port* port_buffer() const { return port_buffer_; } | 77 Dart_Port* port_buffer() const { return port_buffer_; } |
73 int notify_count() const { return notify_count_; } | 78 int notify_count() const { return notify_count_; } |
74 int message_count() const { return message_count_; } | 79 int message_count() const { return message_count_; } |
75 bool start_called() const { return start_called_; } | 80 bool start_called() const { return start_called_; } |
76 bool end_called() const { return end_called_; } | 81 bool end_called() const { return end_called_; } |
77 | 82 |
78 void set_result(bool result) { result_ = result; } | 83 void set_results(MessageStatus* results) { results_ = results; } |
79 | 84 |
80 private: | 85 private: |
81 void AddPortToBuffer(Dart_Port port) { | 86 void AddPortToBuffer(Dart_Port port) { |
82 if (port_buffer_ == NULL) { | 87 if (port_buffer_ == NULL) { |
83 port_buffer_ = new Dart_Port[10]; | 88 port_buffer_ = new Dart_Port[10]; |
84 port_buffer_size_ = 10; | 89 port_buffer_size_ = 10; |
85 } else if (message_count_ == port_buffer_size_) { | 90 } else if (message_count_ == port_buffer_size_) { |
86 int new_port_buffer_size_ = 2 * port_buffer_size_; | 91 int new_port_buffer_size_ = 2 * port_buffer_size_; |
87 Dart_Port* new_port_buffer_ = new Dart_Port[new_port_buffer_size_]; | 92 Dart_Port* new_port_buffer_ = new Dart_Port[new_port_buffer_size_]; |
88 for (int i = 0; i < port_buffer_size_; i++) { | 93 for (int i = 0; i < port_buffer_size_; i++) { |
89 new_port_buffer_[i] = port_buffer_[i]; | 94 new_port_buffer_[i] = port_buffer_[i]; |
90 } | 95 } |
91 delete[] port_buffer_; | 96 delete[] port_buffer_; |
92 port_buffer_ = new_port_buffer_; | 97 port_buffer_ = new_port_buffer_; |
93 port_buffer_size_ = new_port_buffer_size_; | 98 port_buffer_size_ = new_port_buffer_size_; |
94 } | 99 } |
95 port_buffer_[message_count_] = port; | 100 port_buffer_[message_count_] = port; |
96 } | 101 } |
97 | 102 |
98 Dart_Port* port_buffer_; | 103 Dart_Port* port_buffer_; |
99 int port_buffer_size_; | 104 int port_buffer_size_; |
100 int notify_count_; | 105 int notify_count_; |
101 int message_count_; | 106 int message_count_; |
102 bool start_called_; | 107 bool start_called_; |
103 bool end_called_; | 108 bool end_called_; |
104 bool result_; | 109 MessageStatus* results_; |
105 | 110 |
106 DISALLOW_COPY_AND_ASSIGN(TestMessageHandler); | 111 DISALLOW_COPY_AND_ASSIGN(TestMessageHandler); |
107 }; | 112 }; |
108 | 113 |
109 | 114 |
110 bool TestStartFunction(uword data) { | 115 MessageHandler::MessageStatus TestStartFunction(uword data) { |
111 return (reinterpret_cast<TestMessageHandler*>(data))->Start(); | 116 return (reinterpret_cast<TestMessageHandler*>(data))->Start(); |
112 } | 117 } |
113 | 118 |
114 | 119 |
115 void TestEndFunction(uword data) { | 120 void TestEndFunction(uword data) { |
116 return (reinterpret_cast<TestMessageHandler*>(data))->End(); | 121 return (reinterpret_cast<TestMessageHandler*>(data))->End(); |
117 } | 122 } |
118 | 123 |
119 | 124 |
120 UNIT_TEST_CASE(MessageHandler_PostMessage) { | 125 UNIT_TEST_CASE(MessageHandler_PostMessage) { |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 Message* message1 = new Message(port1, NULL, 0, Message::kNormalPriority); | 230 Message* message1 = new Message(port1, NULL, 0, Message::kNormalPriority); |
226 handler_peer.PostMessage(message1); | 231 handler_peer.PostMessage(message1); |
227 Message* oob_message1 = new Message(port2, NULL, 0, Message::kOOBPriority); | 232 Message* oob_message1 = new Message(port2, NULL, 0, Message::kOOBPriority); |
228 handler_peer.PostMessage(oob_message1); | 233 handler_peer.PostMessage(oob_message1); |
229 Message* message2 = new Message(port2, NULL, 0, Message::kNormalPriority); | 234 Message* message2 = new Message(port2, NULL, 0, Message::kNormalPriority); |
230 handler_peer.PostMessage(message2); | 235 handler_peer.PostMessage(message2); |
231 Message* oob_message2 = new Message(port3, NULL, 0, Message::kOOBPriority); | 236 Message* oob_message2 = new Message(port3, NULL, 0, Message::kOOBPriority); |
232 handler_peer.PostMessage(oob_message2); | 237 handler_peer.PostMessage(oob_message2); |
233 | 238 |
234 // We handle both oob messages and a single normal message. | 239 // We handle both oob messages and a single normal message. |
235 EXPECT(handler.HandleNextMessage()); | 240 EXPECT_EQ(MessageHandler::kOK, handler.HandleNextMessage()); |
236 EXPECT_EQ(3, handler.message_count()); | 241 EXPECT_EQ(3, handler.message_count()); |
237 Dart_Port* ports = handler.port_buffer(); | 242 Dart_Port* ports = handler.port_buffer(); |
238 EXPECT_EQ(port2, ports[0]); | 243 EXPECT_EQ(port2, ports[0]); |
239 EXPECT_EQ(port3, ports[1]); | 244 EXPECT_EQ(port3, ports[1]); |
240 EXPECT_EQ(port1, ports[2]); | 245 EXPECT_EQ(port1, ports[2]); |
241 PortMap::ClosePorts(&handler); | 246 PortMap::ClosePorts(&handler); |
242 } | 247 } |
243 | 248 |
244 | 249 |
| 250 UNIT_TEST_CASE(MessageHandler_HandleNextMessage_ProcessOOBAfterError) { |
| 251 TestMessageHandler handler; |
| 252 MessageHandler::MessageStatus results[] = { |
| 253 MessageHandler::kError, // oob_message1 |
| 254 MessageHandler::kOK, // oob_message2 |
| 255 MessageHandler::kOK, // unused |
| 256 }; |
| 257 handler.set_results(results); |
| 258 MessageHandlerTestPeer handler_peer(&handler); |
| 259 Dart_Port port1 = PortMap::CreatePort(&handler); |
| 260 Dart_Port port2 = PortMap::CreatePort(&handler); |
| 261 Dart_Port port3 = PortMap::CreatePort(&handler); |
| 262 Message* message1 = new Message(port1, NULL, 0, Message::kNormalPriority); |
| 263 handler_peer.PostMessage(message1); |
| 264 Message* oob_message1 = new Message(port2, NULL, 0, Message::kOOBPriority); |
| 265 handler_peer.PostMessage(oob_message1); |
| 266 Message* oob_message2 = new Message(port3, NULL, 0, Message::kOOBPriority); |
| 267 handler_peer.PostMessage(oob_message2); |
| 268 |
| 269 // When we get an error, we continue processing oob messages but |
| 270 // stop handling normal messages. |
| 271 EXPECT_EQ(MessageHandler::kError, handler.HandleNextMessage()); |
| 272 EXPECT_EQ(2, handler.message_count()); |
| 273 Dart_Port* ports = handler.port_buffer(); |
| 274 EXPECT_EQ(port2, ports[0]); // oob_message1, error |
| 275 EXPECT_EQ(port3, ports[1]); // oob_message2, ok |
| 276 handler_peer.CloseAllPorts(); |
| 277 } |
| 278 |
| 279 |
| 280 UNIT_TEST_CASE(MessageHandler_HandleNextMessage_Shutdown) { |
| 281 TestMessageHandler handler; |
| 282 MessageHandler::MessageStatus results[] = { |
| 283 MessageHandler::kOK, // oob_message1 |
| 284 MessageHandler::kShutdown, // oob_message2 |
| 285 MessageHandler::kOK, // unused |
| 286 MessageHandler::kOK, // unused |
| 287 }; |
| 288 handler.set_results(results); |
| 289 MessageHandlerTestPeer handler_peer(&handler); |
| 290 Dart_Port port1 = PortMap::CreatePort(&handler); |
| 291 Dart_Port port2 = PortMap::CreatePort(&handler); |
| 292 Dart_Port port3 = PortMap::CreatePort(&handler); |
| 293 Dart_Port port4 = PortMap::CreatePort(&handler); |
| 294 Message* message1 = new Message(port1, NULL, 0, Message::kNormalPriority); |
| 295 handler_peer.PostMessage(message1); |
| 296 Message* oob_message1 = new Message(port2, NULL, 0, Message::kOOBPriority); |
| 297 handler_peer.PostMessage(oob_message1); |
| 298 Message* oob_message2 = new Message(port3, NULL, 0, Message::kOOBPriority); |
| 299 handler_peer.PostMessage(oob_message2); |
| 300 Message* oob_message3 = new Message(port4, NULL, 0, Message::kOOBPriority); |
| 301 handler_peer.PostMessage(oob_message3); |
| 302 |
| 303 // When we get a shutdown message, we stop processing all messages. |
| 304 EXPECT_EQ(MessageHandler::kShutdown, handler.HandleNextMessage()); |
| 305 EXPECT_EQ(2, handler.message_count()); |
| 306 Dart_Port* ports = handler.port_buffer(); |
| 307 EXPECT_EQ(port2, ports[0]); // oob_message1, ok |
| 308 EXPECT_EQ(port3, ports[1]); // oob_message2, shutdown |
| 309 { |
| 310 // The oob queue has been cleared. oob_message3 is gone. |
| 311 MessageHandler::AcquiredQueues aq; |
| 312 handler.AcquireQueues(&aq); |
| 313 EXPECT(aq.oob_queue()->Length() == 0); |
| 314 } |
| 315 handler_peer.CloseAllPorts(); |
| 316 } |
| 317 |
| 318 |
245 UNIT_TEST_CASE(MessageHandler_HandleOOBMessages) { | 319 UNIT_TEST_CASE(MessageHandler_HandleOOBMessages) { |
246 TestMessageHandler handler; | 320 TestMessageHandler handler; |
247 MessageHandlerTestPeer handler_peer(&handler); | 321 MessageHandlerTestPeer handler_peer(&handler); |
248 Dart_Port port1 = PortMap::CreatePort(&handler); | 322 Dart_Port port1 = PortMap::CreatePort(&handler); |
249 Dart_Port port2 = PortMap::CreatePort(&handler); | 323 Dart_Port port2 = PortMap::CreatePort(&handler); |
250 Dart_Port port3 = PortMap::CreatePort(&handler); | 324 Dart_Port port3 = PortMap::CreatePort(&handler); |
251 Dart_Port port4 = PortMap::CreatePort(&handler); | 325 Dart_Port port4 = PortMap::CreatePort(&handler); |
252 Message* message1 = new Message(port1, NULL, 0, Message::kNormalPriority); | 326 Message* message1 = new Message(port1, NULL, 0, Message::kNormalPriority); |
253 handler_peer.PostMessage(message1); | 327 handler_peer.PostMessage(message1); |
254 Message* message2 = new Message(port2, NULL, 0, Message::kNormalPriority); | 328 Message* message2 = new Message(port2, NULL, 0, Message::kNormalPriority); |
255 handler_peer.PostMessage(message2); | 329 handler_peer.PostMessage(message2); |
256 Message* oob_message1 = new Message(port3, NULL, 0, Message::kOOBPriority); | 330 Message* oob_message1 = new Message(port3, NULL, 0, Message::kOOBPriority); |
257 handler_peer.PostMessage(oob_message1); | 331 handler_peer.PostMessage(oob_message1); |
258 Message* oob_message2 = new Message(port4, NULL, 0, Message::kOOBPriority); | 332 Message* oob_message2 = new Message(port4, NULL, 0, Message::kOOBPriority); |
259 handler_peer.PostMessage(oob_message2); | 333 handler_peer.PostMessage(oob_message2); |
260 | 334 |
261 // We handle both oob messages but no normal messages. | 335 // We handle both oob messages but no normal messages. |
262 EXPECT(handler.HandleOOBMessages()); | 336 EXPECT_EQ(MessageHandler::kOK, handler.HandleOOBMessages()); |
263 EXPECT_EQ(2, handler.message_count()); | 337 EXPECT_EQ(2, handler.message_count()); |
264 Dart_Port* ports = handler.port_buffer(); | 338 Dart_Port* ports = handler.port_buffer(); |
265 EXPECT_EQ(port3, ports[0]); | 339 EXPECT_EQ(port3, ports[0]); |
266 EXPECT_EQ(port4, ports[1]); | 340 EXPECT_EQ(port4, ports[1]); |
267 handler_peer.CloseAllPorts(); | 341 handler_peer.CloseAllPorts(); |
268 } | 342 } |
269 | 343 |
270 | 344 |
271 struct ThreadStartInfo { | 345 struct ThreadStartInfo { |
272 MessageHandler* handler; | 346 MessageHandler* handler; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 EXPECT_EQ(port, handler_ports[0]); | 411 EXPECT_EQ(port, handler_ports[0]); |
338 for (int i = 1; i < 11; i++) { | 412 for (int i = 1; i < 11; i++) { |
339 EXPECT_EQ(ports[i - 1], handler_ports[i]); | 413 EXPECT_EQ(ports[i - 1], handler_ports[i]); |
340 } | 414 } |
341 handler_peer.decrement_live_ports(); | 415 handler_peer.decrement_live_ports(); |
342 EXPECT(!handler.HasLivePorts()); | 416 EXPECT(!handler.HasLivePorts()); |
343 PortMap::ClosePorts(&handler); | 417 PortMap::ClosePorts(&handler); |
344 } | 418 } |
345 | 419 |
346 } // namespace dart | 420 } // namespace dart |
OLD | NEW |