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