| 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=32 | 5 // VMOptions=--old_gen_heap_size=32 |
| 6 | 6 |
| 7 library slow_consumer_test; | 7 library slow_consumer_test; |
| 8 | 8 |
| 9 import 'dart:async'; | 9 import 'dart:async'; |
| 10 import 'dart:isolate'; | 10 import 'dart:isolate'; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 | 43 |
| 44 class DataProvider { | 44 class DataProvider { |
| 45 final int chunkSize; | 45 final int chunkSize; |
| 46 final int bytesPerSecond; | 46 final int bytesPerSecond; |
| 47 int sentCount = 0; | 47 int sentCount = 0; |
| 48 int targetCount; | 48 int targetCount; |
| 49 StreamController controller; | 49 StreamController controller; |
| 50 | 50 |
| 51 DataProvider(int this.bytesPerSecond, int this.targetCount, this.chunkSize) { | 51 DataProvider(int this.bytesPerSecond, int this.targetCount, this.chunkSize) { |
| 52 controller = new StreamController(onPauseStateChange: onPauseStateChange); | 52 controller = new StreamController(onPauseStateChange: onPauseStateChange); |
| 53 new Timer(0, (_) => send()); | 53 Timer.run(send); |
| 54 } | 54 } |
| 55 | 55 |
| 56 Stream get stream => controller.stream; | 56 Stream get stream => controller.stream; |
| 57 | 57 |
| 58 send() { | 58 send() { |
| 59 if (controller.isPaused) return; | 59 if (controller.isPaused) return; |
| 60 if (sentCount == targetCount) { | 60 if (sentCount == targetCount) { |
| 61 controller.close(); | 61 controller.close(); |
| 62 return; | 62 return; |
| 63 } | 63 } |
| 64 int listSize = chunkSize; | 64 int listSize = chunkSize; |
| 65 sentCount += listSize; | 65 sentCount += listSize; |
| 66 if (sentCount > targetCount) { | 66 if (sentCount > targetCount) { |
| 67 listSize -= sentCount - targetCount; | 67 listSize -= sentCount - targetCount; |
| 68 sentCount = targetCount; | 68 sentCount = targetCount; |
| 69 } | 69 } |
| 70 controller.add(new List.fixedLength(listSize)); | 70 controller.add(new List.fixedLength(listSize)); |
| 71 int ms = listSize * 1000 ~/ bytesPerSecond; | 71 int ms = listSize * 1000 ~/ bytesPerSecond; |
| 72 if (!controller.isPaused) new Timer(ms, (_) => send()); | 72 Duration duration = new Duration(milliseconds: ms); |
| 73 if (!controller.isPaused) new Timer(duration, send); |
| 73 } | 74 } |
| 74 | 75 |
| 75 onPauseStateChange() { | 76 onPauseStateChange() { |
| 76 // We don't care if we just unpaused or paused. In either case we just | 77 // We don't care if we just unpaused or paused. In either case we just |
| 77 // call send which will test it for us. | 78 // call send which will test it for us. |
| 78 send(); | 79 send(); |
| 79 } | 80 } |
| 80 } | 81 } |
| 81 | 82 |
| 82 main() { | 83 main() { |
| 83 var port = new ReceivePort(); | 84 var port = new ReceivePort(); |
| 84 // The data provider can deliver 800MB/s of data. It sends 100MB of data to | 85 // The data provider can deliver 800MB/s of data. It sends 100MB of data to |
| 85 // the slower consumer who can only read 200MB/s. The data is sent in 1MB | 86 // the slower consumer who can only read 200MB/s. The data is sent in 1MB |
| 86 // chunks. | 87 // chunks. |
| 87 // | 88 // |
| 88 // This test is limited to 32MB of heap-space (see VMOptions on top of the | 89 // This test is limited to 32MB of heap-space (see VMOptions on top of the |
| 89 // file). If the consumer doesn't pause the data-provider it will run out of | 90 // file). If the consumer doesn't pause the data-provider it will run out of |
| 90 // heap-space. | 91 // heap-space. |
| 91 | 92 |
| 92 new DataProvider(800 * MB, 100 * MB, 1 * MB).stream | 93 new DataProvider(800 * MB, 100 * MB, 1 * MB).stream |
| 93 .pipe(new SlowConsumer(200 * MB)) | 94 .pipe(new SlowConsumer(200 * MB)) |
| 94 .then((count) { | 95 .then((count) { |
| 95 port.close(); | 96 port.close(); |
| 96 Expect.equals(100 * MB, count); | 97 Expect.equals(100 * MB, count); |
| 97 }); | 98 }); |
| 98 } | 99 } |
| OLD | NEW |