OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library handle_error_test; |
| 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 isomain1(replyPort) { |
| 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 RawReceivePort reply = new RawReceivePort(null); |
| 33 // Start paused so we have time to set up the error handler. |
| 34 Isolate.spawn(isomain1, reply.sendPort, paused: true).then((Isolate isolate) { |
| 35 isolate.setErrorsFatal(false); |
| 36 Stream errors = isolate.errors; // Broadcast stream, never a done message. |
| 37 SendPort sendPort; |
| 38 StreamSubscription subscription; |
| 39 int state = 0; |
| 40 reply.handler = (port) { |
| 41 sendPort = port; |
| 42 port.send(state); |
| 43 reply.handler = (v) { |
| 44 Expect.equals(0, state); |
| 45 Expect.equals(42, v); |
| 46 state++; |
| 47 sendPort.send(state); |
| 48 }; |
| 49 }; |
| 50 subscription = errors.listen(null, onError: (error, stack) { |
| 51 switch (state) { |
| 52 case 1: |
| 53 Expect.equals(new ArgumentError("whoops").toString(), "$error"); |
| 54 state++; |
| 55 sendPort.send(state); |
| 56 break; |
| 57 case 2: |
| 58 Expect.equals(new RangeError.value(37).toString(), "$error"); |
| 59 state++; |
| 60 sendPort.send(state); |
| 61 reply.close(); |
| 62 subscription.cancel(); |
| 63 asyncEnd(); |
| 64 break; |
| 65 default: |
| 66 throw "Bad state for error: $state: $error"; |
| 67 } |
| 68 }); |
| 69 isolate.resume(isolate.pauseCapability); |
| 70 }); |
| 71 } |
OLD | NEW |