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

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

Issue 13680002: StreamConsumer has an addStream and a close functions. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Update comments. Created 7 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 | Annotate | Revision Log
« no previous file with comments | « tests/lib/async/slow_consumer2_test.dart ('k') | tests/lib/async/slow_consumer_test.dart » ('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) 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=64 5 // VMOptions=--old_gen_heap_size=64
6 6
7 library slow_consumer3_test; 7 library slow_consumer3_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 int receivedCount = 0; 17 int receivedCount = 0;
18 final int bytesPerSecond; 18 final int bytesPerSecond;
19 final int bufferSize; 19 final int bufferSize;
20 final List bufferedData = []; 20 final List bufferedData = [];
21 int usedBufferSize = 0; 21 int usedBufferSize = 0;
22 int finalCount;
22 23
23 SlowConsumer(int this.bytesPerSecond, int this.bufferSize); 24 SlowConsumer(int this.bytesPerSecond, int this.bufferSize);
24 25
25 Future consume(Stream stream) { 26 Future consume(Stream stream) {
27 return addStream(stream).then((_) => close());
28 }
29
30 Future addStream(Stream stream) {
26 Completer result = new Completer(); 31 Completer result = new Completer();
27 var subscription; 32 var subscription;
28 subscription = stream.listen( 33 subscription = stream.listen(
29 (List<int> data) { 34 (List<int> data) {
30 receivedCount += data.length; 35 receivedCount += data.length;
31 usedBufferSize += data.length; 36 usedBufferSize += data.length;
32 bufferedData.add(data); 37 bufferedData.add(data);
33 int currentBufferedDataLength = bufferedData.length; 38 int currentBufferedDataLength = bufferedData.length;
34 if (usedBufferSize > bufferSize) { 39 if (usedBufferSize > bufferSize) {
35 subscription.pause(); 40 subscription.pause();
36 usedBufferSize = 0; 41 usedBufferSize = 0;
37 int ms = data.length * 1000 ~/ bytesPerSecond; 42 int ms = data.length * 1000 ~/ bytesPerSecond;
38 Duration duration = new Duration(milliseconds: ms); 43 Duration duration = new Duration(milliseconds: ms);
39 new Timer(duration, () { 44 new Timer(duration, () {
40 for (int i = 0; i < currentBufferedDataLength; i++) { 45 for (int i = 0; i < currentBufferedDataLength; i++) {
41 bufferedData[i] = null; 46 bufferedData[i] = null;
42 } 47 }
43 subscription.resume(); 48 subscription.resume();
44 }); 49 });
45 } 50 }
46 }, 51 },
47 onDone: () { result.complete(receivedCount); }); 52 onDone: () {
53 finalCount = receivedCount;
54 result.complete(receivedCount);
55 });
48 return result.future; 56 return result.future;
49 } 57 }
58
59 Future close() {
60 return new Future.immediate(finalCount);
61 }
50 } 62 }
51 63
52 Stream<List> dataGenerator(int bytesTotal, int chunkSize) { 64 Stream<List> dataGenerator(int bytesTotal, int chunkSize) {
53 int chunks = bytesTotal ~/ chunkSize; 65 int chunks = bytesTotal ~/ chunkSize;
54 return new Stream.fromIterable(new Iterable.generate(chunks, (_) { 66 return new Stream.fromIterable(new Iterable.generate(chunks, (_) {
55 // This assumes one byte per entry. In practice it will be more. 67 // This assumes one byte per entry. In practice it will be more.
56 return new List<int>(chunkSize); 68 return new List<int>(chunkSize);
57 })); 69 }));
58 } 70 }
59 71
(...skipping 12 matching lines...) Expand all
72 // file). If the consumer doesn't pause the data-provider it will run out of 84 // file). If the consumer doesn't pause the data-provider it will run out of
73 // heap-space. 85 // heap-space.
74 86
75 dataGenerator(100 * MB, 512 * KB) 87 dataGenerator(100 * MB, 512 * KB)
76 .pipe(new SlowConsumer(200 * MB, 3 * MB)) 88 .pipe(new SlowConsumer(200 * MB, 3 * MB))
77 .then((count) { 89 .then((count) {
78 port.close(); 90 port.close();
79 Expect.equals(100 * MB, count); 91 Expect.equals(100 * MB, count);
80 }); 92 });
81 } 93 }
OLDNEW
« no previous file with comments | « tests/lib/async/slow_consumer2_test.dart ('k') | tests/lib/async/slow_consumer_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698