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:async'; |
| 10 import 'dart:collection'; |
| 11 import 'dart:isolate'; |
| 12 import 'package:async_helper/async_helper.dart'; |
| 13 import 'package:expect/expect.dart'; |
| 14 import 'dart:typed_data'; |
| 15 |
| 16 void echoMain(msg) { |
| 17 SendPort replyTo = msg[0]; |
| 18 SendPort pong = msg[1]; |
| 19 ReceivePort port = new ReceivePort(); |
| 20 replyTo.send(port.sendPort); |
| 21 port.listen((msg) { |
| 22 if (msg == "halt") { |
| 23 port.close(); |
| 24 } else { |
| 25 pong.send(msg); |
| 26 } |
| 27 }); |
| 28 } |
| 29 |
| 30 class A { |
| 31 var field = 499; |
| 32 |
| 33 A(); |
| 34 A.named(this.field); |
| 35 } |
| 36 |
| 37 class B extends A { |
| 38 final field2; |
| 39 B() : field2 = 99; |
| 40 B.named(this.field2, x) : super.named(x); |
| 41 } |
| 42 |
| 43 class C extends B { |
| 44 var field = 33; |
| 45 |
| 46 get superField => super.field; |
| 47 get superField2 => super.field2; |
| 48 } |
| 49 |
| 50 class M { |
| 51 get field2 => 11; |
| 52 } |
| 53 |
| 54 class D extends C with M { |
| 55 var gee = 123; |
| 56 } |
| 57 |
| 58 class E { |
| 59 Function fun; |
| 60 E(this.fun); |
| 61 |
| 62 static fooFun() => 499; |
| 63 instanceFun() => 1234; |
| 64 } |
| 65 barFun() => 42; |
| 66 |
| 67 class F { |
| 68 final field = "field"; |
| 69 const F(); |
| 70 } |
| 71 |
| 72 class G { |
| 73 final field; |
| 74 const G(this.field); |
| 75 } |
| 76 |
| 77 class Value { |
| 78 final val; |
| 79 Value(this.val); |
| 80 |
| 81 operator==(other) { |
| 82 if (other is! Value) return false; |
| 83 return other.val == val; |
| 84 } |
| 85 |
| 86 get hashCode => val; |
| 87 } |
| 88 |
| 89 void runTests(SendPort ping, Queue checks) { |
| 90 ping.send("abc"); |
| 91 checks.add((x) => Expect.equals("abc", x)); |
| 92 |
| 93 ping.send([1, 2]); |
| 94 checks.add((x) { |
| 95 Expect.isTrue(x is List); |
| 96 Expect.listEquals([1, 2], x); |
| 97 // Make sure the list is mutable. |
| 98 x[0] = 0; |
| 99 Expect.equals(0, x[0]); |
| 100 // List must be extendable. |
| 101 x.add(3); |
| 102 Expect.equals(3, x[2]); |
| 103 }); |
| 104 |
| 105 List fixed = new List(2); |
| 106 fixed[0] = 0; |
| 107 fixed[1] = 1; |
| 108 ping.send(fixed); |
| 109 checks.add((x) { |
| 110 Expect.isTrue(x is List); |
| 111 Expect.listEquals([0, 1], x); |
| 112 // List must be mutable. |
| 113 x[0] = 3; |
| 114 Expect.equals(3, x[0]); |
| 115 // List must be fixed length. |
| 116 Expect.throws(() { x.add(5); }); |
| 117 }); |
| 118 |
| 119 List cyclic = []; |
| 120 cyclic.add(cyclic); |
| 121 ping.send(cyclic); |
| 122 checks.add((x) { |
| 123 Expect.isTrue(x is List); |
| 124 Expect.equals(1, x.length); |
| 125 Expect.identical(x, x[0]); |
| 126 // List must be mutable. |
| 127 x[0] = 55; |
| 128 Expect.equals(55, x[0]); |
| 129 // List must be extendable. |
| 130 x.add(42); |
| 131 Expect.equals(42, x[1]); |
| 132 }); |
| 133 |
| 134 List cyclic2 = new List(1); |
| 135 cyclic2[0] = cyclic2; |
| 136 ping.send(cyclic2); |
| 137 checks.add((x) { |
| 138 Expect.isTrue(x is List); |
| 139 Expect.equals(1, x.length); |
| 140 Expect.identical(x, x[0]); |
| 141 // List must be mutable. |
| 142 x[0] = 55; |
| 143 Expect.equals(55, x[0]); |
| 144 // List must be fixed. |
| 145 Expect.throws(() => x.add(42)); |
| 146 }); |
| 147 |
| 148 List constList = const [1, 2]; |
| 149 ping.send(constList); |
| 150 checks.add((x) { |
| 151 Expect.isTrue(x is List); |
| 152 Expect.listEquals([1, 2], x); |
| 153 // Make sure the list is immutable. |
| 154 Expect.throws(() => x[0] = 0); /// constList: ok |
| 155 // List must not be extendable. |
| 156 Expect.throws(() => x.add(3)); |
| 157 Expect.identical(x, constList); /// constList_identical: ok |
| 158 }); |
| 159 |
| 160 Uint8List uint8 = new Uint8List(2); |
| 161 uint8[0] = 0; |
| 162 uint8[1] = 1; |
| 163 ping.send(uint8); |
| 164 checks.add((x) { |
| 165 Expect.isTrue(x is Uint8List); |
| 166 Expect.equals(2, x.length); |
| 167 Expect.equals(0, x[0]); |
| 168 Expect.equals(1, x[1]); |
| 169 }); |
| 170 |
| 171 ping.send({"foo": 499, "bar": 32}); |
| 172 checks.add((x) { |
| 173 Expect.isTrue(x is LinkedHashMap); |
| 174 Expect.listEquals(["foo", "bar"], x.keys.toList()); |
| 175 Expect.listEquals([499, 32], x.values.toList()); |
| 176 // Must be mutable. |
| 177 x["foo"] = 22; |
| 178 Expect.equals(22, x["foo"]); |
| 179 // Must be extendable. |
| 180 x["gee"] = 499; |
| 181 Expect.equals(499, x["gee"]); |
| 182 }); |
| 183 |
| 184 ping.send({0: 499, 1: 32}); |
| 185 checks.add((x) { |
| 186 Expect.isTrue(x is LinkedHashMap); |
| 187 Expect.listEquals([0, 1], x.keys.toList()); |
| 188 Expect.listEquals([499, 32], x.values.toList()); |
| 189 // Must be mutable. |
| 190 x[0] = 22; |
| 191 Expect.equals(22, x[0]); |
| 192 // Must be extendable. |
| 193 x["gee"] = 499; |
| 194 Expect.equals(499, x["gee"]); |
| 195 }); |
| 196 |
| 197 Map cyclicMap = {}; |
| 198 cyclicMap["cycle"] = cyclicMap; |
| 199 ping.send(cyclicMap); |
| 200 checks.add((x) { |
| 201 Expect.isTrue(x is LinkedHashMap); |
| 202 Expect.identical(x, x["cycle"]); |
| 203 // Must be mutable. |
| 204 x["cycle"] = 22; |
| 205 Expect.equals(22, x["cycle"]); |
| 206 // Must be extendable. |
| 207 x["gee"] = 499; |
| 208 Expect.equals(499, x["gee"]); |
| 209 }); |
| 210 |
| 211 Map constMap = const {'foo': 499}; |
| 212 ping.send(constMap); |
| 213 checks.add((x) { |
| 214 Expect.isTrue(x is Map); |
| 215 print(x.length); |
| 216 Expect.equals(1, x.length); |
| 217 Expect.equals(499, x['foo']); |
| 218 Expect.identical(constMap, x); /// constMap: ok |
| 219 Expect.throws(() => constMap['bar'] = 42); |
| 220 }); |
| 221 |
| 222 ping.send(new A()); |
| 223 checks.add((x) { |
| 224 Expect.isTrue(x is A); |
| 225 Expect.equals(499, x.field); |
| 226 }); |
| 227 |
| 228 ping.send(new A.named(42)); |
| 229 checks.add((x) { |
| 230 Expect.isTrue(x is A); |
| 231 Expect.equals(42, x.field); |
| 232 }); |
| 233 |
| 234 ping.send(new B()); |
| 235 checks.add((x) { |
| 236 Expect.isTrue(x is A); |
| 237 Expect.isTrue(x is B); |
| 238 Expect.equals(499, x.field); |
| 239 Expect.equals(99, x.field2); |
| 240 Expect.throws(() => x.field2 = 22); |
| 241 }); |
| 242 |
| 243 ping.send(new B.named(1, 2)); |
| 244 checks.add((x) { |
| 245 Expect.isTrue(x is A); |
| 246 Expect.isTrue(x is B); |
| 247 Expect.equals(2, x.field); |
| 248 Expect.equals(1, x.field2); |
| 249 Expect.throws(() => x.field2 = 22); |
| 250 }); |
| 251 |
| 252 ping.send(new C()); |
| 253 checks.add((x) { |
| 254 Expect.isTrue(x is A); |
| 255 Expect.isTrue(x is B); |
| 256 Expect.isTrue(x is C); |
| 257 Expect.equals(33, x.field); |
| 258 Expect.equals(99, x.field2); |
| 259 Expect.equals(499, x.superField); |
| 260 Expect.throws(() => x.field2 = 22); |
| 261 }); |
| 262 |
| 263 ping.send(new D()); |
| 264 checks.add((x) { |
| 265 Expect.isTrue(x is A); |
| 266 Expect.isTrue(x is B); |
| 267 Expect.isTrue(x is C); |
| 268 Expect.isTrue(x is D); |
| 269 Expect.isTrue(x is M); |
| 270 Expect.equals(33, x.field); |
| 271 Expect.equals(11, x.field2); |
| 272 Expect.equals(499, x.superField); |
| 273 Expect.equals(99, x.superField2); |
| 274 Expect.throws(() => x.field2 = 22); |
| 275 }); |
| 276 |
| 277 D cyclicD = new D(); |
| 278 cyclicD.field = cyclicD; |
| 279 ping.send(cyclicD); |
| 280 checks.add((x) { |
| 281 Expect.isTrue(x is A); |
| 282 Expect.isTrue(x is B); |
| 283 Expect.isTrue(x is C); |
| 284 Expect.isTrue(x is D); |
| 285 Expect.isTrue(x is M); |
| 286 Expect.identical(x, x.field); |
| 287 Expect.equals(11, x.field2); |
| 288 Expect.equals(499, x.superField); |
| 289 Expect.equals(99, x.superField2); |
| 290 Expect.throws(() => x.field2 = 22); |
| 291 }); |
| 292 |
| 293 ping.send(new E(E.fooFun)); /// fun: ok |
| 294 checks.add((x) { /// fun: continued |
| 295 Expect.equals(E.fooFun, x.fun); /// fun: continued |
| 296 Expect.equals(499, x.fun()); /// fun: continued |
| 297 }); /// fun: continued |
| 298 |
| 299 ping.send(new E(barFun)); /// fun: continued |
| 300 checks.add((x) { /// fun: continued |
| 301 Expect.equals(barFun, x.fun); /// fun: continued |
| 302 Expect.equals(42, x.fun()); /// fun: continued |
| 303 }); /// fun: continued |
| 304 |
| 305 Expect.throws(() => ping.send(new E(new E(null).instanceFun))); |
| 306 |
| 307 F nonConstF = new F(); |
| 308 ping.send(nonConstF); |
| 309 checks.add((x) { |
| 310 Expect.equals("field", x.field); |
| 311 Expect.isFalse(identical(nonConstF, x)); |
| 312 }); |
| 313 |
| 314 const F constF = const F(); |
| 315 ping.send(constF); |
| 316 checks.add((x) { |
| 317 Expect.equals("field", x.field); |
| 318 Expect.identical(constF, x); /// constInstance: ok |
| 319 }); |
| 320 |
| 321 G g1 = new G(nonConstF); |
| 322 G g2 = new G(constF); |
| 323 G g3 = const G(constF); |
| 324 ping.send(g1); |
| 325 ping.send(g2); |
| 326 ping.send(g3); |
| 327 |
| 328 checks.add((x) { // g1. |
| 329 Expect.isTrue(x is G); |
| 330 Expect.isFalse(identical(g1, x)); |
| 331 F f = x.field; |
| 332 Expect.equals("field", f.field); |
| 333 Expect.isFalse(identical(nonConstF, f)); |
| 334 }); |
| 335 checks.add((x) { // g1. |
| 336 Expect.isTrue(x is G); |
| 337 Expect.isFalse(identical(g1, x)); |
| 338 F f = x.field; |
| 339 Expect.equals("field", f.field); |
| 340 Expect.identical(constF, f); /// constInstance: continued |
| 341 }); |
| 342 checks.add((x) { // g3. |
| 343 Expect.isTrue(x is G); |
| 344 Expect.identical(g1, x); /// constInstance: continued |
| 345 F f = x.field; |
| 346 Expect.equals("field", f.field); |
| 347 Expect.identical(constF, f); /// constInstance: continued |
| 348 }); |
| 349 |
| 350 // Make sure objects in a map are serialized and deserialized in the correct |
| 351 // order. |
| 352 Map m = new Map(); |
| 353 Value val1 = new Value(1); |
| 354 Value val2 = new Value(2); |
| 355 m[val1] = val2; |
| 356 m[val2] = val1; |
| 357 // Possible bug we want to catch: |
| 358 // serializer runs through keys first, and then the values: |
| 359 // - id1 = val1, id2 = val2, ref[id2], ref[id1] |
| 360 // deserializer runs through the keys and values in order: |
| 361 // - val1; // id1. |
| 362 // - ref[id2]; // boom. Wasn't deserialized yet. |
| 363 ping.send(m); |
| 364 checks.add((x) { |
| 365 Expect.isTrue(x is Map); |
| 366 Expect.equals(2, x.length); |
| 367 Expect.equals(val2, x[val1]); |
| 368 Expect.equals(val1, x[val2]); |
| 369 Expect.identical(x.keys.elementAt(0), x.values.elementAt(1)); |
| 370 Expect.identical(x.keys.elementAt(1), x.values.elementAt(0)); |
| 371 }); |
| 372 } |
| 373 |
| 374 void main() { |
| 375 asyncStart(); |
| 376 Queue checks = new Queue(); |
| 377 ReceivePort testPort = new ReceivePort(); |
| 378 Completer completer = new Completer(); |
| 379 |
| 380 testPort.listen((msg) { |
| 381 Function check = checks.removeFirst(); |
| 382 check(msg); |
| 383 if (checks.isEmpty) { |
| 384 completer.complete(); |
| 385 testPort.close(); |
| 386 } |
| 387 }); |
| 388 |
| 389 ReceivePort initialReplyPort = new ReceivePort(); |
| 390 Isolate |
| 391 .spawn(echoMain, [initialReplyPort.sendPort, testPort.sendPort]) |
| 392 .then((_) => initialReplyPort.first) |
| 393 .then((SendPort ping) { |
| 394 runTests(ping, checks); |
| 395 Expect.isTrue(checks.length > 0); |
| 396 completer.future |
| 397 .then((_) => ping.send("halt")) |
| 398 .then((_) => asyncEnd()); |
| 399 }); |
| 400 } |
OLD | NEW |