| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2015, 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 library isolate.test.registry_test; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:isolate'; |
| 9 |
| 10 import 'package:isolate/isolate_runner.dart'; |
| 11 import 'package:isolate/registry.dart'; |
| 12 |
| 13 import 'package:test/test.dart'; |
| 14 |
| 15 const MS = const Duration(milliseconds: 1); |
| 16 |
| 17 void main() { |
| 18 group('lookup', testLookup); |
| 19 group('AddLookup', testAddLookup); |
| 20 group('AddRemoveTags', testAddRemoveTags); |
| 21 group('Remove', testRemove); |
| 22 group('CrossIsolate', testCrossIsolate); |
| 23 group('Timeout', testTimeout); |
| 24 group('MultiRegistry', testMultiRegistry); |
| 25 group('ObjectsAndTags', testObjectsAndTags); |
| 26 } |
| 27 |
| 28 class Oddity { |
| 29 static const int EVEN = 0; |
| 30 static const int ODD = 1; |
| 31 } |
| 32 |
| 33 Future<List> waitAll(int n, Future action(int n)) { |
| 34 return Future.wait(new Iterable.generate(n, action)); |
| 35 } |
| 36 |
| 37 void testLookup() { |
| 38 test("All", () { |
| 39 RegistryManager regman = new RegistryManager(); |
| 40 Registry registry = regman.registry; |
| 41 return waitAll(10, (i) { |
| 42 var element = new Element(i); |
| 43 var tag = i.isEven ? Oddity.EVEN : Oddity.ODD; |
| 44 return registry.add(element, tags: [tag]); |
| 45 }).then((_) { |
| 46 return registry.lookup(); |
| 47 }).then((all) { |
| 48 expect(all.length, 10); |
| 49 expect(all.map((v) => v.id).toList()..sort(), |
| 50 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); |
| 51 }) |
| 52 .whenComplete(regman.close); |
| 53 }); |
| 54 |
| 55 test("Odd", () { |
| 56 RegistryManager regman = new RegistryManager(); |
| 57 Registry registry = regman.registry; |
| 58 return waitAll(10, (i) { |
| 59 var element = new Element(i); |
| 60 var tag = i.isEven ? Oddity.EVEN : Oddity.ODD; |
| 61 return registry.add(element, tags: [tag]); |
| 62 }).then((_) { |
| 63 return registry.lookup(tags:[Oddity.ODD]); |
| 64 }).then((all) { |
| 65 expect(all.length, 5); |
| 66 expect(all.map((v) => v.id).toList()..sort(), |
| 67 [1, 3, 5, 7, 9]); |
| 68 }) |
| 69 .whenComplete(regman.close); |
| 70 }); |
| 71 |
| 72 test("Max", () { |
| 73 RegistryManager regman = new RegistryManager(); |
| 74 Registry registry = regman.registry; |
| 75 return waitAll(10, (i) { |
| 76 var element = new Element(i); |
| 77 var tag = i.isEven ? Oddity.EVEN : Oddity.ODD; |
| 78 return registry.add(element, tags: [tag]); |
| 79 }).then((_) { |
| 80 return registry.lookup(max: 5); |
| 81 }).then((all) { |
| 82 expect(all.length, 5); |
| 83 }) |
| 84 .whenComplete(regman.close); |
| 85 }); |
| 86 |
| 87 test("MultiTag", () { |
| 88 RegistryManager regman = new RegistryManager(); |
| 89 Registry registry = regman.registry; |
| 90 return waitAll(25, (i) { |
| 91 var element = new Element(i); |
| 92 // Collect all numbers dividing i. |
| 93 var tags = [i]; |
| 94 for (int j = 2; j < 25; j++) { |
| 95 if (i % j == 0) tags.add(j); |
| 96 } |
| 97 return registry.add(element, tags: tags); |
| 98 }).then((_) { |
| 99 return registry.lookup(tags: [2, 3]); |
| 100 }).then((all) { |
| 101 expect(all.length, 5); |
| 102 expect(all.map((v) => v.id).toList()..sort(), |
| 103 [0, 6, 12, 18, 24]); |
| 104 }) |
| 105 .whenComplete(regman.close); |
| 106 }); |
| 107 |
| 108 test("MultiTagMax", () { |
| 109 RegistryManager regman = new RegistryManager(); |
| 110 Registry registry = regman.registry; |
| 111 return waitAll(25, (i) { |
| 112 var element = new Element(i); |
| 113 // Collect all numbers dividing i. |
| 114 var tags = [i]; |
| 115 for (int j = 2; j < 25; j++) { |
| 116 if (i % j == 0) tags.add(j); |
| 117 } |
| 118 return registry.add(element, tags: tags); |
| 119 }).then((_) { |
| 120 return registry.lookup(tags: [2, 3], max: 3); |
| 121 }).then((all) { |
| 122 expect(all.length, 3); |
| 123 expect(all.every((v) => (v.id % 6) == 0), isTrue); |
| 124 }) |
| 125 .whenComplete(regman.close); |
| 126 }); |
| 127 } |
| 128 |
| 129 void testAddLookup() { |
| 130 test("Add-lookup-identical", () { |
| 131 RegistryManager regman = new RegistryManager(); |
| 132 Registry registry = regman.registry; |
| 133 var object = new Object(); |
| 134 return registry.add(object).then((_) { |
| 135 return registry.lookup(); |
| 136 }).then((entries) { |
| 137 expect(entries, hasLength(1)); |
| 138 expect(entries.first, same(object)); |
| 139 }).whenComplete(regman.close); |
| 140 }); |
| 141 |
| 142 test("Add-multiple-identical", () { |
| 143 RegistryManager regman = new RegistryManager(); |
| 144 Registry registry = regman.registry; |
| 145 var object1 = new Object(); |
| 146 var object2 = new Object(); |
| 147 var object3 = new Object(); |
| 148 var objects = [object1, object2, object3]; |
| 149 return Future.wait(objects.map(registry.add)).then((_) { |
| 150 return registry.lookup(); |
| 151 }).then((entries) { |
| 152 expect(entries, hasLength(3)); |
| 153 for (var entry in entries) { |
| 154 expect(entry, isIn(objects)); |
| 155 } |
| 156 }).whenComplete(regman.close); |
| 157 }); |
| 158 |
| 159 test("Add-twice", () { |
| 160 RegistryManager regman = new RegistryManager(); |
| 161 Registry registry = regman.registry; |
| 162 var object = new Object(); |
| 163 return registry.add(object).then((_) { |
| 164 return registry.add(object); |
| 165 }).then((_) { |
| 166 fail("Unreachable"); |
| 167 }, onError: (e, s) { |
| 168 expect(e, isStateError); |
| 169 }).whenComplete(regman.close); |
| 170 }); |
| 171 |
| 172 test("Add-lookup-add-lookup", () { |
| 173 RegistryManager regman = new RegistryManager(); |
| 174 Registry registry = regman.registry; |
| 175 var object = new Object(); |
| 176 var object2 = new Object(); |
| 177 return registry.add(object).then((_) { |
| 178 return registry.lookup(); |
| 179 }).then((entries) { |
| 180 expect(entries, hasLength(1)); |
| 181 expect(entries.first, same(object)); |
| 182 return registry.add(object2); |
| 183 }).then((_) { |
| 184 return registry.lookup(); |
| 185 }).then((entries) { |
| 186 expect(entries, hasLength(2)); |
| 187 var entry1 = entries.first; |
| 188 var entry2 = entries.last; |
| 189 if (object == entry1) { |
| 190 expect(entry2, same(object2)); |
| 191 } else { |
| 192 expect(entry1, same(object)); |
| 193 } |
| 194 }).whenComplete(regman.close); |
| 195 }); |
| 196 |
| 197 test("lookup-add-lookup", () { |
| 198 RegistryManager regman = new RegistryManager(); |
| 199 Registry registry = regman.registry; |
| 200 var object = new Object(); |
| 201 return registry.lookup().then((entries) { |
| 202 expect(entries, isEmpty); |
| 203 return registry.add(object); |
| 204 }).then((_) { |
| 205 return registry.lookup(); |
| 206 }).then((entries) { |
| 207 expect(entries, hasLength(1)); |
| 208 expect(entries.first, same(object)); |
| 209 }).whenComplete(regman.close); |
| 210 }); |
| 211 |
| 212 test("Add-multiple-tags", () { |
| 213 RegistryManager regman = new RegistryManager(); |
| 214 Registry registry = regman.registry; |
| 215 var object1 = new Object(); |
| 216 var object2 = new Object(); |
| 217 var object3 = new Object(); |
| 218 return registry.add(object1, tags: [1, 3, 5, 7]).then((_) { |
| 219 return registry.add(object2, tags: [2, 3, 6, 7]); |
| 220 }).then((_) { |
| 221 return registry.add(object3, tags: [4, 5, 6, 7]); |
| 222 }).then((_) { |
| 223 return registry.lookup(tags: [3]); |
| 224 }).then((entries) { |
| 225 expect(entries, hasLength(2)); |
| 226 expect(entries.first == object1 || entries.last == object1, isTrue); |
| 227 expect(entries.first == object2 || entries.last == object2, isTrue); |
| 228 }).then((_) { |
| 229 return registry.lookup(tags: [2]); |
| 230 }).then((entries) { |
| 231 expect(entries, hasLength(1)); |
| 232 expect(entries.first, same(object2)); |
| 233 }).then((_) { |
| 234 return registry.lookup(tags: [3, 6]); |
| 235 }).then((entries) { |
| 236 expect(entries, hasLength(1)); |
| 237 expect(entries.first, same(object2)); |
| 238 }).whenComplete(regman.close); |
| 239 }); |
| 240 } |
| 241 |
| 242 void testRemove() { |
| 243 test("Add-remove", () { |
| 244 RegistryManager regman = new RegistryManager(); |
| 245 Registry registry = regman.registry; |
| 246 var object = new Object(); |
| 247 return registry.add(object).then((removeCapability) { |
| 248 return registry.lookup().then((entries) { |
| 249 expect(entries, hasLength(1)); |
| 250 expect(entries.first, same(object)); |
| 251 return registry.remove(object, removeCapability); |
| 252 }); |
| 253 }).then((removeSuccess) { |
| 254 expect(removeSuccess, isTrue); |
| 255 return registry.lookup(); |
| 256 }).then((entries) { |
| 257 expect(entries, isEmpty); |
| 258 }).whenComplete(regman.close); |
| 259 }); |
| 260 |
| 261 test("Add-remove-fail", () { |
| 262 RegistryManager regman = new RegistryManager(); |
| 263 Registry registry = regman.registry; |
| 264 var object = new Object(); |
| 265 return registry.add(object).then((removeCapability) { |
| 266 return registry.lookup().then((entries) { |
| 267 expect(entries, hasLength(1)); |
| 268 expect(entries.first, same(object)); |
| 269 return registry.remove(object, new Capability()); |
| 270 }); |
| 271 }).then((removeSuccess) { |
| 272 expect(removeSuccess, isFalse); |
| 273 }).whenComplete(regman.close); |
| 274 }); |
| 275 } |
| 276 |
| 277 void testAddRemoveTags() { |
| 278 test("Add-remove-tag", () { |
| 279 RegistryManager regman = new RegistryManager(); |
| 280 Registry registry = regman.registry; |
| 281 var object = new Object(); |
| 282 return registry.add(object).then((removeCapability) { |
| 283 return registry.lookup(tags: ["x"]); |
| 284 }).then((entries) { |
| 285 expect(entries, isEmpty); |
| 286 return registry.addTags([object], ["x"]); |
| 287 }).then((_) { |
| 288 return registry.lookup(tags: ["x"]); |
| 289 }).then((entries) { |
| 290 expect(entries, hasLength(1)); |
| 291 expect(entries.first, same(object)); |
| 292 return registry.removeTags([object], ["x"]); |
| 293 }).then((_) { |
| 294 return registry.lookup(tags: ["x"]); |
| 295 }).then((entries) { |
| 296 expect(entries, isEmpty); |
| 297 }).whenComplete(regman.close); |
| 298 }); |
| 299 |
| 300 test("Tag-twice", () { |
| 301 RegistryManager regman = new RegistryManager(); |
| 302 Registry registry = regman.registry; |
| 303 var object = new Object(); |
| 304 return registry.add(object, tags: ["x"]).then((removeCapability) { |
| 305 return registry.lookup(tags: ["x"]); |
| 306 }).then((entries) { |
| 307 expect(entries, hasLength(1)); |
| 308 expect(entries.first, same(object)); |
| 309 // Adding the same tag twice is allowed, but does nothing. |
| 310 return registry.addTags([object], ["x"]); |
| 311 }).then((_) { |
| 312 return registry.lookup(tags: ["x"]); |
| 313 }).then((entries) { |
| 314 expect(entries, hasLength(1)); |
| 315 expect(entries.first, same(object)); |
| 316 // Removing the tag once is enough to remove it. |
| 317 return registry.removeTags([object], ["x"]); |
| 318 }).then((_) { |
| 319 return registry.lookup(tags: ["x"]); |
| 320 }).then((entries) { |
| 321 expect(entries, isEmpty); |
| 322 }).whenComplete(regman.close); |
| 323 }); |
| 324 |
| 325 test("Add-remove-multiple", () { |
| 326 RegistryManager regman = new RegistryManager(); |
| 327 Registry registry = regman.registry; |
| 328 var object1 = new Object(); |
| 329 var object2 = new Object(); |
| 330 var object3 = new Object(); |
| 331 var objects = [object1, object2, object3]; |
| 332 return Future.wait(objects.map(registry.add)).then((_) { |
| 333 return registry.addTags([object1, object2], ["x", "y"]); |
| 334 }).then((_) { |
| 335 return registry.addTags([object1, object3], ["z", "w"]); |
| 336 }).then((_) { |
| 337 return registry.lookup(tags: ["x", "z"]); |
| 338 }).then((entries) { |
| 339 expect(entries, hasLength(1)); |
| 340 expect(entries.first, same(object1)); |
| 341 return registry.removeTags([object1, object2], ["x", "z"]); |
| 342 }).then((_) { |
| 343 return registry.lookup(tags: ["z"]); |
| 344 }).then((entries) { |
| 345 expect(entries, hasLength(1)); |
| 346 expect(entries.first, same(object3)); |
| 347 }).whenComplete(regman.close); |
| 348 }); |
| 349 |
| 350 test("Remove-wrong-object", () { |
| 351 RegistryManager regman = new RegistryManager(); |
| 352 Registry registry = regman.registry; |
| 353 expect(() => registry.removeTags([new Object()], ["x"]), throws); |
| 354 regman.close(); |
| 355 }); |
| 356 } |
| 357 |
| 358 var _regmen = {}; |
| 359 Registry createRegMan(id) { |
| 360 var regman = new RegistryManager(); |
| 361 _regmen[id] = regman; |
| 362 return regman.registry; |
| 363 } |
| 364 void closeRegMan(id) { |
| 365 _regmen.remove(id).close(); |
| 366 } |
| 367 |
| 368 void testCrossIsolate() { |
| 369 var object = new Object(); |
| 370 test("regman-other-isolate", () { |
| 371 // Add, lookup and remove object in other isolate. |
| 372 return IsolateRunner.spawn().then((isolate) { |
| 373 isolate.run(createRegMan, 1).then((registry) { |
| 374 return registry.add(object, tags: ["a", "b"]).then((removeCapability) { |
| 375 return registry.lookup(tags: ["a"]).then((entries) { |
| 376 expect(entries, hasLength(1)); |
| 377 expect(entries.first, same(object)); |
| 378 return registry.remove(entries.first, removeCapability); |
| 379 }).then((removeSuccess) { |
| 380 expect(removeSuccess, isTrue); |
| 381 }); |
| 382 }); |
| 383 }).whenComplete(() { |
| 384 return isolate.run(closeRegMan, 1); |
| 385 }).whenComplete(() { |
| 386 return isolate.close(); |
| 387 }); |
| 388 }); |
| 389 }); |
| 390 } |
| 391 |
| 392 void testTimeout() { |
| 393 test("Timeout-add", () { |
| 394 RegistryManager regman = new RegistryManager(timeout: MS * 500); |
| 395 Registry registry = regman.registry; |
| 396 regman.close(); |
| 397 return registry.add(new Object()).then((_) { |
| 398 fail("unreachable"); |
| 399 }, onError: (e, s) { |
| 400 expect(e is TimeoutException, isTrue); |
| 401 }); |
| 402 }); |
| 403 |
| 404 test("Timeout-remove", () { |
| 405 RegistryManager regman = new RegistryManager(timeout: MS * 500); |
| 406 Registry registry = regman.registry; |
| 407 var object = new Object(); |
| 408 return registry.add(object).then((rc) { |
| 409 regman.close(); |
| 410 return registry.remove(object, rc).then((_) { |
| 411 fail("unreachable"); |
| 412 }, onError: (e, s) { |
| 413 expect(e is TimeoutException, isTrue); |
| 414 }); |
| 415 }); |
| 416 }); |
| 417 |
| 418 test("Timeout-addTags", () { |
| 419 RegistryManager regman = new RegistryManager(timeout: MS * 500); |
| 420 Registry registry = regman.registry; |
| 421 var object = new Object(); |
| 422 return registry.add(object).then((rc) { |
| 423 regman.close(); |
| 424 return registry.addTags([object], ["x"]).then((_) { |
| 425 fail("unreachable"); |
| 426 }, onError: (e, s) { |
| 427 expect(e is TimeoutException, isTrue); |
| 428 }); |
| 429 }); |
| 430 }); |
| 431 |
| 432 test("Timeout-removeTags", () { |
| 433 RegistryManager regman = new RegistryManager(timeout: MS * 500); |
| 434 Registry registry = regman.registry; |
| 435 var object = new Object(); |
| 436 return registry.add(object).then((rc) { |
| 437 regman.close(); |
| 438 return registry.removeTags([object], ["x"]).then((_) { |
| 439 fail("unreachable"); |
| 440 }, onError: (e, s) { |
| 441 expect(e is TimeoutException, isTrue); |
| 442 }); |
| 443 }); |
| 444 }); |
| 445 |
| 446 test("Timeout-lookup", () { |
| 447 RegistryManager regman = new RegistryManager(timeout: MS * 500); |
| 448 Registry registry = regman.registry; |
| 449 regman.close(); |
| 450 registry.lookup().then((_) { |
| 451 fail("unreachable"); |
| 452 }, onError: (e, s) { |
| 453 expect(e is TimeoutException, isTrue); |
| 454 }); |
| 455 }); |
| 456 } |
| 457 |
| 458 void testMultiRegistry() { |
| 459 test("dual-registry", () { |
| 460 RegistryManager regman = new RegistryManager(); |
| 461 Registry registry1 = regman.registry; |
| 462 Registry registry2 = regman.registry; |
| 463 var l1 = ["x"]; |
| 464 var l2; |
| 465 return registry1.add(l1, tags: ["y"]).then((removeCapability) { |
| 466 return registry2.lookup().then((entries) { |
| 467 expect(entries, hasLength(1)); |
| 468 l2 = entries.first; |
| 469 expect(l2, equals(l1)); |
| 470 // The object for registry2 is not identical the one for registry1. |
| 471 expect(!identical(l1, l2), isTrue); |
| 472 // Removing the registry1 object through registry2 doesn't work. |
| 473 return registry2.remove(l1, removeCapability); |
| 474 }).then((removeSuccess) { |
| 475 expect(removeSuccess, isFalse); |
| 476 return registry2.remove(l2, removeCapability); |
| 477 }).then((removeSuccess) { |
| 478 expect(removeSuccess, isTrue); |
| 479 return registry1.lookup(); |
| 480 }).then((entries) { |
| 481 expect(entries, isEmpty); |
| 482 }); |
| 483 }).whenComplete(regman.close); |
| 484 }); |
| 485 } |
| 486 |
| 487 void testObjectsAndTags() { |
| 488 testObject(object) { |
| 489 String name = "Transfer-${object.runtimeType}"; |
| 490 test(name, () { |
| 491 RegistryManager regman = new RegistryManager(); |
| 492 Registry registry1 = regman.registry; |
| 493 Registry registry2 = regman.registry; |
| 494 return registry1.add(object, tags: [object]).then((removeCapability) { |
| 495 return registry2.lookup().then((entries) { |
| 496 expect(entries, hasLength(1)); |
| 497 expect(entries.first, equals(object)); |
| 498 return registry2.lookup(tags: [object]); |
| 499 }).then((entries) { |
| 500 expect(entries, hasLength(1)); |
| 501 expect(entries.first, equals(object)); |
| 502 return registry2.removeTags([entries.first], [object]); |
| 503 }).then((_) { |
| 504 return registry2.lookup(); |
| 505 }).then((entries) { |
| 506 expect(entries, hasLength(1)); |
| 507 expect(entries.first, equals(object)); |
| 508 return registry2.remove(entries.first, removeCapability); |
| 509 }).then((removeSuccess) { |
| 510 expect(removeSuccess, isTrue); |
| 511 return registry2.lookup(); |
| 512 }).then((entries) { |
| 513 expect(entries, isEmpty); |
| 514 }); |
| 515 }).whenComplete(regman.close); |
| 516 }); |
| 517 } |
| 518 // Test objects that are sendable between equivalent isolates and |
| 519 // that has an operator== that works after cloning (for use as tags). |
| 520 testObject(42); |
| 521 testObject(3.14); |
| 522 testObject("string"); |
| 523 testObject(true); |
| 524 testObject(null); |
| 525 testObject(new Element(42)); |
| 526 testObject(#symbol); |
| 527 testObject(#_privateSymbol); |
| 528 testObject(new Capability()); |
| 529 testObject(topLevelFunction); |
| 530 } |
| 531 |
| 532 class Element { |
| 533 final int id; |
| 534 Element(this.id); |
| 535 int get hashCode => id; |
| 536 bool operator ==(Object other) => other is Element && id == other.id; |
| 537 } |
| 538 |
| 539 void topLevelFunction() {} |
| OLD | NEW |