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 /// Do Isolate.spawn(entry) and get a sendPort from the isolate that it |
| 31 /// expects commands on. |
| 32 /// The isolate has errors set to non-fatal. |
| 33 /// Returns a list of `[isolate, commandPort]` in a future. |
| 34 Future spawn(entry) { |
| 35 ReceivePort reply = new ReceivePort(); |
| 36 Future isolate = Isolate.spawn(entry, reply.sendPort, paused: true); |
| 37 return isolate.then((Isolate isolate) { |
| 38 isolate.setErrorsFatal(false); |
| 39 isolate.resume(isolate.pauseCapability); |
| 40 Future result = reply.first.then((sendPort) { |
| 41 return [isolate, sendPort]; |
| 42 }); |
| 43 return result; |
| 44 }); |
| 45 } |
| 46 |
| 47 main(){ |
| 48 asyncStart(); |
| 49 asyncStart(); |
| 50 RawReceivePort reply = new RawReceivePort(null); |
| 51 RawReceivePort reply2 = new RawReceivePort(null); |
| 52 // Create two isolates waiting for commands, with errors non-fatal. |
| 53 Future iso1 = spawn(isomain1); |
| 54 Future iso2 = spawn(isomain1); |
| 55 Future.wait([iso1, iso2]).then((l) { |
| 56 var isolate1 = l[0][0]; |
| 57 var sendPort1 = l[0][1]; |
| 58 var isolate2 = l[1][0]; |
| 59 var sendPort2 = l[1][1]; |
| 60 // Capture errors from one isolate as stream. |
| 61 Stream errors = isolate1.errors; // Broadcast stream, never a done message. |
| 62 int state = 1; |
| 63 var subscription; |
| 64 subscription = errors.listen(null, onError: (error, stack) { |
| 65 switch (state) { |
| 66 case 1: |
| 67 Expect.equals(new ArgumentError("whoops").toString(), "$error"); |
| 68 state++; |
| 69 break; |
| 70 case 2: |
| 71 Expect.equals(new RangeError.value(37).toString(), "$error"); |
| 72 state++; |
| 73 reply.close(); |
| 74 subscription.cancel(); |
| 75 asyncEnd(); |
| 76 break; |
| 77 default: |
| 78 throw "Bad state for error: $state: $error"; |
| 79 } |
| 80 }); |
| 81 // Capture errors from other isolate as raw messages. |
| 82 RawReceivePort errorPort2 = new RawReceivePort(); |
| 83 int state2 = 1; |
| 84 errorPort2.handler = (message) { |
| 85 String error = message[0]; |
| 86 String stack = message[1]; |
| 87 switch (state2) { |
| 88 case 1: |
| 89 Expect.equals(new ArgumentError("whoops").toString(), "$error"); |
| 90 state2++; |
| 91 break; |
| 92 case 2: |
| 93 Expect.equals(new RangeError.value(37).toString(), "$error"); |
| 94 state2++; |
| 95 reply.close(); |
| 96 isolate2.removeErrorListener(errorPort2.sendPort); |
| 97 errorPort2.close(); |
| 98 asyncEnd(); |
| 99 break; |
| 100 default: |
| 101 throw "Bad state-2 for error: $state: $error"; |
| 102 } |
| 103 }; |
| 104 isolate2.addErrorListener(errorPort2.sendPort); |
| 105 |
| 106 sendPort1.send(0); |
| 107 sendPort2.send(0); |
| 108 sendPort1.send(1); |
| 109 sendPort2.send(1); |
| 110 sendPort1.send(2); |
| 111 sendPort2.send(2); |
| 112 sendPort1.send(3); |
| 113 sendPort2.send(3); |
| 114 }); |
| 115 } |
| 116 |
OLD | NEW |