| 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 var done = expectAsync0((){}); |
| 17 remote.send("World", null); | 17 local.listen(expectAsync1((msg) { |
| 18 remote.send(const [null, 1, 2, 3, 4], null); | 18 switch (msg[0]) { |
| 19 remote.send(const [1, 2.0, true, false, 0xffffffffff], null); | 19 case "init": |
| 20 remote.send(const ["Hello", "World", 0xffffffffff], null); | 20 var remote = msg[1]; |
| 21 // Shutdown the LogRunner. | 21 remote.send(1, null); |
| 22 remote.call(-1).then(expectAsync1((int message) { | 22 remote.send("Hello", null); |
| 23 expect(message, 6); | 23 remote.send("World", null); |
| 24 })); | 24 remote.send(const [null, 1, 2, 3, 4], null); |
| 25 remote.send(const [1, 2.0, true, false, 0xffffffffff], null); |
| 26 remote.send(const ["Hello", "World", 0xffffffffff], null); |
| 27 // Shutdown the LogRunner. |
| 28 remote.send(-1); |
| 29 break; |
| 30 case "done": |
| 31 local.close(); |
| 32 expect(msg[1], 6); |
| 33 done(); |
| 34 } |
| 35 }, count: 2)); |
| 25 }); | 36 }); |
| 26 } | 37 } |
| 27 | 38 |
| 28 | 39 |
| 29 void logMessages() { | 40 void logMessages(mainPort) { |
| 30 int count = 0; | 41 int count = 0; |
| 31 | 42 ReceivePort port = new ReceivePort(); |
| 32 port.receive((var message, SendPort replyTo) { | 43 mainPort.send(["init", port.sendPort]); |
| 44 port.listen((var message) { |
| 33 if (message == -1) { | 45 if (message == -1) { |
| 34 port.close(); | 46 port.close(); |
| 35 replyTo.send(count, null); | 47 mainPort.send(["done", count]); |
| 36 } else { | 48 } else { |
| 37 switch (count) { | 49 switch (count) { |
| 38 case 0: | 50 case 0: |
| 39 expect(message, 1); | 51 expect(message, 1); |
| 40 break; | 52 break; |
| 41 case 1: | 53 case 1: |
| 42 expect(message, "Hello"); | 54 expect(message, "Hello"); |
| 43 break; | 55 break; |
| 44 case 2: | 56 case 2: |
| 45 expect(message, "World"); | 57 expect(message, "World"); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 64 expect(message.length, 3); | 76 expect(message.length, 3); |
| 65 expect(message[0], "Hello"); | 77 expect(message[0], "Hello"); |
| 66 expect(message[1], "World"); | 78 expect(message[1], "World"); |
| 67 expect(message[2], 0xffffffffff); | 79 expect(message[2], 0xffffffffff); |
| 68 break; | 80 break; |
| 69 } | 81 } |
| 70 count++; | 82 count++; |
| 71 } | 83 } |
| 72 }); | 84 }); |
| 73 } | 85 } |
| OLD | NEW |