| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2014, 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 import 'dart:async'; | |
| 6 | |
| 7 import 'package:pool/pool.dart'; | |
| 8 import 'package:stack_trace/stack_trace.dart'; | |
| 9 import 'package:unittest/unittest.dart'; | |
| 10 | |
| 11 void main() { | |
| 12 group("request()", () { | |
| 13 test("resources can be requested freely up to the limit", () { | |
| 14 var pool = new Pool(50); | |
| 15 var requests = []; | |
| 16 for (var i = 0; i < 50; i++) { | |
| 17 expect(pool.request(), completes); | |
| 18 } | |
| 19 }); | |
| 20 | |
| 21 test("resources block past the limit", () { | |
| 22 var pool = new Pool(50); | |
| 23 var requests = []; | |
| 24 for (var i = 0; i < 50; i++) { | |
| 25 expect(pool.request(), completes); | |
| 26 } | |
| 27 expect(pool.request(), doesNotComplete); | |
| 28 }); | |
| 29 | |
| 30 test("a blocked resource is allocated when another is released", () { | |
| 31 var pool = new Pool(50); | |
| 32 var requests = []; | |
| 33 for (var i = 0; i < 49; i++) { | |
| 34 expect(pool.request(), completes); | |
| 35 } | |
| 36 | |
| 37 return pool.request().then((lastAllocatedResource) { | |
| 38 var blockedResource = pool.request(); | |
| 39 | |
| 40 return pumpEventQueue().then((_) { | |
| 41 lastAllocatedResource.release(); | |
| 42 return pumpEventQueue(); | |
| 43 }).then((_) { | |
| 44 expect(blockedResource, completes); | |
| 45 }); | |
| 46 }); | |
| 47 }); | |
| 48 }); | |
| 49 | |
| 50 group("withResource()", () { | |
| 51 test("can be called freely up to the limit", () { | |
| 52 var pool = new Pool(50); | |
| 53 var requests = []; | |
| 54 for (var i = 0; i < 50; i++) { | |
| 55 pool.withResource(expectAsync(() => new Completer().future)); | |
| 56 } | |
| 57 }); | |
| 58 | |
| 59 test("blocks the callback past the limit", () { | |
| 60 var pool = new Pool(50); | |
| 61 var requests = []; | |
| 62 for (var i = 0; i < 50; i++) { | |
| 63 pool.withResource(expectAsync(() => new Completer().future)); | |
| 64 } | |
| 65 pool.withResource(expectNoAsync()); | |
| 66 }); | |
| 67 | |
| 68 test("a blocked resource is allocated when another is released", () { | |
| 69 var pool = new Pool(50); | |
| 70 var requests = []; | |
| 71 for (var i = 0; i < 49; i++) { | |
| 72 pool.withResource(expectAsync(() => new Completer().future)); | |
| 73 } | |
| 74 | |
| 75 var completer = new Completer(); | |
| 76 var lastAllocatedResource = pool.withResource(() => completer.future); | |
| 77 var blockedResourceAllocated = false; | |
| 78 var blockedResource = pool.withResource(() { | |
| 79 blockedResourceAllocated = true; | |
| 80 }); | |
| 81 | |
| 82 return pumpEventQueue().then((_) { | |
| 83 expect(blockedResourceAllocated, isFalse); | |
| 84 completer.complete(); | |
| 85 return pumpEventQueue(); | |
| 86 }).then((_) { | |
| 87 expect(blockedResourceAllocated, isTrue); | |
| 88 }); | |
| 89 }); | |
| 90 }); | |
| 91 | |
| 92 // TODO(nweiz): Test timeouts when it's easier to use third-party packages. | |
| 93 // See r38552. | |
| 94 } | |
| 95 | |
| 96 /// Returns a [Future] that completes after pumping the event queue [times] | |
| 97 /// times. By default, this should pump the event queue enough times to allow | |
| 98 /// any code to run, as long as it's not waiting on some external event. | |
| 99 Future pumpEventQueue([int times = 20]) { | |
| 100 if (times == 0) return new Future.value(); | |
| 101 // We use a delayed future to allow microtask events to finish. The | |
| 102 // Future.value or Future() constructors use scheduleMicrotask themselves and | |
| 103 // would therefore not wait for microtask callbacks that are scheduled after | |
| 104 // invoking this method. | |
| 105 return new Future.delayed(Duration.ZERO, () => pumpEventQueue(times - 1)); | |
| 106 } | |
| 107 | |
| 108 /// Returns a function that will cause the test to fail if it's called. | |
| 109 /// | |
| 110 /// This won't let the test complete until it's confident that the function | |
| 111 /// won't be called. | |
| 112 Function expectNoAsync() { | |
| 113 // Make sure the test lasts long enough for the function to get called if it's | |
| 114 // going to get called. | |
| 115 expect(pumpEventQueue(), completes); | |
| 116 | |
| 117 var stack = new Trace.current(1); | |
| 118 return () => handleExternalError( | |
| 119 new TestFailure("Expected function not to be called."), "", | |
| 120 stack); | |
| 121 } | |
| 122 | |
| 123 /// A matcher for Futures that asserts that they don't complete. | |
| 124 /// | |
| 125 /// This won't let the test complete until it's confident that the function | |
| 126 /// won't be called. | |
| 127 Matcher get doesNotComplete => predicate((future) { | |
| 128 expect(future, new isInstanceOf<Future>('Future')); | |
| 129 // Make sure the test lasts long enough for the function to get called if it's | |
| 130 // going to get called. | |
| 131 expect(pumpEventQueue(), completes); | |
| 132 | |
| 133 var stack = new Trace.current(1); | |
| 134 future.then((_) => handleExternalError( | |
| 135 new TestFailure("Expected future not to complete."), "", | |
| 136 stack)); | |
| 137 return true; | |
| 138 }); | |
| OLD | NEW |