| 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_consumer3_test; | 7 library slow_consumer3_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 Stream<List> dataGenerator(int bytesTotal, int chunkSize) { | 63 Stream<List> dataGenerator(int bytesTotal, int chunkSize) { |
| 66 int chunks = bytesTotal ~/ chunkSize; | 64 int chunks = bytesTotal ~/ chunkSize; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 79 // Notice that we aren't really counting bytes, but words, since we use normal | 77 // Notice that we aren't really counting bytes, but words, since we use normal |
| 80 // lists where each entry takes up a full word. In 64-bit VMs this will be | 78 // lists where each entry takes up a full word. In 64-bit VMs this will be |
| 81 // 8 bytes per entry, so the 3*MB buffer is picked to stay below 32 actual | 79 // 8 bytes per entry, so the 3*MB buffer is picked to stay below 32 actual |
| 82 // MiB. | 80 // MiB. |
| 83 // | 81 // |
| 84 // This test is limited to 32MB of heap-space (see VMOptions on top of the | 82 // 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 | 83 // file). If the consumer doesn't pause the data-provider it will run out of |
| 86 // heap-space. | 84 // heap-space. |
| 87 | 85 |
| 88 dataGenerator(100 * MB, 512 * KB) | 86 dataGenerator(100 * MB, 512 * KB) |
| 89 .pipe(new SlowConsumer(200 * MB, 3 * MB)) | 87 .pipe(new SlowConsumer(200 * MB, 3 * MB)) |
| 90 .then((count) { | 88 .then((count) { |
| 91 Expect.equals(100 * MB, count); | 89 Expect.equals(100 * MB, count); |
| 92 asyncEnd(); | 90 asyncEnd(); |
| 93 }); | 91 }); |
| 94 } | 92 } |
| OLD | NEW |