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 seaneagan's fake_async package lands. |
| 93 } |
| 94 |
| 95 /// Returns a [Future] that completes after pumping the event queue [times] |
| 96 /// times. By default, this should pump the event queue enough times to allow |
| 97 /// any code to run, as long as it's not waiting on some external event. |
| 98 Future pumpEventQueue([int times = 20]) { |
| 99 if (times == 0) return new Future.value(); |
| 100 // We use a delayed future to allow microtask events to finish. The |
| 101 // Future.value or Future() constructors use scheduleMicrotask themselves and |
| 102 // would therefore not wait for microtask callbacks that are scheduled after |
| 103 // invoking this method. |
| 104 return new Future.delayed(Duration.ZERO, () => pumpEventQueue(times - 1)); |
| 105 } |
| 106 |
| 107 /// Returns a function that will cause the test to fail if it's called. |
| 108 /// |
| 109 /// This won't let the test complete until it's confident that the function |
| 110 /// won't be called. |
| 111 Function expectNoAsync() { |
| 112 // Make sure the test lasts long enough for the function to get called if it's |
| 113 // going to get called. |
| 114 expect(pumpEventQueue(), completes); |
| 115 |
| 116 var stack = new Trace.current(1); |
| 117 return () => handleExternalError( |
| 118 new TestFailure("Expected function not to be called."), "", |
| 119 stack); |
| 120 } |
| 121 |
| 122 /// A matcher for Futures that asserts that they don't complete. |
| 123 /// |
| 124 /// This won't let the test complete until it's confident that the function |
| 125 /// won't be called. |
| 126 Matcher get doesNotComplete => predicate((future) { |
| 127 expect(future, new isInstanceOf<Future>('Future')); |
| 128 // Make sure the test lasts long enough for the function to get called if it's |
| 129 // going to get called. |
| 130 expect(pumpEventQueue(), completes); |
| 131 |
| 132 var stack = new Trace.current(1); |
| 133 future.then((_) => handleExternalError( |
| 134 new TestFailure("Expected future not to complete."), "", |
| 135 stack)); |
| 136 return true; |
| 137 }); |
OLD | NEW |