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.io.hfile.bucket; 019 020import static org.junit.Assert.assertTrue; 021 022import java.io.File; 023import java.io.IOException; 024import java.nio.ByteBuffer; 025import org.apache.hadoop.hbase.HBaseClassTestRule; 026import org.apache.hadoop.hbase.io.hfile.bucket.TestByteBufferIOEngine.BufferGrabbingDeserializer; 027import org.apache.hadoop.hbase.nio.ByteBuff; 028import org.apache.hadoop.hbase.testclassification.IOTests; 029import org.apache.hadoop.hbase.testclassification.SmallTests; 030import org.junit.ClassRule; 031import org.junit.Test; 032import org.junit.experimental.categories.Category; 033 034/** 035 * Basic test for {@link FileMmapEngine} 036 */ 037@Category({IOTests.class, SmallTests.class}) 038public class TestFileMmapEngine { 039 040 @ClassRule 041 public static final HBaseClassTestRule CLASS_RULE = 042 HBaseClassTestRule.forClass(TestFileMmapEngine.class); 043 044 @Test 045 public void testFileMmapEngine() throws IOException { 046 int size = 2 * 1024 * 1024; // 2 MB 047 String filePath = "testFileMmapEngine"; 048 try { 049 FileMmapEngine fileMmapEngine = new FileMmapEngine(filePath, size); 050 for (int i = 0; i < 50; i++) { 051 int len = (int) Math.floor(Math.random() * 100); 052 long offset = (long) Math.floor(Math.random() * size % (size - len)); 053 byte[] data1 = new byte[len]; 054 for (int j = 0; j < data1.length; ++j) { 055 data1[j] = (byte) (Math.random() * 255); 056 } 057 fileMmapEngine.write(ByteBuffer.wrap(data1), offset); 058 BufferGrabbingDeserializer deserializer = new BufferGrabbingDeserializer(); 059 fileMmapEngine.read(offset, len, deserializer); 060 ByteBuff data2 = deserializer.getDeserializedByteBuff(); 061 for (int j = 0; j < data1.length; ++j) { 062 assertTrue(data1[j] == data2.get(j)); 063 } 064 } 065 } finally { 066 File file = new File(filePath); 067 if (file.exists()) { 068 file.delete(); 069 } 070 } 071 072 } 073}