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

Side by Side 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, 4 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 | Annotate | Revision Log
« no previous file with comments | « dart/pkg/pool/pubspec.yaml ('k') | dart/third_party/pkg/.gitignore » ('j') | 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';
8 import 'package:pool/pool.dart'; 7 import 'package:pool/pool.dart';
9 import 'package:stack_trace/stack_trace.dart'; 8 import 'package:stack_trace/stack_trace.dart';
10 import 'package:unittest/unittest.dart'; 9 import 'package:unittest/unittest.dart';
11 10
12 void main() { 11 void main() {
13 group("request()", () { 12 group("request()", () {
14 test("resources can be requested freely up to the limit", () { 13 test("resources can be requested freely up to the limit", () {
15 var pool = new Pool(50); 14 var pool = new Pool(50);
16 var requests = []; 15 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 var pool = new Pool(50);
24 var pool = new Pool(50); 23 var requests = [];
25 var requests = []; 24 for (var i = 0; i < 50; i++) {
26 for (var i = 0; i < 50; i++) { 25 expect(pool.request(), completes);
27 expect(pool.request(), completes); 26 }
28 } 27 expect(pool.request(), doesNotComplete);
29 expect(pool.request(), doesNotComplete);
30
31 async.elapse(new Duration(seconds: 1));
32 });
33 }); 28 });
34 29
35 test("a blocked resource is allocated when another is released", () { 30 test("a blocked resource is allocated when another is released", () {
36 new FakeAsync().run((async) { 31 var pool = new Pool(50);
37 var pool = new Pool(50); 32 var requests = [];
38 var requests = []; 33 for (var i = 0; i < 49; i++) {
39 for (var i = 0; i < 49; i++) { 34 expect(pool.request(), completes);
40 expect(pool.request(), completes); 35 }
41 }
42 36
43 pool.request().then((lastAllocatedResource) { 37 return pool.request().then((lastAllocatedResource) {
44 // This will only complete once [lastAllocatedResource] is released. 38 var blockedResource = pool.request();
45 expect(pool.request(), completes);
46 39
47 new Future.delayed(new Duration(microseconds: 1)).then((_) { 40 return pumpEventQueue().then((_) {
48 lastAllocatedResource.release(); 41 lastAllocatedResource.release();
49 }); 42 return pumpEventQueue();
43 }).then((_) {
44 expect(blockedResource, completes);
50 }); 45 });
51
52 async.elapse(new Duration(seconds: 1));
53 }); 46 });
54 }); 47 });
55 }); 48 });
56 49
57 group("withResource()", () { 50 group("withResource()", () {
58 test("can be called freely up to the limit", () { 51 test("can be called freely up to the limit", () {
59 var pool = new Pool(50); 52 var pool = new Pool(50);
60 var requests = []; 53 var requests = [];
61 for (var i = 0; i < 50; i++) { 54 for (var i = 0; i < 50; i++) {
62 pool.withResource(expectAsync(() => new Completer().future)); 55 pool.withResource(expectAsync(() => new Completer().future));
63 } 56 }
64 }); 57 });
65 58
66 test("blocks the callback past the limit", () { 59 test("blocks the callback past the limit", () {
67 new FakeAsync().run((async) { 60 var pool = new Pool(50);
68 var pool = new Pool(50); 61 var requests = [];
69 var requests = []; 62 for (var i = 0; i < 50; i++) {
70 for (var i = 0; i < 50; i++) { 63 pool.withResource(expectAsync(() => new Completer().future));
71 pool.withResource(expectAsync(() => new Completer().future)); 64 }
72 } 65 pool.withResource(expectNoAsync());
73 pool.withResource(expectNoAsync());
74
75 async.elapse(new Duration(seconds: 1));
76 });
77 }); 66 });
78 67
79 test("a blocked resource is allocated when another is released", () { 68 test("a blocked resource is allocated when another is released", () {
80 new FakeAsync().run((async) { 69 var pool = new Pool(50);
81 var pool = new Pool(50); 70 var requests = [];
82 var requests = []; 71 for (var i = 0; i < 49; i++) {
83 for (var i = 0; i < 49; i++) { 72 pool.withResource(expectAsync(() => new Completer().future));
84 pool.withResource(expectAsync(() => new Completer().future)); 73 }
85 }
86 74
87 var completer = new Completer(); 75 var completer = new Completer();
88 var lastAllocatedResource = pool.withResource(() => completer.future); 76 var lastAllocatedResource = pool.withResource(() => completer.future);
89 var blockedResourceAllocated = false; 77 var blockedResourceAllocated = false;
90 var blockedResource = pool.withResource(() { 78 var blockedResource = pool.withResource(() {
91 blockedResourceAllocated = true; 79 blockedResourceAllocated = true;
92 }); 80 });
93 81
94 new Future.delayed(new Duration(microseconds: 1)).then((_) { 82 return pumpEventQueue().then((_) {
95 expect(blockedResourceAllocated, isFalse); 83 expect(blockedResourceAllocated, isFalse);
96 completer.complete(); 84 completer.complete();
97 return new Future.delayed(new Duration(microseconds: 1)); 85 return pumpEventQueue();
98 }).then((_) { 86 }).then((_) {
99 expect(blockedResourceAllocated, isTrue); 87 expect(blockedResourceAllocated, isTrue);
100 });
101
102 async.elapse(new Duration(seconds: 1));
103 }); 88 });
104 }); 89 });
105 }); 90 });
106 91
107 group("with a timeout", () { 92 // TODO(nweiz): Test timeouts when it's easier to use third-party packages.
108 test("doesn't time out if there are no pending requests", () { 93 // See r38552.
109 new FakeAsync().run((async) { 94 }
110 var pool = new Pool(50, timeout: new Duration(seconds: 5));
111 for (var i = 0; i < 50; i++) {
112 expect(pool.request(), completes);
113 }
114 95
115 async.elapse(new Duration(seconds: 6)); 96 /// Returns a [Future] that completes after pumping the event queue [times]
116 }); 97 /// times. By default, this should pump the event queue enough times to allow
117 }); 98 /// any code to run, as long as it's not waiting on some external event.
118 99 Future pumpEventQueue([int times = 20]) {
119 test("resets the timer if a resource is returned", () { 100 if (times == 0) return new Future.value();
120 new FakeAsync().run((async) { 101 // We use a delayed future to allow microtask events to finish. The
121 var pool = new Pool(50, timeout: new Duration(seconds: 5)); 102 // Future.value or Future() constructors use scheduleMicrotask themselves and
122 for (var i = 0; i < 49; i++) { 103 // would therefore not wait for microtask callbacks that are scheduled after
123 expect(pool.request(), completes); 104 // invoking this method.
124 } 105 return new Future.delayed(Duration.ZERO, () => pumpEventQueue(times - 1));
125
126 pool.request().then((lastAllocatedResource) {
127 // This will only complete once [lastAllocatedResource] is released.
128 expect(pool.request(), completes);
129
130 new Future.delayed(new Duration(seconds: 3)).then((_) {
131 lastAllocatedResource.release();
132 expect(pool.request(), doesNotComplete);
133 });
134 });
135
136 async.elapse(new Duration(seconds: 6));
137 });
138 });
139
140 test("resets the timer if a resource is requested", () {
141 new FakeAsync().run((async) {
142 var pool = new Pool(50, timeout: new Duration(seconds: 5));
143 for (var i = 0; i < 50; i++) {
144 expect(pool.request(), completes);
145 }
146 expect(pool.request(), doesNotComplete);
147
148 new Future.delayed(new Duration(seconds: 3)).then((_) {
149 expect(pool.request(), doesNotComplete);
150 });
151
152 async.elapse(new Duration(seconds: 6));
153 });
154 });
155
156 test("times out if nothing happens", () {
157 new FakeAsync().run((async) {
158 var pool = new Pool(50, timeout: new Duration(seconds: 5));
159 for (var i = 0; i < 50; i++) {
160 expect(pool.request(), completes);
161 }
162 expect(pool.request(), throwsA(new isInstanceOf<TimeoutException>()));
163
164 async.elapse(new Duration(seconds: 6));
165 });
166 });
167 });
168 } 106 }
169 107
170 /// Returns a function that will cause the test to fail if it's called. 108 /// Returns a function that will cause the test to fail if it's called.
171 /// 109 ///
172 /// This should only be called within a [FakeAsync.run] zone. 110 /// This won't let the test complete until it's confident that the function
111 /// won't be called.
173 Function expectNoAsync() { 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
174 var stack = new Trace.current(1); 117 var stack = new Trace.current(1);
175 return () => handleExternalError( 118 return () => handleExternalError(
176 new TestFailure("Expected function not to be called."), "", 119 new TestFailure("Expected function not to be called."), "",
177 stack); 120 stack);
178 } 121 }
179 122
180 /// A matcher for Futures that asserts that they don't complete. 123 /// A matcher for Futures that asserts that they don't complete.
181 /// 124 ///
182 /// This should only be called within a [FakeAsync.run] zone. 125 /// This won't let the test complete until it's confident that the function
126 /// won't be called.
183 Matcher get doesNotComplete => predicate((future) { 127 Matcher get doesNotComplete => predicate((future) {
184 expect(future, new isInstanceOf<Future>('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);
185 132
186 var stack = new Trace.current(1); 133 var stack = new Trace.current(1);
187 future.then((_) => handleExternalError( 134 future.then((_) => handleExternalError(
188 new TestFailure("Expected future not to complete."), "", 135 new TestFailure("Expected future not to complete."), "",
189 stack)); 136 stack));
190 return true; 137 return true;
191 }); 138 });
OLDNEW
« 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