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

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

Issue 9182001: OOB messages and general message refactor. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: '' Created 8 years, 11 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 "include/dart_api.h" 5 #include "include/dart_api.h"
6 6
7 #include "vm/unit_test.h" 7 #include "vm/unit_test.h"
8 8
9 // Custom Isolate Test. 9 // Custom Isolate Test.
10 // 10 //
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 " echo('Received: ' + message);\n" 72 " echo('Received: ' + message);\n"
73 " });\n" 73 " });\n"
74 " });\n" 74 " });\n"
75 " return 'success';\n" 75 " return 'success';\n"
76 "}\n"; 76 "}\n";
77 77
78 78
79 // An entry in our event queue. 79 // An entry in our event queue.
80 class Event { 80 class Event {
81 protected: 81 protected:
82 Event() : next_(NULL) {} 82 explicit Event(Dart_Isolate isolate) : isolate_(isolate), next_(NULL) {}
83 83
84 public: 84 public:
85 virtual ~Event() {} 85 virtual ~Event() {}
86 virtual void Process() = 0; 86 virtual void Process() = 0;
87 87
88 virtual bool IsShutdownEvent(Dart_Isolate isolate) { 88 Dart_Isolate isolate() const { return isolate_; }
89 return false; 89
90 private:
91 friend class EventQueue;
92 Dart_Isolate isolate_;
93 Event* next_;
94 };
95
96
97 // A simple event queue for our test.
98 class EventQueue {
99 public:
100 EventQueue() {
101 head_ = NULL;
90 } 102 }
91 virtual bool IsMessageEvent(Dart_Isolate isolate, Dart_Port port) { 103
92 return false; 104 void Add(Event* event) {
105 if (head_ == NULL) {
106 head_ = event;
107 tail_ = event;
108 } else {
109 tail_->next_ = event;
110 tail_ = event;
111 }
112 }
113
114 Event* Get() {
115 if (head_ == NULL) {
116 return NULL;
117 }
118 Event* tmp = head_;
119 head_ = head_->next_;
120 if (head_ == NULL) {
121 // Not necessary, but why not.
122 tail_ = NULL;
123 }
124
125 return tmp;
126 }
127
128 void RemoveEventsForIsolate(Dart_Isolate isolate) {
129 Event* cur = head_;
130 Event* prev = NULL;
131 while (cur != NULL) {
132 Event* next = cur->next_;
133 if (cur->isolate() == isolate) {
134 // Remove matching event.
135 if (prev != NULL) {
136 prev->next_ = next;
137 } else {
138 head_ = next;
139 }
140 delete cur;
141 } else {
142 // Advance.
143 prev = cur;
144 }
145 cur = next;
146 }
147 tail_ = prev;
93 } 148 }
94 149
95 private: 150 private:
96 friend class EventQueue; 151 Event* head_;
97 Event* next_; 152 Event* tail_;
98 }; 153 };
154 EventQueue* event_queue;
99 155
100 156
101 // Start an isolate. 157 // Start an isolate.
102 class StartEvent : public Event { 158 class StartEvent : public Event {
103 public: 159 public:
104 StartEvent(Dart_Isolate isolate, const char* main) 160 StartEvent(Dart_Isolate isolate, const char* main)
105 : isolate_(isolate), main_(main) {} 161 : Event(isolate), main_(main) {}
106 162
107 virtual void Process(); 163 virtual void Process();
108 private: 164 private:
109 Dart_Isolate isolate_;
110 const char* main_; 165 const char* main_;
111 }; 166 };
112 167
113 168
114 void StartEvent::Process() { 169 void StartEvent::Process() {
115 OS::Print(">> StartEvent with isolate(%p)--\n", isolate_); 170 OS::Print(">> StartEvent with isolate(%p)--\n", isolate());
116 Dart_EnterIsolate(isolate_); 171 Dart_EnterIsolate(isolate());
117 Dart_EnterScope(); 172 Dart_EnterScope();
118 Dart_Handle result; 173 Dart_Handle result;
119 174
120 // Reload all the test classes here. 175 // Reload all the test classes here.
121 // 176 //
122 // TODO(turnidge): Use the create isolate callback instead? 177 // TODO(turnidge): Use the create isolate callback instead?
123 Dart_Handle lib = TestCase::LoadTestScript(kCustomIsolateScriptChars, 178 Dart_Handle lib = TestCase::LoadTestScript(kCustomIsolateScriptChars,
124 NativeLookup); 179 NativeLookup);
125 EXPECT_VALID(lib); 180 EXPECT_VALID(lib);
126 EXPECT_VALID(Dart_CompileAll()); 181 EXPECT_VALID(Dart_CompileAll());
(...skipping 16 matching lines...) Expand all
143 NULL); 198 NULL);
144 EXPECT_VALID(result); 199 EXPECT_VALID(result);
145 free(const_cast<char*>(main_)); 200 free(const_cast<char*>(main_));
146 main_ = NULL; 201 main_ = NULL;
147 202
148 Dart_ExitScope(); 203 Dart_ExitScope();
149 Dart_ExitIsolate(); 204 Dart_ExitIsolate();
150 } 205 }
151 206
152 207
153 // Shutdown an isolate. 208 // Notify an isolate of a pending message.
154 class ShutdownEvent : public Event { 209 class MessageEvent : public Event {
155 public: 210 public:
156 explicit ShutdownEvent(Dart_Isolate isolate) : isolate_(isolate) {} 211 explicit MessageEvent(Dart_Isolate isolate) : Event(isolate) {}
157 212
158 virtual bool IsShutdownEvent(Dart_Isolate isolate) { 213 ~MessageEvent() {
159 return isolate == isolate_;
160 } 214 }
161 215
162 virtual void Process(); 216 virtual void Process();
163 private:
164 Dart_Isolate isolate_;
165 };
166
167
168 void ShutdownEvent::Process() {
169 OS::Print("<< ShutdownEvent with isolate(%p)--\n", isolate_);
170 Dart_EnterIsolate(isolate_);
171 Dart_ShutdownIsolate();
172 }
173
174
175 // Deliver a message to an isolate.
176 class MessageEvent : public Event {
177 public:
178 MessageEvent(Dart_Isolate isolate, Dart_Port dest, Dart_Port reply,
179 Dart_Message msg)
180 : isolate_(isolate), dest_(dest), reply_(reply), msg_(msg) {}
181
182 ~MessageEvent() {
183 free(msg_);
184 msg_ = NULL;
185 }
186
187 virtual bool IsMessageEvent(Dart_Isolate isolate, Dart_Port port) {
188 return isolate == isolate_ && (port == kCloseAllPorts || port == dest_);
189 }
190
191 virtual void Process();
192 private:
193 Dart_Isolate isolate_;
194 Dart_Port dest_;
195 Dart_Port reply_;
196 Dart_Message msg_;
197 }; 217 };
198 218
199 219
200 void MessageEvent::Process() { 220 void MessageEvent::Process() {
201 OS::Print("$$ MessageEvent with dest port %lld--\n", dest_); 221 OS::Print("$$ MessageEvent with isolate(%p)\n", isolate());
202 Dart_EnterIsolate(isolate_); 222 Dart_EnterIsolate(isolate());
203 Dart_EnterScope(); 223 Dart_EnterScope();
204 224
205 Dart_Handle result = Dart_HandleMessage(dest_, reply_, msg_); 225 Dart_Handle result = Dart_HandleMessage();
206 EXPECT_VALID(result); 226 EXPECT_VALID(result);
207 227
208 Dart_ExitScope(); 228 if (!Dart_HasLivePorts()) {
209 Dart_ExitIsolate(); 229 OS::Print("<< Shutting down isolate(%p)\n", isolate());
230 event_queue->RemoveEventsForIsolate(isolate());
231 Dart_ShutdownIsolate();
232 } else {
233 Dart_ExitScope();
234 Dart_ExitIsolate();
235 }
236 ASSERT(Dart_CurrentIsolate() == NULL);
210 } 237 }
211 238
212 239
213 // A simple event queue for our test. 240 static void NotifyMessage(Dart_Isolate dest_isolate) {
214 class EventQueue { 241 OS::Print("-- Notify isolate(%p) of pending message --\n", dest_isolate);
215 public:
216 EventQueue() {
217 head_ = NULL;
218 }
219
220 void Add(Event* event) {
221 if (head_ == NULL) {
222 head_ = event;
223 tail_ = event;
224 } else {
225 tail_->next_ = event;
226 tail_ = event;
227 }
228 }
229
230 Event* Get() {
231 if (head_ == NULL) {
232 return NULL;
233 }
234 Event* tmp = head_;
235 head_ = head_->next_;
236 if (head_ == NULL) {
237 tail_ = NULL;
238 }
239
240 return tmp;
241 }
242
243 void ClosePort(Dart_Isolate isolate, Dart_Port port) {
244 Event* cur = head_;
245 Event* prev = NULL;
246 while (cur != NULL) {
247 Event* next = cur->next_;
248 if (cur->IsMessageEvent(isolate, port)) {
249 // Remove matching event.
250 if (prev != NULL) {
251 prev->next_ = next;
252 } else {
253 head_ = next;
254 }
255 delete cur;
256 } else {
257 // Advance.
258 prev = cur;
259 }
260 cur = next;
261 }
262 tail_ = prev;
263 }
264
265 private:
266 Event* head_;
267 Event* tail_;
268 };
269 EventQueue* event_queue;
270 Event* current_event;
271
272 static bool PostMessage(Dart_Isolate dest_isolate,
273 Dart_Port dest_port,
274 Dart_Port reply_port,
275 Dart_Message message) {
276 OS::Print("-- Posting message dest(%d) reply(%d) --\n",
277 dest_port, reply_port);
278 OS::Print("-- Adding MessageEvent to queue --\n"); 242 OS::Print("-- Adding MessageEvent to queue --\n");
279 event_queue->Add( 243 event_queue->Add(new MessageEvent(dest_isolate));
280 new MessageEvent(dest_isolate, dest_port, reply_port, message));
281 return true;
282 }
283
284
285 static void ClosePort(Dart_Isolate isolate,
286 Dart_Port port) {
287 OS::Print("-- Closing port (%lld) for isolate(%p) --\n",
288 port, isolate);
289
290 // Remove any pending events for the isolate/port.
291 event_queue->ClosePort(isolate, port);
292
293 Dart_Isolate current = Dart_CurrentIsolate();
294 if (current) {
295 Dart_ExitIsolate();
296 }
297 Dart_EnterIsolate(isolate);
298 if (!Dart_HasLivePorts() &&
299 (current_event == NULL || !current_event->IsShutdownEvent(isolate))) {
300 OS::Print("-- Adding ShutdownEvent to queue --\n");
301 event_queue->Add(new ShutdownEvent(isolate));
302 }
303 Dart_ExitIsolate();
304 if (current) {
305 Dart_EnterIsolate(current);
306 }
307 } 244 }
308 245
309 246
310 static Dart_NativeFunction NativeLookup(Dart_Handle name, int argc) { 247 static Dart_NativeFunction NativeLookup(Dart_Handle name, int argc) {
311 const char* name_str = NULL; 248 const char* name_str = NULL;
312 EXPECT(Dart_IsString(name)); 249 EXPECT(Dart_IsString(name));
313 EXPECT_VALID(Dart_StringToCString(name, &name_str)); 250 EXPECT_VALID(Dart_StringToCString(name, &name_str));
314 if (strcmp(name_str, "native_echo") == 0) { 251 if (strcmp(name_str, "native_echo") == 0) {
315 return &native_echo; 252 return &native_echo;
316 } else if (strcmp(name_str, "CustomIsolateImpl_start") == 0) { 253 } else if (strcmp(name_str, "CustomIsolateImpl_start") == 0) {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 EXPECT_VALID(Dart_StringToCString(param, &isolate_main)); 288 EXPECT_VALID(Dart_StringToCString(param, &isolate_main));
352 isolate_main = strdup(isolate_main); 289 isolate_main = strdup(isolate_main);
353 290
354 // Save current isolate. 291 // Save current isolate.
355 Dart_Isolate saved_isolate = Dart_CurrentIsolate(); 292 Dart_Isolate saved_isolate = Dart_CurrentIsolate();
356 Dart_ExitIsolate(); 293 Dart_ExitIsolate();
357 294
358 // Create a new Dart_Isolate. 295 // Create a new Dart_Isolate.
359 Dart_Isolate new_isolate = TestCase::CreateTestIsolate(); 296 Dart_Isolate new_isolate = TestCase::CreateTestIsolate();
360 EXPECT(new_isolate != NULL); 297 EXPECT(new_isolate != NULL);
361 Dart_SetMessageCallbacks(&PostMessage, &ClosePort); 298 Dart_SetMessageNotifyCallback(&NotifyMessage);
362 Dart_Port new_port = Dart_GetMainPortId(); 299 Dart_Port new_port = Dart_GetMainPortId();
363 300
364 OS::Print("-- Adding StartEvent to queue --\n"); 301 OS::Print("-- Adding StartEvent to queue --\n");
365 event_queue->Add(new StartEvent(new_isolate, isolate_main)); 302 event_queue->Add(new StartEvent(new_isolate, isolate_main));
366 303
367 // Restore the original isolate. 304 // Restore the original isolate.
368 Dart_ExitIsolate(); 305 Dart_ExitIsolate();
369 Dart_EnterIsolate(saved_isolate); 306 Dart_EnterIsolate(saved_isolate);
370 Dart_EnterScope(); 307 Dart_EnterScope();
371 308
372 Dart_Handle send_port = Dart_NewSendPort(new_port); 309 Dart_Handle send_port = Dart_NewSendPort(new_port);
373 EXPECT_VALID(send_port); 310 EXPECT_VALID(send_port);
374 Dart_SetReturnValue(args, send_port); 311 Dart_SetReturnValue(args, send_port);
375 312
376 OS::Print("-- Exit: CustomIsolateImpl_start --\n"); 313 OS::Print("-- Exit: CustomIsolateImpl_start --\n");
377 Dart_ExitScope(); 314 Dart_ExitScope();
378 } 315 }
379 316
380 317
381 UNIT_TEST_CASE(CustomIsolates) { 318 UNIT_TEST_CASE(CustomIsolates) {
382 event_queue = new EventQueue(); 319 event_queue = new EventQueue();
383 current_event = NULL;
384 320
385 Dart_Isolate dart_isolate = TestCase::CreateTestIsolate(); 321 Dart_Isolate dart_isolate = TestCase::CreateTestIsolate();
386 EXPECT(dart_isolate != NULL); 322 EXPECT(dart_isolate != NULL);
387 Dart_SetMessageCallbacks(&PostMessage, &ClosePort); 323 Dart_SetMessageNotifyCallback(&NotifyMessage);
388 Dart_EnterScope(); 324 Dart_EnterScope();
389 Dart_Handle result; 325 Dart_Handle result;
390 326
391 // Create a test library. 327 // Create a test library.
392 Dart_Handle lib = TestCase::LoadTestScript(kCustomIsolateScriptChars, 328 Dart_Handle lib = TestCase::LoadTestScript(kCustomIsolateScriptChars,
393 NativeLookup); 329 NativeLookup);
394 EXPECT_VALID(lib); 330 EXPECT_VALID(lib);
395 331
396 // Run main. 332 // Run main.
397 result = Dart_InvokeStatic(lib, 333 result = Dart_InvokeStatic(lib,
398 Dart_NewString(""), 334 Dart_NewString(""),
399 Dart_NewString("main"), 335 Dart_NewString("main"),
400 0, 336 0,
401 NULL); 337 NULL);
402 EXPECT_VALID(result); 338 EXPECT_VALID(result);
403 EXPECT(Dart_IsString(result)); 339 EXPECT(Dart_IsString(result));
404 const char* result_str = NULL; 340 const char* result_str = NULL;
405 EXPECT_VALID(Dart_StringToCString(result, &result_str)); 341 EXPECT_VALID(Dart_StringToCString(result, &result_str));
406 EXPECT_STREQ("success", result_str); 342 EXPECT_STREQ("success", result_str);
407 343
408 Dart_ExitScope(); 344 Dart_ExitScope();
409 Dart_ExitIsolate(); 345 Dart_ExitIsolate();
410 346
411 OS::Print("-- Starting event loop --\n"); 347 OS::Print("-- Starting event loop --\n");
412 Event* event = event_queue->Get(); 348 Event* event = event_queue->Get();
413 while (event) { 349 while (event) {
414 current_event = event;
415 event->Process(); 350 event->Process();
416 current_event = NULL;
417 delete event; 351 delete event;
418 event = event_queue->Get(); 352 event = event_queue->Get();
419 } 353 }
420 OS::Print("-- Finished event loop --\n"); 354 OS::Print("-- Finished event loop --\n");
421 EXPECT_STREQ("Received: 43", saved_echo); 355 EXPECT_STREQ("Received: 43", saved_echo);
422 free(const_cast<char*>(saved_echo)); 356 free(const_cast<char*>(saved_echo));
423 357
424 delete event_queue; 358 delete event_queue;
425 } 359 }
426 360
427 #endif // TARGET_ARCH_IA32. 361 #endif // TARGET_ARCH_IA32.
428 362
429 } // namespace dart 363 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698