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

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

Issue 11794044: Add Stream.fromIterable (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Fix bug hit by new test. Created 7 years, 11 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_consumer2_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;
(...skipping 23 matching lines...) Expand all
41 } 41 }
42 subscription.resume(); 42 subscription.resume();
43 }); 43 });
44 } 44 }
45 }, 45 },
46 onDone: () { result.complete(receivedCount); }); 46 onDone: () { result.complete(receivedCount); });
47 return result.future; 47 return result.future;
48 } 48 }
49 } 49 }
50 50
51 class DataProvider extends StreamController { 51 Stream<List> dataGenerator(int bytesTotal, int chunkSize) {
52 final int chunkSize; 52 int chunks = bytesTotal ~/ chunkSize;
53 final int bytesPerSecond; 53 return new Stream.fromIterable(new Iterable.generate(chunks, (_) {
54 int sentCount = 0; 54 // This assumes one byte per entry. In practice it will be more.
55 int targetCount; 55 return new List<int>.fixedLength(chunkSize);
56 56 }));
57 DataProvider(int this.bytesPerSecond, int this.targetCount, this.chunkSize) {
58 new Timer(0, (_) => send());
59 }
60
61 send() {
62 if (isPaused) return;
63 if (sentCount == targetCount) {
64 close();
65 return;
66 }
67 int listSize = chunkSize;
68 sentCount += listSize;
69 if (sentCount > targetCount) {
70 listSize -= sentCount - targetCount;
71 sentCount = targetCount;
72 }
73 add(new List.fixedLength(listSize));
74 int ms = listSize * 1000 ~/ bytesPerSecond;
75 if (!isPaused) new Timer(ms, (_) => send());
76 }
77
78 onPauseStateChange() {
79 // We don't care if we just unpaused or paused. In either case we just
80 // call send which will test it for us.
81 send();
82 }
83 } 57 }
84 58
85 main() { 59 main() {
86 var port = new ReceivePort(); 60 var port = new ReceivePort();
87 // The data provider can deliver 800MB/s of data. It sends 100MB of data to 61 // The data provider can deliver 800MBs of data as fast as it is
88 // the slower consumer who can only read 200MB/s. The data is sent in 1MB 62 // requested. The data is sent in 1MB chunks. The consumer has a buffer of
89 // chunks. The consumer has a buffer of 5MB. That is, it can accept a few 63 // 5MB. That is, it can accept a few packages without pausing its input.
90 // packages without pausing its input.
91 // 64 //
92 // This test is limited to 32MB of heap-space (see VMOptions on top of the 65 // This test is limited to 32MB of heap-space (see VMOptions on top of the
93 // file). If the consumer doesn't pause the data-provider it will run out of 66 // file). If the consumer doesn't pause the data-provider it will run out of
94 // heap-space. 67 // heap-space.
95 68
96 new DataProvider(800 * MB, 100 * MB, 1 * MB) 69 dataGenerator(100 * MB, 1 * MB)
97 .pipe(new SlowConsumer(200 * MB, 5 * MB)) 70 .pipe(new SlowConsumer(200 * MB, 5 * MB))
98 .then((count) { 71 .then((count) {
99 port.close(); 72 port.close();
100 Expect.equals(100 * MB, count); 73 Expect.equals(100 * MB, count);
101 }); 74 });
102 } 75 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698