| 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 'package:async_helper/async_helper.dart'; | 9 import 'package:async_helper/async_helper.dart'; |
| 10 import "package:expect/expect.dart"; | 10 import "package:expect/expect.dart"; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 | 24 |
| 25 SlowConsumer(int this.bytesPerSecond, int this.bufferSize); | 25 SlowConsumer(int this.bytesPerSecond, int this.bufferSize); |
| 26 | 26 |
| 27 Future consume(Stream stream) { | 27 Future consume(Stream stream) { |
| 28 return addStream(stream).then((_) => close()); | 28 return addStream(stream).then((_) => close()); |
| 29 } | 29 } |
| 30 | 30 |
| 31 Future addStream(Stream stream) { | 31 Future addStream(Stream stream) { |
| 32 Completer result = new Completer(); | 32 Completer result = new Completer(); |
| 33 var subscription; | 33 var subscription; |
| 34 subscription = stream.listen( | 34 subscription = stream.listen((List<int> data) { |
| 35 (List<int> data) { | 35 receivedCount += data.length; |
| 36 receivedCount += data.length; | 36 usedBufferSize += data.length; |
| 37 usedBufferSize += data.length; | 37 bufferedData.add(data); |
| 38 bufferedData.add(data); | 38 int currentBufferedDataLength = bufferedData.length; |
| 39 int currentBufferedDataLength = bufferedData.length; | 39 if (usedBufferSize > bufferSize) { |
| 40 if (usedBufferSize > bufferSize) { | 40 subscription.pause(); |
| 41 subscription.pause(); | 41 usedBufferSize = 0; |
| 42 usedBufferSize = 0; | 42 int ms = data.length * 1000 ~/ bytesPerSecond; |
| 43 int ms = data.length * 1000 ~/ bytesPerSecond; | 43 Duration duration = new Duration(milliseconds: ms); |
| 44 Duration duration = new Duration(milliseconds: ms); | 44 new Timer(duration, () { |
| 45 new Timer(duration, () { | 45 for (int i = 0; i < currentBufferedDataLength; i++) { |
| 46 for (int i = 0; i < currentBufferedDataLength; i++) { | 46 bufferedData[i] = null; |
| 47 bufferedData[i] = null; | 47 } |
| 48 } | 48 subscription.resume(); |
| 49 subscription.resume(); | 49 }); |
| 50 }); | 50 } |
| 51 } | 51 }, onDone: () { |
| 52 }, | 52 finalCount = receivedCount; |
| 53 onDone: () { | 53 result.complete(receivedCount); |
| 54 finalCount = receivedCount; | 54 }); |
| 55 result.complete(receivedCount); | |
| 56 }); | |
| 57 return result.future; | 55 return result.future; |
| 58 } | 56 } |
| 59 | 57 |
| 60 Future close() { | 58 Future close() { |
| 61 return new Future.value(finalCount); | 59 return new Future.value(finalCount); |
| 62 } | 60 } |
| 63 } | 61 } |
| 64 | 62 |
| 65 class DataProvider { | 63 class DataProvider { |
| 66 final int chunkSize; | 64 final int chunkSize; |
| 67 final int bytesPerSecond; | 65 final int bytesPerSecond; |
| 68 int sentCount = 0; | 66 int sentCount = 0; |
| 69 int targetCount; | 67 int targetCount; |
| 70 StreamController controller; | 68 StreamController controller; |
| 71 | 69 |
| 72 DataProvider(int this.bytesPerSecond, int this.targetCount, this.chunkSize) { | 70 DataProvider(int this.bytesPerSecond, int this.targetCount, this.chunkSize) { |
| 73 controller = new StreamController( | 71 controller = new StreamController( |
| 74 sync: true, | 72 sync: true, onPause: onPauseStateChange, onResume: onPauseStateChange); |
| 75 onPause: onPauseStateChange, | |
| 76 onResume: onPauseStateChange); | |
| 77 Timer.run(send); | 73 Timer.run(send); |
| 78 } | 74 } |
| 79 | 75 |
| 80 Stream get stream => controller.stream; | 76 Stream get stream => controller.stream; |
| 81 | 77 |
| 82 send() { | 78 send() { |
| 83 if (controller.isPaused) return; | 79 if (controller.isPaused) return; |
| 84 if (sentCount == targetCount) { | 80 if (sentCount == targetCount) { |
| 85 controller.close(); | 81 controller.close(); |
| 86 return; | 82 return; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 108 asyncStart(); | 104 asyncStart(); |
| 109 // The data provider can deliver 800MB/s of data. It sends 100MB of data to | 105 // 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 | 106 // 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 | 107 // chunks. The consumer has a buffer of 5MB. That is, it can accept a few |
| 112 // packages without pausing its input. | 108 // packages without pausing its input. |
| 113 // | 109 // |
| 114 // This test is limited to 32MB of heap-space (see VMOptions on top of the | 110 // 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 | 111 // file). If the consumer doesn't pause the data-provider it will run out of |
| 116 // heap-space. | 112 // heap-space. |
| 117 | 113 |
| 118 new DataProvider(800 * MB, 100 * MB, 1 * MB).stream | 114 new DataProvider(800 * MB, 100 * MB, 1 * MB) |
| 119 .pipe(new SlowConsumer(200 * MB, 5 * MB)) | 115 .stream |
| 120 .then((count) { | 116 .pipe(new SlowConsumer(200 * MB, 5 * MB)) |
| 121 Expect.equals(100 * MB, count); | 117 .then((count) { |
| 122 asyncEnd(); | 118 Expect.equals(100 * MB, count); |
| 123 }); | 119 asyncEnd(); |
| 120 }); |
| 124 } | 121 } |
| OLD | NEW |