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.util;
019
020import static org.junit.Assert.assertFalse;
021import static org.junit.Assert.assertTrue;
022
023import java.util.HashSet;
024import java.util.Random;
025import java.util.Set;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.testclassification.MiscTests;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.junit.ClassRule;
030import org.junit.Test;
031import org.junit.experimental.categories.Category;
032
033@Category({ MiscTests.class, SmallTests.class })
034public class TestLoadTestKVGenerator {
035
036  @ClassRule
037  public static final HBaseClassTestRule CLASS_RULE =
038    HBaseClassTestRule.forClass(TestLoadTestKVGenerator.class);
039
040  private static final int MIN_LEN = 10;
041  private static final int MAX_LEN = 20;
042
043  private Random rand = new Random(28937293L);
044  private LoadTestKVGenerator gen = new LoadTestKVGenerator(MIN_LEN, MAX_LEN);
045
046  @Test
047  public void testValueLength() {
048    for (int i = 0; i < 1000; ++i) {
049      byte[] v = gen.generateRandomSizeValue(Bytes.toBytes(Integer.toString(i)),
050        Bytes.toBytes(String.valueOf(rand.nextInt())));
051      assertTrue(MIN_LEN <= v.length);
052      assertTrue(v.length <= MAX_LEN);
053    }
054  }
055
056  @Test
057  public void testVerification() {
058    for (int i = 0; i < 1000; ++i) {
059      for (int qualIndex = 0; qualIndex < 20; ++qualIndex) {
060        byte[] qual = Bytes.toBytes(String.valueOf(qualIndex));
061        byte[] rowKey = Bytes.toBytes(LoadTestKVGenerator.md5PrefixedKey(i));
062        byte[] v = gen.generateRandomSizeValue(rowKey, qual);
063        assertTrue(LoadTestKVGenerator.verify(v, rowKey, qual));
064        v[0]++;
065        assertFalse(LoadTestKVGenerator.verify(v, rowKey, qual));
066      }
067    }
068  }
069
070  @Test
071  public void testCorrectAndUniqueKeys() {
072    Set<String> keys = new HashSet<>();
073    for (int i = 0; i < 1000; ++i) {
074      String k = LoadTestKVGenerator.md5PrefixedKey(i);
075      assertFalse(keys.contains(k));
076      assertTrue(k.endsWith("-" + i));
077      keys.add(k);
078    }
079  }
080
081}