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