001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.ipc;
019
020import static org.junit.Assert.*;
021
022import java.io.File;
023import java.io.FileOutputStream;
024import java.io.IOException;
025import java.nio.ByteBuffer;
026import java.nio.channels.FileChannel;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.testclassification.RPCTests;
029import org.apache.hadoop.hbase.testclassification.SmallTests;
030import org.apache.hadoop.hbase.util.Bytes;
031import org.junit.After;
032import org.junit.Before;
033import org.junit.ClassRule;
034import org.junit.Test;
035import org.junit.experimental.categories.Category;
036import org.mockito.Mockito;
037
038import org.apache.hbase.thirdparty.com.google.common.base.Charsets;
039import org.apache.hbase.thirdparty.com.google.common.io.Files;
040
041@Category({ RPCTests.class, SmallTests.class })
042public class TestBufferChain {
043
044  @ClassRule
045  public static final HBaseClassTestRule CLASS_RULE =
046    HBaseClassTestRule.forClass(TestBufferChain.class);
047
048  private File tmpFile;
049
050  private static final byte[][] HELLO_WORLD_CHUNKS =
051    new byte[][] { "hello".getBytes(Charsets.UTF_8), " ".getBytes(Charsets.UTF_8),
052      "world".getBytes(Charsets.UTF_8) };
053
054  @Before
055  public void setup() throws IOException {
056    tmpFile = File.createTempFile("TestBufferChain", "txt");
057  }
058
059  @After
060  public void teardown() {
061    tmpFile.delete();
062  }
063
064  @Test
065  public void testGetBackBytesWePutIn() {
066    ByteBuffer[] bufs = wrapArrays(HELLO_WORLD_CHUNKS);
067    BufferChain chain = new BufferChain(bufs);
068    assertTrue(Bytes.equals(Bytes.toBytes("hello world"), chain.getBytes()));
069  }
070
071  @Test
072  public void testChainChunkBiggerThanWholeArray() throws IOException {
073    ByteBuffer[] bufs = wrapArrays(HELLO_WORLD_CHUNKS);
074    BufferChain chain = new BufferChain(bufs);
075    writeAndVerify(chain, "hello world", 8192);
076    assertNoRemaining(bufs);
077  }
078
079  @Test
080  public void testChainChunkBiggerThanSomeArrays() throws IOException {
081    ByteBuffer[] bufs = wrapArrays(HELLO_WORLD_CHUNKS);
082    BufferChain chain = new BufferChain(bufs);
083    writeAndVerify(chain, "hello world", 3);
084    assertNoRemaining(bufs);
085  }
086
087  @Test
088  public void testLimitOffset() throws IOException {
089    ByteBuffer[] bufs = new ByteBuffer[] { stringBuf("XXXhelloYYY", 3, 5), stringBuf(" ", 0, 1),
090      stringBuf("XXXXworldY", 4, 5) };
091    BufferChain chain = new BufferChain(bufs);
092    writeAndVerify(chain, "hello world", 3);
093    assertNoRemaining(bufs);
094  }
095
096  @Test
097  public void testWithSpy() throws IOException {
098    ByteBuffer[] bufs = new ByteBuffer[] { stringBuf("XXXhelloYYY", 3, 5), stringBuf(" ", 0, 1),
099      stringBuf("XXXXworldY", 4, 5) };
100    BufferChain chain = new BufferChain(bufs);
101    FileOutputStream fos = new FileOutputStream(tmpFile);
102    FileChannel ch = Mockito.spy(fos.getChannel());
103    try {
104      chain.write(ch, 2);
105      assertEquals("he", Files.toString(tmpFile, Charsets.UTF_8));
106      chain.write(ch, 2);
107      assertEquals("hell", Files.toString(tmpFile, Charsets.UTF_8));
108      chain.write(ch, 3);
109      assertEquals("hello w", Files.toString(tmpFile, Charsets.UTF_8));
110      chain.write(ch, 8);
111      assertEquals("hello world", Files.toString(tmpFile, Charsets.UTF_8));
112    } finally {
113      ch.close();
114      fos.close();
115    }
116  }
117
118  private ByteBuffer stringBuf(String string, int position, int length) {
119    ByteBuffer buf = ByteBuffer.wrap(string.getBytes(Charsets.UTF_8));
120    buf.position(position);
121    buf.limit(position + length);
122    assertTrue(buf.hasRemaining());
123    return buf;
124  }
125
126  private void assertNoRemaining(ByteBuffer[] bufs) {
127    for (ByteBuffer buf : bufs) {
128      assertFalse(buf.hasRemaining());
129    }
130  }
131
132  private ByteBuffer[] wrapArrays(byte[][] arrays) {
133    ByteBuffer[] ret = new ByteBuffer[arrays.length];
134    for (int i = 0; i < arrays.length; i++) {
135      ret[i] = ByteBuffer.wrap(arrays[i]);
136    }
137    return ret;
138  }
139
140  private void writeAndVerify(BufferChain chain, String string, int chunkSize) throws IOException {
141    FileOutputStream fos = new FileOutputStream(tmpFile);
142    FileChannel ch = fos.getChannel();
143    try {
144      long remaining = string.length();
145      while (chain.hasRemaining()) {
146        long n = chain.write(ch, chunkSize);
147        assertTrue(n == chunkSize || n == remaining);
148        remaining -= n;
149      }
150      assertEquals(0, remaining);
151    } finally {
152      fos.close();
153    }
154    assertFalse(chain.hasRemaining());
155    assertEquals(string, Files.toString(tmpFile, Charsets.UTF_8));
156  }
157}