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

Unified Diff: dart/pkg/pool/test/pool_test.dart

Issue 419683002: Revert r38549, r38552, and r38557. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 5 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « dart/pkg/pool/pubspec.yaml ('k') | dart/third_party/pkg/.gitignore » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: dart/pkg/pool/test/pool_test.dart
diff --git a/dart/pkg/pool/test/pool_test.dart b/dart/pkg/pool/test/pool_test.dart
index bb9e607681fd40bdd29e802df9ff190bdc6562a3..bfeb876b19efaf6f259cc2c33228f6ff76b6d78e 100644
--- a/dart/pkg/pool/test/pool_test.dart
+++ b/dart/pkg/pool/test/pool_test.dart
@@ -4,7 +4,6 @@
import 'dart:async';
-import 'package:fake_async/fake_async.dart';
import 'package:pool/pool.dart';
import 'package:stack_trace/stack_trace.dart';
import 'package:unittest/unittest.dart';
@@ -20,36 +19,30 @@ void main() {
});
test("resources block past the limit", () {
- new FakeAsync().run((async) {
- var pool = new Pool(50);
- var requests = [];
- for (var i = 0; i < 50; i++) {
- expect(pool.request(), completes);
- }
- expect(pool.request(), doesNotComplete);
-
- async.elapse(new Duration(seconds: 1));
- });
+ var pool = new Pool(50);
+ var requests = [];
+ for (var i = 0; i < 50; i++) {
+ expect(pool.request(), completes);
+ }
+ expect(pool.request(), doesNotComplete);
});
test("a blocked resource is allocated when another is released", () {
- new FakeAsync().run((async) {
- var pool = new Pool(50);
- var requests = [];
- for (var i = 0; i < 49; i++) {
- expect(pool.request(), completes);
- }
-
- pool.request().then((lastAllocatedResource) {
- // This will only complete once [lastAllocatedResource] is released.
- expect(pool.request(), completes);
-
- new Future.delayed(new Duration(microseconds: 1)).then((_) {
- lastAllocatedResource.release();
- });
- });
+ var pool = new Pool(50);
+ var requests = [];
+ for (var i = 0; i < 49; i++) {
+ expect(pool.request(), completes);
+ }
+
+ return pool.request().then((lastAllocatedResource) {
+ var blockedResource = pool.request();
- async.elapse(new Duration(seconds: 1));
+ return pumpEventQueue().then((_) {
+ lastAllocatedResource.release();
+ return pumpEventQueue();
+ }).then((_) {
+ expect(blockedResource, completes);
+ });
});
});
});
@@ -64,113 +57,63 @@ void main() {
});
test("blocks the callback past the limit", () {
- new FakeAsync().run((async) {
- var pool = new Pool(50);
- var requests = [];
- for (var i = 0; i < 50; i++) {
- pool.withResource(expectAsync(() => new Completer().future));
- }
- pool.withResource(expectNoAsync());
-
- async.elapse(new Duration(seconds: 1));
- });
+ var pool = new Pool(50);
+ var requests = [];
+ for (var i = 0; i < 50; i++) {
+ pool.withResource(expectAsync(() => new Completer().future));
+ }
+ pool.withResource(expectNoAsync());
});
test("a blocked resource is allocated when another is released", () {
- new FakeAsync().run((async) {
- var pool = new Pool(50);
- var requests = [];
- for (var i = 0; i < 49; i++) {
- pool.withResource(expectAsync(() => new Completer().future));
- }
-
- var completer = new Completer();
- var lastAllocatedResource = pool.withResource(() => completer.future);
- var blockedResourceAllocated = false;
- var blockedResource = pool.withResource(() {
- blockedResourceAllocated = true;
- });
-
- new Future.delayed(new Duration(microseconds: 1)).then((_) {
- expect(blockedResourceAllocated, isFalse);
- completer.complete();
- return new Future.delayed(new Duration(microseconds: 1));
- }).then((_) {
- expect(blockedResourceAllocated, isTrue);
- });
+ var pool = new Pool(50);
+ var requests = [];
+ for (var i = 0; i < 49; i++) {
+ pool.withResource(expectAsync(() => new Completer().future));
+ }
- async.elapse(new Duration(seconds: 1));
+ var completer = new Completer();
+ var lastAllocatedResource = pool.withResource(() => completer.future);
+ var blockedResourceAllocated = false;
+ var blockedResource = pool.withResource(() {
+ blockedResourceAllocated = true;
});
- });
- });
-
- group("with a timeout", () {
- test("doesn't time out if there are no pending requests", () {
- new FakeAsync().run((async) {
- var pool = new Pool(50, timeout: new Duration(seconds: 5));
- for (var i = 0; i < 50; i++) {
- expect(pool.request(), completes);
- }
- async.elapse(new Duration(seconds: 6));
+ return pumpEventQueue().then((_) {
+ expect(blockedResourceAllocated, isFalse);
+ completer.complete();
+ return pumpEventQueue();
+ }).then((_) {
+ expect(blockedResourceAllocated, isTrue);
});
});
+ });
- test("resets the timer if a resource is returned", () {
- new FakeAsync().run((async) {
- var pool = new Pool(50, timeout: new Duration(seconds: 5));
- for (var i = 0; i < 49; i++) {
- expect(pool.request(), completes);
- }
-
- pool.request().then((lastAllocatedResource) {
- // This will only complete once [lastAllocatedResource] is released.
- expect(pool.request(), completes);
-
- new Future.delayed(new Duration(seconds: 3)).then((_) {
- lastAllocatedResource.release();
- expect(pool.request(), doesNotComplete);
- });
- });
-
- async.elapse(new Duration(seconds: 6));
- });
- });
-
- test("resets the timer if a resource is requested", () {
- new FakeAsync().run((async) {
- var pool = new Pool(50, timeout: new Duration(seconds: 5));
- for (var i = 0; i < 50; i++) {
- expect(pool.request(), completes);
- }
- expect(pool.request(), doesNotComplete);
-
- new Future.delayed(new Duration(seconds: 3)).then((_) {
- expect(pool.request(), doesNotComplete);
- });
-
- async.elapse(new Duration(seconds: 6));
- });
- });
-
- test("times out if nothing happens", () {
- new FakeAsync().run((async) {
- var pool = new Pool(50, timeout: new Duration(seconds: 5));
- for (var i = 0; i < 50; i++) {
- expect(pool.request(), completes);
- }
- expect(pool.request(), throwsA(new isInstanceOf<TimeoutException>()));
+ // TODO(nweiz): Test timeouts when it's easier to use third-party packages.
+ // See r38552.
+}
- async.elapse(new Duration(seconds: 6));
- });
- });
- });
+/// Returns a [Future] that completes after pumping the event queue [times]
+/// times. By default, this should pump the event queue enough times to allow
+/// any code to run, as long as it's not waiting on some external event.
+Future pumpEventQueue([int times = 20]) {
+ if (times == 0) return new Future.value();
+ // We use a delayed future to allow microtask events to finish. The
+ // Future.value or Future() constructors use scheduleMicrotask themselves and
+ // would therefore not wait for microtask callbacks that are scheduled after
+ // invoking this method.
+ return new Future.delayed(Duration.ZERO, () => pumpEventQueue(times - 1));
}
/// Returns a function that will cause the test to fail if it's called.
///
-/// This should only be called within a [FakeAsync.run] zone.
+/// This won't let the test complete until it's confident that the function
+/// won't be called.
Function expectNoAsync() {
+ // Make sure the test lasts long enough for the function to get called if it's
+ // going to get called.
+ expect(pumpEventQueue(), completes);
+
var stack = new Trace.current(1);
return () => handleExternalError(
new TestFailure("Expected function not to be called."), "",
@@ -179,9 +122,13 @@ Function expectNoAsync() {
/// A matcher for Futures that asserts that they don't complete.
///
-/// This should only be called within a [FakeAsync.run] zone.
+/// This won't let the test complete until it's confident that the function
+/// won't be called.
Matcher get doesNotComplete => predicate((future) {
expect(future, new isInstanceOf<Future>('Future'));
+ // Make sure the test lasts long enough for the function to get called if it's
+ // going to get called.
+ expect(pumpEventQueue(), completes);
var stack = new Trace.current(1);
future.then((_) => handleExternalError(
« no previous file with comments | « dart/pkg/pool/pubspec.yaml ('k') | dart/third_party/pkg/.gitignore » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698