Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1078)

Side by Side Diff: runtime/vm/message_handler_test.cc

Issue 1371193005: VM restart + shutdown fixes (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: fix ups Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
zra 2015/10/05 16:56:47 Would it be easy to have some unit tests of the ne
turnidge 2015/10/05 22:18:24 Good idea. I added a couple of tests to cover the
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
11 class MessageHandlerTestPeer { 11 class MessageHandlerTestPeer {
(...skipping 20 matching lines...) Expand all
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 result_(kOK) {
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 return result_;
60 } 60 }
61 61
62 bool Start() { 62 MessageStatus Start() {
63 start_called_ = true; 63 start_called_ = true;
64 return true; 64 return kOK;
65 } 65 }
66 66
67 void End() { 67 void End() {
68 end_called_ = true; 68 end_called_ = true;
69 AddPortToBuffer(-2); 69 AddPortToBuffer(-2);
70 } 70 }
71 71
72 Dart_Port* port_buffer() const { return port_buffer_; } 72 Dart_Port* port_buffer() const { return port_buffer_; }
73 int notify_count() const { return notify_count_; } 73 int notify_count() const { return notify_count_; }
74 int message_count() const { return message_count_; } 74 int message_count() const { return message_count_; }
75 bool start_called() const { return start_called_; } 75 bool start_called() const { return start_called_; }
76 bool end_called() const { return end_called_; } 76 bool end_called() const { return end_called_; }
77 77
78 void set_result(bool result) { result_ = result; } 78 void set_result(MessageStatus result) { result_ = result; }
79 79
80 private: 80 private:
81 void AddPortToBuffer(Dart_Port port) { 81 void AddPortToBuffer(Dart_Port port) {
82 if (port_buffer_ == NULL) { 82 if (port_buffer_ == NULL) {
83 port_buffer_ = new Dart_Port[10]; 83 port_buffer_ = new Dart_Port[10];
84 port_buffer_size_ = 10; 84 port_buffer_size_ = 10;
85 } else if (message_count_ == port_buffer_size_) { 85 } else if (message_count_ == port_buffer_size_) {
86 int new_port_buffer_size_ = 2 * port_buffer_size_; 86 int new_port_buffer_size_ = 2 * port_buffer_size_;
87 Dart_Port* new_port_buffer_ = new Dart_Port[new_port_buffer_size_]; 87 Dart_Port* new_port_buffer_ = new Dart_Port[new_port_buffer_size_];
88 for (int i = 0; i < port_buffer_size_; i++) { 88 for (int i = 0; i < port_buffer_size_; i++) {
89 new_port_buffer_[i] = port_buffer_[i]; 89 new_port_buffer_[i] = port_buffer_[i];
90 } 90 }
91 delete[] port_buffer_; 91 delete[] port_buffer_;
92 port_buffer_ = new_port_buffer_; 92 port_buffer_ = new_port_buffer_;
93 port_buffer_size_ = new_port_buffer_size_; 93 port_buffer_size_ = new_port_buffer_size_;
94 } 94 }
95 port_buffer_[message_count_] = port; 95 port_buffer_[message_count_] = port;
96 } 96 }
97 97
98 Dart_Port* port_buffer_; 98 Dart_Port* port_buffer_;
99 int port_buffer_size_; 99 int port_buffer_size_;
100 int notify_count_; 100 int notify_count_;
101 int message_count_; 101 int message_count_;
102 bool start_called_; 102 bool start_called_;
103 bool end_called_; 103 bool end_called_;
104 bool result_; 104 MessageStatus result_;
105 105
106 DISALLOW_COPY_AND_ASSIGN(TestMessageHandler); 106 DISALLOW_COPY_AND_ASSIGN(TestMessageHandler);
107 }; 107 };
108 108
109 109
110 bool TestStartFunction(uword data) { 110 MessageHandler::MessageStatus TestStartFunction(uword data) {
111 return (reinterpret_cast<TestMessageHandler*>(data))->Start(); 111 return (reinterpret_cast<TestMessageHandler*>(data))->Start();
112 } 112 }
113 113
114 114
115 void TestEndFunction(uword data) { 115 void TestEndFunction(uword data) {
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) {
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 Message* message1 = new Message(port1, NULL, 0, Message::kNormalPriority); 225 Message* message1 = new Message(port1, NULL, 0, Message::kNormalPriority);
226 handler_peer.PostMessage(message1); 226 handler_peer.PostMessage(message1);
227 Message* oob_message1 = new Message(port2, NULL, 0, Message::kOOBPriority); 227 Message* oob_message1 = new Message(port2, NULL, 0, Message::kOOBPriority);
228 handler_peer.PostMessage(oob_message1); 228 handler_peer.PostMessage(oob_message1);
229 Message* message2 = new Message(port2, NULL, 0, Message::kNormalPriority); 229 Message* message2 = new Message(port2, NULL, 0, Message::kNormalPriority);
230 handler_peer.PostMessage(message2); 230 handler_peer.PostMessage(message2);
231 Message* oob_message2 = new Message(port3, NULL, 0, Message::kOOBPriority); 231 Message* oob_message2 = new Message(port3, NULL, 0, Message::kOOBPriority);
232 handler_peer.PostMessage(oob_message2); 232 handler_peer.PostMessage(oob_message2);
233 233
234 // We handle both oob messages and a single normal message. 234 // We handle both oob messages and a single normal message.
235 EXPECT(handler.HandleNextMessage()); 235 EXPECT_EQ(MessageHandler::kOK, handler.HandleNextMessage());
236 EXPECT_EQ(3, handler.message_count()); 236 EXPECT_EQ(3, handler.message_count());
237 Dart_Port* ports = handler.port_buffer(); 237 Dart_Port* ports = handler.port_buffer();
238 EXPECT_EQ(port2, ports[0]); 238 EXPECT_EQ(port2, ports[0]);
239 EXPECT_EQ(port3, ports[1]); 239 EXPECT_EQ(port3, ports[1]);
240 EXPECT_EQ(port1, ports[2]); 240 EXPECT_EQ(port1, ports[2]);
241 PortMap::ClosePorts(&handler); 241 PortMap::ClosePorts(&handler);
242 } 242 }
243 243
244 244
245 UNIT_TEST_CASE(MessageHandler_HandleOOBMessages) { 245 UNIT_TEST_CASE(MessageHandler_HandleOOBMessages) {
246 TestMessageHandler handler; 246 TestMessageHandler handler;
247 MessageHandlerTestPeer handler_peer(&handler); 247 MessageHandlerTestPeer handler_peer(&handler);
248 Dart_Port port1 = PortMap::CreatePort(&handler); 248 Dart_Port port1 = PortMap::CreatePort(&handler);
249 Dart_Port port2 = PortMap::CreatePort(&handler); 249 Dart_Port port2 = PortMap::CreatePort(&handler);
250 Dart_Port port3 = PortMap::CreatePort(&handler); 250 Dart_Port port3 = PortMap::CreatePort(&handler);
251 Dart_Port port4 = PortMap::CreatePort(&handler); 251 Dart_Port port4 = PortMap::CreatePort(&handler);
252 Message* message1 = new Message(port1, NULL, 0, Message::kNormalPriority); 252 Message* message1 = new Message(port1, NULL, 0, Message::kNormalPriority);
253 handler_peer.PostMessage(message1); 253 handler_peer.PostMessage(message1);
254 Message* message2 = new Message(port2, NULL, 0, Message::kNormalPriority); 254 Message* message2 = new Message(port2, NULL, 0, Message::kNormalPriority);
255 handler_peer.PostMessage(message2); 255 handler_peer.PostMessage(message2);
256 Message* oob_message1 = new Message(port3, NULL, 0, Message::kOOBPriority); 256 Message* oob_message1 = new Message(port3, NULL, 0, Message::kOOBPriority);
257 handler_peer.PostMessage(oob_message1); 257 handler_peer.PostMessage(oob_message1);
258 Message* oob_message2 = new Message(port4, NULL, 0, Message::kOOBPriority); 258 Message* oob_message2 = new Message(port4, NULL, 0, Message::kOOBPriority);
259 handler_peer.PostMessage(oob_message2); 259 handler_peer.PostMessage(oob_message2);
260 260
261 // We handle both oob messages but no normal messages. 261 // We handle both oob messages but no normal messages.
262 EXPECT(handler.HandleOOBMessages()); 262 EXPECT_EQ(MessageHandler::kOK, handler.HandleOOBMessages());
263 EXPECT_EQ(2, handler.message_count()); 263 EXPECT_EQ(2, handler.message_count());
264 Dart_Port* ports = handler.port_buffer(); 264 Dart_Port* ports = handler.port_buffer();
265 EXPECT_EQ(port3, ports[0]); 265 EXPECT_EQ(port3, ports[0]);
266 EXPECT_EQ(port4, ports[1]); 266 EXPECT_EQ(port4, ports[1]);
267 handler_peer.CloseAllPorts(); 267 handler_peer.CloseAllPorts();
268 } 268 }
269 269
270 270
271 struct ThreadStartInfo { 271 struct ThreadStartInfo {
272 MessageHandler* handler; 272 MessageHandler* handler;
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 EXPECT_EQ(port, handler_ports[0]); 337 EXPECT_EQ(port, handler_ports[0]);
338 for (int i = 1; i < 11; i++) { 338 for (int i = 1; i < 11; i++) {
339 EXPECT_EQ(ports[i - 1], handler_ports[i]); 339 EXPECT_EQ(ports[i - 1], handler_ports[i]);
340 } 340 }
341 handler_peer.decrement_live_ports(); 341 handler_peer.decrement_live_ports();
342 EXPECT(!handler.HasLivePorts()); 342 EXPECT(!handler.HasLivePorts());
343 PortMap::ClosePorts(&handler); 343 PortMap::ClosePorts(&handler);
344 } 344 }
345 345
346 } // namespace dart 346 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698