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 | 4 |
5 // Dart test program for testing isolate communication with | 5 // Dart test program for testing isolate communication with |
6 // complex messages. | 6 // complex messages. |
7 | 7 |
8 library IsolateComplexMessagesTest; | 8 library IsolateComplexMessagesTest; |
9 import 'dart:isolate'; | 9 import 'dart:isolate'; |
10 import '../../pkg/unittest/lib/unittest.dart'; | 10 import '../../pkg/unittest/lib/unittest.dart'; |
11 | 11 |
12 main() { | 12 main() { |
13 test("complex messages are serialized correctly", () { | 13 test("complex messages are serialized correctly", () { |
14 SendPort remote = spawnFunction(logMessages); | 14 ReceivePort local = new ReceivePort(); |
15 remote.send(1, null); | 15 Isolate.spawn(logMessages, local.sendPort); |
16 remote.send("Hello", null); | 16 local.listen(expectAsync1((msg) { |
17 remote.send("World", null); | 17 switch (msg[0]) { |
18 remote.send(const [null, 1, 2, 3, 4], null); | 18 case "init": |
19 remote.send(const [1, 2.0, true, false, 0xffffffffff], null); | 19 var remote = msg[1]; |
20 remote.send(const ["Hello", "World", 0xffffffffff], null); | 20 remote.send(1, null); |
21 // Shutdown the LogRunner. | 21 remote.send("Hello", null); |
22 remote.call(-1).then(expectAsync1((int message) { | 22 remote.send("World", null); |
23 expect(message, 6); | 23 remote.send(const [null, 1, 2, 3, 4], null); |
24 })); | 24 remote.send(const [1, 2.0, true, false, 0xffffffffff], null); |
| 25 remote.send(const ["Hello", "World", 0xffffffffff], null); |
| 26 // Shutdown the LogRunner. |
| 27 remote.send(-1); |
| 28 break; |
| 29 case "done": |
| 30 local.close(); |
| 31 expect(msg[1], 6); |
| 32 } |
| 33 }, count: 2)); |
25 }); | 34 }); |
26 } | 35 } |
27 | 36 |
28 | 37 |
29 void logMessages() { | 38 void logMessages(mainPort) { |
30 int count = 0; | 39 int count = 0; |
31 | 40 ReceivePort port = new ReceivePort(); |
32 port.receive((var message, SendPort replyTo) { | 41 mainPort.send(["init", port.sendPort]); |
| 42 port.listen((var message) { |
33 if (message == -1) { | 43 if (message == -1) { |
34 port.close(); | 44 port.close(); |
35 replyTo.send(count, null); | 45 mainPort.send(["done", count]); |
36 } else { | 46 } else { |
37 switch (count) { | 47 switch (count) { |
38 case 0: | 48 case 0: |
39 expect(message, 1); | 49 expect(message, 1); |
40 break; | 50 break; |
41 case 1: | 51 case 1: |
42 expect(message, "Hello"); | 52 expect(message, "Hello"); |
43 break; | 53 break; |
44 case 2: | 54 case 2: |
45 expect(message, "World"); | 55 expect(message, "World"); |
(...skipping 18 matching lines...) Expand all Loading... |
64 expect(message.length, 3); | 74 expect(message.length, 3); |
65 expect(message[0], "Hello"); | 75 expect(message[0], "Hello"); |
66 expect(message[1], "World"); | 76 expect(message[1], "World"); |
67 expect(message[2], 0xffffffffff); | 77 expect(message[2], 0xffffffffff); |
68 break; | 78 break; |
69 } | 79 } |
70 count++; | 80 count++; |
71 } | 81 } |
72 }); | 82 }); |
73 } | 83 } |
OLD | NEW |