|
21 | 21 | import org.apache.hadoop.fs.FSDataOutputStream; |
22 | 22 | import org.apache.hadoop.fs.FileAlreadyExistsException; |
23 | 23 | import org.apache.hadoop.fs.FileStatus; |
| 24 | +import org.apache.hadoop.fs.FileSystem; |
24 | 25 | import org.apache.hadoop.fs.Path; |
25 | | -import org.apache.hadoop.io.IOUtils; |
26 | 26 | import org.junit.Test; |
27 | 27 | import org.junit.internal.AssumptionViolatedException; |
28 | 28 |
|
29 | 29 | import java.io.FileNotFoundException; |
30 | 30 | import java.io.IOException; |
31 | 31 |
|
32 | 32 | import static org.apache.hadoop.fs.contract.ContractTestUtils.dataset; |
| 33 | +import static org.apache.hadoop.fs.contract.ContractTestUtils.getFileStatusEventually; |
33 | 34 | import static org.apache.hadoop.fs.contract.ContractTestUtils.skip; |
34 | 35 | import static org.apache.hadoop.fs.contract.ContractTestUtils.writeDataset; |
35 | 36 | import static org.apache.hadoop.fs.contract.ContractTestUtils.writeTextFile; |
36 | 37 |
|
37 | 38 | /** |
38 | | - * Test creating files, overwrite options &c |
| 39 | + * Test creating files, overwrite options etc. |
39 | 40 | */ |
40 | 41 | public abstract class AbstractContractCreateTest extends |
41 | 42 | AbstractFSContractTestBase { |
42 | 43 |
|
| 44 | + /** |
| 45 | + * How long to wait for a path to become visible. |
| 46 | + */ |
| 47 | + public static final int CREATE_TIMEOUT = 15000; |
| 48 | + |
43 | 49 | @Test |
44 | 50 | public void testCreateNewFile() throws Throwable { |
45 | 51 | describe("Foundational 'create a file' test"); |
@@ -180,4 +186,90 @@ public void testCreatedFileIsImmediatelyVisible() throws Throwable { |
180 | 186 | } |
181 | 187 | } |
182 | 188 | } |
| 189 | + |
| 190 | + @Test |
| 191 | + public void testCreatedFileIsVisibleOnFlush() throws Throwable { |
| 192 | + describe("verify that a newly created file exists once a flush has taken " |
| 193 | + + "place"); |
| 194 | + Path path = path("testCreatedFileIsVisibleOnFlush"); |
| 195 | + FileSystem fs = getFileSystem(); |
| 196 | + try(FSDataOutputStream out = fs.create(path, |
| 197 | + false, |
| 198 | + 4096, |
| 199 | + (short) 1, |
| 200 | + 1024)) { |
| 201 | + out.write('a'); |
| 202 | + out.flush(); |
| 203 | + if (!fs.exists(path)) { |
| 204 | + |
| 205 | + if (isSupported(IS_BLOBSTORE)) { |
| 206 | + // object store: downgrade to a skip so that the failure is visible |
| 207 | + // in test results |
| 208 | + skip("Filesystem is an object store and newly created files are not " |
| 209 | + + "immediately visible"); |
| 210 | + } |
| 211 | + assertPathExists("expected path to be visible before file closed", |
| 212 | + path); |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + @Test |
| 218 | + public void testCreatedFileIsEventuallyVisible() throws Throwable { |
| 219 | + describe("verify a written to file is visible after the stream is closed"); |
| 220 | + Path path = path("testCreatedFileIsEventuallyVisible"); |
| 221 | + FileSystem fs = getFileSystem(); |
| 222 | + try( |
| 223 | + FSDataOutputStream out = fs.create(path, |
| 224 | + false, |
| 225 | + 4096, |
| 226 | + (short) 1, |
| 227 | + 1024) |
| 228 | + ) { |
| 229 | + out.write(0x01); |
| 230 | + out.close(); |
| 231 | + getFileStatusEventually(fs, path, CREATE_TIMEOUT); |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + @Test |
| 236 | + public void testFileStatusBlocksizeNonEmptyFile() throws Throwable { |
| 237 | + describe("validate the block size of a filesystem and files within it"); |
| 238 | + FileSystem fs = getFileSystem(); |
| 239 | + |
| 240 | + long rootPath = fs.getDefaultBlockSize(path("/")); |
| 241 | + assertTrue("Root block size is invalid " + rootPath, |
| 242 | + rootPath > 0); |
| 243 | + |
| 244 | + Path path = path("testFileStatusBlocksizeNonEmptyFile"); |
| 245 | + byte[] data = dataset(256, 'a', 'z'); |
| 246 | + |
| 247 | + writeDataset(fs, path, data, data.length, 1024 * 1024, false); |
| 248 | + |
| 249 | + validateBlockSize(fs, path, 1); |
| 250 | + } |
| 251 | + |
| 252 | + @Test |
| 253 | + public void testFileStatusBlocksizeEmptyFile() throws Throwable { |
| 254 | + describe("check that an empty file may return a 0-byte blocksize"); |
| 255 | + FileSystem fs = getFileSystem(); |
| 256 | + Path path = path("testFileStatusBlocksizeEmptyFile"); |
| 257 | + ContractTestUtils.touch(fs, path); |
| 258 | + validateBlockSize(fs, path, 0); |
| 259 | + } |
| 260 | + |
| 261 | + private void validateBlockSize(FileSystem fs, Path path, int minValue) |
| 262 | + throws IOException, InterruptedException { |
| 263 | + FileStatus status = |
| 264 | + getFileStatusEventually(fs, path, CREATE_TIMEOUT); |
| 265 | + String statusDetails = status.toString(); |
| 266 | + assertTrue("File status block size too low: " + statusDetails |
| 267 | + + " min value: " + minValue, |
| 268 | + status.getBlockSize() >= minValue); |
| 269 | + long defaultBlockSize = fs.getDefaultBlockSize(path); |
| 270 | + assertTrue("fs.getDefaultBlockSize(" + path + ") size " + |
| 271 | + defaultBlockSize + " is below the minimum of " + minValue, |
| 272 | + defaultBlockSize >= minValue); |
| 273 | + } |
| 274 | + |
183 | 275 | } |
0 commit comments