Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(111)

Side by Side Diff: test/pool_test.dart

Issue 1161433002: pkg/pool: Fixed the homepage, upgraded to pkg/test (Closed) Base URL: https://github.com/dart-lang/pool.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « pubspec.yaml ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 import 'dart:async'; 5 import 'dart:async';
6 6
7 import 'package:fake_async/fake_async.dart'; 7 import 'package:fake_async/fake_async.dart';
8 import 'package:pool/pool.dart'; 8 import 'package:pool/pool.dart';
9 import 'package:stack_trace/stack_trace.dart'; 9 import 'package:stack_trace/stack_trace.dart';
10 import 'package:unittest/unittest.dart'; 10 import 'package:test/test.dart';
11 11
12 void main() { 12 void main() {
13 group("request()", () { 13 group("request()", () {
14 test("resources can be requested freely up to the limit", () { 14 test("resources can be requested freely up to the limit", () {
15 var pool = new Pool(50); 15 var pool = new Pool(50);
16 var requests = [];
17 for (var i = 0; i < 50; i++) { 16 for (var i = 0; i < 50; i++) {
18 expect(pool.request(), completes); 17 expect(pool.request(), completes);
19 } 18 }
20 }); 19 });
21 20
22 test("resources block past the limit", () { 21 test("resources block past the limit", () {
23 new FakeAsync().run((async) { 22 new FakeAsync().run((async) {
24 var pool = new Pool(50); 23 var pool = new Pool(50);
25 var requests = [];
26 for (var i = 0; i < 50; i++) { 24 for (var i = 0; i < 50; i++) {
27 expect(pool.request(), completes); 25 expect(pool.request(), completes);
28 } 26 }
29 expect(pool.request(), doesNotComplete); 27 expect(pool.request(), doesNotComplete);
30 28
31 async.elapse(new Duration(seconds: 1)); 29 async.elapse(new Duration(seconds: 1));
32 }); 30 });
33 }); 31 });
34 32
35 test("a blocked resource is allocated when another is released", () { 33 test("a blocked resource is allocated when another is released", () {
36 new FakeAsync().run((async) { 34 new FakeAsync().run((async) {
37 var pool = new Pool(50); 35 var pool = new Pool(50);
38 var requests = [];
39 for (var i = 0; i < 49; i++) { 36 for (var i = 0; i < 49; i++) {
40 expect(pool.request(), completes); 37 expect(pool.request(), completes);
41 } 38 }
42 39
43 pool.request().then((lastAllocatedResource) { 40 pool.request().then((lastAllocatedResource) {
44 // This will only complete once [lastAllocatedResource] is released. 41 // This will only complete once [lastAllocatedResource] is released.
45 expect(pool.request(), completes); 42 expect(pool.request(), completes);
46 43
47 new Future.delayed(new Duration(microseconds: 1)).then((_) { 44 new Future.delayed(new Duration(microseconds: 1)).then((_) {
48 lastAllocatedResource.release(); 45 lastAllocatedResource.release();
49 }); 46 });
50 }); 47 });
51 48
52 async.elapse(new Duration(seconds: 1)); 49 async.elapse(new Duration(seconds: 1));
53 }); 50 });
54 }); 51 });
55 }); 52 });
56 53
57 group("withResource()", () { 54 group("withResource()", () {
58 test("can be called freely up to the limit", () { 55 test("can be called freely up to the limit", () {
59 var pool = new Pool(50); 56 var pool = new Pool(50);
60 var requests = [];
61 for (var i = 0; i < 50; i++) { 57 for (var i = 0; i < 50; i++) {
62 pool.withResource(expectAsync(() => new Completer().future)); 58 pool.withResource(expectAsync(() => new Completer().future));
63 } 59 }
64 }); 60 });
65 61
66 test("blocks the callback past the limit", () { 62 test("blocks the callback past the limit", () {
67 new FakeAsync().run((async) { 63 new FakeAsync().run((async) {
68 var pool = new Pool(50); 64 var pool = new Pool(50);
69 var requests = [];
70 for (var i = 0; i < 50; i++) { 65 for (var i = 0; i < 50; i++) {
71 pool.withResource(expectAsync(() => new Completer().future)); 66 pool.withResource(expectAsync(() => new Completer().future));
72 } 67 }
73 pool.withResource(expectNoAsync()); 68 pool.withResource(expectNoAsync());
74 69
75 async.elapse(new Duration(seconds: 1)); 70 async.elapse(new Duration(seconds: 1));
76 }); 71 });
77 }); 72 });
78 73
79 test("a blocked resource is allocated when another is released", () { 74 test("a blocked resource is allocated when another is released", () {
80 new FakeAsync().run((async) { 75 new FakeAsync().run((async) {
81 var pool = new Pool(50); 76 var pool = new Pool(50);
82 var requests = [];
83 for (var i = 0; i < 49; i++) { 77 for (var i = 0; i < 49; i++) {
84 pool.withResource(expectAsync(() => new Completer().future)); 78 pool.withResource(expectAsync(() => new Completer().future));
85 } 79 }
86 80
87 var completer = new Completer(); 81 var completer = new Completer();
88 var lastAllocatedResource = pool.withResource(() => completer.future); 82 pool.withResource(() => completer.future);
89 var blockedResourceAllocated = false; 83 var blockedResourceAllocated = false;
90 var blockedResource = pool.withResource(() { 84 pool.withResource(() {
91 blockedResourceAllocated = true; 85 blockedResourceAllocated = true;
92 }); 86 });
93 87
94 new Future.delayed(new Duration(microseconds: 1)).then((_) { 88 new Future.delayed(new Duration(microseconds: 1)).then((_) {
95 expect(blockedResourceAllocated, isFalse); 89 expect(blockedResourceAllocated, isFalse);
96 completer.complete(); 90 completer.complete();
97 return new Future.delayed(new Duration(microseconds: 1)); 91 return new Future.delayed(new Duration(microseconds: 1));
98 }).then((_) { 92 }).then((_) {
99 expect(blockedResourceAllocated, isTrue); 93 expect(blockedResourceAllocated, isTrue);
100 }); 94 });
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 expect(pool.request(), completes); 138 expect(pool.request(), completes);
145 } 139 }
146 expect(pool.request(), doesNotComplete); 140 expect(pool.request(), doesNotComplete);
147 141
148 new Future.delayed(new Duration(seconds: 3)).then((_) { 142 new Future.delayed(new Duration(seconds: 3)).then((_) {
149 expect(pool.request(), doesNotComplete); 143 expect(pool.request(), doesNotComplete);
150 }); 144 });
151 145
152 async.elapse(new Duration(seconds: 6)); 146 async.elapse(new Duration(seconds: 6));
153 }); 147 });
154 }); 148 });
155 149
156 test("times out if nothing happens", () { 150 test("times out if nothing happens", () {
157 new FakeAsync().run((async) { 151 new FakeAsync().run((async) {
158 var pool = new Pool(50, timeout: new Duration(seconds: 5)); 152 var pool = new Pool(50, timeout: new Duration(seconds: 5));
159 for (var i = 0; i < 50; i++) { 153 for (var i = 0; i < 50; i++) {
160 expect(pool.request(), completes); 154 expect(pool.request(), completes);
161 } 155 }
162 expect(pool.request(), throwsA(new isInstanceOf<TimeoutException>())); 156 expect(pool.request(), throwsA(new isInstanceOf<TimeoutException>()));
163 157
164 async.elapse(new Duration(seconds: 6)); 158 async.elapse(new Duration(seconds: 6));
165 }); 159 });
166 }); 160 });
167 }); 161 });
168 } 162 }
169 163
170 /// Returns a function that will cause the test to fail if it's called. 164 /// Returns a function that will cause the test to fail if it's called.
171 /// 165 ///
172 /// This should only be called within a [FakeAsync.run] zone. 166 /// This should only be called within a [FakeAsync.run] zone.
173 Function expectNoAsync() { 167 Function expectNoAsync() {
174 var stack = new Trace.current(1); 168 var stack = new Trace.current(1);
175 return () => handleExternalError( 169 return () => registerException(
176 new TestFailure("Expected function not to be called."), "", 170 new TestFailure("Expected function not to be called."), stack);
177 stack);
178 } 171 }
179 172
180 /// A matcher for Futures that asserts that they don't complete. 173 /// A matcher for Futures that asserts that they don't complete.
181 /// 174 ///
182 /// This should only be called within a [FakeAsync.run] zone. 175 /// This should only be called within a [FakeAsync.run] zone.
183 Matcher get doesNotComplete => predicate((future) { 176 Matcher get doesNotComplete => predicate((future) {
184 expect(future, new isInstanceOf<Future>('Future')); 177 expect(future, new isInstanceOf<Future>());
185 178
186 var stack = new Trace.current(1); 179 var stack = new Trace.current(1);
187 future.then((_) => handleExternalError( 180 future.then((_) => registerException(
188 new TestFailure("Expected future not to complete."), "", 181 new TestFailure("Expected future not to complete."), stack));
189 stack));
190 return true; 182 return true;
191 }); 183 });
OLDNEW
« no previous file with comments | « pubspec.yaml ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698