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_spawnuri; |
| 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 main(){ |
| 13 asyncStart(); |
| 14 // Setup the port for communication with the newly spawned isolate. |
| 15 RawReceivePort reply = new RawReceivePort(null); |
| 16 SendPort sendPort; |
| 17 int state = 0; |
| 18 reply.handler = (port) { |
| 19 sendPort = port; |
| 20 port.send(state); |
| 21 reply.handler = (v) { |
| 22 Expect.equals(0, state); |
| 23 Expect.equals(42, v); |
| 24 state++; |
| 25 sendPort.send(state); |
| 26 }; |
| 27 }; |
| 28 |
| 29 // Capture errors from other isolate as raw messages. |
| 30 RawReceivePort errorPort = new RawReceivePort(); |
| 31 errorPort.handler = (message) { |
| 32 String error = message[0]; |
| 33 String stack = message[1]; |
| 34 switch (state) { |
| 35 case 1: |
| 36 Expect.equals(new ArgumentError("whoops").toString(), "$error"); |
| 37 state++; |
| 38 sendPort.send(state); |
| 39 break; |
| 40 case 2: |
| 41 Expect.equals(new RangeError.value(37).toString(), "$error"); |
| 42 state++; |
| 43 sendPort.send(state); |
| 44 reply.close(); |
| 45 errorPort.close(); |
| 46 break; |
| 47 default: |
| 48 throw "Bad state for error: $state: $error"; |
| 49 } |
| 50 }; |
| 51 |
| 52 // Get exit notifications from other isolate as raw messages. |
| 53 RawReceivePort exitPort = new RawReceivePort(); |
| 54 exitPort.handler = (message) { |
| 55 // onExit ports registered at spawn cannot have a particular message |
| 56 // associated. |
| 57 Expect.equals(null, message); |
| 58 // Only exit after sending the termination message. |
| 59 Expect.equals(3, state); |
| 60 exitPort.close(); |
| 61 asyncEnd(); |
| 62 }; |
| 63 |
| 64 Isolate.spawnUri(Uri.parse("error_exit_at_spawnuri_iso.dart"), [], |
| 65 reply.sendPort, |
| 66 // Setup handlers as part of spawn. |
| 67 errorsAreFatal: false, |
| 68 onError: errorPort.sendPort, |
| 69 onExit: exitPort.sendPort); |
| 70 } |
OLD | NEW |