Skip to content

Commit 44ff1b3

Browse files
committed
refactor functions
1 parent 5597285 commit 44ff1b3

File tree

1 file changed

+21
-26
lines changed

1 file changed

+21
-26
lines changed

hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/common/transport/server/ratis/TestContainerStateMachine.java

+21-26
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public void testWriteFailure(boolean failWithException) throws ExecutionExceptio
131131
when(trx.getStateMachineContext()).thenReturn(context);
132132

133133
setUpMockDispatcherReturn(failWithException);
134-
setUpMockRequestProto(context);
134+
setUpMockRequestProtoReturn(context, "Test Data", 1, 1);
135135

136136
AtomicReference<Throwable> throwable = new AtomicReference<>(null);
137137
Function<Throwable, ? extends Message> throwableSetter = getThrowableSetter(throwable);
@@ -144,13 +144,7 @@ public void testWriteFailure(boolean failWithException) throws ExecutionExceptio
144144
assertResults(failWithException, throwable);
145145

146146
// Writing data to another container(containerId 2) should also fail.
147-
when(context.getRequestProto()).thenReturn(ContainerProtos.ContainerCommandRequestProto.newBuilder()
148-
.setCmdType(ContainerProtos.Type.WriteChunk).setWriteChunk(
149-
ContainerProtos.WriteChunkRequestProto.newBuilder().setData(ByteString.copyFromUtf8("Test Data"))
150-
.setBlockID(
151-
ContainerProtos.DatanodeBlockID.newBuilder().setContainerID(2).setLocalID(1).build()).build())
152-
.setContainerID(2)
153-
.setDatanodeUuid(UUID.randomUUID().toString()).build());
147+
setUpMockRequestProtoReturn(context, "Test Data", 2, 1);
154148
stateMachine.write(entryNext, trx).exceptionally(throwableSetter).get();
155149
verify(dispatcher, times(0)).dispatch(any(ContainerProtos.ContainerCommandRequestProto.class),
156150
any(DispatcherContext.class));
@@ -170,13 +164,15 @@ public final void setUpMockDispatcherReturn(boolean failWithException) {
170164
}
171165
}
172166

173-
public final void setUpMockRequestProto(ContainerStateMachine.Context context) {
167+
public final void setUpMockRequestProtoReturn(ContainerStateMachine.Context context, String content,
168+
int containerId, int localId) {
174169
when(context.getRequestProto()).thenReturn(ContainerProtos.ContainerCommandRequestProto.newBuilder()
175170
.setCmdType(ContainerProtos.Type.WriteChunk).setWriteChunk(
176-
ContainerProtos.WriteChunkRequestProto.newBuilder().setData(ByteString.copyFromUtf8("Test Data"))
171+
ContainerProtos.WriteChunkRequestProto.newBuilder().setData(ByteString.copyFromUtf8(content))
177172
.setBlockID(
178-
ContainerProtos.DatanodeBlockID.newBuilder().setContainerID(1).setLocalID(1).build()).build())
179-
.setContainerID(1)
173+
ContainerProtos.DatanodeBlockID.newBuilder().setContainerID(containerId)
174+
.setLocalID(localId).build()).build())
175+
.setContainerID(containerId)
180176
.setDatanodeUuid(UUID.randomUUID().toString()).build());
181177
}
182178

@@ -198,6 +194,16 @@ public final void assertResults(boolean failWithException, AtomicReference<Throw
198194
}
199195
}
200196

197+
public final void setUpLogProtoReturn(ContainerStateMachine.Context context, int containerId, int localId) {
198+
when(context.getLogProto()).thenReturn(ContainerProtos.ContainerCommandRequestProto.newBuilder()
199+
.setCmdType(ContainerProtos.Type.WriteChunk).setWriteChunk(
200+
ContainerProtos.WriteChunkRequestProto.newBuilder().setBlockID(
201+
ContainerProtos.DatanodeBlockID.newBuilder().
202+
setContainerID(containerId).setLocalID(localId).build()).build())
203+
.setContainerID(containerId)
204+
.setDatanodeUuid(UUID.randomUUID().toString()).build());
205+
}
206+
201207
@ParameterizedTest
202208
@ValueSource(booleans = {true, false})
203209
public void testApplyTransactionFailure(boolean failWithException) throws ExecutionException,
@@ -212,12 +218,7 @@ public void testApplyTransactionFailure(boolean failWithException) throws Execut
212218

213219
setUpMockDispatcherReturn(failWithException);
214220
// Failing apply transaction on congtainer 1.
215-
when(context.getLogProto()).thenReturn(ContainerProtos.ContainerCommandRequestProto.newBuilder()
216-
.setCmdType(ContainerProtos.Type.WriteChunk).setWriteChunk(
217-
ContainerProtos.WriteChunkRequestProto.newBuilder().setBlockID(
218-
ContainerProtos.DatanodeBlockID.newBuilder().setContainerID(1).setLocalID(1).build()).build())
219-
.setContainerID(1)
220-
.setDatanodeUuid(UUID.randomUUID().toString()).build());
221+
setUpLogProtoReturn(context, 1, 1);
221222
AtomicReference<Throwable> throwable = new AtomicReference<>(null);
222223
Function<Throwable, ? extends Message> throwableSetter = getThrowableSetter(throwable);
223224
//apply transaction will fail because of runtime exception thrown by dispatcher, which marks the first
@@ -238,13 +239,7 @@ public void testApplyTransactionFailure(boolean failWithException) throws Execut
238239

239240
// Another apply transaction on a different container 2 shouldn't fail because the previous apply transaction
240241
// failure was only on container 1.
241-
when(context.getLogProto()).thenReturn(ContainerProtos.ContainerCommandRequestProto.newBuilder()
242-
.setCmdType(ContainerProtos.Type.WriteChunk).setWriteChunk(
243-
ContainerProtos.WriteChunkRequestProto.newBuilder().setBlockID(
244-
ContainerProtos.DatanodeBlockID.newBuilder().setContainerID(2).setLocalID(1).build()).build())
245-
.setContainerID(2)
246-
.setDatanodeUuid(UUID.randomUUID().toString()).build());
247-
242+
setUpLogProtoReturn(context, 2, 1);
248243
reset(dispatcher);
249244
throwable.set(null);
250245
when(dispatcher.dispatch(any(), any())).thenReturn(ContainerProtos.ContainerCommandResponseProto
@@ -280,7 +275,7 @@ public void testWriteTimout() throws Exception {
280275
return null;
281276
}).when(dispatcher).dispatch(any(), any());
282277

283-
setUpMockRequestProto(context);
278+
setUpMockRequestProtoReturn(context, "Test data", 1, 1);
284279
AtomicReference<Throwable> throwable = new AtomicReference<>(null);
285280
Function<Throwable, ? extends Message> throwableSetter = t -> {
286281
throwable.set(t);

0 commit comments

Comments
 (0)