OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 // Dart test program for testing isolate communication with | 4 // Dart test program for testing isolate communication with |
5 // typed objects. | 5 // typed objects. |
6 // VMOptions=--checked | 6 // VMOptions=--checked |
7 | 7 |
8 library TypedMessageTest; | 8 library TypedMessageTest; |
| 9 import "dart:async"; |
9 import "package:expect/expect.dart"; | 10 import "package:expect/expect.dart"; |
10 import "dart:isolate"; | 11 import "dart:isolate"; |
11 | 12 |
12 void logMessages() { | 13 void logMessages(SendPort replyTo) { |
13 print("Starting log server."); | 14 print("Starting log server."); |
14 port.receive((List<int> message, SendPort replyTo) { | 15 ReceivePort port = new ReceivePort(); |
| 16 replyTo.send(port.sendPort); |
| 17 port.first.then((List<int> message) { |
15 print("Log $message"); | 18 print("Log $message"); |
16 Expect.equals(5, message.length); | 19 Expect.equals(5, message.length); |
17 Expect.equals(0, message[0]); | 20 Expect.equals(0, message[0]); |
18 Expect.equals(1, message[1]); | 21 Expect.equals(1, message[1]); |
19 Expect.equals(2, message[2]); | 22 Expect.equals(2, message[2]); |
20 Expect.equals(3, message[3]); | 23 Expect.equals(3, message[3]); |
21 Expect.equals(4, message[4]); | 24 Expect.equals(4, message[4]); |
22 port.close(); | 25 port.close(); |
23 replyTo.send(1, null); | 26 replyTo.send(1); |
24 print("Stopping log server."); | 27 print("Stopping log server."); |
25 }); | 28 }); |
26 } | 29 } |
27 | 30 |
28 main() { | 31 main() { |
29 SendPort remote = spawnFunction(logMessages); | 32 ReceivePort receivePort = new ReceivePort(); |
| 33 Future<Isolate> remote = Isolate.spawn(logMessages, receivePort.sendPort); |
30 List<int> msg = new List<int>(5); | 34 List<int> msg = new List<int>(5); |
31 for (int i = 0; i < 5; i++) { | 35 for (int i = 0; i < 5; i++) { |
32 msg[i] = i; | 36 msg[i] = i; |
33 } | 37 } |
34 remote.call(msg).then((int message) { | 38 StreamIterator iterator = new StreamIterator(receivePort); |
35 Expect.equals(1, message); | 39 iterator.moveNext().then((b) { |
| 40 SendPort sendPort = iterator.current; |
| 41 sendPort.send(msg); |
| 42 return iterator.moveNext(); |
| 43 }).then((b) { |
| 44 Expect.equals(1, iterator.current); |
| 45 receivePort.close(); |
36 }); | 46 }); |
37 } | 47 } |
OLD | NEW |