HBASE-26567 Remove IndexType from ChunkCreator (#3947)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / ipc / TestBufferChain.java
blob1280872a6db7f5ba7e5bef24d4167c731fbb9b9d
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.hadoop.hbase.ipc;
20 import static org.junit.Assert.*;
22 import java.io.File;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.nio.ByteBuffer;
26 import java.nio.channels.FileChannel;
27 import org.apache.hadoop.hbase.HBaseClassTestRule;
28 import org.apache.hadoop.hbase.testclassification.RPCTests;
29 import org.apache.hadoop.hbase.testclassification.SmallTests;
30 import org.apache.hadoop.hbase.util.Bytes;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.ClassRule;
34 import org.junit.Test;
35 import org.junit.experimental.categories.Category;
36 import org.mockito.Mockito;
38 import org.apache.hbase.thirdparty.com.google.common.base.Charsets;
39 import org.apache.hbase.thirdparty.com.google.common.io.Files;
41 @Category({RPCTests.class, SmallTests.class})
42 public class TestBufferChain {
44 @ClassRule
45 public static final HBaseClassTestRule CLASS_RULE =
46 HBaseClassTestRule.forClass(TestBufferChain.class);
48 private File tmpFile;
50 private static final byte[][] HELLO_WORLD_CHUNKS = new byte[][] {
51 "hello".getBytes(Charsets.UTF_8),
52 " ".getBytes(Charsets.UTF_8),
53 "world".getBytes(Charsets.UTF_8)
56 @Before
57 public void setup() throws IOException {
58 tmpFile = File.createTempFile("TestBufferChain", "txt");
61 @After
62 public void teardown() {
63 tmpFile.delete();
66 @Test
67 public void testGetBackBytesWePutIn() {
68 ByteBuffer[] bufs = wrapArrays(HELLO_WORLD_CHUNKS);
69 BufferChain chain = new BufferChain(bufs);
70 assertTrue(Bytes.equals(Bytes.toBytes("hello world"), chain.getBytes()));
73 @Test
74 public void testChainChunkBiggerThanWholeArray() throws IOException {
75 ByteBuffer[] bufs = wrapArrays(HELLO_WORLD_CHUNKS);
76 BufferChain chain = new BufferChain(bufs);
77 writeAndVerify(chain, "hello world", 8192);
78 assertNoRemaining(bufs);
81 @Test
82 public void testChainChunkBiggerThanSomeArrays() throws IOException {
83 ByteBuffer[] bufs = wrapArrays(HELLO_WORLD_CHUNKS);
84 BufferChain chain = new BufferChain(bufs);
85 writeAndVerify(chain, "hello world", 3);
86 assertNoRemaining(bufs);
89 @Test
90 public void testLimitOffset() throws IOException {
91 ByteBuffer[] bufs = new ByteBuffer[] {
92 stringBuf("XXXhelloYYY", 3, 5),
93 stringBuf(" ", 0, 1),
94 stringBuf("XXXXworldY", 4, 5) };
95 BufferChain chain = new BufferChain(bufs);
96 writeAndVerify(chain , "hello world", 3);
97 assertNoRemaining(bufs);
100 @Test
101 public void testWithSpy() throws IOException {
102 ByteBuffer[] bufs = new ByteBuffer[] {
103 stringBuf("XXXhelloYYY", 3, 5),
104 stringBuf(" ", 0, 1),
105 stringBuf("XXXXworldY", 4, 5) };
106 BufferChain chain = new BufferChain(bufs);
107 FileOutputStream fos = new FileOutputStream(tmpFile);
108 FileChannel ch = Mockito.spy(fos.getChannel());
109 try {
110 chain.write(ch, 2);
111 assertEquals("he", Files.toString(tmpFile, Charsets.UTF_8));
112 chain.write(ch, 2);
113 assertEquals("hell", Files.toString(tmpFile, Charsets.UTF_8));
114 chain.write(ch, 3);
115 assertEquals("hello w", Files.toString(tmpFile, Charsets.UTF_8));
116 chain.write(ch, 8);
117 assertEquals("hello world", Files.toString(tmpFile, Charsets.UTF_8));
118 } finally {
119 ch.close();
120 fos.close();
124 private ByteBuffer stringBuf(String string, int position, int length) {
125 ByteBuffer buf = ByteBuffer.wrap(string.getBytes(Charsets.UTF_8));
126 buf.position(position);
127 buf.limit(position + length);
128 assertTrue(buf.hasRemaining());
129 return buf;
132 private void assertNoRemaining(ByteBuffer[] bufs) {
133 for (ByteBuffer buf : bufs) {
134 assertFalse(buf.hasRemaining());
138 private ByteBuffer[] wrapArrays(byte[][] arrays) {
139 ByteBuffer[] ret = new ByteBuffer[arrays.length];
140 for (int i = 0; i < arrays.length; i++) {
141 ret[i] = ByteBuffer.wrap(arrays[i]);
143 return ret;
146 private void writeAndVerify(BufferChain chain, String string, int chunkSize)
147 throws IOException {
148 FileOutputStream fos = new FileOutputStream(tmpFile);
149 FileChannel ch = fos.getChannel();
150 try {
151 long remaining = string.length();
152 while (chain.hasRemaining()) {
153 long n = chain.write(ch, chunkSize);
154 assertTrue(n == chunkSize || n == remaining);
155 remaining -= n;
157 assertEquals(0, remaining);
158 } finally {
159 fos.close();
161 assertFalse(chain.hasRemaining());
162 assertEquals(string, Files.toString(tmpFile, Charsets.UTF_8));