Skip to content

Commit 1b3e970

Browse files
more testing combinations
1 parent 62cefcd commit 1b3e970

File tree

3 files changed

+75
-46
lines changed

3 files changed

+75
-46
lines changed

core/src/test/java/org/apache/spark/io/GenericFileInputStreamSuite.java

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public abstract class GenericFileInputStreamSuite {
3737

3838
protected File inputFile;
3939

40-
protected InputStream inputStream;
40+
protected InputStream[] inputStreams;
4141

4242
@Before
4343
public void setUp() throws IOException {
@@ -54,77 +54,91 @@ public void tearDown() {
5454

5555
@Test
5656
public void testReadOneByte() throws IOException {
57-
for (int i = 0; i < randomBytes.length; i++) {
58-
assertEquals(randomBytes[i], (byte) inputStream.read());
57+
for (InputStream inputStream: inputStreams) {
58+
for (int i = 0; i < randomBytes.length; i++) {
59+
assertEquals(randomBytes[i], (byte) inputStream.read());
60+
}
5961
}
6062
}
6163

6264
@Test
6365
public void testReadMultipleBytes() throws IOException {
64-
byte[] readBytes = new byte[8 * 1024];
65-
int i = 0;
66-
while (i < randomBytes.length) {
67-
int read = inputStream.read(readBytes, 0, 8 * 1024);
68-
for (int j = 0; j < read; j++) {
69-
assertEquals(randomBytes[i], readBytes[j]);
70-
i++;
66+
for (InputStream inputStream: inputStreams) {
67+
byte[] readBytes = new byte[8 * 1024];
68+
int i = 0;
69+
while (i < randomBytes.length) {
70+
int read = inputStream.read(readBytes, 0, 8 * 1024);
71+
for (int j = 0; j < read; j++) {
72+
assertEquals(randomBytes[i], readBytes[j]);
73+
i++;
74+
}
7175
}
7276
}
7377
}
7478

7579
@Test
7680
public void testBytesSkipped() throws IOException {
77-
assertEquals(1024, inputStream.skip(1024));
78-
for (int i = 1024; i < randomBytes.length; i++) {
79-
assertEquals(randomBytes[i], (byte) inputStream.read());
81+
for (InputStream inputStream: inputStreams) {
82+
assertEquals(1024, inputStream.skip(1024));
83+
for (int i = 1024; i < randomBytes.length; i++) {
84+
assertEquals(randomBytes[i], (byte) inputStream.read());
85+
}
8086
}
8187
}
8288

8389
@Test
8490
public void testBytesSkippedAfterRead() throws IOException {
85-
for (int i = 0; i < 1024; i++) {
86-
assertEquals(randomBytes[i], (byte) inputStream.read());
87-
}
88-
assertEquals(1024, inputStream.skip(1024));
89-
for (int i = 2048; i < randomBytes.length; i++) {
90-
assertEquals(randomBytes[i], (byte) inputStream.read());
91+
for (InputStream inputStream: inputStreams) {
92+
for (int i = 0; i < 1024; i++) {
93+
assertEquals(randomBytes[i], (byte) inputStream.read());
94+
}
95+
assertEquals(1024, inputStream.skip(1024));
96+
for (int i = 2048; i < randomBytes.length; i++) {
97+
assertEquals(randomBytes[i], (byte) inputStream.read());
98+
}
9199
}
92100
}
93101

94102
@Test
95103
public void testNegativeBytesSkippedAfterRead() throws IOException {
96-
for (int i = 0; i < 1024; i++) {
97-
assertEquals(randomBytes[i], (byte) inputStream.read());
98-
}
99-
// Skipping negative bytes should essential be a no-op
100-
assertEquals(0, inputStream.skip(-1));
101-
assertEquals(0, inputStream.skip(-1024));
102-
assertEquals(0, inputStream.skip(Long.MIN_VALUE));
103-
assertEquals(1024, inputStream.skip(1024));
104-
for (int i = 2048; i < randomBytes.length; i++) {
105-
assertEquals(randomBytes[i], (byte) inputStream.read());
104+
for (InputStream inputStream: inputStreams) {
105+
for (int i = 0; i < 1024; i++) {
106+
assertEquals(randomBytes[i], (byte) inputStream.read());
107+
}
108+
// Skipping negative bytes should essential be a no-op
109+
assertEquals(0, inputStream.skip(-1));
110+
assertEquals(0, inputStream.skip(-1024));
111+
assertEquals(0, inputStream.skip(Long.MIN_VALUE));
112+
assertEquals(1024, inputStream.skip(1024));
113+
for (int i = 2048; i < randomBytes.length; i++) {
114+
assertEquals(randomBytes[i], (byte) inputStream.read());
115+
}
106116
}
107117
}
108118

109119
@Test
110120
public void testSkipFromFileChannel() throws IOException {
111-
// Since the buffer is smaller than the skipped bytes, this will guarantee
112-
// we skip from underlying file channel.
113-
assertEquals(1024, inputStream.skip(1024));
114-
for (int i = 1024; i < 2048; i++) {
115-
assertEquals(randomBytes[i], (byte) inputStream.read());
116-
}
117-
assertEquals(256, inputStream.skip(256));
118-
assertEquals(256, inputStream.skip(256));
119-
assertEquals(512, inputStream.skip(512));
120-
for (int i = 3072; i < randomBytes.length; i++) {
121-
assertEquals(randomBytes[i], (byte) inputStream.read());
121+
for (InputStream inputStream: inputStreams) {
122+
// Since the buffer is smaller than the skipped bytes, this will guarantee
123+
// we skip from underlying file channel.
124+
assertEquals(1024, inputStream.skip(1024));
125+
for (int i = 1024; i < 2048; i++) {
126+
assertEquals(randomBytes[i], (byte) inputStream.read());
127+
}
128+
assertEquals(256, inputStream.skip(256));
129+
assertEquals(256, inputStream.skip(256));
130+
assertEquals(512, inputStream.skip(512));
131+
for (int i = 3072; i < randomBytes.length; i++) {
132+
assertEquals(randomBytes[i], (byte) inputStream.read());
133+
}
122134
}
123135
}
124136

125137
@Test
126138
public void testBytesSkippedAfterEOF() throws IOException {
127-
assertEquals(randomBytes.length, inputStream.skip(randomBytes.length + 1));
128-
assertEquals(-1, inputStream.read());
139+
for (InputStream inputStream: inputStreams) {
140+
assertEquals(randomBytes.length, inputStream.skip(randomBytes.length + 1));
141+
assertEquals(-1, inputStream.read());
142+
}
129143
}
130144
}

core/src/test/java/org/apache/spark/io/NioBufferedInputStreamSuite.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.junit.Before;
2020

21+
import java.io.InputStream;
2122
import java.io.IOException;
2223

2324
/**
@@ -28,6 +29,9 @@ public class NioBufferedInputStreamSuite extends GenericFileInputStreamSuite {
2829
@Before
2930
public void setUp() throws IOException {
3031
super.setUp();
31-
inputStream = new NioBufferedFileInputStream(inputFile);
32+
inputStreams = new InputStream[] {
33+
new NioBufferedFileInputStream(inputFile), // default
34+
new NioBufferedFileInputStream(inputFile, 123) // small, unaligned buffer
35+
};
3236
}
3337
}

core/src/test/java/org/apache/spark/io/ReadAheadInputStreamSuite.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,27 @@
1919
import org.junit.Before;
2020

2121
import java.io.IOException;
22+
import java.io.InputStream;
2223

2324
/**
24-
* Tests functionality of {@link NioBufferedFileInputStream}
25+
* Tests functionality of {@link ReadAheadInputStreamSuite}
2526
*/
2627
public class ReadAheadInputStreamSuite extends GenericFileInputStreamSuite {
2728

2829
@Before
2930
public void setUp() throws IOException {
3031
super.setUp();
31-
inputStream = new ReadAheadInputStream(
32-
new NioBufferedFileInputStream(inputFile, 123), 321);
32+
inputStreams = new InputStream[] {
33+
// Tests equal and aligned buffers of wrapped an outer stream.
34+
new ReadAheadInputStream(new NioBufferedFileInputStream(inputFile, 8 * 1024), 8 * 1024),
35+
// Tests aligned buffers, wrapped bigger than outer.
36+
new ReadAheadInputStream(new NioBufferedFileInputStream(inputFile, 3 * 1024), 2 * 1024),
37+
// Tests aligned buffers, wrapped smaller than outer.
38+
new ReadAheadInputStream(new NioBufferedFileInputStream(inputFile, 2 * 1024), 3 * 1024),
39+
// Tests unaligned buffers, wrapped bigger than outer.
40+
new ReadAheadInputStream(new NioBufferedFileInputStream(inputFile, 321), 123),
41+
// Tests unaligned buffers, wrapped smaller than outer.
42+
new ReadAheadInputStream(new NioBufferedFileInputStream(inputFile, 123), 321)
43+
};
3344
}
3445
}

0 commit comments

Comments
 (0)