HBASE-23723 Ensure MOB compaction works in optimized mode after snapshot clone (...
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / MiniClusterRule.java
blob89fbded4bdeadb5b172ea91692654461cc72bb31
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 org.apache.hadoop.hbase.client.AsyncConnection;
23 import org.apache.hadoop.hbase.client.ConnectionFactory;
24 import org.junit.ClassRule;
25 import org.junit.Rule;
26 import org.junit.rules.ExternalResource;
27 import org.junit.rules.TestRule;
29 /**
30 * A {@link TestRule} that manages an instance of the {@link MiniHBaseCluster}. Can be used in
31 * either the {@link Rule} or {@link ClassRule} positions. Built on top of an instance of
32 * {@link HBaseTestingUtility}, so be weary of intermixing direct use of that class with this Rule.
33 * </p>
34 * Use in combination with {@link ConnectionRule}, for example:
36 * <pre>{@code
37 * public class TestMyClass {
38 * @ClassRule
39 * public static final MiniClusterRule miniClusterRule = new MiniClusterRule();
41 * @Rule
42 * public final ConnectionRule connectionRule =
43 * new ConnectionRule(miniClusterRule::createConnection);
44 * }
45 * }</pre>
47 public class MiniClusterRule extends ExternalResource {
48 private final HBaseTestingUtility testingUtility;
49 private final StartMiniClusterOption miniClusterOptions;
51 private MiniHBaseCluster miniCluster;
53 /**
54 * Create an instance over the default options provided by {@link StartMiniClusterOption}.
56 public MiniClusterRule() {
57 this(StartMiniClusterOption.builder().build());
60 /**
61 * Create an instance using the provided {@link StartMiniClusterOption}.
63 public MiniClusterRule(final StartMiniClusterOption miniClusterOptions) {
64 this.testingUtility = new HBaseTestingUtility();
65 this.miniClusterOptions = miniClusterOptions;
68 /**
69 * @return the underlying instance of {@link HBaseTestingUtility}
71 public HBaseTestingUtility getTestingUtility() {
72 return testingUtility;
75 /**
76 * Create a {@link AsyncConnection} to the managed {@link MiniHBaseCluster}. It's up to the caller
77 * to {@link AsyncConnection#close() close()} the connection when finished.
79 public CompletableFuture<AsyncConnection> createConnection() {
80 if (miniCluster == null) {
81 throw new IllegalStateException("test cluster not initialized");
83 return ConnectionFactory.createAsyncConnection(miniCluster.getConf());
86 @Override
87 protected void before() throws Throwable {
88 miniCluster = testingUtility.startMiniCluster(miniClusterOptions);
91 @Override
92 protected void after() {
93 try {
94 testingUtility.shutdownMiniCluster();
95 } catch (IOException e) {
96 throw new RuntimeException(e);