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.client;
019
020import static org.junit.Assert.assertArrayEquals;
021import static org.junit.Assert.assertNotNull;
022
023import java.io.IOException;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.testclassification.ClientTests;
026import org.apache.hadoop.hbase.testclassification.MediumTests;
027import org.apache.hadoop.hbase.util.Bytes;
028import org.junit.BeforeClass;
029import org.junit.ClassRule;
030import org.junit.experimental.categories.Category;
031import org.junit.runner.RunWith;
032import org.junit.runners.Parameterized;
033
034@RunWith(Parameterized.class)
035@Category({ MediumTests.class, ClientTests.class })
036public class TestAsyncTableRegionReplicasScan extends AbstractTestAsyncTableRegionReplicasRead {
037
038  @ClassRule
039  public static final HBaseClassTestRule CLASS_RULE =
040    HBaseClassTestRule.forClass(TestAsyncTableRegionReplicasScan.class);
041
042  private static int ROW_COUNT = 1000;
043
044  private static byte[] getRow(int i) {
045    return Bytes.toBytes(String.format("%s-%03d", Bytes.toString(ROW), i));
046  }
047
048  private static byte[] getValue(int i) {
049    return Bytes.toBytes(String.format("%s-%03d", Bytes.toString(VALUE), i));
050  }
051
052  @BeforeClass
053  public static void setUpBeforeClass() throws Exception {
054    startClusterAndCreateTable();
055    AsyncTable<?> table = ASYNC_CONN.getTable(TABLE_NAME);
056    for (int i = 0; i < ROW_COUNT; i++) {
057      table.put(new Put(getRow(i)).addColumn(FAMILY, QUALIFIER, getValue(i))).get();
058    }
059    waitUntilAllReplicasHaveRow(getRow(ROW_COUNT - 1));
060  }
061
062  @Override
063  protected void readAndCheck(AsyncTable<?> table, int replicaId) throws IOException {
064    Scan scan = new Scan().setConsistency(Consistency.TIMELINE).setCaching(1);
065    if (replicaId >= 0) {
066      scan.setReplicaId(replicaId);
067    }
068    try (ResultScanner scanner = table.getScanner(scan)) {
069      for (int i = 0; i < ROW_COUNT; i++) {
070        Result result = scanner.next();
071        assertNotNull(result);
072        assertArrayEquals(getValue(i), result.getValue(FAMILY, QUALIFIER));
073      }
074    }
075  }
076}