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 "platform/assert.h" | 5 #include "platform/assert.h" |
6 #include "vm/globals.h" | 6 #include "vm/globals.h" |
7 #include "vm/isolate.h" | 7 #include "vm/isolate.h" |
8 #include "vm/unit_test.h" | 8 #include "vm/unit_test.h" |
9 | 9 |
10 namespace dart { | 10 namespace dart { |
11 | 11 |
12 UNIT_TEST_CASE(IsolateCurrent) { | 12 UNIT_TEST_CASE(IsolateCurrent) { |
13 Isolate* isolate = Isolate::Init(NULL); | 13 Isolate* isolate = Isolate::Init(NULL); |
14 EXPECT_EQ(isolate, Isolate::Current()); | 14 EXPECT_EQ(isolate, Isolate::Current()); |
15 isolate->Shutdown(); | 15 isolate->Shutdown(); |
16 EXPECT_EQ(reinterpret_cast<Isolate*>(NULL), Isolate::Current()); | 16 EXPECT_EQ(reinterpret_cast<Isolate*>(NULL), Isolate::Current()); |
17 delete isolate; | 17 delete isolate; |
18 } | 18 } |
19 | 19 |
20 | 20 |
21 // Test to ensure that an exception is thrown if no isolate creation | 21 // Test to ensure that an exception is thrown if no isolate creation |
22 // callback has been set by the embedder when an isolate is spawned. | 22 // callback has been set by the embedder when an isolate is spawned. |
23 TEST_CASE(IsolateSpawn) { | 23 TEST_CASE(IsolateSpawn) { |
24 const char* kScriptChars = | 24 const char* kScriptChars = |
25 "import 'dart:isolate';\n" | 25 "import 'dart:isolate';\n" |
| 26 // Ignores printed lines. |
| 27 "var _nullPrintClosure = (String line) {};\n" |
26 "void entry(message) {}\n" | 28 "void entry(message) {}\n" |
27 "int testMain() {\n" | 29 "int testMain() {\n" |
28 " try {\n" | 30 " Isolate.spawn(entry, null);\n" |
29 " Isolate.spawn(entry, null);\n" | 31 // TODO(floitsch): the following code is only to bump the event loop |
30 " } catch (e) {\n" | 32 // so it executes asynchronous microtasks. |
31 " rethrow;\n" | 33 " var rp = new RawReceivePort();\n" |
32 " }\n" | 34 " rp.sendPort.send(null);\n" |
33 " return 0;\n" | 35 " rp.handler = (_) { rp.close(); };\n" |
34 "}\n"; | 36 "}\n"; |
35 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | 37 |
36 Dart_Handle result = Dart_Invoke(lib, NewString("testMain"), 0, NULL); | 38 Dart_Handle test_lib = TestCase::LoadTestScript(kScriptChars, NULL); |
| 39 |
| 40 // Setup the internal library's 'internalPrint' function. |
| 41 // Necessary because asynchronous errors use "print" to print their |
| 42 // stack trace. |
| 43 Dart_Handle url = NewString("dart:_collection-dev"); |
| 44 DART_CHECK_VALID(url); |
| 45 Dart_Handle internal_lib = Dart_LookupLibrary(url); |
| 46 DART_CHECK_VALID(internal_lib); |
| 47 Dart_Handle print = Dart_GetField(test_lib, NewString("_nullPrintClosure")); |
| 48 Dart_Handle result = Dart_SetField(internal_lib, |
| 49 NewString("_printClosure"), |
| 50 print); |
| 51 |
| 52 DART_CHECK_VALID(result); |
| 53 |
| 54 // Setup the 'scheduleImmediate' closure. |
| 55 url = NewString("dart:isolate"); |
| 56 DART_CHECK_VALID(url); |
| 57 Dart_Handle isolate_lib = Dart_LookupLibrary(url); |
| 58 DART_CHECK_VALID(isolate_lib); |
| 59 Dart_Handle schedule_immediate_closure = |
| 60 Dart_Invoke(isolate_lib, NewString("_getIsolateScheduleImmediateClosure"), |
| 61 0, NULL); |
| 62 Dart_Handle args[1]; |
| 63 args[0] = schedule_immediate_closure; |
| 64 url = NewString("dart:async"); |
| 65 DART_CHECK_VALID(url); |
| 66 Dart_Handle async_lib = Dart_LookupLibrary(url); |
| 67 DART_CHECK_VALID(async_lib); |
| 68 DART_CHECK_VALID(Dart_Invoke( |
| 69 async_lib, NewString("_setScheduleImmediateClosure"), 1, args)); |
| 70 |
| 71 |
| 72 result = Dart_Invoke(test_lib, NewString("testMain"), 0, NULL); |
| 73 EXPECT(!Dart_IsError(result)); |
| 74 // Run until all ports to isolate are closed. |
| 75 result = Dart_RunLoop(); |
37 EXPECT_ERROR(result, "Null callback specified for isolate creation"); | 76 EXPECT_ERROR(result, "Null callback specified for isolate creation"); |
38 EXPECT(Dart_ErrorHasException(result)); | 77 EXPECT(Dart_ErrorHasException(result)); |
39 Dart_Handle exception_result = Dart_ErrorGetException(result); | 78 Dart_Handle exception_result = Dart_ErrorGetException(result); |
40 EXPECT_VALID(exception_result); | 79 EXPECT_VALID(exception_result); |
41 } | 80 } |
42 | 81 |
43 } // namespace dart | 82 } // namespace dart |
OLD | NEW |