1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.io.hfile;
19
20 import static org.junit.Assert.*;
21 import static org.mockito.Mockito.*;
22
23 import java.io.IOException;
24
25 import org.apache.hadoop.fs.FSDataInputStream;
26 import org.apache.hadoop.hbase.testclassification.IOTests;
27 import org.apache.hadoop.hbase.testclassification.SmallTests;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.experimental.categories.Category;
31 import org.junit.rules.ExpectedException;
32
33
34
35
36 @Category({IOTests.class, SmallTests.class})
37 public class TestHFileBlockPositionalRead {
38
39 @Rule
40 public ExpectedException exception = ExpectedException.none();
41
42 @Test
43 public void testPositionalReadNoExtra() throws IOException {
44 long position = 0;
45 int bufOffset = 0;
46 int necessaryLen = 10;
47 int extraLen = 0;
48 int totalLen = necessaryLen + extraLen;
49 byte[] buf = new byte[totalLen];
50 FSDataInputStream in = mock(FSDataInputStream.class);
51 when(in.read(position, buf, bufOffset, totalLen)).thenReturn(totalLen);
52 boolean ret = HFileBlock.positionalReadWithExtra(in, position, buf,
53 bufOffset, necessaryLen, extraLen);
54 assertFalse("Expect false return when no extra bytes requested", ret);
55 verify(in).read(position, buf, bufOffset, totalLen);
56 verifyNoMoreInteractions(in);
57 }
58
59 @Test
60 public void testPositionalReadShortReadOfNecessaryBytes() throws IOException {
61 long position = 0;
62 int bufOffset = 0;
63 int necessaryLen = 10;
64 int extraLen = 0;
65 int totalLen = necessaryLen + extraLen;
66 byte[] buf = new byte[totalLen];
67 FSDataInputStream in = mock(FSDataInputStream.class);
68 when(in.read(position, buf, bufOffset, totalLen)).thenReturn(5);
69 when(in.read(5, buf, 5, 5)).thenReturn(5);
70 boolean ret = HFileBlock.positionalReadWithExtra(in, position, buf,
71 bufOffset, necessaryLen, extraLen);
72 assertFalse("Expect false return when no extra bytes requested", ret);
73 verify(in).read(position, buf, bufOffset, totalLen);
74 verify(in).read(5, buf, 5, 5);
75 verifyNoMoreInteractions(in);
76 }
77
78 @Test
79 public void testPositionalReadExtraSucceeded() throws IOException {
80 long position = 0;
81 int bufOffset = 0;
82 int necessaryLen = 10;
83 int extraLen = 5;
84 int totalLen = necessaryLen + extraLen;
85 byte[] buf = new byte[totalLen];
86 FSDataInputStream in = mock(FSDataInputStream.class);
87 when(in.read(position, buf, bufOffset, totalLen)).thenReturn(totalLen);
88 boolean ret = HFileBlock.positionalReadWithExtra(in, position, buf,
89 bufOffset, necessaryLen, extraLen);
90 assertTrue("Expect true return when reading extra bytes succeeds", ret);
91 verify(in).read(position, buf, bufOffset, totalLen);
92 verifyNoMoreInteractions(in);
93 }
94
95 @Test
96 public void testPositionalReadExtraFailed() throws IOException {
97 long position = 0;
98 int bufOffset = 0;
99 int necessaryLen = 10;
100 int extraLen = 5;
101 int totalLen = necessaryLen + extraLen;
102 byte[] buf = new byte[totalLen];
103 FSDataInputStream in = mock(FSDataInputStream.class);
104 when(in.read(position, buf, bufOffset, totalLen)).thenReturn(necessaryLen);
105 boolean ret = HFileBlock.positionalReadWithExtra(in, position, buf,
106 bufOffset, necessaryLen, extraLen);
107 assertFalse("Expect false return when reading extra bytes fails", ret);
108 verify(in).read(position, buf, bufOffset, totalLen);
109 verifyNoMoreInteractions(in);
110 }
111
112 @Test
113 public void testPositionalReadShortReadCompletesNecessaryAndExtraBytes()
114 throws IOException {
115 long position = 0;
116 int bufOffset = 0;
117 int necessaryLen = 10;
118 int extraLen = 5;
119 int totalLen = necessaryLen + extraLen;
120 byte[] buf = new byte[totalLen];
121 FSDataInputStream in = mock(FSDataInputStream.class);
122 when(in.read(position, buf, bufOffset, totalLen)).thenReturn(5);
123 when(in.read(5, buf, 5, 10)).thenReturn(10);
124 boolean ret = HFileBlock.positionalReadWithExtra(in, position, buf,
125 bufOffset, necessaryLen, extraLen);
126 assertTrue("Expect true return when reading extra bytes succeeds", ret);
127 verify(in).read(position, buf, bufOffset, totalLen);
128 verify(in).read(5, buf, 5, 10);
129 verifyNoMoreInteractions(in);
130 }
131
132 @Test
133 public void testPositionalReadPrematureEOF() throws IOException {
134 long position = 0;
135 int bufOffset = 0;
136 int necessaryLen = 10;
137 int extraLen = 0;
138 int totalLen = necessaryLen + extraLen;
139 byte[] buf = new byte[totalLen];
140 FSDataInputStream in = mock(FSDataInputStream.class);
141 when(in.read(position, buf, bufOffset, totalLen)).thenReturn(9);
142 when(in.read(position, buf, bufOffset, totalLen)).thenReturn(-1);
143 exception.expect(IOException.class);
144 exception.expectMessage("EOF");
145 HFileBlock.positionalReadWithExtra(in, position, buf, bufOffset,
146 necessaryLen, extraLen);
147 }
148 }