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