OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 library error_exit_at_spawn; | 5 library error_exit_at_spawn; |
6 | 6 |
7 import "dart:isolate"; | 7 import "dart:isolate"; |
8 import "dart:async"; | 8 import "dart:async"; |
9 import "package:async_helper/async_helper.dart"; | 9 import "package:async_helper/async_helper.dart"; |
10 import "package:expect/expect.dart"; | 10 import "package:expect/expect.dart"; |
11 | 11 |
12 isomain(replyPort) { | 12 isomain(replyPort) { |
13 RawReceivePort port = new RawReceivePort(); | 13 RawReceivePort port = new RawReceivePort(); |
14 port.handler = (v) { | 14 port.handler = (v) { |
15 switch (v) { | 15 switch (v) { |
16 case 0: | 16 case 0: |
17 replyPort.send(42); | 17 replyPort.send(42); |
18 break; | 18 break; |
19 case 1: | 19 case 1: |
20 throw new ArgumentError("whoops"); | 20 throw new ArgumentError("whoops"); |
21 case 2: | 21 case 2: |
22 throw new RangeError.value(37); | 22 throw new RangeError.value(37); |
23 case 3: | 23 case 3: |
24 port.close(); | 24 port.close(); |
25 } | 25 } |
26 }; | 26 }; |
27 replyPort.send(port.sendPort); | 27 replyPort.send(port.sendPort); |
28 } | 28 } |
29 | 29 |
30 main(){ | 30 main() { |
31 asyncStart(); | 31 asyncStart(); |
32 // Setup the port for communication with the newly spawned isolate. | 32 // Setup the port for communication with the newly spawned isolate. |
33 RawReceivePort reply = new RawReceivePort(null); | 33 RawReceivePort reply = new RawReceivePort(null); |
34 SendPort sendPort; | 34 SendPort sendPort; |
35 int state = 0; | 35 int state = 0; |
36 reply.handler = (port) { | 36 reply.handler = (port) { |
37 sendPort = port; | 37 sendPort = port; |
38 port.send(state); | 38 port.send(state); |
39 reply.handler = (v) { | 39 reply.handler = (v) { |
40 Expect.equals(0, state); | 40 Expect.equals(0, state); |
(...skipping 18 matching lines...) Expand all Loading... |
59 Expect.equals(new RangeError.value(37).toString(), "$error"); | 59 Expect.equals(new RangeError.value(37).toString(), "$error"); |
60 state++; | 60 state++; |
61 sendPort.send(state); | 61 sendPort.send(state); |
62 reply.close(); | 62 reply.close(); |
63 errorPort.close(); | 63 errorPort.close(); |
64 break; | 64 break; |
65 default: | 65 default: |
66 throw "Bad state for error: $state: $error"; | 66 throw "Bad state for error: $state: $error"; |
67 } | 67 } |
68 }; | 68 }; |
69 | 69 |
70 // Get exit notifications from other isolate as raw messages. | 70 // Get exit notifications from other isolate as raw messages. |
71 RawReceivePort exitPort = new RawReceivePort(); | 71 RawReceivePort exitPort = new RawReceivePort(); |
72 exitPort.handler = (message) { | 72 exitPort.handler = (message) { |
73 // onExit ports registered at spawn cannot have a particular message | 73 // onExit ports registered at spawn cannot have a particular message |
74 // associated. | 74 // associated. |
75 Expect.equals(null, message); | 75 Expect.equals(null, message); |
76 // Only exit after sending the termination message. | 76 // Only exit after sending the termination message. |
77 Expect.equals(3, state); | 77 Expect.equals(3, state); |
78 exitPort.close(); | 78 exitPort.close(); |
79 asyncEnd(); | 79 asyncEnd(); |
80 }; | 80 }; |
81 | 81 |
82 Isolate.spawn(isomain, | 82 Isolate.spawn(isomain, reply.sendPort, |
83 reply.sendPort, | 83 // Setup handlers as part of spawn. |
84 // Setup handlers as part of spawn. | 84 errorsAreFatal: false, |
85 errorsAreFatal: false, | 85 onError: errorPort.sendPort, |
86 onError: errorPort.sendPort, | 86 onExit: exitPort.sendPort); |
87 onExit: exitPort.sendPort); | |
88 } | 87 } |
OLD | NEW |