Skip to content

Commit 4dd0912

Browse files
1996fanruiXComp
authored andcommitted
[hotfix][JUnit5 Migration] Migrate some state tests of adaptive scheduler to junit5
1 parent 79356cc commit 4dd0912

File tree

4 files changed

+474
-483
lines changed

4 files changed

+474
-483
lines changed

flink-runtime/src/test/java/org/apache/flink/runtime/scheduler/adaptive/CreatedTest.java

+65-50
Original file line numberDiff line numberDiff line change
@@ -23,82 +23,97 @@
2323
import org.apache.flink.runtime.executiongraph.ArchivedExecutionGraph;
2424
import org.apache.flink.runtime.executiongraph.ExecutionGraph;
2525
import org.apache.flink.runtime.failure.FailureEnricherUtils;
26-
import org.apache.flink.util.TestLogger;
26+
import org.apache.flink.util.FlinkException;
2727

28-
import org.junit.Test;
28+
import org.junit.jupiter.api.Test;
29+
import org.junit.jupiter.api.extension.AfterEachCallback;
30+
import org.junit.jupiter.api.extension.ExtensionContext;
31+
import org.junit.jupiter.api.extension.RegisterExtension;
32+
import org.slf4j.Logger;
33+
import org.slf4j.LoggerFactory;
2934

3035
import javax.annotation.Nullable;
3136

3237
import java.util.function.Consumer;
3338

34-
import static org.apache.flink.runtime.scheduler.adaptive.WaitingForResourcesTest.assertNonNull;
35-
import static org.hamcrest.CoreMatchers.is;
36-
import static org.junit.Assert.assertThat;
39+
import static org.assertj.core.api.Assertions.assertThat;
3740

3841
/** Tests for the {@link Created} state. */
39-
public class CreatedTest extends TestLogger {
42+
class CreatedTest {
4043

41-
@Test
42-
public void testCancel() throws Exception {
43-
try (MockCreatedContext ctx = new MockCreatedContext()) {
44-
Created created = new Created(ctx, log);
44+
private static final Logger LOG = LoggerFactory.getLogger(CreatedTest.class);
4545

46-
ctx.setExpectFinished(assertNonNull());
46+
@RegisterExtension MockCreatedContext ctx = new MockCreatedContext();
4747

48-
created.cancel();
49-
}
48+
@Test
49+
void testCancel() {
50+
Created created = new Created(ctx, LOG);
51+
52+
ctx.setExpectFinished(
53+
archivedExecutionGraph -> {
54+
assertThat(archivedExecutionGraph.getState()).isEqualTo(JobStatus.CANCELED);
55+
assertThat(archivedExecutionGraph.getFailureInfo()).isNull();
56+
});
57+
created.cancel();
5058
}
5159

5260
@Test
53-
public void testStartScheduling() throws Exception {
54-
try (MockCreatedContext ctx = new MockCreatedContext()) {
55-
Created created = new Created(ctx, log);
61+
void testStartScheduling() {
62+
Created created = new Created(ctx, LOG);
5663

57-
ctx.setExpectWaitingForResources();
64+
ctx.setExpectWaitingForResources();
5865

59-
created.startScheduling();
60-
}
66+
created.startScheduling();
6167
}
6268

6369
@Test
64-
public void testSuspend() throws Exception {
65-
try (MockCreatedContext ctx = new MockCreatedContext()) {
66-
Created created = new Created(ctx, log);
67-
68-
ctx.setExpectFinished(
69-
archivedExecutionGraph -> {
70-
assertThat(archivedExecutionGraph.getState(), is(JobStatus.SUSPENDED));
71-
});
72-
73-
created.suspend(new RuntimeException("Suspend"));
74-
}
70+
void testSuspend() {
71+
FlinkException expectedException = new FlinkException("This is a test exception");
72+
Created created = new Created(ctx, LOG);
73+
74+
ctx.setExpectFinished(
75+
archivedExecutionGraph -> {
76+
assertThat(archivedExecutionGraph.getState()).isEqualTo(JobStatus.SUSPENDED);
77+
assertThat(archivedExecutionGraph.getFailureInfo()).isNotNull();
78+
assertThat(
79+
archivedExecutionGraph
80+
.getFailureInfo()
81+
.getException()
82+
.deserializeError(this.getClass().getClassLoader()))
83+
.isEqualTo(expectedException);
84+
});
85+
86+
created.suspend(expectedException);
7587
}
7688

7789
@Test
78-
public void testFailure() throws Exception {
79-
try (MockCreatedContext ctx = new MockCreatedContext()) {
80-
Created created = new Created(ctx, log);
81-
82-
ctx.setExpectFinished(
83-
archivedExecutionGraph -> {
84-
assertThat(archivedExecutionGraph.getState(), is(JobStatus.FAILED));
85-
});
86-
87-
created.handleGlobalFailure(
88-
new RuntimeException("Global"), FailureEnricherUtils.EMPTY_FAILURE_LABELS);
89-
}
90+
void testFailure() {
91+
Created created = new Created(ctx, LOG);
92+
RuntimeException expectedException = new RuntimeException("This is a test exception");
93+
94+
ctx.setExpectFinished(
95+
archivedExecutionGraph -> {
96+
assertThat(archivedExecutionGraph.getState()).isEqualTo(JobStatus.FAILED);
97+
assertThat(archivedExecutionGraph.getFailureInfo()).isNotNull();
98+
assertThat(
99+
archivedExecutionGraph
100+
.getFailureInfo()
101+
.getException()
102+
.deserializeError(this.getClass().getClassLoader()))
103+
.isEqualTo(expectedException);
104+
});
105+
106+
created.handleGlobalFailure(expectedException, FailureEnricherUtils.EMPTY_FAILURE_LABELS);
90107
}
91108

92109
@Test
93-
public void testJobInformation() throws Exception {
94-
try (MockCreatedContext ctx = new MockCreatedContext()) {
95-
Created created = new Created(ctx, log);
96-
ArchivedExecutionGraph job = created.getJob();
97-
assertThat(job.getState(), is(JobStatus.INITIALIZING));
98-
}
110+
void testJobInformation() {
111+
Created created = new Created(ctx, LOG);
112+
ArchivedExecutionGraph job = created.getJob();
113+
assertThat(job.getState()).isEqualTo(JobStatus.INITIALIZING);
99114
}
100115

101-
static class MockCreatedContext implements Created.Context, AutoCloseable {
116+
static class MockCreatedContext implements Created.Context, AfterEachCallback {
102117
private final StateValidator<ArchivedExecutionGraph> finishedStateValidator =
103118
new StateValidator<>("finished");
104119
private final StateValidator<Void> waitingForResourcesStateValidator =
@@ -130,7 +145,7 @@ public void goToWaitingForResources(@Nullable ExecutionGraph previousExecutionGr
130145
}
131146

132147
@Override
133-
public void close() throws Exception {
148+
public void afterEach(ExtensionContext extensionContext) throws Exception {
134149
finishedStateValidator.close();
135150
waitingForResourcesStateValidator.close();
136151
}

0 commit comments

Comments
 (0)