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

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

Issue 2802973005: Migrate async tests to strong (Closed)
Patch Set: Created 3 years, 8 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(
33 (List<int> data) {
34 current = current
35 .then((count) {
36 // Simulated amount of time it takes to handle the data.
37 int ms = data.length * 1000 ~/ bytesPerSecond;
38 Duration duration = new Duration(milliseconds: ms);
39 if (!done) subscription.pause();
40 return new Future.delayed(duration, () {
41 if (!done) subscription.resume();
42 // Make sure we use data here to keep tracking it.
43 return count + data.length;
44 });
45 });
46 },
47 onDone: () {
48 done = true;
49 current.then((count) {
50 finalCount = count;
51 completer.complete(count);
52 });
53 });
54 return completer.future;
55 }
56
57 Future close() {
58 return new Future.value(finalCount);
59 }
60 }
61
62 class DataProvider {
63 final int chunkSize;
64 final int bytesPerSecond;
65 int sentCount = 0;
66 int targetCount;
67 StreamController controller;
68 Timer pendingSend;
69
70 DataProvider(int this.bytesPerSecond, int this.targetCount, this.chunkSize) {
71 controller = new StreamController(
72 sync: true,
73 onPause: onPauseStateChange,
74 onResume: onPauseStateChange);
75 Timer.run(send);
76 }
77
78 Stream get stream => controller.stream;
79
80 send() {
81 if (pendingSend != null) {
82 pendingSend.cancel();
83 pendingSend = null;
84 }
85 if (controller.isPaused) return;
86 if (sentCount == targetCount) {
87 controller.close();
88 return;
89 }
90 int listSize = chunkSize;
91 sentCount += listSize;
92 if (sentCount > targetCount) {
93 listSize -= sentCount - targetCount;
94 sentCount = targetCount;
95 }
96 controller.add(new List(listSize));
97 int ms = listSize * 1000 ~/ bytesPerSecond;
98 Duration duration = new Duration(milliseconds: ms);
99 if (!controller.isPaused) {
100 pendingSend = new Timer(duration, send);
101 }
102 }
103
104 onPauseStateChange() {
105 // We don't care if we just unpaused or paused. In either case we just
106 // call send which will test it for us.
107 send();
108 }
109 }
110
111 main() {
112 asyncStart();
113 // The data provider can deliver 800MB/s of data. It sends 100MB of data to
114 // the slower consumer who can only read 200MB/s. The data is sent in 1MB
115 // chunks.
116 //
117 // This test is limited to 64MB of heap-space (see VMOptions on top of the
118 // file). If the consumer doesn't pause the data-provider it will run out of
119 // heap-space.
120
121 new DataProvider(800 * MB, 100 * MB, 1 * MB).stream
122 .pipe(new SlowConsumer(200 * MB))
123 .then((count) {
124 Expect.equals(100 * MB, count);
125 asyncEnd();
126 });
127 }
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