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

Side by Side Diff: tests/lib_strong/async/slow_consumer3_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_consumer3_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 int receivedCount = 0;
19 final int bytesPerSecond;
20 final int bufferSize;
21 final List bufferedData = [];
22 int usedBufferSize = 0;
23 int finalCount;
24
25 SlowConsumer(int this.bytesPerSecond, int this.bufferSize);
26
27 Future consume(Stream stream) {
28 return addStream(stream).then((_) => close());
29 }
30
31 Future addStream(Stream stream) {
32 Completer result = new Completer();
33 var subscription;
34 subscription = stream.listen(
35 (List<int> data) {
36 receivedCount += data.length;
37 usedBufferSize += data.length;
38 bufferedData.add(data);
39 int currentBufferedDataLength = bufferedData.length;
40 if (usedBufferSize > bufferSize) {
41 subscription.pause();
42 usedBufferSize = 0;
43 int ms = data.length * 1000 ~/ bytesPerSecond;
44 Duration duration = new Duration(milliseconds: ms);
45 new Timer(duration, () {
46 for (int i = 0; i < currentBufferedDataLength; i++) {
47 bufferedData[i] = null;
48 }
49 subscription.resume();
50 });
51 }
52 },
53 onDone: () {
54 finalCount = receivedCount;
55 result.complete(receivedCount);
56 });
57 return result.future;
58 }
59
60 Future close() {
61 return new Future.value(finalCount);
62 }
63 }
64
65 Stream<List> dataGenerator(int bytesTotal, int chunkSize) {
66 int chunks = bytesTotal ~/ chunkSize;
67 return new Stream.fromIterable(new Iterable.generate(chunks, (_) {
68 // This assumes one byte per entry. In practice it will be more.
69 return new List<int>(chunkSize);
70 }));
71 }
72
73 main() {
74 asyncStart();
75 // The data provider can deliver 800MBs of data as fast as it is
76 // requested. The data is sent in 0.5MB chunks. The consumer has a buffer of
77 // 3MB. That is, it can accept a few packages without pausing its input.
78 //
79 // Notice that we aren't really counting bytes, but words, since we use normal
80 // lists where each entry takes up a full word. In 64-bit VMs this will be
81 // 8 bytes per entry, so the 3*MB buffer is picked to stay below 32 actual
82 // MiB.
83 //
84 // This test is limited to 32MB of heap-space (see VMOptions on top of the
85 // file). If the consumer doesn't pause the data-provider it will run out of
86 // heap-space.
87
88 dataGenerator(100 * MB, 512 * KB)
89 .pipe(new SlowConsumer(200 * MB, 3 * MB))
90 .then((count) {
91 Expect.equals(100 * MB, count);
92 asyncEnd();
93 });
94 }
OLDNEW
« no previous file with comments | « tests/lib_strong/async/slow_consumer2_test.dart ('k') | tests/lib_strong/async/slow_consumer_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698