Migrate common to junit5 (#11431) (#11438)

Motivation:

We should update to use junit5 in all modules.

Modifications:

Adjust common tests to use junit5

Result:

Part of https://github.com/netty/netty/issues/10757
This commit is contained in:
Norman Maurer 2021-07-01 18:19:44 +02:00 committed by GitHub
parent 8003ea8a03
commit c82c17782c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 96 additions and 71 deletions

View File

@ -15,13 +15,13 @@
*/ */
package io.netty.util.concurrent; package io.netty.util.concurrent;
import org.junit.Assert; import org.junit.jupiter.api.Test;
import org.junit.Test; import org.junit.jupiter.api.Assertions;
public class ScheduledFutureTaskTest { public class ScheduledFutureTaskTest {
@Test @Test
public void testDeadlineNanosNotOverflow() { public void testDeadlineNanosNotOverflow() {
Assert.assertEquals(Long.MAX_VALUE, ScheduledFutureTask.deadlineNanos(Long.MAX_VALUE)); Assertions.assertEquals(Long.MAX_VALUE, ScheduledFutureTask.deadlineNanos(Long.MAX_VALUE));
} }
} }

View File

@ -15,10 +15,11 @@
*/ */
package io.netty.util.concurrent; package io.netty.util.concurrent;
import org.junit.Assert; import org.junit.jupiter.api.Test;
import org.junit.Test;
import io.netty.util.concurrent.AbstractEventExecutor.LazyRunnable; import io.netty.util.concurrent.AbstractEventExecutor.LazyRunnable;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.function.Executable;
import java.util.Collections; import java.util.Collections;
import java.util.Set; import java.util.Set;
@ -36,7 +37,11 @@ import java.util.concurrent.atomic.AtomicReference;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
public class SingleThreadEventExecutorTest { public class SingleThreadEventExecutorTest {
@ -44,7 +49,8 @@ public class SingleThreadEventExecutorTest {
public void testWrappedExecutorIsShutdown() { public void testWrappedExecutorIsShutdown() {
ExecutorService executorService = Executors.newSingleThreadExecutor(); ExecutorService executorService = Executors.newSingleThreadExecutor();
SingleThreadEventExecutor executor = new SingleThreadEventExecutor(null, executorService, false) { final SingleThreadEventExecutor executor =
new SingleThreadEventExecutor(null, executorService, false) {
@Override @Override
protected void run() { protected void run() {
while (!confirmShutdown()) { while (!confirmShutdown()) {
@ -59,27 +65,27 @@ public class SingleThreadEventExecutorTest {
executorService.shutdownNow(); executorService.shutdownNow();
executeShouldFail(executor); executeShouldFail(executor);
executeShouldFail(executor); executeShouldFail(executor);
try { assertThrows(RejectedExecutionException.class, new Executable() {
executor.shutdownGracefully().syncUninterruptibly(); @Override
Assert.fail(); public void execute() {
} catch (RejectedExecutionException expected) { executor.shutdownGracefully().syncUninterruptibly();
// expected }
} });
Assert.assertTrue(executor.isShutdown()); assertTrue(executor.isShutdown());
} }
private static void executeShouldFail(Executor executor) { private static void executeShouldFail(final Executor executor) {
try { assertThrows(RejectedExecutionException.class, new Executable() {
executor.execute(new Runnable() { @Override
@Override public void execute() {
public void run() { executor.execute(new Runnable() {
// Noop. @Override
} public void run() {
}); // Noop.
Assert.fail(); }
} catch (RejectedExecutionException expected) { });
// expected }
} });
} }
@Test @Test
@ -101,31 +107,35 @@ public class SingleThreadEventExecutorTest {
ThreadProperties threadProperties = executor.threadProperties(); ThreadProperties threadProperties = executor.threadProperties();
Thread thread = threadRef.get(); Thread thread = threadRef.get();
Assert.assertEquals(thread.getId(), threadProperties.id()); assertEquals(thread.getId(), threadProperties.id());
Assert.assertEquals(thread.getName(), threadProperties.name()); assertEquals(thread.getName(), threadProperties.name());
Assert.assertEquals(thread.getPriority(), threadProperties.priority()); assertEquals(thread.getPriority(), threadProperties.priority());
Assert.assertEquals(thread.isAlive(), threadProperties.isAlive()); assertEquals(thread.isAlive(), threadProperties.isAlive());
Assert.assertEquals(thread.isDaemon(), threadProperties.isDaemon()); assertEquals(thread.isDaemon(), threadProperties.isDaemon());
Assert.assertTrue(threadProperties.stackTrace().length > 0); assertTrue(threadProperties.stackTrace().length > 0);
executor.shutdownGracefully(); executor.shutdownGracefully();
} }
@Test(expected = RejectedExecutionException.class, timeout = 3000) @Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testInvokeAnyInEventLoop() { public void testInvokeAnyInEventLoop() {
testInvokeInEventLoop(true, false); testInvokeInEventLoop(true, false);
} }
@Test(expected = RejectedExecutionException.class, timeout = 3000) @Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testInvokeAnyInEventLoopWithTimeout() { public void testInvokeAnyInEventLoopWithTimeout() {
testInvokeInEventLoop(true, true); testInvokeInEventLoop(true, true);
} }
@Test(expected = RejectedExecutionException.class, timeout = 3000) @Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testInvokeAllInEventLoop() { public void testInvokeAllInEventLoop() {
testInvokeInEventLoop(false, false); testInvokeInEventLoop(false, false);
} }
@Test(expected = RejectedExecutionException.class, timeout = 3000) @Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testInvokeAllInEventLoopWithTimeout() { public void testInvokeAllInEventLoopWithTimeout() {
testInvokeInEventLoop(false, true); testInvokeInEventLoop(false, true);
} }
@ -144,38 +154,44 @@ public class SingleThreadEventExecutorTest {
} }
}; };
try { try {
final Promise<Void> promise = executor.newPromise(); assertThrows(RejectedExecutionException.class, new Executable() {
executor.execute(new Runnable() {
@Override @Override
public void run() { public void execute() throws Throwable {
try { final Promise<Void> promise = executor.newPromise();
Set<Callable<Boolean>> set = Collections.<Callable<Boolean>>singleton(new Callable<Boolean>() { executor.execute(new Runnable() {
@Override @Override
public Boolean call() throws Exception { public void run() {
promise.setFailure(new AssertionError("Should never execute the Callable")); try {
return Boolean.TRUE; Set<Callable<Boolean>> set = Collections.<Callable<Boolean>>singleton(
} new Callable<Boolean>() {
}); @Override
if (any) { public Boolean call() throws Exception {
if (timeout) { promise.setFailure(new AssertionError("Should never execute the Callable"));
executor.invokeAny(set, 10, TimeUnit.SECONDS); return Boolean.TRUE;
} else { }
executor.invokeAny(set); });
} if (any) {
} else { if (timeout) {
if (timeout) { executor.invokeAny(set, 10, TimeUnit.SECONDS);
executor.invokeAll(set, 10, TimeUnit.SECONDS); } else {
} else { executor.invokeAny(set);
executor.invokeAll(set); }
} else {
if (timeout) {
executor.invokeAll(set, 10, TimeUnit.SECONDS);
} else {
executor.invokeAll(set);
}
}
promise.setFailure(new AssertionError("Should never reach here"));
} catch (Throwable cause) {
promise.setFailure(cause);
} }
} }
promise.setFailure(new AssertionError("Should never reach here")); });
} catch (Throwable cause) { promise.syncUninterruptibly();
promise.setFailure(cause);
}
} }
}); });
promise.syncUninterruptibly();
} finally { } finally {
executor.shutdownGracefully(0, 0, TimeUnit.MILLISECONDS); executor.shutdownGracefully(0, 0, TimeUnit.MILLISECONDS);
} }
@ -210,7 +226,7 @@ public class SingleThreadEventExecutorTest {
runAllTasks(); runAllTasks();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
Assert.fail(e.toString()); fail(e.toString());
} }
} }
} }
@ -322,7 +338,8 @@ public class SingleThreadEventExecutorTest {
} }
} }
@Test(timeout = 5000) @Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testTakeTask() throws Exception { public void testTakeTask() throws Exception {
final SingleThreadEventExecutor executor = final SingleThreadEventExecutor executor =
new SingleThreadEventExecutor(null, Executors.defaultThreadFactory(), true) { new SingleThreadEventExecutor(null, Executors.defaultThreadFactory(), true) {
@ -356,7 +373,8 @@ public class SingleThreadEventExecutorTest {
assertThat(afterTask.ran.get(), is(true)); assertThat(afterTask.ran.get(), is(true));
} }
@Test(timeout = 5000) @Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testTakeTaskAlwaysHasTask() throws Exception { public void testTakeTaskAlwaysHasTask() throws Exception {
//for https://github.com/netty/netty/issues/1614 //for https://github.com/netty/netty/issues/1614

View File

@ -15,8 +15,9 @@
package io.netty.util.collection; package io.netty.util.collection;
import io.netty.util.collection.@K@ObjectMap.PrimitiveEntry; import io.netty.util.collection.@K@ObjectMap.PrimitiveEntry;
import org.junit.Before;
import org.junit.Test; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
@ -26,7 +27,13 @@ import java.util.Map;
import java.util.Random; import java.util.Random;
import java.util.Set; import java.util.Set;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
* Tests for {@link @K@ObjectHashMap}. * Tests for {@link @K@ObjectHashMap}.
@ -73,13 +80,13 @@ public class @K@ObjectHashMapTest {
private @K@ObjectHashMap<Value> map; private @K@ObjectHashMap<Value> map;
@Before @BeforeEach
public void setup() { public void setup() {
map = new @K@ObjectHashMap<Value>(); map = new @K@ObjectHashMap<Value>();
} }
@Test @Test
public void iteartorRemoveShouldNotNPE() { public void iteratorRemoveShouldNotNPE() {
map = new @K@ObjectHashMap<Value>(4, 1); map = new @K@ObjectHashMap<Value>(4, 1);
map.put((@O@)(@k@) 0, new Value("A")); map.put((@O@)(@k@) 0, new Value("A"));
map.put((@O@)(@k@) 1, new Value("B")); map.put((@O@)(@k@) 1, new Value("B"));