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.regionserver;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022import static org.junit.Assert.fail;
023
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.client.Mutation;
026import org.apache.hadoop.hbase.client.Put;
027import org.apache.hadoop.hbase.testclassification.RegionServerTests;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.apache.hadoop.hbase.util.Bytes;
030import org.apache.hadoop.hbase.util.Pair;
031import org.apache.hadoop.hbase.wal.WALEdit;
032import org.junit.ClassRule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035
036@Category({ RegionServerTests.class, SmallTests.class })
037public class TestMiniBatchOperationInProgress {
038
039  @ClassRule
040  public static final HBaseClassTestRule CLASS_RULE =
041    HBaseClassTestRule.forClass(TestMiniBatchOperationInProgress.class);
042
043  @Test
044  public void testMiniBatchOperationInProgressMethods() {
045    Pair<Mutation, Integer>[] operations = new Pair[10];
046    OperationStatus[] retCodeDetails = new OperationStatus[10];
047    WALEdit[] walEditsFromCoprocessors = new WALEdit[10];
048    for (int i = 0; i < 10; i++) {
049      operations[i] = new Pair<>(new Put(Bytes.toBytes(i)), null);
050    }
051    MiniBatchOperationInProgress<Pair<Mutation, Integer>> miniBatch =
052      new MiniBatchOperationInProgress<>(operations, retCodeDetails, walEditsFromCoprocessors, 0, 5,
053        5);
054
055    assertEquals(5, miniBatch.size());
056    assertTrue(Bytes.equals(Bytes.toBytes(0), miniBatch.getOperation(0).getFirst().getRow()));
057    assertTrue(Bytes.equals(Bytes.toBytes(2), miniBatch.getOperation(2).getFirst().getRow()));
058    assertTrue(Bytes.equals(Bytes.toBytes(4), miniBatch.getOperation(4).getFirst().getRow()));
059    try {
060      miniBatch.getOperation(5);
061      fail("Should throw Exception while accessing out of range");
062    } catch (ArrayIndexOutOfBoundsException e) {
063    }
064    miniBatch.setOperationStatus(1, OperationStatus.FAILURE);
065    assertEquals(OperationStatus.FAILURE, retCodeDetails[1]);
066    try {
067      miniBatch.setOperationStatus(6, OperationStatus.FAILURE);
068      fail("Should throw Exception while accessing out of range");
069    } catch (ArrayIndexOutOfBoundsException e) {
070    }
071    try {
072      miniBatch.setWalEdit(5, new WALEdit());
073      fail("Should throw Exception while accessing out of range");
074    } catch (ArrayIndexOutOfBoundsException e) {
075    }
076
077    miniBatch = new MiniBatchOperationInProgress<>(operations, retCodeDetails,
078      walEditsFromCoprocessors, 7, 10, 3);
079    try {
080      miniBatch.setWalEdit(-1, new WALEdit());
081      fail("Should throw Exception while accessing out of range");
082    } catch (ArrayIndexOutOfBoundsException e) {
083    }
084    try {
085      miniBatch.getOperation(-1);
086      fail("Should throw Exception while accessing out of range");
087    } catch (ArrayIndexOutOfBoundsException e) {
088    }
089    try {
090      miniBatch.getOperation(3);
091      fail("Should throw Exception while accessing out of range");
092    } catch (ArrayIndexOutOfBoundsException e) {
093    }
094    try {
095      miniBatch.getOperationStatus(9);
096      fail("Should throw Exception while accessing out of range");
097    } catch (ArrayIndexOutOfBoundsException e) {
098    }
099    try {
100      miniBatch.setOperationStatus(3, OperationStatus.FAILURE);
101      fail("Should throw Exception while accessing out of range");
102    } catch (ArrayIndexOutOfBoundsException e) {
103    }
104    assertTrue(Bytes.equals(Bytes.toBytes(7), miniBatch.getOperation(0).getFirst().getRow()));
105    assertTrue(Bytes.equals(Bytes.toBytes(9), miniBatch.getOperation(2).getFirst().getRow()));
106    miniBatch.setOperationStatus(1, OperationStatus.SUCCESS);
107    assertEquals(OperationStatus.SUCCESS, retCodeDetails[8]);
108    WALEdit wal = new WALEdit();
109    miniBatch.setWalEdit(0, wal);
110    assertEquals(wal, walEditsFromCoprocessors[7]);
111  }
112}