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 serialization of messages. | 5 // Dart test program for testing serialization of messages. |
6 // VMOptions=--enable_type_checks --enable_asserts | 6 // VMOptions=--enable_type_checks --enable_asserts |
7 | 7 |
8 library MessageTest; | 8 library MessageTest; |
9 import 'dart:isolate'; | 9 import 'dart:isolate'; |
10 import '../../pkg/unittest/lib/unittest.dart'; | 10 import '../../pkg/unittest/lib/unittest.dart'; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 | 56 |
57 static void VerifyObject(int index, var actual) { | 57 static void VerifyObject(int index, var actual) { |
58 var expected = elms[index]; | 58 var expected = elms[index]; |
59 expect(expected, isList); | 59 expect(expected, isList); |
60 expect(actual, isList); | 60 expect(actual, isList); |
61 expect(actual.length, expected.length); | 61 expect(actual.length, expected.length); |
62 VerifyList(expected, actual); | 62 VerifyList(expected, actual); |
63 } | 63 } |
64 } | 64 } |
65 | 65 |
66 pingPong() { | 66 pingPong(replyTo) { |
| 67 ReceivePort port = new ReceivePort(); |
67 int count = 0; | 68 int count = 0; |
68 port.receive((var message, SendPort replyTo) { | 69 port.listen((pair) { |
| 70 var message = pair[0]; |
| 71 var replyTo = pair[1]; |
69 if (message == -1) { | 72 if (message == -1) { |
70 port.close(); | 73 port.close(); |
71 replyTo.send(count, null); | 74 replyTo.send(count); |
72 } else { | 75 } else { |
73 // Check if the received object is correct. | 76 // Check if the received object is correct. |
74 if (count < MessageTest.elms.length) { | 77 if (count < MessageTest.elms.length) { |
75 MessageTest.VerifyObject(count, message); | 78 MessageTest.VerifyObject(count, message); |
76 } | 79 } |
77 // Bounce the received object back so that the sender | 80 // Bounce the received object back so that the sender |
78 // can make sure that the object matches. | 81 // can make sure that the object matches. |
79 replyTo.send(message, null); | 82 replyTo.send(message); |
80 count++; | 83 count++; |
81 } | 84 } |
82 }); | 85 }); |
| 86 replyTo.send(port.sendPort); |
| 87 } |
| 88 |
| 89 Future remoteCall(SendPort port, message) { |
| 90 ReceivePort receivePort = new ReceivePort(); |
| 91 port.send([message, receivePort.sendPort]); |
| 92 return receivePort.first; |
83 } | 93 } |
84 | 94 |
85 main() { | 95 main() { |
86 test("send objects and receive them back", () { | 96 test("send objects and receive them back", () { |
87 SendPort remote = spawnFunction(pingPong); | 97 ReceivePort port = new ReceivePort(); |
88 // Send objects and receive them back. | 98 Isolate.spawn(pingPong, port.sendPort); |
89 for (int i = 0; i < MessageTest.elms.length; i++) { | 99 port.first.then(expectAsync1((remote) { |
90 var sentObject = MessageTest.elms[i]; | 100 // Send objects and receive them back. |
91 // TODO(asiva): remove this local var idx once thew new for-loop | 101 for (int i = 0; i < MessageTest.elms.length; i++) { |
92 // semantics for closures is implemented. | 102 var sentObject = MessageTest.elms[i]; |
93 var idx = i; | 103 remoteCall(remote, sentObject).then(expectAsync1((var receivedObject) { |
94 remote.call(sentObject).then(expectAsync1((var receivedObject) { | 104 MessageTest.VerifyObject(i, receivedObject); |
95 MessageTest.VerifyObject(idx, receivedObject); | 105 })); |
| 106 } |
| 107 |
| 108 // Send recursive objects and receive them back. |
| 109 List local_list1 = ["Hello", "World", "Hello", 0xffffffffff]; |
| 110 List local_list2 = [null, local_list1, local_list1 ]; |
| 111 List local_list3 = [local_list2, 2.0, true, false, 0xffffffffff]; |
| 112 List sendObject = new List(5); |
| 113 sendObject[0] = local_list1; |
| 114 sendObject[1] = sendObject; |
| 115 sendObject[2] = local_list2; |
| 116 sendObject[3] = sendObject; |
| 117 sendObject[4] = local_list3; |
| 118 remoteCall(remote, sendObject).then((var replyObject) { |
| 119 expect(sendObject, isList); |
| 120 expect(replyObject, isList); |
| 121 expect(sendObject.length, equals(replyObject.length)); |
| 122 expect(replyObject[1], same(replyObject)); |
| 123 expect(replyObject[3], same(replyObject)); |
| 124 expect(replyObject[0], same(replyObject[2][1])); |
| 125 expect(replyObject[0], same(replyObject[2][2])); |
| 126 expect(replyObject[2], same(replyObject[4][0])); |
| 127 expect(replyObject[0][0], same(replyObject[0][2])); |
| 128 // Bigint literals are not canonicalized so do a == check. |
| 129 expect(replyObject[0][3], equals(replyObject[4][4])); |
| 130 }); |
| 131 |
| 132 // Shutdown the MessageServer. |
| 133 remoteCall(remote, -1).then(expectAsync1((int message) { |
| 134 expect(message, MessageTest.elms.length + 1); |
96 })); | 135 })); |
97 } | 136 })); |
98 | |
99 // Send recursive objects and receive them back. | |
100 List local_list1 = ["Hello", "World", "Hello", 0xffffffffff]; | |
101 List local_list2 = [null, local_list1, local_list1 ]; | |
102 List local_list3 = [local_list2, 2.0, true, false, 0xffffffffff]; | |
103 List sendObject = new List(5); | |
104 sendObject[0] = local_list1; | |
105 sendObject[1] = sendObject; | |
106 sendObject[2] = local_list2; | |
107 sendObject[3] = sendObject; | |
108 sendObject[4] = local_list3; | |
109 remote.call(sendObject).then((var replyObject) { | |
110 expect(sendObject, isList); | |
111 expect(replyObject, isList); | |
112 expect(sendObject.length, equals(replyObject.length)); | |
113 expect(replyObject[1], same(replyObject)); | |
114 expect(replyObject[3], same(replyObject)); | |
115 expect(replyObject[0], same(replyObject[2][1])); | |
116 expect(replyObject[0], same(replyObject[2][2])); | |
117 expect(replyObject[2], same(replyObject[4][0])); | |
118 expect(replyObject[0][0], same(replyObject[0][2])); | |
119 // Bigint literals are not canonicalized so do a == check. | |
120 expect(replyObject[0][3], equals(replyObject[4][4])); | |
121 }); | |
122 | |
123 // Shutdown the MessageServer. | |
124 remote.call(-1).then(expectAsync1((int message) { | |
125 expect(message, MessageTest.elms.length + 1); | |
126 })); | |
127 }); | 137 }); |
128 } | 138 } |
OLD | NEW |