1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, software |
---|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
15 | * See the License for the specific language governing permissions and |
---|
16 | * limitations under the License. |
---|
17 | */ |
---|
18 | package org.apache.hadoop.fs; |
---|
19 | |
---|
20 | import junit.framework.TestCase; |
---|
21 | import java.io.*; |
---|
22 | import org.apache.hadoop.conf.Configuration; |
---|
23 | import org.apache.hadoop.fs.FSDataInputStream; |
---|
24 | import org.apache.hadoop.fs.FileSystem; |
---|
25 | import org.apache.hadoop.fs.LocalFileSystem; |
---|
26 | import org.apache.hadoop.fs.Path; |
---|
27 | import org.apache.hadoop.util.StringUtils; |
---|
28 | |
---|
29 | /** |
---|
30 | * test for the input truncation bug when mark/reset is used. |
---|
31 | * HADOOP-1489 |
---|
32 | */ |
---|
33 | public class TestTruncatedInputBug extends TestCase { |
---|
34 | private static String TEST_ROOT_DIR = |
---|
35 | new Path(System.getProperty("test.build.data","/tmp")) |
---|
36 | .toString().replace(' ', '+'); |
---|
37 | |
---|
38 | private void writeFile(FileSystem fileSys, |
---|
39 | Path name, int nBytesToWrite) |
---|
40 | throws IOException { |
---|
41 | DataOutputStream out = fileSys.create(name); |
---|
42 | for (int i = 0; i < nBytesToWrite; ++i) { |
---|
43 | out.writeByte(0); |
---|
44 | } |
---|
45 | out.close(); |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | * When mark() is used on BufferedInputStream, the request |
---|
50 | * size on the checksum file system can be small. However, |
---|
51 | * checksum file system currently depends on the request size |
---|
52 | * >= bytesPerSum to work properly. |
---|
53 | */ |
---|
54 | public void testTruncatedInputBug() throws IOException { |
---|
55 | final int ioBufSize = 512; |
---|
56 | final int fileSize = ioBufSize*4; |
---|
57 | int filePos = 0; |
---|
58 | |
---|
59 | Configuration conf = new Configuration(); |
---|
60 | conf.setInt("io.file.buffer.size", ioBufSize); |
---|
61 | FileSystem fileSys = FileSystem.getLocal(conf); |
---|
62 | |
---|
63 | try { |
---|
64 | // First create a test input file. |
---|
65 | Path testFile = new Path(TEST_ROOT_DIR, "HADOOP-1489"); |
---|
66 | writeFile(fileSys, testFile, fileSize); |
---|
67 | assertTrue(fileSys.exists(testFile)); |
---|
68 | assertTrue(fileSys.getLength(testFile) == fileSize); |
---|
69 | |
---|
70 | // Now read the file for ioBufSize bytes |
---|
71 | FSDataInputStream in = fileSys.open(testFile, ioBufSize); |
---|
72 | // seek beyond data buffered by open |
---|
73 | filePos += ioBufSize * 2 + (ioBufSize - 10); |
---|
74 | in.seek(filePos); |
---|
75 | |
---|
76 | // read 4 more bytes before marking |
---|
77 | for (int i = 0; i < 4; ++i) { |
---|
78 | if (in.read() == -1) { |
---|
79 | break; |
---|
80 | } |
---|
81 | ++filePos; |
---|
82 | } |
---|
83 | |
---|
84 | // Now set mark() to trigger the bug |
---|
85 | // NOTE: in the fixed code, mark() does nothing (not supported) and |
---|
86 | // hence won't trigger this bug. |
---|
87 | in.mark(1); |
---|
88 | System.out.println("MARKED"); |
---|
89 | |
---|
90 | // Try to read the rest |
---|
91 | while (filePos < fileSize) { |
---|
92 | if (in.read() == -1) { |
---|
93 | break; |
---|
94 | } |
---|
95 | ++filePos; |
---|
96 | } |
---|
97 | in.close(); |
---|
98 | |
---|
99 | System.out.println("Read " + filePos + " bytes." |
---|
100 | + " file size=" + fileSize); |
---|
101 | assertTrue(filePos == fileSize); |
---|
102 | |
---|
103 | } finally { |
---|
104 | try { |
---|
105 | fileSys.close(); |
---|
106 | } catch (Exception e) { |
---|
107 | // noop |
---|
108 | } |
---|
109 | } |
---|
110 | } // end testTruncatedInputBug |
---|
111 | } |
---|