OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library error_exit_at_spawn; | |
6 | |
7 import "dart:isolate"; | |
8 import "dart:async"; | |
9 import "package:async_helper/async_helper.dart"; | |
10 import "package:expect/expect.dart"; | |
11 | |
12 isomain(replyPort) { | |
floitsch
2015/07/20 13:03:49
Create two other tests:
- one that tests that the
Ivan Posva
2015/07/20 14:28:32
Done.
| |
13 RawReceivePort port = new RawReceivePort(); | |
14 port.handler = (v) { | |
15 switch (v) { | |
16 case 0: | |
17 replyPort.send(42); | |
18 break; | |
19 case 1: | |
20 throw new ArgumentError("whoops"); | |
21 case 2: | |
22 throw new RangeError.value(37); | |
23 case 3: | |
24 port.close(); | |
25 } | |
26 }; | |
27 replyPort.send(port.sendPort); | |
28 } | |
29 | |
30 main(){ | |
31 asyncStart(); | |
32 // Setup the port for communication with the newly spawned isolate. | |
33 RawReceivePort reply = new RawReceivePort(null); | |
34 SendPort sendPort; | |
35 int state = 0; | |
36 reply.handler = (port) { | |
37 sendPort = port; | |
38 port.send(state); | |
39 reply.handler = (v) { | |
40 Expect.equals(0, state); | |
41 Expect.equals(42, v); | |
42 state++; | |
43 sendPort.send(state); | |
44 }; | |
45 }; | |
46 | |
47 // Capture errors from other isolate as raw messages. | |
48 RawReceivePort errorPort = new RawReceivePort(); | |
49 errorPort.handler = (message) { | |
50 String error = message[0]; | |
51 String stack = message[1]; | |
52 switch (state) { | |
53 case 1: | |
54 Expect.equals(new ArgumentError("whoops").toString(), "$error"); | |
55 state++; | |
56 sendPort.send(state); | |
57 break; | |
58 case 2: | |
59 Expect.equals(new RangeError.value(37).toString(), "$error"); | |
60 state++; | |
61 sendPort.send(state); | |
62 reply.close(); | |
63 errorPort.close(); | |
64 break; | |
65 default: | |
66 throw "Bad state for error: $state: $error"; | |
67 } | |
68 }; | |
69 | |
70 // Get exit notifications from other isolate as raw messages. | |
71 RawReceivePort exitPort = new RawReceivePort(); | |
72 exitPort.handler = (message) { | |
73 // onExit ports registered at spawn cannot have a particular message associa ted. | |
floitsch
2015/07/20 13:03:49
long line.
Ivan Posva
2015/07/20 14:28:32
Done.
| |
74 Expect.equals(null, message); | |
75 // Only exit after sending the termination message. | |
76 Expect.equals(3, state); | |
77 exitPort.close(); | |
78 asyncEnd(); | |
79 }; | |
80 | |
81 Isolate.spawn(isomain, | |
82 reply.sendPort, | |
83 // Setup handlers as part of spawn. | |
84 errorsAreFatal: false, | |
85 onError: errorPort.sendPort, | |
86 onExit: exitPort.sendPort); | |
87 } | |
OLD | NEW |