Skip to content

Commit 13e2224

Browse files
committed
Add HBase batch current behavior tests
1 parent 8e25e07 commit 13e2224

1 file changed

Lines changed: 83 additions & 37 deletions

File tree

  • instrumentation/hbase-client-2.0/testing/src/main/java/io/opentelemetry/javaagent/instrumentation/hbase/testing

instrumentation/hbase-client-2.0/testing/src/main/java/io/opentelemetry/javaagent/instrumentation/hbase/testing/AbstractHbaseTest.java

Lines changed: 83 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.util.ArrayList;
4242
import java.util.List;
4343
import java.util.function.Consumer;
44+
import java.util.stream.Stream;
4445
import org.apache.hadoop.conf.Configuration;
4546
import org.apache.hadoop.hbase.CompareOperator;
4647
import org.apache.hadoop.hbase.HBaseConfiguration;
@@ -59,6 +60,7 @@
5960
import org.apache.hadoop.hbase.client.Put;
6061
import org.apache.hadoop.hbase.client.Result;
6162
import org.apache.hadoop.hbase.client.ResultScanner;
63+
import org.apache.hadoop.hbase.client.Row;
6264
import org.apache.hadoop.hbase.client.RowMutations;
6365
import org.apache.hadoop.hbase.client.Scan;
6466
import org.apache.hadoop.hbase.client.Table;
@@ -71,6 +73,9 @@
7173
import org.junit.jupiter.api.Test;
7274
import org.junit.jupiter.api.TestInstance;
7375
import org.junit.jupiter.api.extension.RegisterExtension;
76+
import org.junit.jupiter.params.ParameterizedTest;
77+
import org.junit.jupiter.params.provider.Arguments;
78+
import org.junit.jupiter.params.provider.MethodSource;
7479
import org.testcontainers.containers.GenericContainer;
7580
import org.testcontainers.containers.wait.strategy.Wait;
7681
import org.testcontainers.containers.wait.strategy.WaitAllStrategy;
@@ -98,7 +103,6 @@ public abstract class AbstractHbaseTest {
98103
private static final int GET_TIMEOUT_OPERATION_TIMEOUT_MILLIS = 1000;
99104
private static final int GET_TIMEOUT_RPC_TIMEOUT_MILLIS = 200;
100105
private static final String ROW_1 = "row1";
101-
private static final String ROW_2 = "row2";
102106
private static final String ROW_3 = "row3";
103107
private static final String ROW_4 = "row4";
104108
private static final String ROW_5 = "row5";
@@ -354,47 +358,56 @@ void testScan() throws IOException {
354358
testing().waitAndAssertTraces(traceAssertConsumer(TABLE_NAME, SCAN, REGION_SERVER_PORT, true));
355359
}
356360

357-
@Test
358-
void testBatchGet() throws IOException {
359-
Result[] results;
361+
// Describes the batch cases: empty, single action, two actions with the same type, and two
362+
// actions with different types. Current HBase instrumentation reports all non-empty
363+
// Table.batch(...) calls as Multi.
364+
@ParameterizedTest
365+
@MethodSource("batchScenarios")
366+
void testBatch(BatchScenario scenario) throws IOException, InterruptedException {
360367
try (Table table = connection.getTable(TABLE_NAME)) {
361-
List<Get> getList = new ArrayList<>();
362-
getList.add(new Get(Bytes.toBytes(ROW_1)));
363-
getList.add(new Get(Bytes.toBytes(ROW_2)));
364-
getList.add(new Get(Bytes.toBytes(ROW_5)));
365-
results = table.get(getList);
366-
}
367-
assertThat(results).hasSize(3);
368-
{
369-
assertThat(Bytes.toString(results[0].getRow())).isEqualTo(ROW_1);
370-
assertThat(value(results[0], "col1")).isEqualTo("col1_val_1");
371-
assertThat(value(results[0], "col2")).isEqualTo("col2_val_1");
368+
table.batch(scenario.actions, new Object[scenario.actions.size()]);
372369
}
373-
{
374-
assertThat(Bytes.toString(results[1].getRow())).isNull();
375-
assertThat(value(results[1], "col1")).isNull();
376-
assertThat(value(results[1], "col2")).isNull();
377-
}
378-
{
379-
assertThat(Bytes.toString(results[2].getRow())).isNull();
380-
assertThat(value(results[2], "col1")).isNull();
381-
assertThat(value(results[2], "col2")).isNull();
370+
371+
if (scenario.actions.isEmpty()) {
372+
assertThat(testing().spans()).isEmpty();
373+
return;
382374
}
383-
testing().waitAndAssertTraces(traceAssertConsumer(TABLE_NAME, MULTI, REGION_SERVER_PORT, true));
375+
376+
testing()
377+
.waitAndAssertTraces(
378+
traceAssertConsumer(TABLE_NAME, scenario.operationName, REGION_SERVER_PORT, true));
384379
}
385380

386-
@Test
387-
void testBatchPut() throws IOException {
388-
try (Table table = connection.getTable(TABLE_NAME)) {
389-
List<Put> putList = new ArrayList<>();
390-
for (int i = 2; i < 5; i++) {
391-
Put put = new Put(Bytes.toBytes("batch-put-row" + i));
392-
put.addColumn(COLUMN_FAMILY, Bytes.toBytes("col1"), Bytes.toBytes("col1_val_" + i));
393-
putList.add(put);
394-
}
395-
table.put(putList);
396-
}
397-
testing().waitAndAssertTraces(traceAssertConsumer(TABLE_NAME, MULTI, REGION_SERVER_PORT, true));
381+
private static Stream<Arguments> batchScenarios() {
382+
return Stream.of(
383+
// an empty batch produces no span
384+
Arguments.argumentSet("empty", BatchScenario.builder().build()),
385+
Arguments.argumentSet(
386+
"single", BatchScenario.builder().addAction(get(ROW_1)).operationName(MULTI).build()),
387+
Arguments.argumentSet(
388+
"twoSameOperation",
389+
BatchScenario.builder()
390+
.addAction(get(ROW_1))
391+
.addAction(get(ROW_5))
392+
.operationName(MULTI)
393+
.build()),
394+
Arguments.argumentSet(
395+
"twoDifferentOperations",
396+
BatchScenario.builder()
397+
.addAction(put("batch-matrix-put-row"))
398+
.addAction(get(ROW_1))
399+
.operationName(MULTI)
400+
.build()));
401+
}
402+
403+
private static Get get(String rowKey) {
404+
return new Get(Bytes.toBytes(rowKey));
405+
}
406+
407+
private static Put put(String rowKey) {
408+
Put put = new Put(Bytes.toBytes(rowKey));
409+
put.addColumn(COLUMN_FAMILY, Bytes.toBytes("col1"), Bytes.toBytes("col1_val"));
410+
return put;
398411
}
399412

400413
@Test
@@ -569,4 +582,37 @@ private static String dbCollectionName(TableName table, boolean hasTable) {
569582
}
570583
return null;
571584
}
585+
586+
private static final class BatchScenario {
587+
final List<Row> actions;
588+
final String operationName;
589+
590+
BatchScenario(Builder builder) {
591+
this.actions = builder.actions;
592+
this.operationName = builder.operationName;
593+
}
594+
595+
static Builder builder() {
596+
return new Builder();
597+
}
598+
599+
static final class Builder {
600+
private final List<Row> actions = new ArrayList<>();
601+
private String operationName;
602+
603+
Builder operationName(String operationName) {
604+
this.operationName = operationName;
605+
return this;
606+
}
607+
608+
Builder addAction(Row action) {
609+
this.actions.add(action);
610+
return this;
611+
}
612+
613+
BatchScenario build() {
614+
return new BatchScenario(this);
615+
}
616+
}
617+
}
572618
}

0 commit comments

Comments
 (0)