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