This is an automated email from the ASF dual-hosted git repository. zjureel pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/flink.git
The following commit(s) were added to refs/heads/master by this push: new 18c9d09fe6e [FLINK-32848][tests][JUnit5 migration] Migrate flink-runtime/registration tests to JUnit5 (#23300) 18c9d09fe6e is described below commit 18c9d09fe6eb4ffc41b48b0308d6f00809516965 Author: Zhanghao Chen <m...@outlook.com> AuthorDate: Fri Sep 15 14:38:12 2023 +0800 [FLINK-32848][tests][JUnit5 migration] Migrate flink-runtime/registration tests to JUnit5 (#23300) Co-authored-by: Shammon FY <zjur...@gmail.com> --- .../registration/RegisteredRpcConnectionTest.java | 89 +++++++------- .../RetryingRegistrationConfigurationTest.java | 47 +++----- .../registration/RetryingRegistrationTest.java | 128 ++++++++++----------- 3 files changed, 121 insertions(+), 143 deletions(-) diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/registration/RegisteredRpcConnectionTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/registration/RegisteredRpcConnectionTest.java index b47dc608dc1..66c415fa989 100644 --- a/flink-runtime/src/test/java/org/apache/flink/runtime/registration/RegisteredRpcConnectionTest.java +++ b/flink-runtime/src/test/java/org/apache/flink/runtime/registration/RegisteredRpcConnectionTest.java @@ -23,11 +23,10 @@ import org.apache.flink.runtime.registration.RetryingRegistrationTest.TestRegist import org.apache.flink.runtime.rpc.RpcService; import org.apache.flink.runtime.rpc.TestingRpcService; import org.apache.flink.types.Either; -import org.apache.flink.util.TestLogger; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.slf4j.LoggerFactory; import java.util.UUID; @@ -35,33 +34,28 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; /** Tests for RegisteredRpcConnection, validating the successful, failure and close behavior. */ -public class RegisteredRpcConnectionTest extends TestLogger { +class RegisteredRpcConnectionTest { private TestingRpcService rpcService; - @Before - public void setup() { + @BeforeEach + void setup() { rpcService = new TestingRpcService(); } - @After - public void tearDown() throws ExecutionException, InterruptedException { + @AfterEach + void tearDown() throws ExecutionException, InterruptedException { if (rpcService != null) { rpcService.closeAsync().get(); } } @Test - public void testSuccessfulRpcConnection() throws Exception { + void testSuccessfulRpcConnection() throws Exception { final String testRpcConnectionEndpointAddress = "<TestRpcConnectionEndpointAddress>"; final UUID leaderId = UUID.randomUUID(); final String connectionID = "Test RPC Connection ID"; @@ -86,23 +80,23 @@ public class RegisteredRpcConnectionTest extends TestLogger { final Either<TestRegistrationSuccess, TestRegistrationRejection> connectionResult = connection.getConnectionFuture().get(); - assertTrue(connectionResult.isLeft()); + assertThat(connectionResult.isLeft()).isTrue(); final String actualConnectionId = connectionResult.left().getCorrelationId(); // validate correct invocation and result - assertTrue(connection.isConnected()); - assertEquals(testRpcConnectionEndpointAddress, connection.getTargetAddress()); - assertEquals(leaderId, connection.getTargetLeaderId()); - assertEquals(testGateway, connection.getTargetGateway()); - assertEquals(connectionID, actualConnectionId); + assertThat(connection.isConnected()).isTrue(); + assertThat(connection.getTargetAddress()).isEqualTo(testRpcConnectionEndpointAddress); + assertThat(connection.getTargetLeaderId()).isEqualTo(leaderId); + assertThat(connection.getTargetGateway()).isEqualTo(testGateway); + assertThat(actualConnectionId).isEqualTo(connectionID); } finally { testGateway.stop(); } } @Test - public void testRpcConnectionFailures() throws Exception { + void testRpcConnectionFailures() throws Exception { final String connectionFailureMessage = "Test RPC Connection failure"; final String testRpcConnectionEndpointAddress = "<TestRpcConnectionEndpointAddress>"; final UUID leaderId = UUID.randomUUID(); @@ -130,22 +124,20 @@ public class RegisteredRpcConnectionTest extends TestLogger { connection.start(); // wait for connection failure - try { - connection.getConnectionFuture().get(); - fail("expected failure."); - } catch (ExecutionException ee) { - assertEquals(registrationException, ee.getCause()); - } + assertThatThrownBy(() -> connection.getConnectionFuture().get()) + .withFailMessage("expected failure.") + .isInstanceOf(ExecutionException.class) + .hasCause(registrationException); // validate correct invocation and result - assertFalse(connection.isConnected()); - assertEquals(testRpcConnectionEndpointAddress, connection.getTargetAddress()); - assertEquals(leaderId, connection.getTargetLeaderId()); - assertNull(connection.getTargetGateway()); + assertThat(connection.isConnected()).isFalse(); + assertThat(connection.getTargetAddress()).isEqualTo(testRpcConnectionEndpointAddress); + assertThat(connection.getTargetLeaderId()).isEqualTo(leaderId); + assertThat(connection.getTargetGateway()).isNull(); } @Test - public void testRpcConnectionRejectionCallsOnRegistrationRejection() { + void testRpcConnectionRejectionCallsOnRegistrationRejection() { TestRegistrationGateway testRegistrationGateway = DefaultTestRegistrationGateway.newBuilder() .setRegistrationFunction( @@ -169,16 +161,15 @@ public class RegisteredRpcConnectionTest extends TestLogger { final Either<TestRegistrationSuccess, TestRegistrationRejection> connectionResult = connection.getConnectionFuture().join(); - assertTrue(connectionResult.isRight()); + assertThat(connectionResult.isRight()).isTrue(); final TestRegistrationRejection registrationRejection = connectionResult.right(); - assertThat( - registrationRejection.getRejectionReason(), - is(TestRegistrationRejection.RejectionReason.REJECTED)); + assertThat(registrationRejection.getRejectionReason()) + .isEqualTo(TestRegistrationRejection.RejectionReason.REJECTED); } @Test - public void testRpcConnectionClose() throws Exception { + void testRpcConnectionClose() throws Exception { final String testRpcConnectionEndpointAddress = "<TestRpcConnectionEndpointAddress>"; final UUID leaderId = UUID.randomUUID(); final String connectionID = "Test RPC Connection ID"; @@ -201,16 +192,16 @@ public class RegisteredRpcConnectionTest extends TestLogger { connection.close(); // validate connection is closed - assertEquals(testRpcConnectionEndpointAddress, connection.getTargetAddress()); - assertEquals(leaderId, connection.getTargetLeaderId()); - assertTrue(connection.isClosed()); + assertThat(connection.getTargetAddress()).isEqualTo(testRpcConnectionEndpointAddress); + assertThat(connection.getTargetLeaderId()).isEqualTo(leaderId); + assertThat(connection.isClosed()).isTrue(); } finally { testGateway.stop(); } } @Test - public void testReconnect() throws Exception { + void testReconnect() throws Exception { final String connectionId1 = "Test RPC Connection ID 1"; final String connectionId2 = "Test RPC Connection ID 2"; final String testRpcConnectionEndpointAddress = "<TestRpcConnectionEndpointAddress>"; @@ -233,22 +224,22 @@ public class RegisteredRpcConnectionTest extends TestLogger { final Either<TestRegistrationSuccess, TestRegistrationRejection> firstConnectionResult = connection.getConnectionFuture().get(); - assertTrue(firstConnectionResult.isLeft()); + assertThat(firstConnectionResult.isLeft()).isTrue(); final String actualConnectionId1 = firstConnectionResult.left().getCorrelationId(); - assertEquals(actualConnectionId1, connectionId1); + assertThat(actualConnectionId1).isEqualTo(connectionId1); - assertTrue(connection.tryReconnect()); + assertThat(connection.tryReconnect()).isTrue(); final Either<TestRegistrationSuccess, TestRegistrationRejection> secondConnectionResult = connection.getConnectionFuture().get(); - assertTrue(secondConnectionResult.isLeft()); + assertThat(secondConnectionResult.isLeft()).isTrue(); final String actualConnectionId2 = secondConnectionResult.left().getCorrelationId(); - assertEquals(actualConnectionId2, connectionId2); + assertThat(actualConnectionId2).isEqualTo(connectionId2); } // ------------------------------------------------------------------------ diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/registration/RetryingRegistrationConfigurationTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/registration/RetryingRegistrationConfigurationTest.java index c4aaa2dff05..48381317c40 100644 --- a/flink-runtime/src/test/java/org/apache/flink/runtime/registration/RetryingRegistrationConfigurationTest.java +++ b/flink-runtime/src/test/java/org/apache/flink/runtime/registration/RetryingRegistrationConfigurationTest.java @@ -21,20 +21,18 @@ package org.apache.flink.runtime.registration; import org.apache.flink.configuration.ClusterOptions; import org.apache.flink.configuration.Configuration; import org.apache.flink.configuration.TaskManagerOptions; -import org.apache.flink.util.TestLogger; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.time.Duration; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; /** Tests for the {@link RetryingRegistrationConfiguration}. */ -public class RetryingRegistrationConfigurationTest extends TestLogger { +class RetryingRegistrationConfigurationTest { @Test - public void testConfigurationParsing() { + void testConfigurationParsing() { final Configuration configuration = new Configuration(); final long initialRegistrationTimeout = 1L; @@ -51,22 +49,18 @@ public class RetryingRegistrationConfigurationTest extends TestLogger { final RetryingRegistrationConfiguration retryingRegistrationConfiguration = RetryingRegistrationConfiguration.fromConfiguration(configuration); - assertThat( - retryingRegistrationConfiguration.getInitialRegistrationTimeoutMillis(), - is(initialRegistrationTimeout)); - assertThat( - retryingRegistrationConfiguration.getMaxRegistrationTimeoutMillis(), - is(maxRegistrationTimeout)); - assertThat( - retryingRegistrationConfiguration.getRefusedDelayMillis(), - is(refusedRegistrationDelay)); - assertThat( - retryingRegistrationConfiguration.getErrorDelayMillis(), - is(errorRegistrationDelay)); + assertThat(retryingRegistrationConfiguration.getInitialRegistrationTimeoutMillis()) + .isEqualTo(initialRegistrationTimeout); + assertThat(retryingRegistrationConfiguration.getMaxRegistrationTimeoutMillis()) + .isEqualTo(maxRegistrationTimeout); + assertThat(retryingRegistrationConfiguration.getRefusedDelayMillis()) + .isEqualTo(refusedRegistrationDelay); + assertThat(retryingRegistrationConfiguration.getErrorDelayMillis()) + .isEqualTo(errorRegistrationDelay); } @Test - public void testConfigurationWithDeprecatedOptions() { + void testConfigurationWithDeprecatedOptions() { final Configuration configuration = new Configuration(); final Duration refusedRegistrationBackoff = Duration.ofMinutes(42L); @@ -82,14 +76,11 @@ public class RetryingRegistrationConfigurationTest extends TestLogger { final RetryingRegistrationConfiguration retryingRegistrationConfiguration = RetryingRegistrationConfiguration.fromConfiguration(configuration); - assertThat( - retryingRegistrationConfiguration.getInitialRegistrationTimeoutMillis(), - is(ClusterOptions.INITIAL_REGISTRATION_TIMEOUT.defaultValue())); - assertThat( - retryingRegistrationConfiguration.getRefusedDelayMillis(), - is(ClusterOptions.REFUSED_REGISTRATION_DELAY.defaultValue())); - assertThat( - retryingRegistrationConfiguration.getMaxRegistrationTimeoutMillis(), - is(ClusterOptions.MAX_REGISTRATION_TIMEOUT.defaultValue())); + assertThat(retryingRegistrationConfiguration.getInitialRegistrationTimeoutMillis()) + .isEqualTo(ClusterOptions.INITIAL_REGISTRATION_TIMEOUT.defaultValue()); + assertThat(retryingRegistrationConfiguration.getRefusedDelayMillis()) + .isEqualTo(ClusterOptions.REFUSED_REGISTRATION_DELAY.defaultValue()); + assertThat(retryingRegistrationConfiguration.getMaxRegistrationTimeoutMillis()) + .isEqualTo(ClusterOptions.MAX_REGISTRATION_TIMEOUT.defaultValue()); } } diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/registration/RetryingRegistrationTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/registration/RetryingRegistrationTest.java index ae32dafb3f8..b3e44b292f3 100644 --- a/flink-runtime/src/test/java/org/apache/flink/runtime/registration/RetryingRegistrationTest.java +++ b/flink-runtime/src/test/java/org/apache/flink/runtime/registration/RetryingRegistrationTest.java @@ -21,16 +21,16 @@ package org.apache.flink.runtime.registration; import org.apache.flink.runtime.rpc.RpcService; import org.apache.flink.runtime.rpc.TestingRpcService; import org.apache.flink.testutils.TestingUtils; -import org.apache.flink.testutils.executor.TestExecutorResource; +import org.apache.flink.testutils.executor.TestExecutorExtension; import org.apache.flink.util.FlinkException; -import org.apache.flink.util.TestLogger; import org.apache.flink.util.concurrent.FutureUtils; import org.apache.flink.util.concurrent.ScheduledExecutorServiceAdapter; -import org.junit.After; -import org.junit.Before; -import org.junit.ClassRule; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; +import org.junit.jupiter.api.extension.RegisterExtension; import org.slf4j.LoggerFactory; import java.util.ArrayDeque; @@ -43,13 +43,8 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicInteger; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.lessThanOrEqualTo; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.Mockito.any; import static org.mockito.Mockito.anyString; import static org.mockito.Mockito.mock; @@ -59,28 +54,28 @@ import static org.mockito.Mockito.when; * Tests for the generic retrying registration class, validating the failure, retry, and back-off * behavior. */ -public class RetryingRegistrationTest extends TestLogger { +class RetryingRegistrationTest { - @ClassRule - public static final TestExecutorResource<ScheduledExecutorService> EXECUTOR_RESOURCE = - TestingUtils.defaultExecutorResource(); + @RegisterExtension + public static final TestExecutorExtension<ScheduledExecutorService> EXECUTOR_EXTENSION = + TestingUtils.defaultExecutorExtension(); private TestingRpcService rpcService; - @Before - public void setup() { + @BeforeEach + void setup() { rpcService = new TestingRpcService(); } - @After - public void tearDown() throws ExecutionException, InterruptedException { + @AfterEach + void tearDown() throws ExecutionException, InterruptedException { if (rpcService != null) { rpcService.closeAsync().get(); } } @Test - public void testSimpleSuccessfulRegistration() throws Exception { + void testSimpleSuccessfulRegistration() throws Exception { final String testId = "laissez les bon temps roulez"; final String testEndpointAddress = "<test-address>"; final UUID leaderId = UUID.randomUUID(); @@ -102,10 +97,10 @@ public class RetryingRegistrationTest extends TestLogger { TestRegistrationSuccess, TestRegistrationRejection>> future = registration.getFuture(); - assertNotNull(future); + assertThat(future).isNotNull(); // multiple accesses return the same future - assertEquals(future, registration.getFuture()); + assertThat(registration.getFuture()).isEqualTo(future); RetryingRegistration.RetryingRegistrationResult< TestRegistrationGateway, @@ -114,15 +109,15 @@ public class RetryingRegistrationTest extends TestLogger { registrationResponse = future.get(10L, TimeUnit.SECONDS); // validate correct invocation and result - assertEquals(testId, registrationResponse.getSuccess().getCorrelationId()); - assertEquals(leaderId, testGateway.getInvocations().take().leaderId()); + assertThat(registrationResponse.getSuccess().getCorrelationId()).isEqualTo(testId); + assertThat(testGateway.getInvocations().take().leaderId()).isEqualTo(leaderId); } finally { testGateway.stop(); } } @Test - public void testPropagateFailures() throws Exception { + void testPropagateFailures() throws Exception { final String testExceptionMessage = "testExceptionMessage"; // RPC service that fails with exception upon the connection @@ -135,24 +130,22 @@ public class RetryingRegistrationTest extends TestLogger { registration.startRegistration(); CompletableFuture<?> future = registration.getFuture(); - assertTrue(future.isDone()); + assertThat(future).isDone(); - try { - future.get(); - - fail("We expected an ExecutionException."); - } catch (ExecutionException e) { - assertEquals(testExceptionMessage, e.getCause().getMessage()); - } + assertThatThrownBy(future::get) + .withFailMessage("We expected an ExecutionException.") + .isInstanceOf(ExecutionException.class) + .cause() + .hasMessage(testExceptionMessage); } @Test - public void testRetryConnectOnFailure() throws Exception { + void testRetryConnectOnFailure() throws Exception { final String testId = "laissez les bon temps roulez"; final UUID leaderId = UUID.randomUUID(); ScheduledExecutorServiceAdapter executor = - new ScheduledExecutorServiceAdapter(EXECUTOR_RESOURCE.getExecutor()); + new ScheduledExecutorServiceAdapter(EXECUTOR_EXTENSION.getExecutor()); ManualResponseTestRegistrationGateway testGateway = new ManualResponseTestRegistrationGateway(new TestRegistrationSuccess(testId)); @@ -186,20 +179,22 @@ public class RetryingRegistrationTest extends TestLogger { // measure the duration of the registration --> should be longer than the error delay long duration = System.currentTimeMillis() - start; - assertTrue( - "The registration should have failed the first time. Thus the duration should be longer than at least a single error delay.", - duration > TestRetryingRegistration.DELAY_ON_ERROR); + assertThat(duration) + .withFailMessage( + "The registration should have failed the first time. Thus the duration should be longer than at least a single error delay.") + .isGreaterThan(TestRetryingRegistration.DELAY_ON_ERROR); // validate correct invocation and result - assertEquals(testId, registrationResponse.getSuccess().getCorrelationId()); - assertEquals(leaderId, testGateway.getInvocations().take().leaderId()); + assertThat(registrationResponse.getSuccess().getCorrelationId()).isEqualTo(testId); + assertThat(testGateway.getInvocations().take().leaderId()).isEqualTo(leaderId); } finally { testGateway.stop(); } } - @Test(timeout = 10000) - public void testRetriesOnTimeouts() throws Exception { + @Test + @Timeout(10000) + void testRetriesOnTimeouts() throws Exception { final String testId = "rien ne va plus"; final String testEndpointAddress = "<test-address>"; final UUID leaderId = UUID.randomUUID(); @@ -247,18 +242,20 @@ public class RetryingRegistrationTest extends TestLogger { long elapsedMillis = (finished - started) / 1000000; // validate correct invocation and result - assertEquals(testId, registrationResponse.getSuccess().getCorrelationId()); - assertEquals(leaderId, testGateway.getInvocations().take().leaderId()); + assertThat(registrationResponse.getSuccess().getCorrelationId()).isEqualTo(testId); + assertThat(testGateway.getInvocations().take().leaderId()).isEqualTo(leaderId); // validate that some retry-delay / back-off behavior happened - assertTrue("retries did not properly back off", elapsedMillis >= 3 * initialTimeout); + assertThat(elapsedMillis) + .withFailMessage("retries did not properly back off") + .isGreaterThanOrEqualTo(3 * initialTimeout); } finally { testGateway.stop(); } } @Test - public void testFailure() throws Exception { + void testFailure() throws Exception { final String testId = "qui a coupe le fromage"; final String testEndpointAddress = "<test-address>"; final UUID leaderId = UUID.randomUUID(); @@ -296,14 +293,14 @@ public class RetryingRegistrationTest extends TestLogger { long elapsedMillis = (finished - started) / 1000000; // validate correct invocation and result - assertEquals(testId, registrationResponse.getSuccess().getCorrelationId()); - assertEquals(leaderId, testGateway.getInvocations().take().leaderId()); + assertThat(registrationResponse.getSuccess().getCorrelationId()).isEqualTo(testId); + assertThat(testGateway.getInvocations().take().leaderId()).isEqualTo(leaderId); // validate that some retry-delay / back-off behavior happened - assertTrue( - "retries did not properly back off", - elapsedMillis - >= 2 * TestRetryingRegistration.INITIAL_TIMEOUT + assertThat(elapsedMillis) + .withFailMessage("retries did not properly back off") + .isGreaterThanOrEqualTo( + 2 * TestRetryingRegistration.INITIAL_TIMEOUT + TestRetryingRegistration.DELAY_ON_FAILURE); } finally { testGateway.stop(); @@ -311,7 +308,7 @@ public class RetryingRegistrationTest extends TestLogger { } @Test - public void testRegistrationRejection() { + void testRegistrationRejection() { final TestRegistrationGateway testRegistrationGateway = new ManualResponseTestRegistrationGateway( new TestRegistrationRejection( @@ -329,15 +326,14 @@ public class RetryingRegistrationTest extends TestLogger { TestRegistrationGateway, TestRegistrationSuccess, TestRegistrationRejection> response = testRetryingRegistration.getFuture().join(); - assertTrue(response.isRejection()); - assertThat( - response.getRejection().getRejectionReason(), - is(TestRegistrationRejection.RejectionReason.REJECTED)); + assertThat(response.isRejection()).isTrue(); + assertThat(response.getRejection().getRejectionReason()) + .isEqualTo(TestRegistrationRejection.RejectionReason.REJECTED); } @Test @SuppressWarnings("unchecked") - public void testRetryOnError() throws Exception { + void testRetryOnError() throws Exception { final String testId = "Petit a petit, l'oiseau fait son nid"; final String testEndpointAddress = "<test-address>"; final UUID leaderId = UUID.randomUUID(); @@ -373,16 +369,16 @@ public class RetryingRegistrationTest extends TestLogger { long finished = System.nanoTime(); long elapsedMillis = (finished - started) / 1000000; - assertEquals(testId, registrationResponse.getSuccess().getCorrelationId()); + assertThat(registrationResponse.getSuccess().getCorrelationId()).isEqualTo(testId); // validate that some retry-delay / back-off behavior happened - assertTrue( - "retries did not properly back off", - elapsedMillis >= TestRetryingRegistration.DELAY_ON_ERROR); + assertThat(elapsedMillis) + .withFailMessage("retries did not properly back off") + .isGreaterThanOrEqualTo(TestRetryingRegistration.DELAY_ON_ERROR); } @Test - public void testCancellation() throws Exception { + void testCancellation() throws Exception { final String testEndpointAddress = "my-test-address"; final UUID leaderId = UUID.randomUUID(); @@ -409,7 +405,7 @@ public class RetryingRegistrationTest extends TestLogger { result.completeExceptionally(new TimeoutException()); // there should not be a second registration attempt - assertThat(registrationCallCounter.get(), is(lessThanOrEqualTo(1))); + assertThat(registrationCallCounter).hasValueLessThanOrEqualTo(1); } // ------------------------------------------------------------------------