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 RawReceivePort reply = new RawReceivePort(null); |
| 50 RawReceivePort reply2 = new RawReceivePort(null); |
| 51 // Create two isolates waiting for commands, with errors non-fatal. |
| 52 Future iso1 = spawn(isomain1); |
| 53 Future iso2 = spawn(isomain1); |
| 54 Future.wait([iso1, iso2]).then((l) { |
| 55 var isolate1 = l[0][0]; |
| 56 var sendPort1 = l[0][1]; |
| 57 var isolate2 = l[1][0]; |
| 58 var sendPort2 = l[1][1]; |
| 59 Stream errors = isolate1.errors; // Broadcast stream, never a done message. |
| 60 int state = 1; |
| 61 var subscription; |
| 62 subscription = errors.listen(null, onError: (error, stack) { |
| 63 switch (state) { |
| 64 case 1: |
| 65 Expect.equals(new ArgumentError("whoops").toString(), "$error"); |
| 66 state++; |
| 67 break; |
| 68 case 2: |
| 69 Expect.equals(new RangeError.value(37).toString(), "$error"); |
| 70 state++; |
| 71 reply.close(); |
| 72 subscription.cancel(); |
| 73 asyncEnd(); |
| 74 break; |
| 75 default: |
| 76 throw "Bad state for error: $state: $error"; |
| 77 } |
| 78 }); |
| 79 sendPort1.send(0); |
| 80 sendPort2.send(0); |
| 81 sendPort1.send(1); |
| 82 sendPort2.send(1); |
| 83 sendPort1.send(2); |
| 84 sendPort2.send(2); |
| 85 sendPort1.send(3); |
| 86 sendPort2.send(3); |
| 87 }); |
| 88 } |
| 89 |
OLD | NEW |