Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 // Dart test program for testing file I/O. | 5 // Dart test program for testing file I/O. |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 import 'dart:collection'; | 9 import 'dart:collection'; |
| 10 import 'dart:io'; | 10 import 'dart:io'; |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 127 file2.deleteSync(); | 127 file2.deleteSync(); |
| 128 Expect.isFalse(file2.existsSync()); | 128 Expect.isFalse(file2.existsSync()); |
| 129 asyncTestDone("testReadWriteStream"); | 129 asyncTestDone("testReadWriteStream"); |
| 130 }); | 130 }); |
| 131 }); | 131 }); |
| 132 }); | 132 }); |
| 133 } | 133 } |
| 134 | 134 |
| 135 // Test for file stream buffered handling of large files. | 135 // Test for file stream buffered handling of large files. |
| 136 static void testReadWriteStreamLargeFile() { | 136 static void testReadWriteStreamLargeFile() { |
| 137 asyncTestStarted(); | |
| 138 | |
| 139 // Create the test data - arbitrary binary data. | 137 // Create the test data - arbitrary binary data. |
| 140 List<int> buffer = new List<int>(100000); | 138 List<int> buffer = new List<int>(100000); |
| 141 for (var i = 0; i < buffer.length; ++i) { | 139 for (var i = 0; i < buffer.length; ++i) { |
| 142 buffer[i] = i % 256; | 140 buffer[i] = i % 256; |
| 143 } | 141 } |
| 144 String filename = | 142 String filename = |
| 145 tempDirectory.path + "/out_read_write_stream_large_file"; | 143 tempDirectory.path + "/out_read_write_stream_large_file"; |
| 146 File file = new File(filename); | 144 File file = new File(filename); |
| 147 IOSink output = file.openWrite(); | 145 IOSink output = file.openWrite(); |
| 148 output.add(buffer); | 146 output.add(buffer); |
| 149 output.add(buffer); | 147 output.add(buffer); |
| 150 output.flush().then((_) => output.close()); | 148 output.flush().then((_) => output.close()); |
| 149 | |
| 150 asyncTestStarted(); | |
| 151 output.done.then((_) { | 151 output.done.then((_) { |
| 152 Stream input = file.openRead(); | 152 Stream input = file.openRead(); |
| 153 int position = 0; | 153 int position = 0; |
| 154 final int expectedLength = 200000; | 154 final int expectedLength = 200000; |
| 155 | |
| 155 // Start an independent asynchronous check on the length. | 156 // Start an independent asynchronous check on the length. |
| 156 asyncTestStarted(); | 157 Future lengthTest() { |
| 157 file.length().then((len) { | 158 asyncTestStarted(); |
| 158 Expect.equals(expectedLength, len); | 159 return file.length().then((len) { |
| 159 asyncTestDone('testReadWriteStreamLargeFile: length check'); | 160 Expect.equals(expectedLength, len); |
| 160 }); | 161 asyncTestDone('testReadWriteStreamLargeFile: length check'); |
| 162 }); | |
| 163 } | |
| 161 | 164 |
| 162 // Immediate read should read 0 bytes. | 165 // Immediate read should read 0 bytes. |
| 163 input.listen( | 166 Future contentTest() { |
| 164 (d) { | 167 asyncTestStarted(); |
| 165 for (int i = 0; i < d.length; ++i) { | 168 var completer = new Completer(); |
| 166 Expect.equals(buffer[(i + position) % buffer.length], d[i]); | 169 input.listen( |
| 167 } | 170 (data) { |
| 168 position += d.length; | 171 for (int i = 0; i < data.length; ++i) { |
| 169 }, | 172 Expect.equals(buffer[(i + position) % buffer.length], data[i]); |
| 170 onError: (error, trace) { | 173 } |
| 171 print('Error on input in testReadWriteStreamLargeFile'); | 174 position += data.length; |
| 172 print('with error $error'); | 175 }, |
| 173 if (trace != null) print("StackTrace: $trace"); | 176 onError: (error, trace) { |
| 174 throw error; | 177 print('Error on input in testReadWriteStreamLargeFile'); |
| 175 }, | 178 print('with error $error'); |
| 176 onDone: () { | 179 if (trace != null) print("StackTrace: $trace"); |
| 177 Expect.equals(expectedLength, position); | 180 throw error; |
| 178 testPipe(file, buffer) | 181 }, |
| 179 .then((_) => file.delete()) | 182 onDone: () { |
|
kustermann
2015/02/11 13:38:23
This `file.delete()` was racing with `file.length(
| |
| 180 .then((_) { | 183 Expect.equals(expectedLength, position); |
| 181 asyncTestDone('testReadWriteStreamLargeFile: main test'); | 184 testPipe(file, buffer).then((_) { |
| 182 }) | 185 asyncTestDone('testReadWriteStreamLargeFile: main test'); |
| 183 .catchError((e, trace) { | 186 }).catchError((error, trace) { |
| 184 print('Exception while deleting ReadWriteStreamLargeFile file'); | 187 print('Exception while deleting ReadWriteStreamLargeFile file'); |
| 185 print('Exception $e'); | 188 print('Exception $error'); |
| 186 if (trace != null) print("StackTrace: $trace"); | 189 if (trace != null) print("StackTrace: $trace"); |
| 187 }); | 190 throw error; |
| 188 }); | 191 }).whenComplete(completer.complete); |
| 192 }); | |
| 193 return completer.future; | |
| 194 } | |
| 195 | |
| 196 return Future.forEach([lengthTest, contentTest], (test) => test()); | |
| 197 }).whenComplete(file.delete).whenComplete(() { | |
| 198 asyncTestDone('testReadWriteStreamLargeFile finished'); | |
| 189 }); | 199 }); |
| 190 } | 200 } |
| 191 | 201 |
| 192 static Future testPipe(File file, buffer) { | 202 static Future testPipe(File file, buffer) { |
| 193 String outputFilename = '${file.path}_copy'; | 203 String outputFilename = '${file.path}_copy'; |
| 194 File outputFile = new File(outputFilename); | 204 File outputFile = new File(outputFilename); |
| 195 var input = file.openRead(); | 205 var input = file.openRead(); |
| 196 var output = outputFile.openWrite(); | 206 var output = outputFile.openWrite(); |
| 197 Completer done = new Completer(); | 207 Completer done = new Completer(); |
| 198 input.pipe(output).then((_) { | 208 input.pipe(output).then((_) { |
| (...skipping 1138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1337 testLastModified(); | 1347 testLastModified(); |
| 1338 testDoubleAsyncOperation(); | 1348 testDoubleAsyncOperation(); |
| 1339 asyncEnd(); | 1349 asyncEnd(); |
| 1340 }); | 1350 }); |
| 1341 } | 1351 } |
| 1342 } | 1352 } |
| 1343 | 1353 |
| 1344 main() { | 1354 main() { |
| 1345 FileTest.testMain(); | 1355 FileTest.testMain(); |
| 1346 } | 1356 } |
| OLD | NEW |