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

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

Issue 12319010: Do not allow streams to send events during state-change callbacks. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: More changes to Stream. Created 7 years, 10 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
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 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 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 // VMOptions=--old_gen_heap_size=32 5 // VMOptions=--old_gen_heap_size=32
6 6
7 library slow_consumer_test; 7 library slow_consumer_test;
8 8
9 import 'dart:async'; 9 import 'dart:async';
10 import 'dart:isolate'; 10 import 'dart:isolate';
11 11
12 const int KB = 1024; 12 const int KB = 1024;
13 const int MB = KB * KB; 13 const int MB = KB * KB;
14 const int GB = KB * KB * KB; 14 const int GB = KB * KB * KB;
15 15
16 class SlowConsumer extends StreamConsumer { 16 class SlowConsumer extends StreamConsumer {
17 var current = new Future.immediate(0); 17 var current = new Future.immediate(0);
18 final int bytesPerSecond; 18 final int bytesPerSecond;
19 19
20 SlowConsumer(int this.bytesPerSecond); 20 SlowConsumer(int this.bytesPerSecond);
21 21
22 Future consume(Stream stream) { 22 Future consume(Stream stream) {
23 bool done = false;
23 Completer completer = new Completer(); 24 Completer completer = new Completer();
24 var subscription; 25 var subscription;
25 subscription = stream.listen( 26 subscription = stream.listen(
26 (List<int> data) { 27 (List<int> data) {
27 current = current 28 current = current
28 .then((count) { 29 .then((count) {
29 // Simulated amount of time it takes to handle the data. 30 // Simulated amount of time it takes to handle the data.
30 int ms = data.length * 1000 ~/ bytesPerSecond; 31 int ms = data.length * 1000 ~/ bytesPerSecond;
31 Duration duration = new Duration(milliseconds: ms); 32 Duration duration = new Duration(milliseconds: ms);
32 subscription.pause(); 33 if (!done) subscription.pause();
33 return new Future.delayed(duration, () { 34 return new Future.delayed(duration, () {
34 subscription.resume(); 35 if (!done) subscription.resume();
35 // Make sure we use data here to keep tracking it. 36 // Make sure we use data here to keep tracking it.
36 return count + data.length; 37 return count + data.length;
37 }); 38 });
38 }); 39 });
39 }, 40 },
40 onDone: () { current.then((count) { completer.complete(count); }); }); 41 onDone: () {
42 done = true;
43 current.then((count) { completer.complete(count); });
44 });
41 return completer.future; 45 return completer.future;
42 } 46 }
43 } 47 }
44 48
45 class DataProvider { 49 class DataProvider {
46 final int chunkSize; 50 final int chunkSize;
47 final int bytesPerSecond; 51 final int bytesPerSecond;
48 int sentCount = 0; 52 int sentCount = 0;
49 int targetCount; 53 int targetCount;
50 StreamController controller; 54 StreamController controller;
55 Timer pendingSend;
51 56
52 DataProvider(int this.bytesPerSecond, int this.targetCount, this.chunkSize) { 57 DataProvider(int this.bytesPerSecond, int this.targetCount, this.chunkSize) {
53 controller = new StreamController(onPauseStateChange: onPauseStateChange); 58 controller = new StreamController(onPauseStateChange: onPauseStateChange);
54 Timer.run(send); 59 Timer.run(send);
55 } 60 }
56 61
57 Stream get stream => controller.stream; 62 Stream get stream => controller.stream;
58
Anders Johnsen 2013/02/20 12:42:02 Reintroduce this space.
59 send() { 63 send() {
64 if (pendingSend != null) {
65 pendingSend.cancel();
66 pendingSend = null;
67 }
60 if (controller.isPaused) return; 68 if (controller.isPaused) return;
61 if (sentCount == targetCount) { 69 if (sentCount == targetCount) {
62 controller.close(); 70 controller.close();
63 return; 71 return;
64 } 72 }
65 int listSize = chunkSize; 73 int listSize = chunkSize;
66 sentCount += listSize; 74 sentCount += listSize;
67 if (sentCount > targetCount) { 75 if (sentCount > targetCount) {
68 listSize -= sentCount - targetCount; 76 listSize -= sentCount - targetCount;
69 sentCount = targetCount; 77 sentCount = targetCount;
70 } 78 }
71 controller.add(new List.fixedLength(listSize)); 79 controller.add(new List.fixedLength(listSize));
72 int ms = listSize * 1000 ~/ bytesPerSecond; 80 int ms = listSize * 1000 ~/ bytesPerSecond;
73 Duration duration = new Duration(milliseconds: ms); 81 Duration duration = new Duration(milliseconds: ms);
74 if (!controller.isPaused) new Timer(duration, send); 82 if (!controller.isPaused) {
83 pendingSend = new Timer(duration, send);
84 }
75 } 85 }
76 86
77 onPauseStateChange() { 87 onPauseStateChange() {
78 // We don't care if we just unpaused or paused. In either case we just 88 // We don't care if we just unpaused or paused. In either case we just
79 // call send which will test it for us. 89 // call send which will test it for us.
80 send(); 90 send();
81 } 91 }
82 } 92 }
83 93
84 main() { 94 main() {
85 var port = new ReceivePort(); 95 var port = new ReceivePort();
86 // The data provider can deliver 800MB/s of data. It sends 100MB of data to 96 // The data provider can deliver 800MB/s of data. It sends 100MB of data to
87 // the slower consumer who can only read 200MB/s. The data is sent in 1MB 97 // the slower consumer who can only read 200MB/s. The data is sent in 1MB
88 // chunks. 98 // chunks.
89 // 99 //
90 // This test is limited to 32MB of heap-space (see VMOptions on top of the 100 // This test is limited to 32MB of heap-space (see VMOptions on top of the
91 // file). If the consumer doesn't pause the data-provider it will run out of 101 // file). If the consumer doesn't pause the data-provider it will run out of
92 // heap-space. 102 // heap-space.
93 103
94 new DataProvider(800 * MB, 100 * MB, 1 * MB).stream 104 new DataProvider(800 * MB, 100 * MB, 1 * MB).stream
95 .pipe(new SlowConsumer(200 * MB)) 105 .pipe(new SlowConsumer(200 * MB))
96 .then((count) { 106 .then((count) {
97 port.close(); 107 port.close();
98 Expect.equals(100 * MB, count); 108 Expect.equals(100 * MB, count);
99 }); 109 });
100 } 110 }
OLDNEW
« sdk/lib/async/stream_impl.dart ('K') | « sdk/lib/async/stream_impl.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698