| OLD | NEW |
| 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_consumer2_test; | 7 library slow_consumer2_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 class DataProvider { | 64 class DataProvider { |
| 53 final int chunkSize; | 65 final int chunkSize; |
| 54 final int bytesPerSecond; | 66 final int bytesPerSecond; |
| 55 int sentCount = 0; | 67 int sentCount = 0; |
| 56 int targetCount; | 68 int targetCount; |
| 57 StreamController controller; | 69 StreamController controller; |
| 58 | 70 |
| 59 DataProvider(int this.bytesPerSecond, int this.targetCount, this.chunkSize) { | 71 DataProvider(int this.bytesPerSecond, int this.targetCount, this.chunkSize) { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 // file). If the consumer doesn't pause the data-provider it will run out of | 111 // file). If the consumer doesn't pause the data-provider it will run out of |
| 100 // heap-space. | 112 // heap-space. |
| 101 | 113 |
| 102 new DataProvider(800 * MB, 100 * MB, 1 * MB).stream | 114 new DataProvider(800 * MB, 100 * MB, 1 * MB).stream |
| 103 .pipe(new SlowConsumer(200 * MB, 5 * MB)) | 115 .pipe(new SlowConsumer(200 * MB, 5 * MB)) |
| 104 .then((count) { | 116 .then((count) { |
| 105 port.close(); | 117 port.close(); |
| 106 Expect.equals(100 * MB, count); | 118 Expect.equals(100 * MB, count); |
| 107 }); | 119 }); |
| 108 } | 120 } |
| OLD | NEW |