Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(260)

Unified Diff: tests/standalone/src/FileTest.dart

Issue 9474004: Make FileInputStream and FileOutputStream actually asynchronous. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Remove accidental edit Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: tests/standalone/src/FileTest.dart
diff --git a/tests/standalone/src/FileTest.dart b/tests/standalone/src/FileTest.dart
index e90244a9bdfa4ca63d9dcb2d2d98a27b4e3d5fe8..6f7f7f2588c86e1e9234fae9196b9dd152bef11e 100644
--- a/tests/standalone/src/FileTest.dart
+++ b/tests/standalone/src/FileTest.dart
@@ -44,28 +44,32 @@ class FileTest {
String filename = getFilename("bin/file_test.cc");
File file = new File(filename);
InputStream input = file.openInputStreamSync();
- List<int> buffer = new List<int>(42);
- int bytesRead = input.readInto(buffer, 0, 12);
- Expect.equals(12, bytesRead);
- bytesRead = input.readInto(buffer, 12, 30);
- input.close();
- Expect.equals(30, bytesRead);
- Expect.equals(47, buffer[0]); // represents '/' in the file.
- Expect.equals(47, buffer[1]); // represents '/' in the file.
- Expect.equals(32, buffer[2]); // represents ' ' in the file.
- Expect.equals(67, buffer[3]); // represents 'C' in the file.
- Expect.equals(111, buffer[4]); // represents 'o' in the file.
- Expect.equals(112, buffer[5]); // represents 'p' in the file.
- Expect.equals(121, buffer[6]); // represents 'y' in the file.
- Expect.equals(114, buffer[7]); // represents 'r' in the file.
- Expect.equals(105, buffer[8]); // represents 'i' in the file.
- Expect.equals(103, buffer[9]); // represents 'g' in the file.
- Expect.equals(104, buffer[10]); // represents 'h' in the file.
- Expect.equals(116, buffer[11]); // represents 't' in the file.
+ input.dataHandler = () {
+ List<int> buffer = new List<int>(42);
+ int bytesRead = input.readInto(buffer, 0, 12);
+ Expect.equals(12, bytesRead);
+ bytesRead = input.readInto(buffer, 12, 30);
+ input.close();
+ Expect.equals(30, bytesRead);
+ Expect.equals(47, buffer[0]); // represents '/' in the file.
+ Expect.equals(47, buffer[1]); // represents '/' in the file.
+ Expect.equals(32, buffer[2]); // represents ' ' in the file.
+ Expect.equals(67, buffer[3]); // represents 'C' in the file.
+ Expect.equals(111, buffer[4]); // represents 'o' in the file.
+ Expect.equals(112, buffer[5]); // represents 'p' in the file.
+ Expect.equals(121, buffer[6]); // represents 'y' in the file.
+ Expect.equals(114, buffer[7]); // represents 'r' in the file.
+ Expect.equals(105, buffer[8]); // represents 'i' in the file.
+ Expect.equals(103, buffer[9]); // represents 'g' in the file.
+ Expect.equals(104, buffer[10]); // represents 'h' in the file.
+ Expect.equals(116, buffer[11]); // represents 't' in the file.
+ };
}
// Test for file read and write functionality.
static void testReadWriteStream() {
+ asyncTestStarted();
+
// Read a file.
String inFilename = getFilename("tests/vm/data/fixed_length_file");
File file;
@@ -75,58 +79,71 @@ class FileTest {
// Test reading all using readInto.
file = new File(inFilename);
input = file.openInputStreamSync();
- List<int> buffer1 = new List<int>(42);
- bytesRead = input.readInto(buffer1, 0, 42);
- Expect.equals(42, bytesRead);
- Expect.isTrue(input.closed);
+ input.dataHandler = () {
+ List<int> buffer1 = new List<int>(42);
+ bytesRead = input.readInto(buffer1, 0, 42);
+ Expect.equals(42, bytesRead);
+ Expect.isTrue(input.closed);
- // Test reading all using readInto and read.
- file = new File(inFilename);
- input = file.openInputStreamSync();
- bytesRead = input.readInto(buffer1, 0, 21);
- Expect.equals(21, bytesRead);
- buffer1 = input.read();
- Expect.equals(21, buffer1.length);
- Expect.isTrue(input.closed);
+ // Test reading all using readInto and read.
+ file = new File(inFilename);
+ input = file.openInputStreamSync();
+ input.dataHandler = () {
+ bytesRead = input.readInto(buffer1, 0, 21);
+ Expect.equals(21, bytesRead);
+ buffer1 = input.read();
+ Expect.equals(21, buffer1.length);
+ Expect.isTrue(input.closed);
- // Test reading all using read and readInto.
- file = new File(inFilename);
- input = file.openInputStreamSync();
- buffer1 = input.read(21);
- Expect.equals(21, buffer1.length);
- bytesRead = input.readInto(buffer1, 0, 21);
- Expect.equals(21, bytesRead);
- Expect.isTrue(input.closed);
+ // Test reading all using read and readInto.
+ file = new File(inFilename);
+ input = file.openInputStreamSync();
+ input.dataHandler = () {
+ buffer1 = input.read(21);
+ Expect.equals(21, buffer1.length);
+ bytesRead = input.readInto(buffer1, 0, 21);
+ Expect.equals(21, bytesRead);
+ Expect.isTrue(input.closed);
- // Test reading all using read.
- file = new File(inFilename);
- input = file.openInputStreamSync();
- buffer1 = input.read();
- Expect.equals(42, buffer1.length);
- Expect.isTrue(input.closed);
+ // Test reading all using read.
+ file = new File(inFilename);
+ input = file.openInputStreamSync();
+ input.dataHandler = () {
+ buffer1 = input.read();
+ Expect.equals(42, buffer1.length);
+ Expect.isTrue(input.closed);
- // Write the contents of the file just read into another file.
- String outFilename = tempDirectory.path + "/out_read_write_stream";
- file = new File(outFilename);
- OutputStream output = file.openOutputStreamSync();
- bool writeDone = output.writeFrom(buffer1, 0, 42);
- Expect.equals(true, writeDone);
- output.close();
+ // Write the contents of the file just read into another file.
+ String outFilename = tempDirectory.path + "/out_read_write_stream";
+ file = new File(outFilename);
+ OutputStream output = file.openOutputStreamSync();
+ bool writeDone = output.writeFrom(buffer1, 0, 42);
+ Expect.equals(false, writeDone);
+ output.noPendingWriteHandler = () {
+ output.close();
- // Now read the contents of the file just written.
- List<int> buffer2 = new List<int>(42);
- file = new File(outFilename);
- input = file.openInputStreamSync();
- bytesRead = input.readInto(buffer2, 0, 42);
- Expect.isTrue(input.closed);
- Expect.equals(42, bytesRead);
- // Now compare the two buffers to check if they are identical.
- for (int i = 0; i < buffer1.length; i++) {
- Expect.equals(buffer1[i], buffer2[i]);
- }
- // Delete the output file.
- file.deleteSync();
- Expect.isFalse(file.existsSync());
+ // Now read the contents of the file just written.
+ List<int> buffer2 = new List<int>(42);
+ file = new File(outFilename);
+ input = file.openInputStreamSync();
+ input.dataHandler = () {
+ bytesRead = input.readInto(buffer2, 0, 42);
+ Expect.isTrue(input.closed);
+ Expect.equals(42, bytesRead);
+ // Now compare the two buffers to check if they are identical.
+ for (int i = 0; i < buffer1.length; i++) {
+ Expect.equals(buffer1[i], buffer2[i]);
+ }
+ // Delete the output file.
+ file.deleteSync();
+ Expect.isFalse(file.existsSync());
+ asyncTestDone("testReadWriteStream");
+ };
+ };
+ };
+ };
+ };
+ };
}
static void testRead() {
@@ -304,26 +321,30 @@ class FileTest {
List<int> buffer = content.charCodes();
OutputStream outStream = file.openOutputStreamSync();
outStream.write(buffer);
- outStream.close();
- File file2 = new File(filename);
- OutputStream appendingOutput = file2.openOutputStreamSync(FileMode.APPEND);
- appendingOutput.write(buffer);
- appendingOutput.close();
- File file3 = new File(filename);
- file3.openHandler = (RandomAccessFile openedFile) {
- openedFile.lengthHandler = (int length) {
- Expect.equals(content.length * 2, length);
- openedFile.closeHandler = () {
- file3.deleteHandler = () {
- asyncTestDone("testOutputStreamWriteAppend");
+ outStream.noPendingWriteHandler = () {
+ outStream.close();
+ File file2 = new File(filename);
+ OutputStream appendingOutput = file2.openOutputStreamSync(FileMode.APPEND);
Søren Gjesse 2012/02/28 07:40:25 Long line.
Mads Ager (google) 2012/02/28 08:21:00 Done.
+ appendingOutput.write(buffer);
+ appendingOutput.noPendingWriteHandler = () {
+ appendingOutput.close();
+ File file3 = new File(filename);
+ file3.openHandler = (RandomAccessFile openedFile) {
+ openedFile.lengthHandler = (int length) {
+ Expect.equals(content.length * 2, length);
+ openedFile.closeHandler = () {
+ file3.deleteHandler = () {
+ asyncTestDone("testOutputStreamWriteAppend");
+ };
+ file3.delete();
+ };
+ openedFile.close();
};
- file3.delete();
+ openedFile.length();
};
- openedFile.close();
+ file3.open();
};
- openedFile.length();
};
- file3.open();
asyncTestStarted();
}
@@ -741,13 +762,14 @@ class FileTest {
File file = new File(tempDirectory.path + "/out_close_exception_stream");
file.createSync();
InputStream input = file.openInputStreamSync();
- Expect.isTrue(input.closed);
- Expect.isNull(input.readInto(buffer, 0, 12));
- OutputStream output = file.openOutputStreamSync();
- output.close();
- Expect.throws(( ) => output.writeFrom(buffer, 0, 12),
- (e) => e is FileIOException);
- file.deleteSync();
+ input.closeHandler = () {
+ Expect.isTrue(input.closed);
+ Expect.isNull(input.readInto(buffer, 0, 12));
+ OutputStream output = file.openOutputStreamSync();
+ output.close();
+ Expect.throws(() => output.writeFrom(buffer, 0, 12));
+ file.deleteSync();
+ };
}
// Tests buffer out of bounds exception.
@@ -991,13 +1013,26 @@ class FileTest {
Expect.equals(10, lines.length);
}
+
static void testReadAsErrors() {
+ var port = new ReceivePort.singleShot();
+ port.receive((message, _) {
+ Expect.equals(1, message);
+ });
var f = new File('.');
Expect.throws(f.readAsBytesSync, (e) => e is FileIOException);
Expect.throws(f.readAsTextSync, (e) => e is FileIOException);
Expect.throws(f.readAsLinesSync, (e) => e is FileIOException);
- // TODO(ager): Add tests of errors in readAsBytes when input
- // streams are truely async.
+ f.readAsBytes();
+ f.errorHandler = (e) {
+ f.readAsText();
+ f.errorHandler = (e) {
+ f.readAsLines();
+ f.errorHandler = (e) {
+ port.toSendPort().send(1);
+ };
+ };
+ };
}
// Test that opens the same file for writing then for appending to test

Powered by Google App Engine
This is Rietveld 408576698