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

Side by Side Diff: pkg/http_throttle/test/http_throttle_test.dart

Issue 418103004: Add an http_throttle package. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
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 | « pkg/http_throttle/pubspec.yaml ('k') | sdk/lib/_internal/pub/lib/src/log.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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:http/http.dart' as http;
8 import 'package:http/testing.dart';
9 import 'package:http_throttle/http_throttle.dart';
10 import 'package:unittest/unittest.dart';
11
12 void main() {
13 test("makes requests until the limit is hit", () {
14 var pendingResponses = [];
15 var client = new ThrottleClient(10, new MockClient((request) {
16 var completer = new Completer();
17 pendingResponses.add(completer);
18 return completer.future.then((response) {
19 pendingResponses.remove(completer);
20 return response;
21 });
22 }));
23
24 // Make the first batch of requests. All of these should be sent
25 // immediately.
26 for (var i = 0; i < 10; i++) {
27 client.get('/');
28 }
29
30 return pumpEventQueue().then((_) {
31 // All ten of the requests should have responses pending.
32 expect(pendingResponses, hasLength(10));
33
34 // Make the second batch of requests. None of these should be sent
35 // until the previous batch has finished.
36 for (var i = 0; i < 5; i++) {
37 client.get('/');
38 }
39
40 return pumpEventQueue();
41 }).then((_) {
42 // Only the original ten requests should have responses pending.
43 expect(pendingResponses, hasLength(10));
44
45 // Send the first ten responses, allowing the next batch of requests to
46 // fire.
47 for (var completer in pendingResponses) {
48 completer.complete(new http.Response("done", 200));
49 }
50
51 return pumpEventQueue();
52 }).then((_) {
53 // Now the second batch of responses should be pending.
54 expect(pendingResponses, hasLength(5));
55 });
56 });
57 }
58
59 /// Returns a [Future] that completes after pumping the event queue [times]
60 /// times. By default, this should pump the event queue enough times to allow
61 /// any code to run, as long as it's not waiting on some external event.
62 Future pumpEventQueue([int times = 20]) {
63 if (times == 0) return new Future.value();
64 // We use a delayed future to allow microtask events to finish. The
65 // Future.value or Future() constructors use scheduleMicrotask themselves and
66 // would therefore not wait for microtask callbacks that are scheduled after
67 // invoking this method.
68 return new Future.delayed(Duration.ZERO, () => pumpEventQueue(times - 1));
69 }
OLDNEW
« no previous file with comments | « pkg/http_throttle/pubspec.yaml ('k') | sdk/lib/_internal/pub/lib/src/log.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698