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
{
33 * A do-nothing environment for BasicProcedure.
35 static class BasicProcedureEnv
{};
38 * A do-nothing basic procedure just for testing toString.
40 static class BasicProcedure
extends Procedure
<BasicProcedureEnv
> {
42 protected Procedure
<BasicProcedureEnv
>[] execute(BasicProcedureEnv env
)
43 throws ProcedureYieldException
, InterruptedException
{
44 return new Procedure
[] {this};
48 protected void rollback(BasicProcedureEnv env
) throws IOException
, InterruptedException
{
52 protected boolean abort(BasicProcedureEnv env
) {
57 protected void serializeStateData(ProcedureStateSerializer serializer
)
62 protected void deserializeStateData(ProcedureStateSerializer serializer
)
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
{
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
);
81 * Test that I can override the toString for its state value.
82 * @throws ProcedureYieldException
83 * @throws InterruptedException
86 public void testBasicToString() throws ProcedureYieldException
, InterruptedException
{
87 BasicProcedure p
= new BasicProcedure();
88 ProcedureState state
= ProcedureState
.RUNNABLE
;
90 // Just assert that the toString basically works and has state in it.
91 assertTrue(p
.toString().contains(state
.toString()));
92 p
= new DoublingStateStringBasicProcedure();
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
));
100 * Do-nothing SimpleMachineProcedure for checking its toString.
102 static class SimpleStateMachineProcedure
103 extends StateMachineProcedure
<BasicProcedureEnv
, ServerCrashState
> {
105 protected org
.apache
.hadoop
.hbase
.procedure2
.StateMachineProcedure
.Flow
executeFromState(BasicProcedureEnv env
,
106 ServerCrashState state
) throws ProcedureYieldException
, InterruptedException
{
111 protected void rollbackState(BasicProcedureEnv env
, ServerCrashState state
) throws IOException
,
112 InterruptedException
{
116 protected ServerCrashState
getState(int stateId
) {
117 return ServerCrashState
.valueOf(stateId
);
121 protected int getStateId(ServerCrashState state
) {
122 return state
.getNumber();
126 protected ServerCrashState
getInitialState() {
131 protected boolean abort(BasicProcedureEnv env
) {
137 public void testStateMachineProcedure() {
138 SimpleStateMachineProcedure p
= new SimpleStateMachineProcedure();
139 ProcedureState state
= ProcedureState
.RUNNABLE
;
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()));