-
Notifications
You must be signed in to change notification settings - Fork 521
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HDDS-12604. Reduce duplication in TestContainerStateMachine #8104
base: master
Are you sure you want to change the base?
Conversation
@peterxcli would you like to review? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @chiacyu for this cleanup. Left some comment inline, PTAL.
I have 2 additional suggestion that I can't directly comment then inline:
- We can have those helper to the bottom of this class then this patch would be more readable.
- I think we can also cleanup the "set value with property name" part in
testWriteTimeout
:
Want:
Field writeChunkWaitMaxNs = stateMachine.getClass().getDeclaredField("writeChunkWaitMaxNs");
writeChunkWaitMaxNs.setAccessible(true);
writeChunkWaitMaxNs.set(stateMachine, 1000_000_000);
to be:
@Test
public void testWriteTimout() throws Exception {
conf.setDuration(HDDS_CONTAINER_RATIS_STATEMACHINE_WRITE_WAIT_INTERVAL, 1000_000_000, TimeUnit.NANOSECONDS)
stateMachine = new ContainerStateMachine(null,
RaftGroupId.randomId(), dispatcher, controller, executor, ratisServer, conf, "containerOp");
|
||
public final void setUpMockRequestProtoReturn(ContainerStateMachine.Context context, String content, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public final void setUpMockRequestProtoReturn(ContainerStateMachine.Context context, String content, | |
private final void setUpMockRequestProtoReturn(ContainerStateMachine.Context context, |
Since all content are the same, it can be moved to class Initialization
abstract class TestContainerStateMachine {
...
private final ByteString CONTAINER_DATA = ByteString.copyFromUtf8("Test Data");
assertEquals(ContainerProtos.Result.CONTAINER_UNHEALTHY, sce.getResult()); | ||
} | ||
|
||
public final void setUpMockDispatcherReturn(boolean failWithException) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
public final void setUpMockDispatcherReturn(boolean failWithException) { | |
private final void setUpMockDispatcherReturn(boolean failWithException) { |
ContainerProtos.DatanodeBlockID.newBuilder().setContainerID(1).setLocalID(1).build()).build()) | ||
.setContainerID(1) | ||
.setDatanodeUuid(UUID.randomUUID().toString()).build()); | ||
setUpMockRequestProtoReturn(context, "Test data", 1, 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
setUpMockRequestProtoReturn(context, "Test data", 1, 1); | |
setUpMockRequestProtoReturn(context, 1, 1); |
assertResults(failWithException, throwable); | ||
|
||
// Writing data to another container(containerId 2) should also fail. | ||
setUpMockRequestProtoReturn(context, "Test Data", 2, 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
setUpMockRequestProtoReturn(context, "Test Data", 2, 1); | |
setUpMockRequestProtoReturn(context, 2, 1); |
@@ -129,6 +129,31 @@ public void testWriteFailure(boolean failWithException) throws ExecutionExceptio | |||
TransactionContext trx = mock(TransactionContext.class); | |||
ContainerStateMachine.Context context = mock(ContainerStateMachine.Context.class); | |||
when(trx.getStateMachineContext()).thenReturn(context); | |||
|
|||
setUpMockDispatcherReturn(failWithException); | |||
setUpMockRequestProtoReturn(context, "Test Data", 1, 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
setUpMockRequestProtoReturn(context, "Test Data", 1, 1); | |
setUpMockRequestProtoReturn(context, 1, 1); |
AtomicReference<Throwable> throwable = new AtomicReference<>(null); | ||
} | ||
|
||
public final Function<Throwable, ? extends Message> getThrowableSetter(AtomicReference<Throwable> throwable) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
another suggestion is replace getThrowableSetter
with:
private static class ThrowableCatcher {
private final AtomicReference<Throwable> caught = new AtomicReference<>(null);
public Function<Throwable, ? extends Message> asSetter() {
return t -> {
caught.set(t);
return null;
};
}
public Throwable getReceived() {
return caught.get();
}
public void reset() {
caught.set(null);
}
}
Then we can use:
ThrowableCatcher catcher = new ThrowableCatcher();
stateMachine.write(entry, trx).exceptionally(catcher.asSetter()).get();
assertNotNull(catcher.getReceived());
But this is up to you, this change has few benefit at this time.
Hi @chiacyu, if you have time, it would be really helpful to include the CI run link in the PR description. Thank you! |
Hi, @peterxcli |
What changes were proposed in this pull request?
In
TestContainerStateMachine
, some of thewhen().return()
section are duplicated and can be reduced by wrapping into helper functions. Please take a look, thanks!What is the link to the Apache JIRA
HDDS-12604
How was this patch tested?
CI