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

Side by Side Diff: tests/standalone/io/file_input_stream_test.dart

Issue 23886004: Update dart:io tests to use asyncStart/asyncEnd (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed review comments Created 7 years, 3 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 // Testing file input stream, VM-only, standalone test. 4 // Testing file input stream, VM-only, standalone test.
5 5
6 import "dart:convert"; 6 import "dart:convert";
7 import "dart:io"; 7 import "dart:io";
8 import "dart:isolate";
9 8
9 import "package:async_helper/async_helper.dart";
10 import "package:expect/expect.dart"; 10 import "package:expect/expect.dart";
11 11
12 // Helper method to be able to run the test from the runtime 12 // Helper method to be able to run the test from the runtime
13 // directory, or the top directory. 13 // directory, or the top directory.
14 String getFilename(String path) => 14 String getFilename(String path) =>
15 new File(path).existsSync() ? path : '../$path'; 15 new File(path).existsSync() ? path : '../$path';
16 16
17 void testStringLineSplitter() { 17 void testStringLineSplitter() {
18 String fileName = getFilename("tests/standalone/io/readuntil_test.dat"); 18 String fileName = getFilename("tests/standalone/io/readuntil_test.dat");
19 // File contains "Hello Dart\nwassup!\n" 19 // File contains "Hello Dart\nwassup!\n"
20 File file = new File(fileName); 20 File file = new File(fileName);
21 int linesRead = 0; 21 int linesRead = 0;
22 var lineStream = file.openRead() 22 var lineStream = file.openRead()
23 .transform(UTF8.decoder) 23 .transform(UTF8.decoder)
24 .transform(new LineSplitter()); 24 .transform(new LineSplitter());
25 lineStream.listen((line) { 25 lineStream.listen((line) {
26 linesRead++; 26 linesRead++;
27 if (linesRead == 1) { 27 if (linesRead == 1) {
28 Expect.equals("Hello Dart", line); 28 Expect.equals("Hello Dart", line);
29 } else if (linesRead == 2) { 29 } else if (linesRead == 2) {
30 Expect.equals("wassup!", line); 30 Expect.equals("wassup!", line);
31 } else { 31 } else {
32 Expect.fail("More or less than 2 lines read ($linesRead lines read)."); 32 Expect.fail("More or less than 2 lines read ($linesRead lines read).");
33 } 33 }
34 }); 34 });
35 } 35 }
36 36
37 37
38 void testOpenStreamAsync() { 38 void testOpenStreamAsync() {
39 var keepAlive = new ReceivePort(); 39 asyncStart();
40 String fileName = getFilename("tests/standalone/io/readuntil_test.dat"); 40 String fileName = getFilename("tests/standalone/io/readuntil_test.dat");
41 // File contains "Hello Dart\nwassup!\n" 41 // File contains "Hello Dart\nwassup!\n"
42 var expected = "Hello Dart\nwassup!\n".codeUnits; 42 var expected = "Hello Dart\nwassup!\n".codeUnits;
43 var byteCount = 0; 43 var byteCount = 0;
44 (new File(fileName)).openRead().listen( 44 (new File(fileName)).openRead().listen(
45 (d) => byteCount += d.length, 45 (d) => byteCount += d.length,
46 onDone: () { 46 onDone: () {
47 Expect.equals(expected.length, byteCount); 47 Expect.equals(expected.length, byteCount);
48 keepAlive.close(); 48 asyncEnd();
49 }); 49 });
50 } 50 }
51 51
52 52
53 // Create a file that is big enough that a file stream will 53 // Create a file that is big enough that a file stream will
54 // read it in multiple chunks. 54 // read it in multiple chunks.
55 int writeLongFileSync(File file) { 55 int writeLongFileSync(File file) {
56 file.createSync(); 56 file.createSync();
57 StringBuffer buffer = new StringBuffer(); 57 StringBuffer buffer = new StringBuffer();
58 for (var i = 0; i < 10000; i++) { 58 for (var i = 0; i < 10000; i++) {
59 buffer.write("Hello, world"); 59 buffer.write("Hello, world");
60 } 60 }
61 file.writeAsStringSync(buffer.toString()); 61 file.writeAsStringSync(buffer.toString());
62 var length = file.lengthSync(); 62 var length = file.lengthSync();
63 Expect.equals(buffer.length, length); 63 Expect.equals(buffer.length, length);
64 return length; 64 return length;
65 } 65 }
66 66
67 67
68 void testInputStreamTruncate() { 68 void testInputStreamTruncate() {
69 var keepAlive = new ReceivePort(); 69 asyncStart();
70 var temp = new Directory('').createTempSync(); 70 var temp = new Directory('').createTempSync();
71 var file = new File('${temp.path}/input_stream_truncate.txt'); 71 var file = new File('${temp.path}/input_stream_truncate.txt');
72 var originalLength = writeLongFileSync(file); 72 var originalLength = writeLongFileSync(file);
73 // Start streaming the file. Pause after first chunk. Truncate 73 // Start streaming the file. Pause after first chunk. Truncate
74 // underlying file and check that the streaming stops with or 74 // underlying file and check that the streaming stops with or
75 // without getting all data. 75 // without getting all data.
76 var streamedBytes = 0; 76 var streamedBytes = 0;
77 var subscription; 77 var subscription;
78 subscription = file.openRead().listen( 78 subscription = file.openRead().listen(
79 (d) { 79 (d) {
80 if (streamedBytes == 0) { 80 if (streamedBytes == 0) {
81 subscription.pause(); 81 subscription.pause();
82 // Truncate the file by opening it for writing. 82 // Truncate the file by opening it for writing.
83 file.open(mode: FileMode.WRITE).then((opened) { 83 file.open(mode: FileMode.WRITE).then((opened) {
84 opened.close().then((_) { 84 opened.close().then((_) {
85 Expect.equals(0, file.lengthSync()); 85 Expect.equals(0, file.lengthSync());
86 subscription.resume(); 86 subscription.resume();
87 }); 87 });
88 }); 88 });
89 } 89 }
90 streamedBytes += d.length; 90 streamedBytes += d.length;
91 }, 91 },
92 onDone: () { 92 onDone: () {
93 Expect.isTrue(streamedBytes > 0 && streamedBytes <= originalLength); 93 Expect.isTrue(streamedBytes > 0 && streamedBytes <= originalLength);
94 temp.delete(recursive: true).then((_) => keepAlive.close()); 94 temp.delete(recursive: true).then((_) => asyncEnd());
95 }, 95 },
96 onError: (e) { 96 onError: (e) {
97 Expect.fail("Unexpected error"); 97 Expect.fail("Unexpected error");
98 }); 98 });
99 } 99 }
100 100
101 void testInputStreamDelete() { 101 void testInputStreamDelete() {
102 var keepAlive = new ReceivePort(); 102 asyncStart();
103 var temp = new Directory('').createTempSync(); 103 var temp = new Directory('').createTempSync();
104 var file = new File('${temp.path}/input_stream_delete.txt'); 104 var file = new File('${temp.path}/input_stream_delete.txt');
105 var originalLength = writeLongFileSync(file); 105 var originalLength = writeLongFileSync(file);
106 // Start streaming the file. Pause after first chunk. Truncate 106 // Start streaming the file. Pause after first chunk. Truncate
107 // underlying file and check that the streaming stops with or 107 // underlying file and check that the streaming stops with or
108 // without getting all data. 108 // without getting all data.
109 var streamedBytes = 0; 109 var streamedBytes = 0;
110 var subscription; 110 var subscription;
111 subscription = file.openRead().listen( 111 subscription = file.openRead().listen(
112 (d) { 112 (d) {
(...skipping 10 matching lines...) Expand all
123 // somewhere else. The stream has this file open 123 // somewhere else. The stream has this file open
124 // and therefore we get an error on deletion on Windows. 124 // and therefore we get an error on deletion on Windows.
125 Expect.equals('windows', Platform.operatingSystem); 125 Expect.equals('windows', Platform.operatingSystem);
126 subscription.resume(); 126 subscription.resume();
127 }); 127 });
128 } 128 }
129 streamedBytes += d.length; 129 streamedBytes += d.length;
130 }, 130 },
131 onDone: () { 131 onDone: () {
132 Expect.equals(originalLength, streamedBytes); 132 Expect.equals(originalLength, streamedBytes);
133 temp.delete(recursive: true).then((_) => keepAlive.close()); 133 temp.delete(recursive: true).then((_) => asyncEnd());
134 }, 134 },
135 onError: (e) { 135 onError: (e) {
136 Expect.fail("Unexpected error"); 136 Expect.fail("Unexpected error");
137 }); 137 });
138 } 138 }
139 139
140 140
141 void testInputStreamAppend() { 141 void testInputStreamAppend() {
142 var keepAlive = new ReceivePort(); 142 asyncStart();
143 var temp = new Directory('').createTempSync(); 143 var temp = new Directory('').createTempSync();
144 var file = new File('${temp.path}/input_stream_append.txt'); 144 var file = new File('${temp.path}/input_stream_append.txt');
145 var originalLength = writeLongFileSync(file); 145 var originalLength = writeLongFileSync(file);
146 // Start streaming the file. Pause after first chunk. Append to 146 // Start streaming the file. Pause after first chunk. Append to
147 // underlying file and check that the stream gets all the data. 147 // underlying file and check that the stream gets all the data.
148 var streamedBytes = 0; 148 var streamedBytes = 0;
149 var subscription; 149 var subscription;
150 subscription = file.openRead().listen( 150 subscription = file.openRead().listen(
151 (d) { 151 (d) {
152 if (streamedBytes == 0) { 152 if (streamedBytes == 0) {
153 subscription.pause(); 153 subscription.pause();
154 // Double the length of the underlying file. 154 // Double the length of the underlying file.
155 file.readAsBytes().then((bytes) { 155 file.readAsBytes().then((bytes) {
156 file.writeAsBytes(bytes, mode: FileMode.APPEND).then((_) { 156 file.writeAsBytes(bytes, mode: FileMode.APPEND).then((_) {
157 Expect.equals(2 * originalLength, file.lengthSync()); 157 Expect.equals(2 * originalLength, file.lengthSync());
158 subscription.resume(); 158 subscription.resume();
159 }); 159 });
160 }); 160 });
161 } 161 }
162 streamedBytes += d.length; 162 streamedBytes += d.length;
163 }, 163 },
164 onDone: () { 164 onDone: () {
165 Expect.equals(2 * originalLength, streamedBytes); 165 Expect.equals(2 * originalLength, streamedBytes);
166 temp.delete(recursive: true).then((_) => keepAlive.close()); 166 temp.delete(recursive: true).then((_) => asyncEnd());
167 }, 167 },
168 onError: (e) { 168 onError: (e) {
169 Expect.fail("Unexpected error"); 169 Expect.fail("Unexpected error");
170 }); 170 });
171 } 171 }
172 172
173 173
174 void testInputStreamOffset() { 174 void testInputStreamOffset() {
175 void test(int start, int end, int expectedBytes) { 175 void test(int start, int end, int expectedBytes) {
176 var keepAlive = new ReceivePort(); 176 asyncStart();
177 var temp = new Directory('').createTempSync(); 177 var temp = new Directory('').createTempSync();
178 var file = new File('${temp.path}/input_stream_offset.txt'); 178 var file = new File('${temp.path}/input_stream_offset.txt');
179 var originalLength = writeLongFileSync(file); 179 var originalLength = writeLongFileSync(file);
180 var streamedBytes = 0; 180 var streamedBytes = 0;
181 if (expectedBytes < 0) expectedBytes = originalLength + expectedBytes; 181 if (expectedBytes < 0) expectedBytes = originalLength + expectedBytes;
182 file.openRead(start, end).listen( 182 file.openRead(start, end).listen(
183 (d) { 183 (d) {
184 streamedBytes += d.length; 184 streamedBytes += d.length;
185 }, 185 },
186 onDone: () { 186 onDone: () {
187 Expect.equals(expectedBytes, streamedBytes); 187 Expect.equals(expectedBytes, streamedBytes);
188 temp.delete(recursive: true).then((_) => keepAlive.close()); 188 temp.delete(recursive: true).then((_) => asyncEnd());
189 }, 189 },
190 onError: (e) { 190 onError: (e) {
191 Expect.fail("Unexpected error"); 191 Expect.fail("Unexpected error");
192 }); 192 });
193 } 193 }
194 test(10, 20, 10); 194 test(10, 20, 10);
195 test(10, 11, 1); 195 test(10, 11, 1);
196 test(10, 10, 0); 196 test(10, 10, 0);
197 test(100000000, null, 0); 197 test(100000000, null, 0);
198 test(null, 0, 0); 198 test(null, 0, 0);
199 test(null, 1, 1); 199 test(null, 1, 1);
200 test(1, null, -1); 200 test(1, null, -1);
201 test(20, null, -20); 201 test(20, null, -20);
202 } 202 }
203 203
204 204
205 void testInputStreamBadOffset() { 205 void testInputStreamBadOffset() {
206 void test(int start, int end) { 206 void test(int start, int end) {
207 var keepAlive = new ReceivePort(); 207 asyncStart();
208 var temp = new Directory('').createTempSync(); 208 var temp = new Directory('').createTempSync();
209 var file = new File('${temp.path}/input_stream_bad_offset.txt'); 209 var file = new File('${temp.path}/input_stream_bad_offset.txt');
210 var originalLength = writeLongFileSync(file); 210 var originalLength = writeLongFileSync(file);
211 var streamedBytes = 0; 211 var streamedBytes = 0;
212 file.openRead(start, end).listen( 212 file.openRead(start, end).listen(
213 (d) { 213 (d) {
214 streamedBytes += d.length; 214 streamedBytes += d.length;
215 }, 215 },
216 onDone: () { 216 onDone: () {
217 temp.delete(recursive: true); 217 temp.delete(recursive: true);
218 }, 218 },
219 onError: (e) { 219 onError: (e) {
220 keepAlive.close(); 220 asyncEnd();
221 }); 221 });
222 } 222 }
223 test(-1, null); 223 test(-1, null);
224 test(100, 99); 224 test(100, 99);
225 test(null, -1); 225 test(null, -1);
226 } 226 }
227 227
228 228
229 void testStringLineSplitterEnding(String name, int length) { 229 void testStringLineSplitterEnding(String name, int length) {
230 String fileName = getFilename("tests/standalone/io/$name"); 230 String fileName = getFilename("tests/standalone/io/$name");
(...skipping 25 matching lines...) Expand all
256 testInputStreamDelete(); 256 testInputStreamDelete();
257 testInputStreamAppend(); 257 testInputStreamAppend();
258 testInputStreamOffset(); 258 testInputStreamOffset();
259 testInputStreamBadOffset(); 259 testInputStreamBadOffset();
260 // Check the length of these files as both are text files where one 260 // Check the length of these files as both are text files where one
261 // is without a terminating line separator which can easily be added 261 // is without a terminating line separator which can easily be added
262 // back if accidentally opened in a text editor. 262 // back if accidentally opened in a text editor.
263 testStringLineSplitterEnding("readline_test1.dat", 111); 263 testStringLineSplitterEnding("readline_test1.dat", 111);
264 testStringLineSplitterEnding("readline_test2.dat", 114); 264 testStringLineSplitterEnding("readline_test2.dat", 114);
265 } 265 }
OLDNEW
« no previous file with comments | « tests/standalone/io/file_fuzz_test.dart ('k') | tests/standalone/io/file_invalid_arguments_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698