HBASE-18106 Redo ProcedureInfo and LockInfo
[hbase.git] / hbase-procedure / src / test / java / org / apache / hadoop / hbase / procedure2 / TestProcedureToString.java
blobaf25108d1725b3452a2cbf3a122f64ac43c0fc3d
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.procedure2;
20 import static org.junit.Assert.assertTrue;
22 import java.io.IOException;
23 import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.ServerCrashState;
24 import org.apache.hadoop.hbase.shaded.protobuf.generated.ProcedureProtos.ProcedureState;
25 import org.apache.hadoop.hbase.testclassification.MasterTests;
26 import org.apache.hadoop.hbase.testclassification.SmallTests;
27 import org.junit.Test;
28 import org.junit.experimental.categories.Category;
30 @Category({MasterTests.class, SmallTests.class})
31 public class TestProcedureToString {
32 /**
33 * A do-nothing environment for BasicProcedure.
35 static class BasicProcedureEnv {};
37 /**
38 * A do-nothing basic procedure just for testing toString.
40 static class BasicProcedure extends Procedure<BasicProcedureEnv> {
41 @Override
42 protected Procedure<BasicProcedureEnv>[] execute(BasicProcedureEnv env)
43 throws ProcedureYieldException, InterruptedException {
44 return new Procedure [] {this};
47 @Override
48 protected void rollback(BasicProcedureEnv env) throws IOException, InterruptedException {
51 @Override
52 protected boolean abort(BasicProcedureEnv env) {
53 return false;
56 @Override
57 protected void serializeStateData(ProcedureStateSerializer serializer)
58 throws IOException {
61 @Override
62 protected void deserializeStateData(ProcedureStateSerializer serializer)
63 throws IOException {
67 /**
68 * A do-nothing basic procedure that overrides the toStringState method. It just doubles the
69 * current state string.
71 static class DoublingStateStringBasicProcedure extends BasicProcedure {
72 @Override
73 protected void toStringState(StringBuilder builder) {
74 // Call twice to get the state string twice as our state value.
75 super.toStringState(builder);
76 super.toStringState(builder);
80 /**
81 * Test that I can override the toString for its state value.
82 * @throws ProcedureYieldException
83 * @throws InterruptedException
85 @Test
86 public void testBasicToString() throws ProcedureYieldException, InterruptedException {
87 BasicProcedure p = new BasicProcedure();
88 ProcedureState state = ProcedureState.RUNNABLE;
89 p.setState(state);
90 // Just assert that the toString basically works and has state in it.
91 assertTrue(p.toString().contains(state.toString()));
92 p = new DoublingStateStringBasicProcedure();
93 p.setState(state);
94 // Assert our override works and that we get double the state...
95 String testStr = state.toString() + state.toString();
96 assertTrue(p.toString().contains(testStr));
99 /**
100 * Do-nothing SimpleMachineProcedure for checking its toString.
102 static class SimpleStateMachineProcedure
103 extends StateMachineProcedure<BasicProcedureEnv, ServerCrashState> {
104 @Override
105 protected org.apache.hadoop.hbase.procedure2.StateMachineProcedure.Flow executeFromState(BasicProcedureEnv env,
106 ServerCrashState state) throws ProcedureYieldException, InterruptedException {
107 return null;
110 @Override
111 protected void rollbackState(BasicProcedureEnv env, ServerCrashState state) throws IOException,
112 InterruptedException {
115 @Override
116 protected ServerCrashState getState(int stateId) {
117 return ServerCrashState.valueOf(stateId);
120 @Override
121 protected int getStateId(ServerCrashState state) {
122 return state.getNumber();
125 @Override
126 protected ServerCrashState getInitialState() {
127 return null;
130 @Override
131 protected boolean abort(BasicProcedureEnv env) {
132 return false;
136 @Test
137 public void testStateMachineProcedure() {
138 SimpleStateMachineProcedure p = new SimpleStateMachineProcedure();
139 ProcedureState state = ProcedureState.RUNNABLE;
140 p.setState(state);
141 p.setNextState(ServerCrashState.SERVER_CRASH_ASSIGN);
142 // Just assert that the toString basically works and has state in it.
143 assertTrue(p.toString().contains(state.toString()));
144 assertTrue(p.toString().contains(ServerCrashState.SERVER_CRASH_ASSIGN.toString()));