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 import 'dart:isolate'; |
| 6 import 'dart:async'; |
| 7 import "package:expect/expect.dart"; |
| 8 |
| 9 class FromMainIsolate { |
| 10 String toString() => 'from main isolate'; |
| 11 int get fld => 10; |
| 12 } |
| 13 |
| 14 func1Child(args) { |
| 15 var receivePort = new ReceivePort(); |
| 16 var sendPort = args[0]; |
| 17 sendPort.send(receivePort.sendPort); |
| 18 receivePort.listen( |
| 19 (msg) { |
| 20 Expect.isTrue(msg is FromMainIsolate); |
| 21 Expect.equals(10, msg.fld); |
| 22 receivePort.close(); |
| 23 }, |
| 24 onError: (e) => print('$e') |
| 25 ); |
| 26 } |
| 27 |
| 28 |
| 29 func2Child(args) { |
| 30 var receivePort = new ReceivePort(); |
| 31 var sendPort = args[0]; |
| 32 sendPort.send(receivePort.sendPort); |
| 33 receivePort.listen( |
| 34 (msg) { |
| 35 Expect.isTrue(msg is SendPort); |
| 36 msg.send(new FromMainIsolate()); |
| 37 receivePort.close(); |
| 38 }, |
| 39 onError: (e) => print('$e') |
| 40 ); |
| 41 } |
| 42 |
| 43 |
| 44 spawnFuncTest() { |
| 45 var receive1 = new ReceivePort(); |
| 46 var receive2 = new ReceivePort(); |
| 47 |
| 48 var spawnFunctionIsolate1SendPort; |
| 49 var spawnFunctionIsolate2SendPort; |
| 50 |
| 51 // First spawn the first isolate using spawnFunction, this isolate will |
| 52 // create a receivePort and send it's sendPort back and then it will just |
| 53 // sit there listening for a message from the second isolate spawned |
| 54 // using spawnFunction. |
| 55 Isolate.spawn(func1Child, [receive1.sendPort]).then( |
| 56 (isolate) { |
| 57 receive1.listen( |
| 58 (msg) { |
| 59 Expect.isTrue(msg is SendPort); |
| 60 spawnFunctionIsolate1SendPort = msg; |
| 61 receive1.close(); |
| 62 |
| 63 // Now spawn the second isolate using spawnFunction, this isolate |
| 64 // will create a receivePort and send it's sendPort back and then |
| 65 // wait for the third isolate spawned using spawnUri to send it |
| 66 // a sendPort to which it will try and send a non "literal-like" |
| 67 // object. |
| 68 Isolate.spawn(func2Child, [receive2.sendPort]).then( |
| 69 (isolate) { |
| 70 receive2.listen( |
| 71 (msg) { |
| 72 spawnFunctionIsolate2SendPort = msg; |
| 73 receive2.close(); |
| 74 |
| 75 // Now spawn an isolate using spawnUri and send these send |
| 76 // ports over to it. This isolate will send one of the |
| 77 // sendports over to the other. |
| 78 Isolate.spawnUri(Uri.parse('issue_21398_child_isolate1.dart'), |
| 79 [spawnFunctionIsolate1SendPort, |
| 80 spawnFunctionIsolate2SendPort], "no-msg"); |
| 81 }, |
| 82 onError: (e) => print('$e') |
| 83 ); |
| 84 } |
| 85 ); |
| 86 }, |
| 87 onError: (e) => print('$e') |
| 88 ); |
| 89 } |
| 90 ); |
| 91 } |
| 92 |
| 93 |
| 94 uriChild(args) { |
| 95 var receivePort = new ReceivePort(); |
| 96 var sendPort = args[0]; |
| 97 sendPort.send(receivePort.sendPort); |
| 98 receivePort.listen( |
| 99 (msg) { |
| 100 Expect.isTrue(msg is String); |
| 101 Expect.equals("Invalid Argument(s).", msg); |
| 102 receivePort.close(); |
| 103 }, |
| 104 onError: (e) => print('$e') |
| 105 ); |
| 106 } |
| 107 |
| 108 |
| 109 spawnUriTest() { |
| 110 var receive1 = new ReceivePort(); |
| 111 var receive2 = new ReceivePort(); |
| 112 |
| 113 var spawnFunctionIsolateSendPort; |
| 114 var spawnUriIsolateSendPort; |
| 115 |
| 116 // First spawn the first isolate using spawnFunction, this isolate will |
| 117 // create a receivePort and send it's sendPort back and then it will just |
| 118 // sit there listening for a message from the second isolate spawned |
| 119 // using spawnFunction. |
| 120 Isolate.spawn(uriChild, [receive1.sendPort]).then( |
| 121 (isolate) { |
| 122 receive1.listen( |
| 123 (msg) { |
| 124 Expect.isTrue(msg is SendPort); |
| 125 spawnFunctionIsolateSendPort = msg; |
| 126 receive1.close(); |
| 127 |
| 128 // Now spawn the second isolate using spawnUri, this isolate |
| 129 // will create a receivePort and send it's sendPort back and then |
| 130 // wait for the third isolate spawned using spawnUri to send it |
| 131 // a sendPort to which it will try and send a non "literal-like" |
| 132 // object. |
| 133 Isolate.spawnUri(Uri.parse('issue_21398_child_isolate11.dart'), |
| 134 [], |
| 135 receive2.sendPort).then( |
| 136 (isolate) { |
| 137 receive2.listen( |
| 138 (msg) { |
| 139 spawnUriIsolateSendPort = msg; |
| 140 receive2.close(); |
| 141 |
| 142 // Now spawn an isolate using spawnUri and send these send |
| 143 // ports over to it. This isolate will send one of the |
| 144 // sendports over to the other. |
| 145 Isolate.spawnUri(Uri.parse('issue_21398_child_isolate1.dart'), |
| 146 [spawnFunctionIsolateSendPort, |
| 147 spawnUriIsolateSendPort], "no-msg"); |
| 148 }, |
| 149 onError: (e) => print('$e') |
| 150 ); |
| 151 } |
| 152 ); |
| 153 }, |
| 154 onError: (e) => print('$e') |
| 155 ); |
| 156 } |
| 157 ); |
| 158 } |
| 159 |
| 160 |
| 161 main() { |
| 162 spawnFuncTest(); |
| 163 spawnUriTest(); |
| 164 } |
OLD | NEW |