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