| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 class ListInputStream implements InputStream { | |
| 6 List<int> read() { | |
| 7 var result = _data; | |
| 8 _data = null; | |
| 9 return result; | |
| 10 } | |
| 11 | |
| 12 void set dataHandler(void callback()) { | |
| 13 _dataHandler = callback; | |
| 14 } | |
| 15 | |
| 16 void set closeHandler(void callback()) { | |
| 17 _closeHandler = callback; | |
| 18 } | |
| 19 | |
| 20 void set errorHandler(void callback(int error)) { | |
| 21 } | |
| 22 | |
| 23 void write(List<int> data) { | |
| 24 Expect.equals(null, _data); | |
| 25 _data = data; | |
| 26 if (_dataHandler != null) { | |
| 27 // Data handler is not called through the event loop here. | |
| 28 _dataHandler(); | |
| 29 } | |
| 30 } | |
| 31 | |
| 32 void close() { | |
| 33 Expect.equals(null, _data); | |
| 34 // Close handler is not called through the event loop here. | |
| 35 if (_closeHandler != null) _closeHandler(); | |
| 36 } | |
| 37 | |
| 38 List<int> _data; | |
| 39 var _dataHandler; | |
| 40 var _closeHandler; | |
| 41 } | |
| 42 | |
| 43 void test1() { | 5 void test1() { |
| 44 void testWithChunkSize(var data, int chunkSize, Function testDone) { | 6 void testWithChunkSize(var data, int chunkSize, Function testDone) { |
| 45 InputStream s = new ListInputStream(); | 7 InputStream s = new ListInputStream(data); |
| 46 ChunkedInputStream stream = new ChunkedInputStream(s); | 8 ChunkedInputStream stream = new ChunkedInputStream(s); |
| 47 int chunkCount = 0; | 9 int chunkCount = 0; |
| 48 int byteCount = 0; | 10 int byteCount = 0; |
| 49 void chunkData() { | 11 void chunkData() { |
| 50 List<int> chunk = stream.read(); | 12 List<int> chunk = stream.read(); |
| 51 if (byteCount + chunkSize < data.length) { | 13 if (byteCount + chunkSize < data.length) { |
| 52 Expect.equals(chunkSize, chunk.length); | 14 Expect.equals(chunkSize, chunk.length); |
| 53 } else { | 15 } else { |
| 54 if (byteCount == data.length) { | 16 if (byteCount == data.length) { |
| 55 Expect.equals(null, chunk); | 17 Expect.equals(null, chunk); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 67 } | 29 } |
| 68 | 30 |
| 69 void closeHandler() { | 31 void closeHandler() { |
| 70 Expect.equals(data.length, byteCount); | 32 Expect.equals(data.length, byteCount); |
| 71 testDone(byteCount); | 33 testDone(byteCount); |
| 72 } | 34 } |
| 73 | 35 |
| 74 stream.dataHandler = chunkData; | 36 stream.dataHandler = chunkData; |
| 75 stream.closeHandler = closeHandler; | 37 stream.closeHandler = closeHandler; |
| 76 stream.chunkSize = chunkSize; | 38 stream.chunkSize = chunkSize; |
| 77 s.write(data); | |
| 78 s.close(); | |
| 79 } | 39 } |
| 80 | 40 |
| 81 for (int i = 1; i <= 10; i++) { | 41 for (int i = 1; i <= 10; i++) { |
| 82 var data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | 42 var data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
| 83 | 43 |
| 84 void testDone(int byteCount) { | 44 void testDone(int byteCount) { |
| 85 Expect.equals(data.length, byteCount); | 45 Expect.equals(data.length, byteCount); |
| 86 } | 46 } |
| 87 | 47 |
| 88 testWithChunkSize(data, i, testDone); | 48 testWithChunkSize(data, i, testDone); |
| 89 } | 49 } |
| 90 | 50 |
| 91 var _16k = 1024 * 16; | 51 var _16k = 1024 * 16; |
| 92 var data = new List<int>(_16k); | 52 var data = new List<int>(_16k); |
| 93 for (int i = 0; i < _16k; i++) { data[i] = i % 256; } | 53 for (int i = 0; i < _16k; i++) { data[i] = i % 256; } |
| 94 | 54 |
| 95 void testDone(int byteCount) { | 55 void testDone(int byteCount) { |
| 96 Expect.equals(data.length, byteCount); | 56 Expect.equals(data.length, byteCount); |
| 97 } | 57 } |
| 98 | 58 |
| 99 testWithChunkSize(data, 512, testDone); | 59 testWithChunkSize(data, 512, testDone); |
| 100 testWithChunkSize(data, 1024, testDone); | 60 testWithChunkSize(data, 1024, testDone); |
| 101 testWithChunkSize(data, 2048, testDone); | 61 testWithChunkSize(data, 2048, testDone); |
| 102 } | 62 } |
| 103 | 63 |
| 104 | 64 |
| 105 void test2() { | 65 void test2() { |
| 106 InputStream s = new ListInputStream(); | 66 InputStream s = new DynamicListInputStream(); |
| 107 ChunkedInputStream stream = new ChunkedInputStream(s); | 67 ChunkedInputStream stream = new ChunkedInputStream(s); |
| 108 stream.chunkSize = 5; | 68 stream.chunkSize = 5; |
| 69 ReceivePort donePort = new ReceivePort(); |
| 109 | 70 |
| 110 var stage = 0; | 71 var stage = 0; |
| 111 | 72 |
| 112 void chunkData() { | 73 void chunkData() { |
| 113 var chunk; | 74 var chunk; |
| 114 if (stage == 0) { | 75 if (stage == 0) { |
| 115 Expect.equals(stream.chunkSize, 5); | 76 Expect.equals(stream.chunkSize, 5); |
| 116 chunk = stream.read(); // 5 bytes read from stream. | 77 chunk = stream.read(); // 5 bytes read from stream. |
| 117 Expect.equals(stream.chunkSize, chunk.length); | 78 Expect.equals(stream.chunkSize, chunk.length); |
| 118 chunk = stream.read(); // 5 bytes read from stream. | 79 chunk = stream.read(); // 5 bytes read from stream. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 136 Expect.equals(null, chunk); | 97 Expect.equals(null, chunk); |
| 137 stage++; | 98 stage++; |
| 138 stream.chunkSize = 3; | 99 stream.chunkSize = 3; |
| 139 } else if (stage == 3) { | 100 } else if (stage == 3) { |
| 140 Expect.equals(stream.chunkSize, 3); | 101 Expect.equals(stream.chunkSize, 3); |
| 141 chunk = stream.read(); // 18 bytes read from stream. | 102 chunk = stream.read(); // 18 bytes read from stream. |
| 142 Expect.equals(stream.chunkSize, chunk.length); | 103 Expect.equals(stream.chunkSize, chunk.length); |
| 143 chunk = stream.read(); // 18 bytes read from stream. | 104 chunk = stream.read(); // 18 bytes read from stream. |
| 144 Expect.equals(null, chunk); | 105 Expect.equals(null, chunk); |
| 145 stage++; | 106 stage++; |
| 146 s.close(); // 18 bytes written to stream. | 107 s.markEndOfStream(); // 18 bytes written to stream. |
| 147 } else if (stage == 4) { | 108 } else if (stage == 4) { |
| 148 chunk = stream.read(); // 19 bytes read from stream. | 109 chunk = stream.read(); // 19 bytes read from stream. |
| 149 Expect.equals(1, chunk.length); | 110 Expect.equals(1, chunk.length); |
| 150 chunk = stream.read(); // 19 bytes read from stream. | 111 chunk = stream.read(); // 19 bytes read from stream. |
| 151 Expect.equals(null, chunk); | 112 Expect.equals(null, chunk); |
| 152 stage++; | 113 stage++; |
| 114 donePort.toSendPort().send(stage); |
| 153 } | 115 } |
| 154 } | 116 } |
| 155 | 117 |
| 156 void streamClosed() { | 118 void streamClosed() { |
| 157 Expect.equals(5, stage); | 119 Expect.equals(5, stage); |
| 158 } | 120 } |
| 159 | 121 |
| 160 stream.dataHandler = chunkData; | 122 stream.dataHandler = chunkData; |
| 161 stream.closeHandler = streamClosed; | 123 stream.closeHandler = streamClosed; |
| 162 s.write([0, 1, 2, 3]); // 4 bytes written to stream. | 124 s.write([0, 1, 2, 3]); // 4 bytes written to stream. |
| 163 Expect.equals(0, stage); | 125 Expect.equals(0, stage); |
| 164 s.write([4, 5, 6]); // 7 bytes written to stream. | 126 s.write([4, 5, 6]); // 7 bytes written to stream. |
| 165 Expect.equals(4, stage); | 127 |
| 128 donePort.receive((x,y) => donePort.close()); |
| 166 } | 129 } |
| 167 | 130 |
| 168 | 131 |
| 169 main() { | 132 main() { |
| 170 test1(); | 133 test1(); |
| 171 test2(); | 134 test2(); |
| 172 } | 135 } |
| OLD | NEW |