OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 handle_error_test; | 5 library handle_error_test; |
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 isomain1(replyPort) { | 12 isomain1(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 RawReceivePort reply = new RawReceivePort(null); | 32 RawReceivePort reply = new RawReceivePort(null); |
33 // Start paused so we have time to set up the error handler. | 33 // Start paused so we have time to set up the error handler. |
34 Isolate.spawn(isomain1, reply.sendPort, paused: true).then((Isolate isolate) { | 34 Isolate.spawn(isomain1, reply.sendPort, paused: true).then((Isolate isolate) { |
35 isolate.setErrorsFatal(false); | 35 isolate.setErrorsFatal(false); |
36 Stream errors = isolate.errors; // Broadcast stream, never a done message. | 36 Stream errors = isolate.errors; // Broadcast stream, never a done message. |
37 SendPort sendPort; | 37 SendPort sendPort; |
38 StreamSubscription subscription; | 38 StreamSubscription subscription; |
39 int state = 0; | 39 int state = 0; |
40 reply.handler = (port) { | 40 reply.handler = (port) { |
41 sendPort = port; | 41 sendPort = port; |
42 port.send(state); | 42 port.send(state); |
43 reply.handler = (v) { | 43 reply.handler = (v) { |
44 Expect.equals(0, state); | 44 Expect.equals(0, state); |
45 Expect.equals(42, v); | 45 Expect.equals(42, v); |
46 state++; | 46 state++; |
(...skipping 15 matching lines...) Expand all Loading... |
62 subscription.cancel(); | 62 subscription.cancel(); |
63 asyncEnd(); | 63 asyncEnd(); |
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 isolate.resume(isolate.pauseCapability); | 69 isolate.resume(isolate.pauseCapability); |
70 }); | 70 }); |
71 } | 71 } |
OLD | NEW |