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