Skip to content

Commit

Permalink
[FLINK-32858][tests][JUnit5 migration] Migrate flink-runtime/utils te…
Browse files Browse the repository at this point in the history
…sts to JUnit5
  • Loading branch information
X-czh committed Aug 24, 2023
1 parent 3cd0bf0 commit 618e0e7
Show file tree
Hide file tree
Showing 26 changed files with 1,627 additions and 1,665 deletions.
Expand Up @@ -21,28 +21,27 @@
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.rpc.AddressResolution;
import org.apache.flink.runtime.rpc.RpcSystem;
import org.apache.flink.util.TestLogger;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.net.InetAddress;
import java.net.UnknownHostException;

import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assumptions.assumeThat;

/** Unit tests for respecting {@link AddressResolution}. */
public class AddressResolutionTest extends TestLogger {
class AddressResolutionTest {

private static final RpcSystem RPC_SYSTEM = RpcSystem.load();

private static final String ENDPOINT_NAME = "endpoint";
private static final String NON_EXISTING_HOSTNAME = "foo.bar.com.invalid";
private static final int PORT = 17234;

@BeforeClass
public static void check() {
@BeforeAll
static void check() {
checkPreconditions();
}

Expand All @@ -60,11 +59,11 @@ private static void checkPreconditions() {
throwsException = true;
}

assumeTrue(throwsException);
assumeThat(throwsException).isTrue();
}

@Test
public void testNoAddressResolution() throws UnknownHostException {
void testNoAddressResolution() throws UnknownHostException {
RPC_SYSTEM.getRpcUrl(
NON_EXISTING_HOSTNAME,
PORT,
Expand All @@ -74,17 +73,15 @@ public void testNoAddressResolution() throws UnknownHostException {
}

@Test
public void testTryAddressResolution() {
try {
RPC_SYSTEM.getRpcUrl(
NON_EXISTING_HOSTNAME,
PORT,
ENDPOINT_NAME,
AddressResolution.TRY_ADDRESS_RESOLUTION,
new Configuration());
fail("This should fail with an UnknownHostException");
} catch (UnknownHostException ignore) {
// expected
}
void testTryAddressResolution() {
assertThatThrownBy(
() ->
RPC_SYSTEM.getRpcUrl(
NON_EXISTING_HOSTNAME,
PORT,
ENDPOINT_NAME,
AddressResolution.TRY_ADDRESS_RESOLUTION,
new Configuration()))
.isInstanceOf(UnknownHostException.class);
}
}
Expand Up @@ -21,27 +21,26 @@
import org.apache.flink.runtime.testutils.TestJvmProcess;
import org.apache.flink.util.OperatingSystem;

import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.UUID;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assumptions.assumeThat;

/**
* Test that verifies the behavior of blocking shutdown hooks and of the {@link
* JvmShutdownSafeguard} that guards against it.
*/
public class BlockingShutdownTest {
class BlockingShutdownTest {

@Test
public void testProcessShutdownBlocking() throws Exception {
void testProcessShutdownBlocking() throws Exception {
// this test works only on linux
assumeTrue(OperatingSystem.isLinux());
assumeThat(OperatingSystem.isLinux()).isTrue();

final File markerFile =
new File(
Expand All @@ -54,7 +53,7 @@ public void testProcessShutdownBlocking() throws Exception {
try {
blockingProcess.startProcess();
long pid = blockingProcess.getProcessId();
assertTrue("Cannot determine process ID", pid != -1);
assertThat(pid).withFailMessage("Cannot determine process ID").isNotEqualTo(-1);

// wait for the marker file to appear, which means the process is up properly
TestJvmProcess.waitForMarkerFile(markerFile, 30000);
Expand All @@ -69,9 +68,9 @@ public void testProcessShutdownBlocking() throws Exception {
Thread.sleep(50);

// the process should not go away by itself
assertTrue(
"Test broken, process shutdown blocking does not work",
blockingProcess.isAlive());
assertThat(blockingProcess.isAlive())
.withFailMessage("Test broken, process shutdown blocking does not work")
.isTrue();
} finally {
blockingProcess.destroy();

Expand All @@ -81,9 +80,9 @@ public void testProcessShutdownBlocking() throws Exception {
}

@Test
public void testProcessExitsDespiteBlockingShutdownHook() throws Exception {
void testProcessExitsDespiteBlockingShutdownHook() throws Exception {
// this test works only on linux
assumeTrue(OperatingSystem.isLinux());
assumeThat(OperatingSystem.isLinux()).isTrue();

final File markerFile =
new File(
Expand All @@ -96,7 +95,7 @@ public void testProcessExitsDespiteBlockingShutdownHook() throws Exception {
try {
blockingProcess.startProcess();
long pid = blockingProcess.getProcessId();
assertTrue("Cannot determine process ID", pid != -1);
assertThat(pid).withFailMessage("Cannot determine process ID").isNotEqualTo(-1);

// wait for the marker file to appear, which means the process is up properly
TestJvmProcess.waitForMarkerFile(markerFile, 30000);
Expand All @@ -109,9 +108,9 @@ public void testProcessExitsDespiteBlockingShutdownHook() throws Exception {
Thread.sleep(50);
}

assertFalse(
"shutdown blocking process does not properly terminate itself",
blockingProcess.isAlive());
assertThat(blockingProcess.isAlive())
.withFailMessage("shutdown blocking process does not properly terminate itself")
.isFalse();
} finally {
blockingProcess.destroy();

Expand Down
Expand Up @@ -18,90 +18,83 @@

package org.apache.flink.runtime.util;

import org.apache.flink.util.TestLogger;
import org.junit.jupiter.api.Test;

import org.junit.Test;

import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
import static org.hamcrest.collection.IsIterableWithSize.iterableWithSize;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/** {@code BoundedFIFOQueueTest} tests {@link BoundedFIFOQueue}. */
public class BoundedFIFOQueueTest extends TestLogger {
class BoundedFIFOQueueTest {

@Test(expected = IllegalArgumentException.class)
public void testConstructorFailing() {
new BoundedFIFOQueue<>(-1);
@Test
void testConstructorFailing() {
assertThatThrownBy(() -> new BoundedFIFOQueue<>(-1))
.isInstanceOf(IllegalArgumentException.class);
}

@Test
public void testQueueWithMaxSize0() {
void testQueueWithMaxSize0() {
final BoundedFIFOQueue<Integer> testInstance = new BoundedFIFOQueue<>(0);
assertThat(testInstance, iterableWithSize(0));
assertThat(testInstance).isEmpty();
testInstance.add(1);
assertThat(testInstance, iterableWithSize(0));
assertThat(testInstance).isEmpty();
}

@Test
public void testQueueWithMaxSize2() {
void testQueueWithMaxSize2() {
final BoundedFIFOQueue<Integer> testInstance = new BoundedFIFOQueue<>(2);
assertThat(testInstance, iterableWithSize(0));
assertThat(testInstance).isEmpty();

testInstance.add(1);
assertThat(testInstance, contains(1));
assertThat(testInstance).contains(1);

testInstance.add(2);
assertThat(testInstance, contains(1, 2));
assertThat(testInstance).contains(1, 2);

testInstance.add(3);
assertThat(testInstance, contains(2, 3));
assertThat(testInstance).contains(2, 3);
}

@Test
public void testAddNullHandling() {
void testAddNullHandling() {
final BoundedFIFOQueue<Integer> testInstance = new BoundedFIFOQueue<>(1);
try {
testInstance.add(null);
fail("A NullPointerException is expected to be thrown.");
} catch (NullPointerException e) {
// NullPointerException is expected
}

assertThat(testInstance, iterableWithSize(0));
assertThatThrownBy(() -> testInstance.add(null))
.withFailMessage("A NullPointerException is expected to be thrown.")
.isInstanceOf(NullPointerException.class);

assertThat(testInstance).isEmpty();
}

/**
* Tests that {@link BoundedFIFOQueue#size()} returns the number of elements currently stored in
* the queue with a {@code maxSize} of 0.
*/
@Test
public void testSizeWithMaxSize0() {
void testSizeWithMaxSize0() {
final BoundedFIFOQueue<Integer> testInstance = new BoundedFIFOQueue<>(0);
assertThat(testInstance.size(), is(0));
assertThat(testInstance).isEmpty();

testInstance.add(1);
assertThat(testInstance.size(), is(0));
assertThat(testInstance).isEmpty();
}

/**
* Tests that {@link BoundedFIFOQueue#size()} returns the number of elements currently stored in
* the queue with a {@code maxSize} of 2.
*/
@Test
public void testSizeWithMaxSize2() {
void testSizeWithMaxSize2() {
final BoundedFIFOQueue<Integer> testInstance = new BoundedFIFOQueue<>(2);
assertThat(testInstance.size(), is(0));
assertThat(testInstance).isEmpty();

testInstance.add(5);
assertThat(testInstance.size(), is(1));
assertThat(testInstance).hasSize(1);

testInstance.add(6);
assertThat(testInstance.size(), is(2));
assertThat(testInstance).hasSize(2);

// adding a 3rd element won't increase the size anymore
testInstance.add(7);
assertThat(testInstance.size(), is(2));
assertThat(testInstance).hasSize(2);
}
}

0 comments on commit 618e0e7

Please sign in to comment.