OLD | NEW |
(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_consumer2_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 class DataProvider { |
| 66 final int chunkSize; |
| 67 final int bytesPerSecond; |
| 68 int sentCount = 0; |
| 69 int targetCount; |
| 70 StreamController controller; |
| 71 |
| 72 DataProvider(int this.bytesPerSecond, int this.targetCount, this.chunkSize) { |
| 73 controller = new StreamController( |
| 74 sync: true, |
| 75 onPause: onPauseStateChange, |
| 76 onResume: onPauseStateChange); |
| 77 Timer.run(send); |
| 78 } |
| 79 |
| 80 Stream get stream => controller.stream; |
| 81 |
| 82 send() { |
| 83 if (controller.isPaused) return; |
| 84 if (sentCount == targetCount) { |
| 85 controller.close(); |
| 86 return; |
| 87 } |
| 88 int listSize = chunkSize; |
| 89 sentCount += listSize; |
| 90 if (sentCount > targetCount) { |
| 91 listSize -= sentCount - targetCount; |
| 92 sentCount = targetCount; |
| 93 } |
| 94 controller.add(new List(listSize)); |
| 95 int ms = listSize * 1000 ~/ bytesPerSecond; |
| 96 Duration duration = new Duration(milliseconds: ms); |
| 97 if (!controller.isPaused) new Timer(duration, send); |
| 98 } |
| 99 |
| 100 onPauseStateChange() { |
| 101 // We don't care if we just unpaused or paused. In either case we just |
| 102 // call send which will test it for us. |
| 103 send(); |
| 104 } |
| 105 } |
| 106 |
| 107 main() { |
| 108 asyncStart(); |
| 109 // The data provider can deliver 800MB/s of data. It sends 100MB of data to |
| 110 // the slower consumer who can only read 200MB/s. The data is sent in 1MB |
| 111 // chunks. The consumer has a buffer of 5MB. That is, it can accept a few |
| 112 // packages without pausing its input. |
| 113 // |
| 114 // This test is limited to 32MB of heap-space (see VMOptions on top of the |
| 115 // file). If the consumer doesn't pause the data-provider it will run out of |
| 116 // heap-space. |
| 117 |
| 118 new DataProvider(800 * MB, 100 * MB, 1 * MB).stream |
| 119 .pipe(new SlowConsumer(200 * MB, 5 * MB)) |
| 120 .then((count) { |
| 121 Expect.equals(100 * MB, count); |
| 122 asyncEnd(); |
| 123 }); |
| 124 } |
OLD | NEW |