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

Side by Side Diff: tests/lib_strong/async/slow_consumer_test.dart

Issue 3001793002: Migrated test block 171 to Dart 2.0. (Closed)
Patch Set: Addressed Bob's comment Created 3 years, 3 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
OLDNEW
(Empty)
1 // Copyright (c) 2012, 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 // VMOptions=--old_gen_heap_size=64
6
7 library slow_consumer_test;
8
9 import 'package:async_helper/async_helper.dart';
10 import "package:expect/expect.dart";
11 import 'dart:async';
12
13 const int KB = 1024;
14 const int MB = KB * KB;
15 const int GB = KB * KB * KB;
16
17 class SlowConsumer extends StreamConsumer {
18 var current = new Future.value(0);
19 final int bytesPerSecond;
20 int finalCount;
21
22 SlowConsumer(int this.bytesPerSecond);
23
24 Future consume(Stream stream) {
25 return addStream(stream).then((_) => close());
26 }
27
28 Future addStream(Stream stream) {
29 bool done = false;
30 Completer completer = new Completer();
31 var subscription;
32 subscription = stream.listen((List<int> data) {
33 current = current.then((count) {
34 // Simulated amount of time it takes to handle the data.
35 int ms = data.length * 1000 ~/ bytesPerSecond;
36 Duration duration = new Duration(milliseconds: ms);
37 if (!done) subscription.pause();
38 return new Future.delayed(duration, () {
39 if (!done) subscription.resume();
40 // Make sure we use data here to keep tracking it.
41 return count + data.length;
42 });
43 });
44 }, onDone: () {
45 done = true;
46 current.then((count) {
47 finalCount = count;
48 completer.complete(count);
49 });
50 });
51 return completer.future;
52 }
53
54 Future close() {
55 return new Future.value(finalCount);
56 }
57 }
58
59 class DataProvider {
60 final int chunkSize;
61 final int bytesPerSecond;
62 int sentCount = 0;
63 int targetCount;
64 StreamController controller;
65 Timer pendingSend;
66
67 DataProvider(int this.bytesPerSecond, int this.targetCount, this.chunkSize) {
68 controller = new StreamController(
69 sync: true, onPause: onPauseStateChange, onResume: onPauseStateChange);
70 Timer.run(send);
71 }
72
73 Stream get stream => controller.stream;
74
75 send() {
76 if (pendingSend != null) {
77 pendingSend.cancel();
78 pendingSend = null;
79 }
80 if (controller.isPaused) return;
81 if (sentCount == targetCount) {
82 controller.close();
83 return;
84 }
85 int listSize = chunkSize;
86 sentCount += listSize;
87 if (sentCount > targetCount) {
88 listSize -= sentCount - targetCount;
89 sentCount = targetCount;
90 }
91 controller.add(new List(listSize));
92 int ms = listSize * 1000 ~/ bytesPerSecond;
93 Duration duration = new Duration(milliseconds: ms);
94 if (!controller.isPaused) {
95 pendingSend = new Timer(duration, send);
96 }
97 }
98
99 onPauseStateChange() {
100 // We don't care if we just unpaused or paused. In either case we just
101 // call send which will test it for us.
102 send();
103 }
104 }
105
106 main() {
107 asyncStart();
108 // The data provider can deliver 800MB/s of data. It sends 100MB of data to
109 // the slower consumer who can only read 200MB/s. The data is sent in 1MB
110 // chunks.
111 //
112 // This test is limited to 64MB of heap-space (see VMOptions on top of the
113 // file). If the consumer doesn't pause the data-provider it will run out of
114 // heap-space.
115
116 new DataProvider(800 * MB, 100 * MB, 1 * MB)
117 .stream
118 .pipe(new SlowConsumer(200 * MB))
119 .then((count) {
120 Expect.equals(100 * MB, count);
121 asyncEnd();
122 });
123 }
OLDNEW
« no previous file with comments | « tests/lib_strong/async/slow_consumer3_test.dart ('k') | tests/lib_strong/async/stack_trace01_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698