HBASE-26921 Rewrite the counting cells part in TestMultiVersions (#4316)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / MiniClusterRule.java
blobf13258f937365ea5b2827c7f20f772bf61d38e95
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase;
20 import java.io.IOException;
21 import java.util.concurrent.CompletableFuture;
22 import java.util.concurrent.ExecutionException;
23 import java.util.function.Supplier;
24 import org.apache.hadoop.conf.Configuration;
25 import org.apache.hadoop.hbase.client.AsyncConnection;
26 import org.apache.hadoop.hbase.client.Connection;
27 import org.apache.hadoop.hbase.client.ConnectionFactory;
28 import org.junit.ClassRule;
29 import org.junit.Rule;
30 import org.junit.rules.ExternalResource;
31 import org.junit.rules.TestRule;
33 /**
34 * A {@link TestRule} that manages an instance of the {@link SingleProcessHBaseCluster}. Can be used
35 * in either the {@link Rule} or {@link ClassRule} positions. Built on top of an instance of
36 * {@link HBaseTestingUtil}, so be weary of intermixing direct use of that class with this Rule.
37 * </p>
38 * Use in combination with {@link ConnectionRule}, for example:
40 * <pre>
41 * {
42 * &#64;code
43 * public class TestMyClass {
44 * &#64;ClassRule
45 * public static final MiniClusterRule miniClusterRule = MiniClusterRule.newBuilder().build();
47 * &#64;Rule
48 * public final ConnectionRule connectionRule =
49 * ConnectionRule.createAsyncConnectionRule(miniClusterRule::createAsyncConnection);
50 * }
51 * }
52 * </pre>
54 public final class MiniClusterRule extends ExternalResource {
56 /**
57 * A builder for fluent composition of a new {@link MiniClusterRule}.
59 public static class Builder {
61 private StartTestingClusterOption miniClusterOption;
62 private Configuration conf;
64 /**
65 * Use the provided {@link StartTestingClusterOption} to construct the
66 * {@link SingleProcessHBaseCluster}.
68 public Builder setMiniClusterOption(final StartTestingClusterOption miniClusterOption) {
69 this.miniClusterOption = miniClusterOption;
70 return this;
73 /**
74 * Seed the underlying {@link HBaseTestingUtil} with the provided {@link Configuration}.
76 public Builder setConfiguration(final Configuration conf) {
77 this.conf = conf;
78 return this;
81 public Builder setConfiguration(Supplier<Configuration> supplier) {
82 return setConfiguration(supplier.get());
85 public MiniClusterRule build() {
86 return new MiniClusterRule(conf, miniClusterOption != null ? miniClusterOption :
87 StartTestingClusterOption.builder().build());
91 private final HBaseTestingUtil testingUtility;
92 private final StartTestingClusterOption miniClusterOptions;
94 private SingleProcessHBaseCluster miniCluster;
96 private MiniClusterRule(final Configuration conf,
97 final StartTestingClusterOption miniClusterOptions) {
98 this.testingUtility = new HBaseTestingUtil(conf);
99 this.miniClusterOptions = miniClusterOptions;
102 public static Builder newBuilder() {
103 return new Builder();
107 * Returns the underlying instance of {@link HBaseTestingUtil}
109 public HBaseTestingUtil getTestingUtility() {
110 return testingUtility;
114 * Create a {@link Connection} to the managed {@link SingleProcessHBaseCluster}. It's up to
115 * the caller to {@link Connection#close() close()} the connection when finished.
117 public Connection createConnection() {
118 try {
119 return createAsyncConnection().get().toConnection();
120 } catch (InterruptedException | ExecutionException e) {
121 throw new RuntimeException(e);
126 * Create a {@link AsyncConnection} to the managed {@link SingleProcessHBaseCluster}. It's up to
127 * the caller to {@link AsyncConnection#close() close()} the connection when finished.
129 public CompletableFuture<AsyncConnection> createAsyncConnection() {
130 if (miniCluster == null) {
131 throw new IllegalStateException("test cluster not initialized");
133 return ConnectionFactory.createAsyncConnection(miniCluster.getConf());
136 @Override
137 protected void before() throws Throwable {
138 miniCluster = testingUtility.startMiniCluster(miniClusterOptions);
141 @Override
142 protected void after() {
143 try {
144 testingUtility.shutdownMiniCluster();
145 } catch (IOException e) {
146 throw new RuntimeException(e);