| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 // Dart test program for testing serialization of messages. | |
| 6 // VMOptions=--enable_type_checks --enable_asserts | |
| 7 | |
| 8 library MessageTest; | |
| 9 import 'dart:isolate'; | |
| 10 import '../../pkg/unittest/lib/unittest.dart'; | |
| 11 | |
| 12 // --------------------------------------------------------------------------- | |
| 13 // Message passing test. | |
| 14 // --------------------------------------------------------------------------- | |
| 15 | |
| 16 class MessageTest { | |
| 17 static const List list1 = const ["Hello", "World", "Hello", 0xfffffffffff]; | |
| 18 static const List list2 = const [null, list1, list1, list1, list1]; | |
| 19 static const List list3 = const [list2, 2.0, true, false, 0xfffffffffff]; | |
| 20 static const Map map1 = const { | |
| 21 "a=1" : 1, "b=2" : 2, "c=3" : 3, | |
| 22 }; | |
| 23 static const Map map2 = const { | |
| 24 "list1" : list1, "list2" : list2, "list3" : list3, | |
| 25 }; | |
| 26 static const List list4 = const [map1, map2]; | |
| 27 static const List elms = const [ | |
| 28 list1, list2, list3, list4, | |
| 29 ]; | |
| 30 | |
| 31 static void VerifyMap(Map expected, Map actual) { | |
| 32 expect(expected, isMap); | |
| 33 expect(actual, isMap); | |
| 34 expect(actual.length, expected.length); | |
| 35 testForEachMap(key, value) { | |
| 36 if (value is List) { | |
| 37 VerifyList(value, actual[key]); | |
| 38 } else { | |
| 39 expect(actual[key], value); | |
| 40 } | |
| 41 } | |
| 42 expected.forEach(testForEachMap); | |
| 43 } | |
| 44 | |
| 45 static void VerifyList(List expected, List actual) { | |
| 46 for (int i = 0; i < expected.length; i++) { | |
| 47 if (expected[i] is List) { | |
| 48 VerifyList(expected[i], actual[i]); | |
| 49 } else if (expected[i] is Map) { | |
| 50 VerifyMap(expected[i], actual[i]); | |
| 51 } else { | |
| 52 expect(actual[i], expected[i]); | |
| 53 } | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 static void VerifyObject(int index, var actual) { | |
| 58 var expected = elms[index]; | |
| 59 expect(expected, isList); | |
| 60 expect(actual, isList); | |
| 61 expect(actual.length, expected.length); | |
| 62 VerifyList(expected, actual); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 pingPong() { | |
| 67 int count = 0; | |
| 68 bool isFirst = true; | |
| 69 IsolateSink replyTo; | |
| 70 stream.listen((var message) { | |
| 71 if (isFirst) { | |
| 72 isFirst = false; | |
| 73 replyTo = message; | |
| 74 return; | |
| 75 } | |
| 76 // Check if the received object is correct. | |
| 77 if (count < MessageTest.elms.length) { | |
| 78 MessageTest.VerifyObject(count, message); | |
| 79 } | |
| 80 // Bounce the received object back so that the sender | |
| 81 // can make sure that the object matches. | |
| 82 replyTo.add(message); | |
| 83 count++; | |
| 84 }, onDone: () { | |
| 85 replyTo.add(count); | |
| 86 replyTo.close(); | |
| 87 }); | |
| 88 } | |
| 89 | |
| 90 main() { | |
| 91 test("send objects and receive them back", () { | |
| 92 IsolateSink remote = streamSpawnFunction(pingPong); | |
| 93 MessageBox box = new MessageBox(); | |
| 94 remote.add(box.sink); | |
| 95 | |
| 96 // Send objects and receive them back. | |
| 97 for (int i = 0; i < MessageTest.elms.length; i++) { | |
| 98 var sentObject = MessageTest.elms[i]; | |
| 99 // TODO(asiva): remove this local var idx once thew new for-loop | |
| 100 // semantics for closures is implemented. | |
| 101 var idx = i; | |
| 102 remote.add(sentObject); | |
| 103 } | |
| 104 | |
| 105 // Send recursive objects and receive them back. | |
| 106 List local_list1 = ["Hello", "World", "Hello", 0xffffffffff]; | |
| 107 List local_list2 = [null, local_list1, local_list1 ]; | |
| 108 List local_list3 = [local_list2, 2.0, true, false, 0xffffffffff]; | |
| 109 List sendObject = new List(5); | |
| 110 sendObject[0] = local_list1; | |
| 111 sendObject[1] = sendObject; | |
| 112 sendObject[2] = local_list2; | |
| 113 sendObject[3] = sendObject; | |
| 114 sendObject[4] = local_list3; | |
| 115 remote.add(sendObject); | |
| 116 | |
| 117 // Shutdown the MessageServer. | |
| 118 remote.close(); | |
| 119 | |
| 120 int receivedCounter = 0; | |
| 121 box.stream.listen((message) { | |
| 122 if (receivedCounter < MessageTest.elms.length) { | |
| 123 MessageTest.VerifyObject(receivedCounter, message); | |
| 124 } else if (receivedCounter == MessageTest.elms.length) { | |
| 125 var replyObject = message; | |
| 126 expect(sendObject, isList); | |
| 127 expect(replyObject, isList); | |
| 128 expect(sendObject.length, equals(replyObject.length)); | |
| 129 expect(replyObject[1], same(replyObject)); | |
| 130 expect(replyObject[3], same(replyObject)); | |
| 131 expect(replyObject[0], same(replyObject[2][1])); | |
| 132 expect(replyObject[0], same(replyObject[2][2])); | |
| 133 expect(replyObject[2], same(replyObject[4][0])); | |
| 134 expect(replyObject[0][0], same(replyObject[0][2])); | |
| 135 // Bigint literals are not canonicalized so do a == check. | |
| 136 expect(replyObject[0][3], equals(replyObject[4][4])); | |
| 137 } else { | |
| 138 // Reply from done. | |
| 139 expect(message, MessageTest.elms.length + 1); | |
| 140 } | |
| 141 receivedCounter++; | |
| 142 }); | |
| 143 }); | |
| 144 } | |
| OLD | NEW |