View Javadoc

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
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
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.
17   */
18  package org.apache.hadoop.hbase.procedure2;
19  
20  import static org.junit.Assert.assertTrue;
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  
26  import org.apache.hadoop.hbase.protobuf.generated.MasterProcedureProtos.ServerCrashState;
27  import org.apache.hadoop.hbase.protobuf.generated.ProcedureProtos.ProcedureState;
28  import org.apache.hadoop.hbase.testclassification.SmallTests;
29  import org.junit.Test;
30  import org.junit.experimental.categories.Category;
31  
32  @Category({SmallTests.class})
33  public class TestProcedureToString {
34    /**
35     * A do-nothing environment for BasicProcedure.
36     */
37    static class BasicProcedureEnv {};
38  
39    /**
40     * A do-nothing basic procedure just for testing toString.
41     */
42    static class BasicProcedure extends Procedure<BasicProcedureEnv> {
43      @Override
44      protected Procedure<?>[] execute(BasicProcedureEnv env)
45          throws ProcedureYieldException, InterruptedException {
46        return new Procedure [] {this};
47      }
48  
49      @Override
50      protected void rollback(BasicProcedureEnv env) throws IOException, InterruptedException {
51      }
52  
53      @Override
54      protected boolean abort(BasicProcedureEnv env) {
55        return false;
56      }
57  
58      @Override
59      protected void serializeStateData(OutputStream stream) throws IOException {
60      }
61  
62      @Override
63      protected void deserializeStateData(InputStream stream) throws IOException {
64      }
65    }
66  
67    /**
68     * A do-nothing basic procedure that overrides the toStringState method. It just doubles the
69     * current state string.
70     */
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);
77      }
78    }
79  
80  
81  
82    /**
83     * Test that I can override the toString for its state value.
84     * @throws ProcedureYieldException
85     * @throws InterruptedException
86     */
87    @Test
88    public void testBasicToString() throws ProcedureYieldException, InterruptedException {
89      BasicProcedure p = new BasicProcedure();
90      ProcedureState state = ProcedureState.RUNNABLE;
91      p.setState(state);
92      // Just assert that the toString basically works and has state in it.
93      assertTrue(p.toString().contains(state.toString()));
94      p = new DoublingStateStringBasicProcedure();
95      p.setState(state);
96      // Assert our override works and that we get double the state...
97      String testStr = state.toString() + state.toString();
98      assertTrue(p.toString().contains(testStr));
99    }
100 
101   /**
102    * Do-nothing SimpleMachineProcedure for checking its toString.
103    */
104   static class SimpleStateMachineProcedure
105   extends StateMachineProcedure<BasicProcedureEnv, ServerCrashState> {
106     @Override
107     protected org.apache.hadoop.hbase.procedure2.StateMachineProcedure.Flow executeFromState(BasicProcedureEnv env,
108         ServerCrashState state) throws ProcedureYieldException, InterruptedException {
109       return null;
110     }
111 
112     @Override
113     protected void rollbackState(BasicProcedureEnv env, ServerCrashState state) throws IOException,
114         InterruptedException {
115     }
116 
117     @Override
118     protected ServerCrashState getState(int stateId) {
119       return ServerCrashState.valueOf(stateId);
120     }
121 
122     @Override
123     protected int getStateId(ServerCrashState state) {
124       return state.getNumber();
125     }
126 
127     @Override
128     protected ServerCrashState getInitialState() {
129       return null;
130     }
131 
132     @Override
133     protected boolean abort(BasicProcedureEnv env) {
134       return false;
135     }
136   }
137 
138   @Test
139   public void testStateMachineProcedure() {
140     SimpleStateMachineProcedure p = new SimpleStateMachineProcedure();
141     ProcedureState state = ProcedureState.RUNNABLE;
142     p.setState(state);
143     p.setNextState(ServerCrashState.SERVER_CRASH_ASSIGN);
144     // Just assert that the toString basically works and has state in it.
145     assertTrue(p.toString().contains(state.toString()));
146     assertTrue(p.toString().contains(ServerCrashState.SERVER_CRASH_ASSIGN.toString()));
147   }
148 }