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