OLD | NEW |
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:test/test.dart'; | 10 import 'package:test/test.dart'; |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 expect(blockedResourceAllocated, isFalse); | 89 expect(blockedResourceAllocated, isFalse); |
90 completer.complete(); | 90 completer.complete(); |
91 return new Future.delayed(new Duration(microseconds: 1)); | 91 return new Future.delayed(new Duration(microseconds: 1)); |
92 }).then((_) { | 92 }).then((_) { |
93 expect(blockedResourceAllocated, isTrue); | 93 expect(blockedResourceAllocated, isTrue); |
94 }); | 94 }); |
95 | 95 |
96 async.elapse(new Duration(seconds: 1)); | 96 async.elapse(new Duration(seconds: 1)); |
97 }); | 97 }); |
98 }); | 98 }); |
| 99 |
| 100 // Regression test for #3. |
| 101 test("can be called immediately before close()", () async { |
| 102 var pool = new Pool(1); |
| 103 pool.withResource(expectAsync(() {})); |
| 104 await pool.close(); |
| 105 }); |
99 }); | 106 }); |
100 | 107 |
101 group("with a timeout", () { | 108 group("with a timeout", () { |
102 test("doesn't time out if there are no pending requests", () { | 109 test("doesn't time out if there are no pending requests", () { |
103 new FakeAsync().run((async) { | 110 new FakeAsync().run((async) { |
104 var pool = new Pool(50, timeout: new Duration(seconds: 5)); | 111 var pool = new Pool(50, timeout: new Duration(seconds: 5)); |
105 for (var i = 0; i < 50; i++) { | 112 for (var i = 0; i < 50; i++) { |
106 expect(pool.request(), completes); | 113 expect(pool.request(), completes); |
107 } | 114 } |
108 | 115 |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 | 272 |
266 resource.allowRelease(expectAsync(() { | 273 resource.allowRelease(expectAsync(() { |
267 expect(Zone.current, equals(innerZone)); | 274 expect(Zone.current, equals(innerZone)); |
268 })); | 275 })); |
269 }); | 276 }); |
270 | 277 |
271 pool.request(); | 278 pool.request(); |
272 }); | 279 }); |
273 }); | 280 }); |
274 | 281 |
| 282 test("done doesn't complete without close", () async { |
| 283 var pool = new Pool(1); |
| 284 pool.done.then(expectAsync1((_) {}, count: 0)); |
| 285 |
| 286 var resource = await pool.request(); |
| 287 resource.release(); |
| 288 |
| 289 await new Future.delayed(Duration.ZERO); |
| 290 }); |
| 291 |
275 group("close()", () { | 292 group("close()", () { |
276 test("disallows request() and withResource()", () { | 293 test("disallows request() and withResource()", () { |
277 var pool = new Pool(1)..close(); | 294 var pool = new Pool(1)..close(); |
278 expect(pool.request, throwsStateError); | 295 expect(pool.request, throwsStateError); |
279 expect(() => pool.withResource(() {}), throwsStateError); | 296 expect(() => pool.withResource(() {}), throwsStateError); |
280 }); | 297 }); |
281 | 298 |
282 test("pending requests are fulfilled", () async { | 299 test("pending requests are fulfilled", () async { |
283 var pool = new Pool(1); | 300 var pool = new Pool(1); |
284 var resource1 = await pool.request(); | 301 var resource1 = await pool.request(); |
285 expect(pool.request().then((resource2) { | 302 expect(pool.request().then((resource2) { |
286 resource2.release(); | 303 resource2.release(); |
287 }), completes); | 304 }), completes); |
| 305 expect(pool.done, completes); |
288 expect(pool.close(), completes); | 306 expect(pool.close(), completes); |
289 resource1.release(); | 307 resource1.release(); |
290 }); | 308 }); |
291 | 309 |
292 test("pending requests are fulfilled with allowRelease", () async { | 310 test("pending requests are fulfilled with allowRelease", () async { |
293 var pool = new Pool(1); | 311 var pool = new Pool(1); |
294 var resource1 = await pool.request(); | 312 var resource1 = await pool.request(); |
295 | 313 |
296 var completer = new Completer(); | 314 var completer = new Completer(); |
297 expect(pool.request().then((resource2) { | 315 expect(pool.request().then((resource2) { |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 completer.complete(); | 409 completer.complete(); |
392 }); | 410 }); |
393 | 411 |
394 test("an onRelease error is piped to the return value", () async { | 412 test("an onRelease error is piped to the return value", () async { |
395 var pool = new Pool(1); | 413 var pool = new Pool(1); |
396 var resource = await pool.request(); | 414 var resource = await pool.request(); |
397 | 415 |
398 var completer = new Completer(); | 416 var completer = new Completer(); |
399 resource.allowRelease(() => completer.future); | 417 resource.allowRelease(() => completer.future); |
400 | 418 |
| 419 expect(pool.done, throwsA("oh no!")); |
401 expect(pool.close(), throwsA("oh no!")); | 420 expect(pool.close(), throwsA("oh no!")); |
402 | 421 |
403 await new Future.delayed(Duration.ZERO); | 422 await new Future.delayed(Duration.ZERO); |
404 completer.completeError("oh no!"); | 423 completer.completeError("oh no!"); |
405 }); | 424 }); |
406 }); | 425 }); |
407 } | 426 } |
408 | 427 |
409 /// Returns a function that will cause the test to fail if it's called. | 428 /// Returns a function that will cause the test to fail if it's called. |
410 /// | 429 /// |
411 /// This should only be called within a [FakeAsync.run] zone. | 430 /// This should only be called within a [FakeAsync.run] zone. |
412 Function expectNoAsync() { | 431 Function expectNoAsync() { |
413 var stack = new Trace.current(1); | 432 var stack = new Trace.current(1); |
414 return () => registerException( | 433 return () => registerException( |
415 new TestFailure("Expected function not to be called."), stack); | 434 new TestFailure("Expected function not to be called."), stack); |
416 } | 435 } |
417 | 436 |
418 /// A matcher for Futures that asserts that they don't complete. | 437 /// A matcher for Futures that asserts that they don't complete. |
419 /// | 438 /// |
420 /// This should only be called within a [FakeAsync.run] zone. | 439 /// This should only be called within a [FakeAsync.run] zone. |
421 Matcher get doesNotComplete => predicate((future) { | 440 Matcher get doesNotComplete => predicate((future) { |
422 expect(future, new isInstanceOf<Future>()); | 441 expect(future, new isInstanceOf<Future>()); |
423 | 442 |
424 var stack = new Trace.current(1); | 443 var stack = new Trace.current(1); |
425 future.then((_) => registerException( | 444 future.then((_) => registerException( |
426 new TestFailure("Expected future not to complete."), stack)); | 445 new TestFailure("Expected future not to complete."), stack)); |
427 return true; | 446 return true; |
428 }); | 447 }); |
OLD | NEW |