| 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 23 matching lines...) Expand all Loading... |
| 34 // Make sure we use data here to keep tracking it. | 34 // Make sure we use data here to keep tracking it. |
| 35 return count + data.length; | 35 return count + data.length; |
| 36 }); | 36 }); |
| 37 }); | 37 }); |
| 38 }, | 38 }, |
| 39 onDone: () { current.then((count) { completer.complete(count); }); }); | 39 onDone: () { current.then((count) { completer.complete(count); }); }); |
| 40 return completer.future; | 40 return completer.future; |
| 41 } | 41 } |
| 42 } | 42 } |
| 43 | 43 |
| 44 class DataProvider extends StreamController { | 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 | 50 |
| 50 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); |
| 51 new Timer(0, (_) => send()); | 53 new Timer(0, (_) => send()); |
| 52 } | 54 } |
| 53 | 55 |
| 56 Stream get stream => controller.stream; |
| 57 |
| 54 send() { | 58 send() { |
| 55 if (isPaused) return; | 59 if (controller.isPaused) return; |
| 56 if (sentCount == targetCount) { | 60 if (sentCount == targetCount) { |
| 57 close(); | 61 controller.close(); |
| 58 return; | 62 return; |
| 59 } | 63 } |
| 60 int listSize = chunkSize; | 64 int listSize = chunkSize; |
| 61 sentCount += listSize; | 65 sentCount += listSize; |
| 62 if (sentCount > targetCount) { | 66 if (sentCount > targetCount) { |
| 63 listSize -= sentCount - targetCount; | 67 listSize -= sentCount - targetCount; |
| 64 sentCount = targetCount; | 68 sentCount = targetCount; |
| 65 } | 69 } |
| 66 add(new List.fixedLength(listSize)); | 70 controller.add(new List.fixedLength(listSize)); |
| 67 int ms = listSize * 1000 ~/ bytesPerSecond; | 71 int ms = listSize * 1000 ~/ bytesPerSecond; |
| 68 if (!isPaused) new Timer(ms, (_) => send()); | 72 if (!controller.isPaused) new Timer(ms, (_) => send()); |
| 69 } | 73 } |
| 70 | 74 |
| 71 onPauseStateChange() { | 75 onPauseStateChange() { |
| 72 // We don't care if we just unpaused or paused. In either case we just | 76 // We don't care if we just unpaused or paused. In either case we just |
| 73 // call send which will test it for us. | 77 // call send which will test it for us. |
| 74 send(); | 78 send(); |
| 75 } | 79 } |
| 76 } | 80 } |
| 77 | 81 |
| 78 main() { | 82 main() { |
| 79 var port = new ReceivePort(); | 83 var port = new ReceivePort(); |
| 80 // The data provider can deliver 800MB/s of data. It sends 100MB of data to | 84 // The data provider can deliver 800MB/s of data. It sends 100MB of data to |
| 81 // the slower consumer who can only read 200MB/s. The data is sent in 1MB | 85 // the slower consumer who can only read 200MB/s. The data is sent in 1MB |
| 82 // chunks. | 86 // chunks. |
| 83 // | 87 // |
| 84 // This test is limited to 32MB of heap-space (see VMOptions on top of the | 88 // This test is limited to 32MB of heap-space (see VMOptions on top of the |
| 85 // file). If the consumer doesn't pause the data-provider it will run out of | 89 // file). If the consumer doesn't pause the data-provider it will run out of |
| 86 // heap-space. | 90 // heap-space. |
| 87 | 91 |
| 88 new DataProvider(800 * MB, 100 * MB, 1 * MB) | 92 new DataProvider(800 * MB, 100 * MB, 1 * MB).stream |
| 89 .pipe(new SlowConsumer(200 * MB)) | 93 .pipe(new SlowConsumer(200 * MB)) |
| 90 .then((count) { | 94 .then((count) { |
| 91 port.close(); | 95 port.close(); |
| 92 Expect.equals(100 * MB, count); | 96 Expect.equals(100 * MB, count); |
| 93 }); | 97 }); |
| 94 } | 98 } |
| OLD | NEW |