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.procedure2; 019 020import static org.junit.jupiter.api.Assertions.assertTrue; 021 022import java.io.IOException; 023import org.apache.hadoop.hbase.testclassification.MasterTests; 024import org.apache.hadoop.hbase.testclassification.SmallTests; 025import org.junit.jupiter.api.Tag; 026import org.junit.jupiter.api.Test; 027 028import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.ServerCrashState; 029import org.apache.hadoop.hbase.shaded.protobuf.generated.ProcedureProtos.ProcedureState; 030 031@Tag(MasterTests.TAG) 032@Tag(SmallTests.TAG) 033public class TestProcedureToString { 034 035 /** 036 * A do-nothing environment for BasicProcedure. 037 */ 038 static class BasicProcedureEnv { 039 } 040 041 /** 042 * A do-nothing basic procedure just for testing toString. 043 */ 044 static class BasicProcedure extends Procedure<BasicProcedureEnv> { 045 @Override 046 protected Procedure<BasicProcedureEnv>[] execute(BasicProcedureEnv env) 047 throws ProcedureYieldException, InterruptedException { 048 return new Procedure[] { this }; 049 } 050 051 @Override 052 protected void rollback(BasicProcedureEnv env) throws IOException, InterruptedException { 053 } 054 055 @Override 056 protected boolean abort(BasicProcedureEnv env) { 057 return false; 058 } 059 060 @Override 061 protected void serializeStateData(ProcedureStateSerializer serializer) throws IOException { 062 } 063 064 @Override 065 protected void deserializeStateData(ProcedureStateSerializer serializer) throws IOException { 066 } 067 } 068 069 /** 070 * A do-nothing basic procedure that overrides the toStringState method. It just doubles the 071 * current state string. 072 */ 073 static class DoublingStateStringBasicProcedure extends BasicProcedure { 074 @Override 075 protected void toStringState(StringBuilder builder) { 076 // Call twice to get the state string twice as our state value. 077 super.toStringState(builder); 078 super.toStringState(builder); 079 } 080 } 081 082 /** 083 * Test that I can override the toString for its state value. 084 */ 085 @Test 086 public void testBasicToString() { 087 BasicProcedure p = new BasicProcedure(); 088 ProcedureState state = ProcedureState.RUNNABLE; 089 p.setState(state); 090 // Just assert that the toString basically works and has state in it. 091 assertTrue(p.toString().contains(state.toString())); 092 p = new DoublingStateStringBasicProcedure(); 093 p.setState(state); 094 // Assert our override works and that we get double the state... 095 String testStr = state.toString() + state.toString(); 096 assertTrue(p.toString().contains(testStr)); 097 } 098 099 /** 100 * Do-nothing SimpleMachineProcedure for checking its toString. 101 */ 102 static class SimpleStateMachineProcedure 103 extends StateMachineProcedure<BasicProcedureEnv, ServerCrashState> { 104 @Override 105 protected org.apache.hadoop.hbase.procedure2.StateMachineProcedure.Flow 106 executeFromState(BasicProcedureEnv env, ServerCrashState state) 107 throws ProcedureYieldException, InterruptedException { 108 return null; 109 } 110 111 @Override 112 protected void rollbackState(BasicProcedureEnv env, ServerCrashState state) 113 throws IOException, InterruptedException { 114 } 115 116 @Override 117 protected ServerCrashState getState(int stateId) { 118 return ServerCrashState.valueOf(stateId); 119 } 120 121 @Override 122 protected int getStateId(ServerCrashState state) { 123 return state.getNumber(); 124 } 125 126 @Override 127 protected ServerCrashState getInitialState() { 128 return null; 129 } 130 131 @Override 132 protected boolean abort(BasicProcedureEnv env) { 133 return false; 134 } 135 } 136 137 @Test 138 public void testStateMachineProcedure() { 139 SimpleStateMachineProcedure p = new SimpleStateMachineProcedure(); 140 ProcedureState state = ProcedureState.RUNNABLE; 141 p.setState(state); 142 p.setNextState(ServerCrashState.SERVER_CRASH_ASSIGN); 143 // Just assert that the toString basically works and has state in it. 144 assertTrue(p.toString().contains(state.toString())); 145 assertTrue(p.toString().contains(ServerCrashState.SERVER_CRASH_ASSIGN.toString())); 146 } 147}