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 // OtherResources=readline_test1.dat | 5 // OtherResources=readline_test1.dat |
6 // | 6 // |
7 // VMOptions= | 7 // VMOptions= |
8 // VMOptions=--short_socket_read | 8 // VMOptions=--short_socket_read |
9 // VMOptions=--short_socket_write | 9 // VMOptions=--short_socket_write |
10 // VMOptions=--short_socket_read --short_socket_write | 10 // VMOptions=--short_socket_read --short_socket_write |
11 | 11 |
12 import "dart:io"; | 12 import "dart:io"; |
13 | 13 |
14 import "package:async_helper/async_helper.dart"; | 14 import "package:async_helper/async_helper.dart"; |
15 import "package:expect/expect.dart"; | 15 import "package:expect/expect.dart"; |
16 | 16 |
17 // Helper method to be able to run the test from the runtime | 17 // Helper method to be able to run the test from the runtime |
18 // directory, or the top directory. | 18 // directory, or the top directory. |
19 String getDataFilename(String path) => | 19 String getDataFilename(String path) => |
20 Platform.script.resolve(path).toFilePath(); | 20 Platform.script.resolve(path).toFilePath(); |
21 | 21 |
22 | 22 bool compareFileContent(String fileName1, String fileName2, |
23 bool compareFileContent(String fileName1, | 23 {int file1Offset: 0, int file2Offset: 0, int count}) { |
24 String fileName2, | |
25 {int file1Offset: 0, | |
26 int file2Offset: 0, | |
27 int count}) { | |
28 var file1 = new File(fileName1).openSync(); | 24 var file1 = new File(fileName1).openSync(); |
29 var file2 = new File(fileName2).openSync(); | 25 var file2 = new File(fileName2).openSync(); |
30 var length1 = file1.lengthSync(); | 26 var length1 = file1.lengthSync(); |
31 var length2 = file2.lengthSync(); | 27 var length2 = file2.lengthSync(); |
32 if (file1Offset == 0 && file2Offset == 0 && count == null) { | 28 if (file1Offset == 0 && file2Offset == 0 && count == null) { |
33 if (length1 != length2) { | 29 if (length1 != length2) { |
34 file1.closeSync(); | 30 file1.closeSync(); |
35 file2.closeSync(); | 31 file2.closeSync(); |
36 return false; | 32 return false; |
37 } | 33 } |
(...skipping 12 matching lines...) Expand all Loading... |
50 file1.closeSync(); | 46 file1.closeSync(); |
51 file2.closeSync(); | 47 file2.closeSync(); |
52 return false; | 48 return false; |
53 } | 49 } |
54 } | 50 } |
55 file1.closeSync(); | 51 file1.closeSync(); |
56 file2.closeSync(); | 52 file2.closeSync(); |
57 return true; | 53 return true; |
58 } | 54 } |
59 | 55 |
60 | |
61 // Test piping from one file to another and closing both streams | 56 // Test piping from one file to another and closing both streams |
62 // after wards. | 57 // after wards. |
63 testFileToFilePipe1() { | 58 testFileToFilePipe1() { |
64 // Force test to timeout if one of the handlers is | 59 // Force test to timeout if one of the handlers is |
65 // not called. | 60 // not called. |
66 asyncStart(); | 61 asyncStart(); |
67 | 62 |
68 String srcFileName = | 63 String srcFileName = getDataFilename("readline_test1.dat"); |
69 getDataFilename("readline_test1.dat"); | |
70 var srcStream = new File(srcFileName).openRead(); | 64 var srcStream = new File(srcFileName).openRead(); |
71 | 65 |
72 var tempDir = Directory.systemTemp.createTempSync('dart_stream_pipe'); | 66 var tempDir = Directory.systemTemp.createTempSync('dart_stream_pipe'); |
73 String dstFileName = tempDir.path + "/readline_test1.dat"; | 67 String dstFileName = tempDir.path + "/readline_test1.dat"; |
74 new File(dstFileName).createSync(); | 68 new File(dstFileName).createSync(); |
75 var output = new File(dstFileName).openWrite(); | 69 var output = new File(dstFileName).openWrite(); |
76 srcStream.pipe(output).then((_) { | 70 srcStream.pipe(output).then((_) { |
77 bool result = compareFileContent(srcFileName, dstFileName); | 71 bool result = compareFileContent(srcFileName, dstFileName); |
78 new File(dstFileName).deleteSync(); | 72 new File(dstFileName).deleteSync(); |
79 tempDir.deleteSync(); | 73 tempDir.deleteSync(); |
80 Expect.isTrue(result); | 74 Expect.isTrue(result); |
81 asyncEnd(); | 75 asyncEnd(); |
82 }); | 76 }); |
83 } | 77 } |
84 | 78 |
85 | |
86 // Test piping from one file to another and write additional data to | 79 // Test piping from one file to another and write additional data to |
87 // the output stream after piping finished. | 80 // the output stream after piping finished. |
88 testFileToFilePipe2() { | 81 testFileToFilePipe2() { |
89 // Force test to timeout if one of the handlers is | 82 // Force test to timeout if one of the handlers is |
90 // not called. | 83 // not called. |
91 asyncStart(); | 84 asyncStart(); |
92 | 85 |
93 String srcFileName = | 86 String srcFileName = getDataFilename("readline_test1.dat"); |
94 getDataFilename("readline_test1.dat"); | |
95 var srcFile = new File(srcFileName); | 87 var srcFile = new File(srcFileName); |
96 var srcStream = srcFile.openRead(); | 88 var srcStream = srcFile.openRead(); |
97 | 89 |
98 var tempDir = Directory.systemTemp.createTempSync('dart_stream_pipe'); | 90 var tempDir = Directory.systemTemp.createTempSync('dart_stream_pipe'); |
99 var dstFileName = tempDir.path + "/readline_test1.dat"; | 91 var dstFileName = tempDir.path + "/readline_test1.dat"; |
100 var dstFile = new File(dstFileName); | 92 var dstFile = new File(dstFileName); |
101 dstFile.createSync(); | 93 dstFile.createSync(); |
102 var output = dstFile.openWrite(); | 94 var output = dstFile.openWrite(); |
103 output.addStream(srcStream).then((_) { | 95 output.addStream(srcStream).then((_) { |
104 output.add([32]); | 96 output.add([32]); |
105 output.close(); | 97 output.close(); |
106 output.done.then((_) { | 98 output.done.then((_) { |
107 var src = srcFile.openSync(); | 99 var src = srcFile.openSync(); |
108 var dst = dstFile.openSync(); | 100 var dst = dstFile.openSync(); |
109 var srcLength = src.lengthSync(); | 101 var srcLength = src.lengthSync(); |
110 var dstLength = dst.lengthSync(); | 102 var dstLength = dst.lengthSync(); |
111 Expect.equals(srcLength + 1, dstLength); | 103 Expect.equals(srcLength + 1, dstLength); |
112 Expect.isTrue(compareFileContent(srcFileName, | 104 Expect.isTrue( |
113 dstFileName, | 105 compareFileContent(srcFileName, dstFileName, count: srcLength)); |
114 count: srcLength)); | |
115 dst.setPositionSync(srcLength); | 106 dst.setPositionSync(srcLength); |
116 var data = new List<int>(1); | 107 var data = new List<int>(1); |
117 var read2 = dst.readIntoSync(data, 0, 1); | 108 var read2 = dst.readIntoSync(data, 0, 1); |
118 Expect.equals(32, data[0]); | 109 Expect.equals(32, data[0]); |
119 src.closeSync(); | 110 src.closeSync(); |
120 dst.closeSync(); | 111 dst.closeSync(); |
121 dstFile.deleteSync(); | 112 dstFile.deleteSync(); |
122 tempDir.deleteSync(); | 113 tempDir.deleteSync(); |
123 asyncEnd(); | 114 asyncEnd(); |
124 }); | 115 }); |
125 }); | 116 }); |
126 } | 117 } |
127 | 118 |
128 | |
129 // Test piping two copies of one file to another. | 119 // Test piping two copies of one file to another. |
130 testFileToFilePipe3() { | 120 testFileToFilePipe3() { |
131 // Force test to timeout if one of the handlers is | 121 // Force test to timeout if one of the handlers is |
132 // not called. | 122 // not called. |
133 asyncStart(); | 123 asyncStart(); |
134 | 124 |
135 String srcFileName = | 125 String srcFileName = getDataFilename("readline_test1.dat"); |
136 getDataFilename("readline_test1.dat"); | |
137 var srcFile = new File(srcFileName); | 126 var srcFile = new File(srcFileName); |
138 var srcStream = srcFile.openRead(); | 127 var srcStream = srcFile.openRead(); |
139 | 128 |
140 var tempDir = Directory.systemTemp.createTempSync('dart_stream_pipe'); | 129 var tempDir = Directory.systemTemp.createTempSync('dart_stream_pipe'); |
141 var dstFileName = tempDir.path + "/readline_test1.dat"; | 130 var dstFileName = tempDir.path + "/readline_test1.dat"; |
142 var dstFile = new File(dstFileName); | 131 var dstFile = new File(dstFileName); |
143 dstFile.createSync(); | 132 dstFile.createSync(); |
144 var output = dstFile.openWrite(); | 133 var output = dstFile.openWrite(); |
145 output.addStream(srcStream).then((_) { | 134 output.addStream(srcStream).then((_) { |
146 var srcStream2 = srcFile.openRead(); | 135 var srcStream2 = srcFile.openRead(); |
147 output.addStream(srcStream2).then((_) { | 136 output.addStream(srcStream2).then((_) { |
148 output.close(); | 137 output.close(); |
149 output.done.then((_) { | 138 output.done.then((_) { |
150 var src = srcFile.openSync(); | 139 var src = srcFile.openSync(); |
151 var dst = dstFile.openSync(); | 140 var dst = dstFile.openSync(); |
152 var srcLength = src.lengthSync(); | 141 var srcLength = src.lengthSync(); |
153 var dstLength = dst.lengthSync(); | 142 var dstLength = dst.lengthSync(); |
154 Expect.equals(srcLength * 2, dstLength); | 143 Expect.equals(srcLength * 2, dstLength); |
155 Expect.isTrue(compareFileContent(srcFileName, | 144 Expect.isTrue( |
156 dstFileName, | 145 compareFileContent(srcFileName, dstFileName, count: srcLength)); |
157 count: srcLength)); | 146 Expect.isTrue(compareFileContent(srcFileName, dstFileName, |
158 Expect.isTrue(compareFileContent(srcFileName, | 147 file2Offset: srcLength, count: srcLength)); |
159 dstFileName, | |
160 file2Offset: srcLength, | |
161 count: srcLength)); | |
162 src.closeSync(); | 148 src.closeSync(); |
163 dst.closeSync(); | 149 dst.closeSync(); |
164 dstFile.deleteSync(); | 150 dstFile.deleteSync(); |
165 tempDir.deleteSync(); | 151 tempDir.deleteSync(); |
166 asyncEnd(); | 152 asyncEnd(); |
167 }); | 153 }); |
168 }); | 154 }); |
169 }); | 155 }); |
170 } | 156 } |
171 | 157 |
172 | |
173 main() { | 158 main() { |
174 testFileToFilePipe1(); | 159 testFileToFilePipe1(); |
175 testFileToFilePipe2(); | 160 testFileToFilePipe2(); |
176 testFileToFilePipe3(); | 161 testFileToFilePipe3(); |
177 } | 162 } |
OLD | NEW |