Migrate transport tests to JUnit 5 (#11315)

Motivation:

JUnit 5 is more expressive, extensible, and composable in many ways, and it's better able to run tests in parallel.

Modifications:

Use JUnit5 in tests

Result:

Related to https://github.com/netty/netty/issues/10757
This commit is contained in:
Riley Park 2021-05-26 01:47:15 -07:00 committed by Norman Maurer
parent 22faf32c25
commit bfd2c5ac8a
37 changed files with 648 additions and 404 deletions

View File

@ -43,8 +43,10 @@ import io.netty.util.AttributeKey;
import io.netty.util.concurrent.EventExecutor; import io.netty.util.concurrent.EventExecutor;
import io.netty.util.concurrent.Future; import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.Promise; import io.netty.util.concurrent.Promise;
import org.junit.AfterClass; import org.junit.jupiter.api.AfterAll;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.function.Executable;
import java.net.ConnectException; import java.net.ConnectException;
import java.net.SocketAddress; import java.net.SocketAddress;
@ -58,10 +60,19 @@ import java.util.UUID;
import java.util.concurrent.BlockingQueue; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.*; import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class BootstrapTest { public class BootstrapTest {
@ -69,7 +80,7 @@ public class BootstrapTest {
private static final EventLoopGroup groupB = new DefaultEventLoopGroup(1); private static final EventLoopGroup groupB = new DefaultEventLoopGroup(1);
private static final ChannelInboundHandler dummyHandler = new DummyHandler(); private static final ChannelInboundHandler dummyHandler = new DummyHandler();
@AfterClass @AfterAll
public static void destroy() { public static void destroy() {
groupA.shutdownGracefully(); groupA.shutdownGracefully();
groupB.shutdownGracefully(); groupB.shutdownGracefully();
@ -118,7 +129,8 @@ public class BootstrapTest {
.bind(LocalAddress.ANY).sync(); .bind(LocalAddress.ANY).sync();
} }
@Test(timeout = 10000) @Test
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
public void testBindDeadLock() throws Exception { public void testBindDeadLock() throws Exception {
final Bootstrap bootstrapA = new Bootstrap(); final Bootstrap bootstrapA = new Bootstrap();
bootstrapA.group(groupA); bootstrapA.group(groupA);
@ -154,7 +166,8 @@ public class BootstrapTest {
} }
} }
@Test(timeout = 10000) @Test
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
public void testConnectDeadLock() throws Exception { public void testConnectDeadLock() throws Exception {
final Bootstrap bootstrapA = new Bootstrap(); final Bootstrap bootstrapA = new Bootstrap();
bootstrapA.group(groupA); bootstrapA.group(groupA);
@ -267,7 +280,8 @@ public class BootstrapTest {
} }
} }
@Test(expected = ConnectException.class, timeout = 10000) @Test
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
public void testLateRegistrationConnect() throws Exception { public void testLateRegistrationConnect() throws Exception {
EventLoopGroup group = new DelayedEventLoopGroup(); EventLoopGroup group = new DelayedEventLoopGroup();
try { try {
@ -275,7 +289,12 @@ public class BootstrapTest {
bootstrapA.group(group); bootstrapA.group(group);
bootstrapA.channel(LocalChannel.class); bootstrapA.channel(LocalChannel.class);
bootstrapA.handler(dummyHandler); bootstrapA.handler(dummyHandler);
assertThrows(ConnectException.class, new Executable() {
@Override
public void execute() {
bootstrapA.connect(LocalAddress.ANY).syncUninterruptibly(); bootstrapA.connect(LocalAddress.ANY).syncUninterruptibly();
}
});
} finally { } finally {
group.shutdownGracefully(); group.shutdownGracefully();
} }

View File

@ -29,20 +29,23 @@ import io.netty.channel.local.LocalChannel;
import io.netty.channel.local.LocalEventLoopGroup; import io.netty.channel.local.LocalEventLoopGroup;
import io.netty.channel.local.LocalServerChannel; import io.netty.channel.local.LocalServerChannel;
import io.netty.util.AttributeKey; import io.netty.util.AttributeKey;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public class ServerBootstrapTest { public class ServerBootstrapTest {
@Test(timeout = 5000) @Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testHandlerRegister() throws Exception { public void testHandlerRegister() throws Exception {
final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Throwable> error = new AtomicReference<Throwable>(); final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
@ -72,12 +75,14 @@ public class ServerBootstrapTest {
} }
} }
@Test(timeout = 3000) @Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testParentHandler() throws Exception { public void testParentHandler() throws Exception {
testParentHandler(false); testParentHandler(false);
} }
@Test(timeout = 3000) @Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testParentHandlerViaChannelInitializer() throws Exception { public void testParentHandlerViaChannelInitializer() throws Exception {
testParentHandler(true); testParentHandler(true);
} }

View File

@ -21,11 +21,12 @@ import java.net.SocketAddress;
import java.nio.channels.ClosedChannelException; import java.nio.channels.ClosedChannelException;
import io.netty.util.NetUtil; import io.netty.util.NetUtil;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.mockito.invocation.InvocationOnMock; import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer; import org.mockito.stubbing.Answer;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
public class AbstractChannelTest { public class AbstractChannelTest {
@ -53,9 +54,9 @@ public class AbstractChannelTest {
// This allows us to have a single-threaded test // This allows us to have a single-threaded test
when(eventLoop.inEventLoop()).thenReturn(true); when(eventLoop.inEventLoop()).thenReturn(true);
doAnswer(new Answer() { doAnswer(new Answer<Object>() {
@Override @Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable { public Object answer(InvocationOnMock invocationOnMock) {
((Runnable) invocationOnMock.getArgument(0)).run(); ((Runnable) invocationOnMock.getArgument(0)).run();
return null; return null;
} }

View File

@ -20,13 +20,13 @@ import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.ReferenceCountUtil; import io.netty.util.ReferenceCountUtil;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.nio.channels.ClosedChannelException; import java.nio.channels.ClosedChannelException;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public class AbstractCoalescingBufferQueueTest { public class AbstractCoalescingBufferQueueTest {

View File

@ -22,11 +22,12 @@ import io.netty.util.concurrent.DefaultEventExecutorGroup;
import io.netty.util.concurrent.EventExecutor; import io.netty.util.concurrent.EventExecutor;
import io.netty.util.concurrent.EventExecutorGroup; import io.netty.util.concurrent.EventExecutorGroup;
import io.netty.util.concurrent.Future; import io.netty.util.concurrent.Future;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.*;
public abstract class AbstractEventLoopTest { public abstract class AbstractEventLoopTest {
@ -43,11 +44,11 @@ public abstract class AbstractEventLoopTest {
ChannelFuture future = bootstrap.channel(newChannel()).group(group) ChannelFuture future = bootstrap.channel(newChannel()).group(group)
.childHandler(new ChannelInitializer<SocketChannel>() { .childHandler(new ChannelInitializer<SocketChannel>() {
@Override @Override
public void initChannel(SocketChannel ch) throws Exception { public void initChannel(SocketChannel ch) {
} }
}).handler(new ChannelInitializer<ServerSocketChannel>() { }).handler(new ChannelInitializer<ServerSocketChannel>() {
@Override @Override
public void initChannel(ServerSocketChannel ch) throws Exception { public void initChannel(ServerSocketChannel ch) {
ch.pipeline().addLast(new TestChannelHandler()); ch.pipeline().addLast(new TestChannelHandler());
ch.pipeline().addLast(eventExecutorGroup, new TestChannelHandler2()); ch.pipeline().addLast(eventExecutorGroup, new TestChannelHandler2());
} }
@ -63,7 +64,8 @@ public abstract class AbstractEventLoopTest {
assertSame(executor, future.channel().pipeline().context(TestChannelHandler2.class).executor()); assertSame(executor, future.channel().pipeline().context(TestChannelHandler2.class).executor());
} }
@Test(timeout = 5000) @Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testShutdownGracefullyNoQuietPeriod() throws Exception { public void testShutdownGracefullyNoQuietPeriod() throws Exception {
EventLoopGroup loop = newEventLoopGroup(); EventLoopGroup loop = newEventLoopGroup();
ServerBootstrap b = new ServerBootstrap(); ServerBootstrap b = new ServerBootstrap();

View File

@ -18,11 +18,11 @@ package io.netty.channel;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.UnpooledByteBufAllocator; import io.netty.buffer.UnpooledByteBufAllocator;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mock; import org.mockito.Mock;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@ -32,7 +32,7 @@ public class AdaptiveRecvByteBufAllocatorTest {
private ByteBufAllocator alloc = UnpooledByteBufAllocator.DEFAULT; private ByteBufAllocator alloc = UnpooledByteBufAllocator.DEFAULT;
private RecvByteBufAllocator.ExtendedHandle handle; private RecvByteBufAllocator.ExtendedHandle handle;
@Before @BeforeEach
public void setup() { public void setup() {
config = mock(ChannelConfig.class); config = mock(ChannelConfig.class);
when(config.isAutoRead()).thenReturn(true); when(config.isAutoRead()).thenReturn(true);

View File

@ -15,7 +15,6 @@
*/ */
package io.netty.channel; package io.netty.channel;
import io.netty.bootstrap.Bootstrap; import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap; import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
@ -23,7 +22,7 @@ import io.netty.buffer.Unpooled;
import io.netty.channel.local.LocalChannel; import io.netty.channel.local.LocalChannel;
import io.netty.channel.local.LocalServerChannel; import io.netty.channel.local.LocalServerChannel;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.assertEquals;
class BaseChannelTest { class BaseChannelTest {

View File

@ -23,9 +23,10 @@ import io.netty.channel.local.LocalChannel;
import io.netty.channel.local.LocalServerChannel; import io.netty.channel.local.LocalServerChannel;
import io.netty.util.concurrent.EventExecutor; import io.netty.util.concurrent.EventExecutor;
import io.netty.util.concurrent.Future; import io.netty.util.concurrent.Future;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
@ -37,11 +38,12 @@ import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.Assert.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.Assert.assertSame; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
public class ChannelInitializerTest { public class ChannelInitializerTest {
private static final int TIMEOUT_MILLIS = 1000; private static final int TIMEOUT_MILLIS = 1000;
@ -51,7 +53,7 @@ public class ChannelInitializerTest {
private Bootstrap client; private Bootstrap client;
private InspectableHandler testHandler; private InspectableHandler testHandler;
@Before @BeforeEach
public void setUp() { public void setUp() {
group = new DefaultEventLoopGroup(1); group = new DefaultEventLoopGroup(1);
server = new ServerBootstrap() server = new ServerBootstrap()
@ -65,7 +67,7 @@ public class ChannelInitializerTest {
testHandler = new InspectableHandler(); testHandler = new InspectableHandler();
} }
@After @AfterEach
public void tearDown() { public void tearDown() {
group.shutdownGracefully(0, TIMEOUT_MILLIS, TimeUnit.MILLISECONDS).syncUninterruptibly(); group.shutdownGracefully(0, TIMEOUT_MILLIS, TimeUnit.MILLISECONDS).syncUninterruptibly();
} }
@ -120,11 +122,11 @@ public class ChannelInitializerTest {
client.handler(new ChannelInitializer<Channel>() { client.handler(new ChannelInitializer<Channel>() {
@Override @Override
protected void initChannel(Channel ch) throws Exception { protected void initChannel(Channel ch) {
ch.pipeline().addLast(handler1); ch.pipeline().addLast(handler1);
ch.pipeline().addLast(new ChannelInitializer<Channel>() { ch.pipeline().addLast(new ChannelInitializer<Channel>() {
@Override @Override
protected void initChannel(Channel ch) throws Exception { protected void initChannel(Channel ch) {
ch.pipeline().addLast(handler2); ch.pipeline().addLast(handler2);
ch.pipeline().addLast(handler3); ch.pipeline().addLast(handler3);
} }
@ -159,14 +161,14 @@ public class ChannelInitializerTest {
final AtomicInteger registeredCalled = new AtomicInteger(0); final AtomicInteger registeredCalled = new AtomicInteger(0);
final ChannelInboundHandlerAdapter handler1 = new ChannelInboundHandlerAdapter() { final ChannelInboundHandlerAdapter handler1 = new ChannelInboundHandlerAdapter() {
@Override @Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception { public void channelRegistered(ChannelHandlerContext ctx) {
registeredCalled.incrementAndGet(); registeredCalled.incrementAndGet();
} }
}; };
final AtomicInteger initChannelCalled = new AtomicInteger(0); final AtomicInteger initChannelCalled = new AtomicInteger(0);
client.handler(new ChannelInitializer<Channel>() { client.handler(new ChannelInitializer<Channel>() {
@Override @Override
protected void initChannel(Channel ch) throws Exception { protected void initChannel(Channel ch) {
initChannelCalled.incrementAndGet(); initChannelCalled.incrementAndGet();
ch.pipeline().addLast(handler1); ch.pipeline().addLast(handler1);
ch.pipeline().fireChannelRegistered(); ch.pipeline().fireChannelRegistered();
@ -190,7 +192,8 @@ public class ChannelInitializerTest {
} }
} }
@Test(timeout = TIMEOUT_MILLIS) @Test
@Timeout(value = TIMEOUT_MILLIS, unit = TimeUnit.MILLISECONDS)
public void firstHandlerInPipelineShouldReceiveChannelRegisteredEvent() { public void firstHandlerInPipelineShouldReceiveChannelRegisteredEvent() {
testChannelRegisteredEventPropagation(new ChannelInitializer<LocalChannel>() { testChannelRegisteredEventPropagation(new ChannelInitializer<LocalChannel>() {
@Override @Override
@ -200,7 +203,8 @@ public class ChannelInitializerTest {
}); });
} }
@Test(timeout = TIMEOUT_MILLIS) @Test
@Timeout(value = TIMEOUT_MILLIS, unit = TimeUnit.MILLISECONDS)
public void lastHandlerInPipelineShouldReceiveChannelRegisteredEvent() { public void lastHandlerInPipelineShouldReceiveChannelRegisteredEvent() {
testChannelRegisteredEventPropagation(new ChannelInitializer<LocalChannel>() { testChannelRegisteredEventPropagation(new ChannelInitializer<LocalChannel>() {
@Override @Override
@ -224,10 +228,10 @@ public class ChannelInitializerTest {
final AtomicBoolean called = new AtomicBoolean(); final AtomicBoolean called = new AtomicBoolean();
EmbeddedChannel channel = new EmbeddedChannel(new ChannelInitializer<Channel>() { EmbeddedChannel channel = new EmbeddedChannel(new ChannelInitializer<Channel>() {
@Override @Override
protected void initChannel(Channel ch) throws Exception { protected void initChannel(Channel ch) {
ChannelHandler handler = new ChannelInitializer<Channel>() { ChannelHandler handler = new ChannelInitializer<Channel>() {
@Override @Override
protected void initChannel(Channel ch) throws Exception { protected void initChannel(Channel ch) {
called.set(true); called.set(true);
} }
}; };
@ -256,7 +260,8 @@ public class ChannelInitializerTest {
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@Test(timeout = 10000) @Test
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
public void testChannelInitializerEventExecutor() throws Throwable { public void testChannelInitializerEventExecutor() throws Throwable {
final AtomicInteger invokeCount = new AtomicInteger(); final AtomicInteger invokeCount = new AtomicInteger();
final AtomicInteger completeCount = new AtomicInteger(); final AtomicInteger completeCount = new AtomicInteger();

View File

@ -15,14 +15,13 @@
*/ */
package io.netty.channel; package io.netty.channel;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
public class ChannelOptionTest { public class ChannelOptionTest {

View File

@ -22,18 +22,23 @@ import io.netty.util.CharsetUtil;
import io.netty.util.concurrent.DefaultThreadFactory; import io.netty.util.concurrent.DefaultThreadFactory;
import io.netty.util.concurrent.RejectedExecutionHandlers; import io.netty.util.concurrent.RejectedExecutionHandlers;
import io.netty.util.concurrent.SingleThreadEventExecutor; import io.netty.util.concurrent.SingleThreadEventExecutor;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Queue; import java.util.Queue;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;
import static io.netty.buffer.Unpooled.*; import static io.netty.buffer.Unpooled.*;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class ChannelOutboundBufferTest { public class ChannelOutboundBufferTest {
@ -61,11 +66,11 @@ public class ChannelOutboundBufferTest {
ByteBuf buf = copiedBuffer("buf1", CharsetUtil.US_ASCII); ByteBuf buf = copiedBuffer("buf1", CharsetUtil.US_ASCII);
ByteBuffer nioBuf = buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes()); ByteBuffer nioBuf = buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes());
buffer.addMessage(buf, buf.readableBytes(), channel.voidPromise()); buffer.addMessage(buf, buf.readableBytes(), channel.voidPromise());
assertEquals("Should still be 0 as not flushed yet", 0, buffer.nioBufferCount()); assertEquals(0, buffer.nioBufferCount(), "Should still be 0 as not flushed yet");
buffer.addFlush(); buffer.addFlush();
ByteBuffer[] buffers = buffer.nioBuffers(); ByteBuffer[] buffers = buffer.nioBuffers();
assertNotNull(buffers); assertNotNull(buffers);
assertEquals("Should still be 0 as not flushed yet", 1, buffer.nioBufferCount()); assertEquals(1, buffer.nioBufferCount(), "Should still be 0 as not flushed yet");
for (int i = 0; i < buffer.nioBufferCount(); i++) { for (int i = 0; i < buffer.nioBufferCount(); i++) {
if (i == 0) { if (i == 0) {
assertEquals(buffers[i], nioBuf); assertEquals(buffers[i], nioBuf);
@ -86,7 +91,7 @@ public class ChannelOutboundBufferTest {
for (int i = 0; i < 64; i++) { for (int i = 0; i < 64; i++) {
buffer.addMessage(buf.copy(), buf.readableBytes(), channel.voidPromise()); buffer.addMessage(buf.copy(), buf.readableBytes(), channel.voidPromise());
} }
assertEquals("Should still be 0 as not flushed yet", 0, buffer.nioBufferCount()); assertEquals(0, buffer.nioBufferCount(), "Should still be 0 as not flushed yet");
buffer.addFlush(); buffer.addFlush();
ByteBuffer[] buffers = buffer.nioBuffers(); ByteBuffer[] buffers = buffer.nioBuffers();
assertEquals(64, buffer.nioBufferCount()); assertEquals(64, buffer.nioBufferCount());
@ -110,7 +115,7 @@ public class ChannelOutboundBufferTest {
} }
buffer.addMessage(comp, comp.readableBytes(), channel.voidPromise()); buffer.addMessage(comp, comp.readableBytes(), channel.voidPromise());
assertEquals("Should still be 0 as not flushed yet", 0, buffer.nioBufferCount()); assertEquals(0, buffer.nioBufferCount(), "Should still be 0 as not flushed yet");
buffer.addFlush(); buffer.addFlush();
ByteBuffer[] buffers = buffer.nioBuffers(); ByteBuffer[] buffers = buffer.nioBuffers();
assertEquals(65, buffer.nioBufferCount()); assertEquals(65, buffer.nioBufferCount());
@ -138,11 +143,11 @@ public class ChannelOutboundBufferTest {
} }
assertEquals(65, comp.nioBufferCount()); assertEquals(65, comp.nioBufferCount());
buffer.addMessage(comp, comp.readableBytes(), channel.voidPromise()); buffer.addMessage(comp, comp.readableBytes(), channel.voidPromise());
assertEquals("Should still be 0 as not flushed yet", 0, buffer.nioBufferCount()); assertEquals(0, buffer.nioBufferCount(), "Should still be 0 as not flushed yet");
buffer.addFlush(); buffer.addFlush();
final int maxCount = 10; // less than comp.nioBufferCount() final int maxCount = 10; // less than comp.nioBufferCount()
ByteBuffer[] buffers = buffer.nioBuffers(maxCount, Integer.MAX_VALUE); ByteBuffer[] buffers = buffer.nioBuffers(maxCount, Integer.MAX_VALUE);
assertTrue("Should not be greater than maxCount", buffer.nioBufferCount() <= maxCount); assertTrue(buffer.nioBufferCount() <= maxCount, "Should not be greater than maxCount");
for (int i = 0; i < buffer.nioBufferCount(); i++) { for (int i = 0; i < buffer.nioBufferCount(); i++) {
assertEquals(buffers[i], buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes())); assertEquals(buffers[i], buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes()));
} }
@ -187,27 +192,27 @@ public class ChannelOutboundBufferTest {
} }
@Override @Override
protected void doBind(SocketAddress localAddress) throws Exception { protected void doBind(SocketAddress localAddress) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override @Override
protected void doDisconnect() throws Exception { protected void doDisconnect() {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override @Override
protected void doClose() throws Exception { protected void doClose() {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override @Override
protected void doBeginRead() throws Exception { protected void doBeginRead() {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override @Override
protected void doWrite(ChannelOutboundBuffer in) throws Exception { protected void doWrite(ChannelOutboundBuffer in) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@ -244,7 +249,7 @@ public class ChannelOutboundBufferTest {
final StringBuilder buf = new StringBuilder(); final StringBuilder buf = new StringBuilder();
EmbeddedChannel ch = new EmbeddedChannel(new ChannelInboundHandlerAdapter() { EmbeddedChannel ch = new EmbeddedChannel(new ChannelInboundHandlerAdapter() {
@Override @Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception { public void channelWritabilityChanged(ChannelHandlerContext ctx) {
buf.append(ctx.channel().isWritable()); buf.append(ctx.channel().isWritable());
buf.append(' '); buf.append(' ');
} }
@ -279,7 +284,7 @@ public class ChannelOutboundBufferTest {
final StringBuilder buf = new StringBuilder(); final StringBuilder buf = new StringBuilder();
EmbeddedChannel ch = new EmbeddedChannel(new ChannelInboundHandlerAdapter() { EmbeddedChannel ch = new EmbeddedChannel(new ChannelInboundHandlerAdapter() {
@Override @Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception { public void channelWritabilityChanged(ChannelHandlerContext ctx) {
buf.append(ctx.channel().isWritable()); buf.append(ctx.channel().isWritable());
buf.append(' '); buf.append(' ');
} }
@ -313,7 +318,7 @@ public class ChannelOutboundBufferTest {
final StringBuilder buf = new StringBuilder(); final StringBuilder buf = new StringBuilder();
EmbeddedChannel ch = new EmbeddedChannel(new ChannelInboundHandlerAdapter() { EmbeddedChannel ch = new EmbeddedChannel(new ChannelInboundHandlerAdapter() {
@Override @Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception { public void channelWritabilityChanged(ChannelHandlerContext ctx) {
buf.append(ctx.channel().isWritable()); buf.append(ctx.channel().isWritable());
buf.append(' '); buf.append(' ');
} }
@ -353,7 +358,7 @@ public class ChannelOutboundBufferTest {
final StringBuilder buf = new StringBuilder(); final StringBuilder buf = new StringBuilder();
EmbeddedChannel ch = new EmbeddedChannel(new ChannelInboundHandlerAdapter() { EmbeddedChannel ch = new EmbeddedChannel(new ChannelInboundHandlerAdapter() {
@Override @Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception { public void channelWritabilityChanged(ChannelHandlerContext ctx) {
buf.append(ctx.channel().isWritable()); buf.append(ctx.channel().isWritable());
buf.append(' '); buf.append(' ');
} }
@ -387,7 +392,8 @@ public class ChannelOutboundBufferTest {
safeClose(ch); safeClose(ch);
} }
@Test(timeout = 5000) @Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testWriteTaskRejected() throws Exception { public void testWriteTaskRejected() throws Exception {
final SingleThreadEventExecutor executor = new SingleThreadEventExecutor( final SingleThreadEventExecutor executor = new SingleThreadEventExecutor(
null, new DefaultThreadFactory("executorPool"), null, new DefaultThreadFactory("executorPool"),
@ -413,7 +419,7 @@ public class ChannelOutboundBufferTest {
EmbeddedChannel ch = new EmbeddedChannel(); EmbeddedChannel ch = new EmbeddedChannel();
ch.pipeline().addLast(executor, "handler", new ChannelOutboundHandlerAdapter() { ch.pipeline().addLast(executor, "handler", new ChannelOutboundHandlerAdapter() {
@Override @Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
promise.setFailure(new AssertionError("Should not be called")); promise.setFailure(new AssertionError("Should not be called"));
} }

View File

@ -19,14 +19,14 @@ import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil; import io.netty.util.ReferenceCountUtil;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertSame; import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
* Tests for {@link CoalescingBufferQueue}. * Tests for {@link CoalescingBufferQueue}.
@ -46,7 +46,7 @@ public class CoalescingBufferQueueTest {
private EmbeddedChannel channel; private EmbeddedChannel channel;
private CoalescingBufferQueue writeQueue; private CoalescingBufferQueue writeQueue;
@Before @BeforeEach
public void setup() { public void setup() {
mouseDone = false; mouseDone = false;
mouseSuccess = false; mouseSuccess = false;
@ -67,7 +67,7 @@ public class CoalescingBufferQueueTest {
mouse = Unpooled.wrappedBuffer("mouse".getBytes(CharsetUtil.US_ASCII)); mouse = Unpooled.wrappedBuffer("mouse".getBytes(CharsetUtil.US_ASCII));
} }
@After @AfterEach
public void finish() { public void finish() {
assertFalse(channel.finish()); assertFalse(channel.finish());
} }

View File

@ -16,14 +16,21 @@
package io.netty.channel; package io.netty.channel;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.function.Executable;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.util.ArrayDeque; import java.util.ArrayDeque;
import java.util.Queue; import java.util.Queue;
import java.util.concurrent.TimeUnit;
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.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class CombinedChannelDuplexHandlerTest { public class CombinedChannelDuplexHandlerTest {
@ -53,40 +60,65 @@ public class CombinedChannelDuplexHandlerTest {
DISCONNECT DISCONNECT
} }
@Test(expected = IllegalStateException.class) @Test
public void testInboundRemoveBeforeAdded() { public void testInboundRemoveBeforeAdded() {
CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler> handler = final CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler> handler =
new CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler>( new CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler>(
new ChannelInboundHandlerAdapter(), new ChannelOutboundHandlerAdapter()); new ChannelInboundHandlerAdapter(), new ChannelOutboundHandlerAdapter());
assertThrows(IllegalStateException.class, new Executable() {
@Override
public void execute() {
handler.removeInboundHandler(); handler.removeInboundHandler();
} }
});
@Test(expected = IllegalStateException.class)
public void testOutboundRemoveBeforeAdded() {
CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler> handler =
new CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler>(
new ChannelInboundHandlerAdapter(), new ChannelOutboundHandlerAdapter());
handler.removeOutboundHandler();
} }
@Test(expected = IllegalArgumentException.class) @Test
public void testOutboundRemoveBeforeAdded() {
final CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler> handler =
new CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler>(
new ChannelInboundHandlerAdapter(), new ChannelOutboundHandlerAdapter());
assertThrows(IllegalStateException.class, new Executable() {
@Override
public void execute() {
handler.removeOutboundHandler();
}
});
}
@Test
public void testInboundHandlerImplementsOutboundHandler() { public void testInboundHandlerImplementsOutboundHandler() {
assertThrows(IllegalArgumentException.class, new Executable() {
@Override
public void execute() {
new CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler>( new CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler>(
new ChannelDuplexHandler(), new ChannelOutboundHandlerAdapter()); new ChannelDuplexHandler(), new ChannelOutboundHandlerAdapter());
} }
});
}
@Test(expected = IllegalArgumentException.class) @Test
public void testOutboundHandlerImplementsInboundHandler() { public void testOutboundHandlerImplementsInboundHandler() {
assertThrows(IllegalArgumentException.class, new Executable() {
@Override
public void execute() {
new CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler>( new CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler>(
new ChannelInboundHandlerAdapter(), new ChannelDuplexHandler()); new ChannelInboundHandlerAdapter(), new ChannelDuplexHandler());
} }
});
}
@Test(expected = IllegalStateException.class) @Test
public void testInitNotCalledBeforeAdded() throws Exception { public void testInitNotCalledBeforeAdded() {
CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler> handler = final CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler> handler =
new CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler>() { }; new CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler>() { };
assertThrows(IllegalStateException.class, new Executable() {
@Override
public void execute() throws Throwable {
handler.handlerAdded(null); handler.handlerAdded(null);
} }
});
}
@Test @Test
public void testExceptionCaughtBothCombinedHandlers() { public void testExceptionCaughtBothCombinedHandlers() {
@ -95,15 +127,16 @@ public class CombinedChannelDuplexHandlerTest {
ChannelInboundHandler inboundHandler = new ChannelInboundHandlerAdapter() { ChannelInboundHandler inboundHandler = new ChannelInboundHandlerAdapter() {
@Override @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
assertSame(exception, cause); assertSame(exception, cause);
queue.add(this); queue.add(this);
ctx.fireExceptionCaught(cause); ctx.fireExceptionCaught(cause);
} }
}; };
ChannelOutboundHandler outboundHandler = new ChannelOutboundHandlerAdapter() { ChannelOutboundHandler outboundHandler = new ChannelOutboundHandlerAdapter() {
@SuppressWarnings("deprecation")
@Override @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
assertSame(exception, cause); assertSame(exception, cause);
queue.add(this); queue.add(this);
ctx.fireExceptionCaught(cause); ctx.fireExceptionCaught(cause);
@ -111,7 +144,7 @@ public class CombinedChannelDuplexHandlerTest {
}; };
ChannelInboundHandler lastHandler = new ChannelInboundHandlerAdapter() { ChannelInboundHandler lastHandler = new ChannelInboundHandlerAdapter() {
@Override @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
assertSame(exception, cause); assertSame(exception, cause);
queue.add(this); queue.add(this);
} }
@ -133,57 +166,57 @@ public class CombinedChannelDuplexHandlerTest {
ChannelInboundHandler inboundHandler = new ChannelInboundHandlerAdapter() { ChannelInboundHandler inboundHandler = new ChannelInboundHandlerAdapter() {
@Override @Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception { public void handlerAdded(ChannelHandlerContext ctx) {
queue.add(Event.HANDLER_ADDED); queue.add(Event.HANDLER_ADDED);
} }
@Override @Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { public void handlerRemoved(ChannelHandlerContext ctx) {
queue.add(Event.HANDLER_REMOVED); queue.add(Event.HANDLER_REMOVED);
} }
@Override @Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception { public void channelRegistered(ChannelHandlerContext ctx) {
queue.add(Event.REGISTERED); queue.add(Event.REGISTERED);
} }
@Override @Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { public void channelUnregistered(ChannelHandlerContext ctx) {
queue.add(Event.UNREGISTERED); queue.add(Event.UNREGISTERED);
} }
@Override @Override
public void channelActive(ChannelHandlerContext ctx) throws Exception { public void channelActive(ChannelHandlerContext ctx) {
queue.add(Event.ACTIVE); queue.add(Event.ACTIVE);
} }
@Override @Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception { public void channelInactive(ChannelHandlerContext ctx) {
queue.add(Event.INACTIVE); queue.add(Event.INACTIVE);
} }
@Override @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { public void channelRead(ChannelHandlerContext ctx, Object msg) {
queue.add(Event.CHANNEL_READ); queue.add(Event.CHANNEL_READ);
} }
@Override @Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { public void channelReadComplete(ChannelHandlerContext ctx) {
queue.add(Event.CHANNEL_READ_COMPLETE); queue.add(Event.CHANNEL_READ_COMPLETE);
} }
@Override @Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
queue.add(Event.USER_EVENT_TRIGGERED); queue.add(Event.USER_EVENT_TRIGGERED);
} }
@Override @Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception { public void channelWritabilityChanged(ChannelHandlerContext ctx) {
queue.add(Event.CHANNEL_WRITABILITY_CHANGED); queue.add(Event.CHANNEL_WRITABILITY_CHANGED);
} }
@Override @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
queue.add(Event.EXCEPTION_CAUGHT); queue.add(Event.EXCEPTION_CAUGHT);
} }
}; };
@ -228,54 +261,53 @@ public class CombinedChannelDuplexHandlerTest {
ChannelInboundHandler inboundHandler = new ChannelInboundHandlerAdapter(); ChannelInboundHandler inboundHandler = new ChannelInboundHandlerAdapter();
ChannelOutboundHandler outboundHandler = new ChannelOutboundHandlerAdapter() { ChannelOutboundHandler outboundHandler = new ChannelOutboundHandlerAdapter() {
@Override @Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception { public void handlerAdded(ChannelHandlerContext ctx) {
queue.add(Event.HANDLER_ADDED); queue.add(Event.HANDLER_ADDED);
} }
@Override @Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { public void handlerRemoved(ChannelHandlerContext ctx) {
queue.add(Event.HANDLER_REMOVED); queue.add(Event.HANDLER_REMOVED);
} }
@Override @Override
public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) {
throws Exception {
queue.add(Event.BIND); queue.add(Event.BIND);
} }
@Override @Override
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress,
SocketAddress localAddress, ChannelPromise promise) throws Exception { SocketAddress localAddress, ChannelPromise promise) {
queue.add(Event.CONNECT); queue.add(Event.CONNECT);
} }
@Override @Override
public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) {
queue.add(Event.DISCONNECT); queue.add(Event.DISCONNECT);
} }
@Override @Override
public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { public void close(ChannelHandlerContext ctx, ChannelPromise promise) {
queue.add(Event.CLOSE); queue.add(Event.CLOSE);
} }
@Override @Override
public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) {
queue.add(Event.DEREGISTER); queue.add(Event.DEREGISTER);
} }
@Override @Override
public void read(ChannelHandlerContext ctx) throws Exception { public void read(ChannelHandlerContext ctx) {
queue.add(Event.READ); queue.add(Event.READ);
} }
@Override @Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
queue.add(Event.WRITE); queue.add(Event.WRITE);
} }
@Override @Override
public void flush(ChannelHandlerContext ctx) throws Exception { public void flush(ChannelHandlerContext ctx) {
queue.add(Event.FLUSH); queue.add(Event.FLUSH);
} }
}; };
@ -322,38 +354,39 @@ public class CombinedChannelDuplexHandlerTest {
channel.pipeline().deregister(); channel.pipeline().deregister();
} }
@Test(timeout = 3000) @Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testPromisesPassed() { public void testPromisesPassed() {
ChannelOutboundHandler outboundHandler = new ChannelOutboundHandlerAdapter() { ChannelOutboundHandler outboundHandler = new ChannelOutboundHandlerAdapter() {
@Override @Override
public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, public void bind(ChannelHandlerContext ctx, SocketAddress localAddress,
ChannelPromise promise) throws Exception { ChannelPromise promise) {
promise.setSuccess(); promise.setSuccess();
} }
@Override @Override
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress,
SocketAddress localAddress, ChannelPromise promise) throws Exception { SocketAddress localAddress, ChannelPromise promise) {
promise.setSuccess(); promise.setSuccess();
} }
@Override @Override
public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) {
promise.setSuccess(); promise.setSuccess();
} }
@Override @Override
public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { public void close(ChannelHandlerContext ctx, ChannelPromise promise) {
promise.setSuccess(); promise.setSuccess();
} }
@Override @Override
public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) {
promise.setSuccess(); promise.setSuccess();
} }
@Override @Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
promise.setSuccess(); promise.setSuccess();
} }
}; };
@ -388,8 +421,11 @@ public class CombinedChannelDuplexHandlerTest {
ch.finish(); ch.finish();
} }
@Test(expected = IllegalStateException.class) @Test
public void testNotSharable() { public void testNotSharable() {
assertThrows(IllegalStateException.class, new Executable() {
@Override
public void execute() {
new CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler>() { new CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler>() {
@Override @Override
public boolean isSharable() { public boolean isSharable() {
@ -397,4 +433,6 @@ public class CombinedChannelDuplexHandlerTest {
} }
}; };
} }
});
}
} }

View File

@ -15,22 +15,30 @@
*/ */
package io.netty.channel; package io.netty.channel;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.mockito.Mockito; import org.mockito.Mockito;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class CompleteChannelFutureTest { public class CompleteChannelFutureTest {
@Test(expected = NullPointerException.class) @Test
public void shouldDisallowNullChannel() { public void shouldDisallowNullChannel() {
assertThrows(NullPointerException.class, new Executable() {
@Override
public void execute() {
new CompleteChannelFutureImpl(null); new CompleteChannelFutureImpl(null);
} }
});
}
@Test @Test
public void shouldNotDoAnythingOnRemove() throws Exception { public void shouldNotDoAnythingOnRemove() {
Channel channel = Mockito.mock(Channel.class); Channel channel = Mockito.mock(Channel.class);
CompleteChannelFuture future = new CompleteChannelFutureImpl(channel); CompleteChannelFuture future = new CompleteChannelFutureImpl(channel);
ChannelFutureListener l = Mockito.mock(ChannelFutureListener.class); ChannelFutureListener l = Mockito.mock(ChannelFutureListener.class);

View File

@ -20,7 +20,7 @@ import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream; import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream; import io.netty.buffer.ByteBufOutputStream;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
@ -29,7 +29,7 @@ import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.sameInstance; import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@SuppressWarnings("DynamicRegexReplaceableByCompiledPattern") @SuppressWarnings("DynamicRegexReplaceableByCompiledPattern")
public class DefaultChannelIdTest { public class DefaultChannelIdTest {

View File

@ -15,8 +15,8 @@
*/ */
package io.netty.channel; package io.netty.channel;
import static org.junit.Assert.assertSame; import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
@ -25,9 +25,9 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import org.junit.AfterClass; import org.junit.jupiter.api.AfterAll;
import org.junit.BeforeClass; import org.junit.jupiter.api.BeforeAll;
import org.junit.Test; import org.junit.jupiter.api.Test;
import io.netty.bootstrap.Bootstrap; import io.netty.bootstrap.Bootstrap;
@ -35,12 +35,12 @@ public class DefaultChannelPipelineTailTest {
private static EventLoopGroup GROUP; private static EventLoopGroup GROUP;
@BeforeClass @BeforeAll
public static void init() { public static void init() {
GROUP = new DefaultEventLoopGroup(1); GROUP = new DefaultEventLoopGroup(1);
} }
@AfterClass @AfterAll
public static void destroy() { public static void destroy() {
GROUP.shutdownGracefully(); GROUP.shutdownGracefully();
} }
@ -305,20 +305,20 @@ public class DefaultChannelPipelineTailTest {
} }
@Override @Override
protected void doBind(SocketAddress localAddress) throws Exception { protected void doBind(SocketAddress localAddress) {
} }
@Override @Override
protected void doDisconnect() throws Exception { protected void doDisconnect() {
} }
@Override @Override
protected void doClose() throws Exception { protected void doClose() {
closed = true; closed = true;
} }
@Override @Override
protected void doBeginRead() throws Exception { protected void doBeginRead() {
} }
@Override @Override

View File

@ -41,10 +41,12 @@ import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.ImmediateEventExecutor; import io.netty.util.concurrent.ImmediateEventExecutor;
import io.netty.util.concurrent.Promise; import io.netty.util.concurrent.Promise;
import io.netty.util.concurrent.UnorderedThreadPoolEventExecutor; import io.netty.util.concurrent.UnorderedThreadPoolEventExecutor;
import org.junit.After; import org.junit.jupiter.api.AfterAll;
import org.junit.AfterClass; import org.junit.jupiter.api.AfterEach;
import org.junit.BeforeClass; import org.junit.jupiter.api.BeforeAll;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.function.Executable;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.util.ArrayDeque; import java.util.ArrayDeque;
@ -64,14 +66,15 @@ import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.LockSupport; import java.util.concurrent.locks.LockSupport;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.Assert.assertNotSame; import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.Assert.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.Assert.fail; import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.fail;
public class DefaultChannelPipelineTest { public class DefaultChannelPipelineTest {
@ -80,12 +83,12 @@ public class DefaultChannelPipelineTest {
private Channel self; private Channel self;
private Channel peer; private Channel peer;
@BeforeClass @BeforeAll
public static void beforeClass() throws Exception { public static void beforeClass() throws Exception {
group = new DefaultEventLoopGroup(1); group = new DefaultEventLoopGroup(1);
} }
@AfterClass @AfterAll
public static void afterClass() throws Exception { public static void afterClass() throws Exception {
group.shutdownGracefully().sync(); group.shutdownGracefully().sync();
} }
@ -96,12 +99,12 @@ public class DefaultChannelPipelineTest {
sb.group(group).channel(LocalServerChannel.class); sb.group(group).channel(LocalServerChannel.class);
sb.childHandler(new ChannelInboundHandlerAdapter() { sb.childHandler(new ChannelInboundHandlerAdapter() {
@Override @Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception { public void channelRegistered(ChannelHandlerContext ctx) {
peerRef.set(ctx.channel()); peerRef.set(ctx.channel());
} }
@Override @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { public void channelRead(ChannelHandlerContext ctx, Object msg) {
ReferenceCountUtil.release(msg); ReferenceCountUtil.release(msg);
} }
}); });
@ -112,7 +115,7 @@ public class DefaultChannelPipelineTest {
b.group(group).channel(LocalChannel.class); b.group(group).channel(LocalChannel.class);
b.handler(new ChannelInitializer<LocalChannel>() { b.handler(new ChannelInitializer<LocalChannel>() {
@Override @Override
protected void initChannel(LocalChannel ch) throws Exception { protected void initChannel(LocalChannel ch) {
ch.pipeline().addLast(handlers); ch.pipeline().addLast(handlers);
} }
}); });
@ -123,7 +126,7 @@ public class DefaultChannelPipelineTest {
bindFuture.channel().close().sync(); bindFuture.channel().close().sync();
} }
@After @AfterEach
public void tearDown() throws Exception { public void tearDown() throws Exception {
if (peer != null) { if (peer != null) {
peer.close(); peer.close();
@ -230,15 +233,20 @@ public class DefaultChannelPipelineTest {
assertNotNull(pipeline.get("handler1")); assertNotNull(pipeline.get("handler1"));
} }
@Test(expected = NoSuchElementException.class) @Test
public void testRemoveThrowNoSuchElementException() { public void testRemoveThrowNoSuchElementException() {
DefaultChannelPipeline pipeline = new DefaultChannelPipeline(new LocalChannel()); final DefaultChannelPipeline pipeline = new DefaultChannelPipeline(new LocalChannel());
ChannelHandler handler1 = newHandler(); ChannelHandler handler1 = newHandler();
pipeline.addLast("handler1", handler1); pipeline.addLast("handler1", handler1);
assertThrows(NoSuchElementException.class, new Executable() {
@Override
public void execute() {
pipeline.remove("handlerXXX"); pipeline.remove("handlerXXX");
} }
});
}
@Test @Test
public void testReplaceChannelHandler() { public void testReplaceChannelHandler() {
@ -265,18 +273,23 @@ public class DefaultChannelPipelineTest {
assertSame(pipeline.get("handler2"), newHandler2); assertSame(pipeline.get("handler2"), newHandler2);
} }
@Test(expected = IllegalArgumentException.class) @Test
public void testReplaceHandlerChecksDuplicateNames() { public void testReplaceHandlerChecksDuplicateNames() {
ChannelPipeline pipeline = new LocalChannel().pipeline(); final ChannelPipeline pipeline = new LocalChannel().pipeline();
ChannelHandler handler1 = newHandler(); ChannelHandler handler1 = newHandler();
ChannelHandler handler2 = newHandler(); ChannelHandler handler2 = newHandler();
pipeline.addLast("handler1", handler1); pipeline.addLast("handler1", handler1);
pipeline.addLast("handler2", handler2); pipeline.addLast("handler2", handler2);
ChannelHandler newHandler1 = newHandler(); final ChannelHandler newHandler1 = newHandler();
assertThrows(IllegalArgumentException.class, new Executable() {
@Override
public void execute() {
pipeline.replace("handler1", "handler2", newHandler1); pipeline.replace("handler1", "handler2", newHandler1);
} }
});
}
@Test @Test
public void testReplaceNameWithGenerated() { public void testReplaceNameWithGenerated() {
@ -334,7 +347,8 @@ public class DefaultChannelPipelineTest {
verifyContextNumber(pipeline, HANDLER_ARRAY_LEN * 2); verifyContextNumber(pipeline, HANDLER_ARRAY_LEN * 2);
} }
@Test(timeout = 3000) @Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testThrowInExceptionCaught() throws InterruptedException { public void testThrowInExceptionCaught() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1);
final AtomicInteger counter = new AtomicInteger(); final AtomicInteger counter = new AtomicInteger();
@ -372,7 +386,8 @@ public class DefaultChannelPipelineTest {
} }
} }
@Test(timeout = 3000) @Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testThrowInOtherHandlerAfterInvokedFromExceptionCaught() throws InterruptedException { public void testThrowInOtherHandlerAfterInvokedFromExceptionCaught() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1);
final AtomicInteger counter = new AtomicInteger(); final AtomicInteger counter = new AtomicInteger();
@ -421,10 +436,10 @@ public class DefaultChannelPipelineTest {
ChannelPipeline pipeline = new LocalChannel().pipeline(); ChannelPipeline pipeline = new LocalChannel().pipeline();
pipeline.addLast(new ChannelInitializer<Channel>() { pipeline.addLast(new ChannelInitializer<Channel>() {
@Override @Override
protected void initChannel(Channel ch) throws Exception { protected void initChannel(Channel ch) {
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() { ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
@Override @Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception { public void channelRegistered(ChannelHandlerContext ctx) {
latch.countDown(); latch.countDown();
} }
}); });
@ -493,7 +508,8 @@ public class DefaultChannelPipelineTest {
verifyContextNumber(pipeline, 8); verifyContextNumber(pipeline, 8);
} }
@Test(timeout = 10000) @Test
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
public void testLifeCycleAwareness() throws Exception { public void testLifeCycleAwareness() throws Exception {
setUp(); setUp();
@ -542,7 +558,8 @@ public class DefaultChannelPipelineTest {
removeLatch.await(); removeLatch.await();
} }
@Test(timeout = 100000) @Test
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
public void testRemoveAndForwardInbound() throws Exception { public void testRemoveAndForwardInbound() throws Exception {
final BufferedTestHandler handler1 = new BufferedTestHandler(); final BufferedTestHandler handler1 = new BufferedTestHandler();
final BufferedTestHandler handler2 = new BufferedTestHandler(); final BufferedTestHandler handler2 = new BufferedTestHandler();
@ -563,7 +580,8 @@ public class DefaultChannelPipelineTest {
}).sync(); }).sync();
} }
@Test(timeout = 10000) @Test
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
public void testRemoveAndForwardOutbound() throws Exception { public void testRemoveAndForwardOutbound() throws Exception {
final BufferedTestHandler handler1 = new BufferedTestHandler(); final BufferedTestHandler handler1 = new BufferedTestHandler();
final BufferedTestHandler handler2 = new BufferedTestHandler(); final BufferedTestHandler handler2 = new BufferedTestHandler();
@ -584,7 +602,8 @@ public class DefaultChannelPipelineTest {
}).sync(); }).sync();
} }
@Test(timeout = 10000) @Test
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
public void testReplaceAndForwardOutbound() throws Exception { public void testReplaceAndForwardOutbound() throws Exception {
final BufferedTestHandler handler1 = new BufferedTestHandler(); final BufferedTestHandler handler1 = new BufferedTestHandler();
final BufferedTestHandler handler2 = new BufferedTestHandler(); final BufferedTestHandler handler2 = new BufferedTestHandler();
@ -604,7 +623,8 @@ public class DefaultChannelPipelineTest {
}).sync(); }).sync();
} }
@Test(timeout = 10000) @Test
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
public void testReplaceAndForwardInboundAndOutbound() throws Exception { public void testReplaceAndForwardInboundAndOutbound() throws Exception {
final BufferedTestHandler handler1 = new BufferedTestHandler(); final BufferedTestHandler handler1 = new BufferedTestHandler();
final BufferedTestHandler handler2 = new BufferedTestHandler(); final BufferedTestHandler handler2 = new BufferedTestHandler();
@ -630,7 +650,8 @@ public class DefaultChannelPipelineTest {
}).sync(); }).sync();
} }
@Test(timeout = 10000) @Test
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
public void testRemoveAndForwardInboundOutbound() throws Exception { public void testRemoveAndForwardInboundOutbound() throws Exception {
final BufferedTestHandler handler1 = new BufferedTestHandler(); final BufferedTestHandler handler1 = new BufferedTestHandler();
final BufferedTestHandler handler2 = new BufferedTestHandler(); final BufferedTestHandler handler2 = new BufferedTestHandler();
@ -660,7 +681,7 @@ public class DefaultChannelPipelineTest {
// Tests for https://github.com/netty/netty/issues/2349 // Tests for https://github.com/netty/netty/issues/2349
@Test @Test
public void testCancelBind() throws Exception { public void testCancelBind() {
ChannelPipeline pipeline = new LocalChannel().pipeline(); ChannelPipeline pipeline = new LocalChannel().pipeline();
group.register(pipeline.channel()); group.register(pipeline.channel());
@ -671,7 +692,7 @@ public class DefaultChannelPipelineTest {
} }
@Test @Test
public void testCancelConnect() throws Exception { public void testCancelConnect() {
ChannelPipeline pipeline = new LocalChannel().pipeline(); ChannelPipeline pipeline = new LocalChannel().pipeline();
group.register(pipeline.channel()); group.register(pipeline.channel());
@ -682,7 +703,7 @@ public class DefaultChannelPipelineTest {
} }
@Test @Test
public void testCancelDisconnect() throws Exception { public void testCancelDisconnect() {
ChannelPipeline pipeline = new LocalChannel().pipeline(); ChannelPipeline pipeline = new LocalChannel().pipeline();
group.register(pipeline.channel()); group.register(pipeline.channel());
@ -693,7 +714,7 @@ public class DefaultChannelPipelineTest {
} }
@Test @Test
public void testCancelClose() throws Exception { public void testCancelClose() {
ChannelPipeline pipeline = new LocalChannel().pipeline(); ChannelPipeline pipeline = new LocalChannel().pipeline();
group.register(pipeline.channel()); group.register(pipeline.channel());
@ -703,51 +724,66 @@ public class DefaultChannelPipelineTest {
assertTrue(future.isCancelled()); assertTrue(future.isCancelled());
} }
@Test(expected = IllegalArgumentException.class) @Test
public void testWrongPromiseChannel() throws Exception { public void testWrongPromiseChannel() throws Exception {
ChannelPipeline pipeline = new LocalChannel().pipeline(); final ChannelPipeline pipeline = new LocalChannel().pipeline();
group.register(pipeline.channel()).sync(); group.register(pipeline.channel()).sync();
ChannelPipeline pipeline2 = new LocalChannel().pipeline(); ChannelPipeline pipeline2 = new LocalChannel().pipeline();
group.register(pipeline2.channel()).sync(); group.register(pipeline2.channel()).sync();
try { try {
ChannelPromise promise2 = pipeline2.channel().newPromise(); final ChannelPromise promise2 = pipeline2.channel().newPromise();
assertThrows(IllegalArgumentException.class, new Executable() {
@Override
public void execute() {
pipeline.close(promise2); pipeline.close(promise2);
}
});
} finally { } finally {
pipeline.close(); pipeline.close();
pipeline2.close(); pipeline2.close();
} }
} }
@Test(expected = IllegalArgumentException.class) @Test
public void testUnexpectedVoidChannelPromise() throws Exception { public void testUnexpectedVoidChannelPromise() throws Exception {
ChannelPipeline pipeline = new LocalChannel().pipeline(); final ChannelPipeline pipeline = new LocalChannel().pipeline();
group.register(pipeline.channel()).sync(); group.register(pipeline.channel()).sync();
try { try {
ChannelPromise promise = new VoidChannelPromise(pipeline.channel(), false); final ChannelPromise promise = new VoidChannelPromise(pipeline.channel(), false);
assertThrows(IllegalArgumentException.class, new Executable() {
@Override
public void execute() {
pipeline.close(promise); pipeline.close(promise);
} finally {
pipeline.close();
} }
} });
@Test(expected = IllegalArgumentException.class)
public void testUnexpectedVoidChannelPromiseCloseFuture() throws Exception {
ChannelPipeline pipeline = new LocalChannel().pipeline();
group.register(pipeline.channel()).sync();
try {
ChannelPromise promise = (ChannelPromise) pipeline.channel().closeFuture();
pipeline.close(promise);
} finally { } finally {
pipeline.close(); pipeline.close();
} }
} }
@Test @Test
public void testCancelDeregister() throws Exception { public void testUnexpectedVoidChannelPromiseCloseFuture() throws Exception {
final ChannelPipeline pipeline = new LocalChannel().pipeline();
group.register(pipeline.channel()).sync();
try {
final ChannelPromise promise = (ChannelPromise) pipeline.channel().closeFuture();
assertThrows(IllegalArgumentException.class, new Executable() {
@Override
public void execute() {
pipeline.close(promise);
}
});
} finally {
pipeline.close();
}
}
@Test
public void testCancelDeregister() {
ChannelPipeline pipeline = new LocalChannel().pipeline(); ChannelPipeline pipeline = new LocalChannel().pipeline();
group.register(pipeline.channel()); group.register(pipeline.channel());
@ -758,7 +794,7 @@ public class DefaultChannelPipelineTest {
} }
@Test @Test
public void testCancelWrite() throws Exception { public void testCancelWrite() {
ChannelPipeline pipeline = new LocalChannel().pipeline(); ChannelPipeline pipeline = new LocalChannel().pipeline();
group.register(pipeline.channel()); group.register(pipeline.channel());
@ -772,7 +808,7 @@ public class DefaultChannelPipelineTest {
} }
@Test @Test
public void testCancelWriteAndFlush() throws Exception { public void testCancelWriteAndFlush() {
ChannelPipeline pipeline = new LocalChannel().pipeline(); ChannelPipeline pipeline = new LocalChannel().pipeline();
group.register(pipeline.channel()); group.register(pipeline.channel());
@ -786,37 +822,38 @@ public class DefaultChannelPipelineTest {
} }
@Test @Test
public void testFirstContextEmptyPipeline() throws Exception { public void testFirstContextEmptyPipeline() {
ChannelPipeline pipeline = new LocalChannel().pipeline(); ChannelPipeline pipeline = new LocalChannel().pipeline();
assertNull(pipeline.firstContext()); assertNull(pipeline.firstContext());
} }
@Test @Test
public void testLastContextEmptyPipeline() throws Exception { public void testLastContextEmptyPipeline() {
ChannelPipeline pipeline = new LocalChannel().pipeline(); ChannelPipeline pipeline = new LocalChannel().pipeline();
assertNull(pipeline.lastContext()); assertNull(pipeline.lastContext());
} }
@Test @Test
public void testFirstHandlerEmptyPipeline() throws Exception { public void testFirstHandlerEmptyPipeline() {
ChannelPipeline pipeline = new LocalChannel().pipeline(); ChannelPipeline pipeline = new LocalChannel().pipeline();
assertNull(pipeline.first()); assertNull(pipeline.first());
} }
@Test @Test
public void testLastHandlerEmptyPipeline() throws Exception { public void testLastHandlerEmptyPipeline() {
ChannelPipeline pipeline = new LocalChannel().pipeline(); ChannelPipeline pipeline = new LocalChannel().pipeline();
assertNull(pipeline.last()); assertNull(pipeline.last());
} }
@Test(timeout = 5000) @Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testChannelInitializerException() throws Exception { public void testChannelInitializerException() throws Exception {
final IllegalStateException exception = new IllegalStateException(); final IllegalStateException exception = new IllegalStateException();
final AtomicReference<Throwable> error = new AtomicReference<Throwable>(); final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1);
EmbeddedChannel channel = new EmbeddedChannel(new ChannelInitializer<Channel>() { EmbeddedChannel channel = new EmbeddedChannel(new ChannelInitializer<Channel>() {
@Override @Override
protected void initChannel(Channel ch) throws Exception { protected void initChannel(Channel ch) {
throw exception; throw exception;
} }
@ -839,17 +876,17 @@ public class DefaultChannelPipelineTest {
ChannelPipeline pipeline = new LocalChannel().pipeline(); ChannelPipeline pipeline = new LocalChannel().pipeline();
pipeline.addLast(new ChannelInitializer<Channel>() { pipeline.addLast(new ChannelInitializer<Channel>() {
@Override @Override
protected void initChannel(Channel ch) throws Exception { protected void initChannel(Channel ch) {
ch.pipeline().addLast(new WrapperExecutor(), ch.pipeline().addLast(new WrapperExecutor(),
new ChannelInboundHandlerAdapter() { new ChannelInboundHandlerAdapter() {
@Override @Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { public void channelUnregistered(ChannelHandlerContext ctx) {
channelLatch.countDown(); channelLatch.countDown();
} }
@Override @Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { public void handlerRemoved(ChannelHandlerContext ctx) {
handlerLatch.countDown(); handlerLatch.countDown();
} }
}); });
@ -863,7 +900,8 @@ public class DefaultChannelPipelineTest {
assertTrue(handlerLatch.await(2, TimeUnit.SECONDS)); assertTrue(handlerLatch.await(2, TimeUnit.SECONDS));
} }
@Test(timeout = 3000) @Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testAddHandlerBeforeRegisteredThenRemove() { public void testAddHandlerBeforeRegisteredThenRemove() {
final EventLoop loop = group.next(); final EventLoop loop = group.next();
@ -877,7 +915,8 @@ public class DefaultChannelPipelineTest {
handler.removedPromise.syncUninterruptibly(); handler.removedPromise.syncUninterruptibly();
} }
@Test(timeout = 3000) @Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testAddHandlerBeforeRegisteredThenReplace() throws Exception { public void testAddHandlerBeforeRegisteredThenReplace() throws Exception {
final EventLoop loop = group.next(); final EventLoop loop = group.next();
final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1);
@ -890,7 +929,7 @@ public class DefaultChannelPipelineTest {
handler.addedPromise.syncUninterruptibly(); handler.addedPromise.syncUninterruptibly();
pipeline.replace(handler, null, new ChannelHandlerAdapter() { pipeline.replace(handler, null, new ChannelHandlerAdapter() {
@Override @Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception { public void handlerAdded(ChannelHandlerContext ctx) {
latch.countDown(); latch.countDown();
} }
}); });
@ -926,7 +965,8 @@ public class DefaultChannelPipelineTest {
} }
} }
@Test(timeout = 3000) @Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testHandlerAddedAndRemovedCalledInCorrectOrder() throws Throwable { public void testHandlerAddedAndRemovedCalledInCorrectOrder() throws Throwable {
final EventExecutorGroup group1 = new DefaultEventExecutorGroup(1); final EventExecutorGroup group1 = new DefaultEventExecutorGroup(1);
final EventExecutorGroup group2 = new DefaultEventExecutorGroup(1); final EventExecutorGroup group2 = new DefaultEventExecutorGroup(1);
@ -969,7 +1009,8 @@ public class DefaultChannelPipelineTest {
} }
} }
@Test(timeout = 3000) @Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testHandlerAddedExceptionFromChildHandlerIsPropagated() { public void testHandlerAddedExceptionFromChildHandlerIsPropagated() {
final EventExecutorGroup group1 = new DefaultEventExecutorGroup(1); final EventExecutorGroup group1 = new DefaultEventExecutorGroup(1);
try { try {
@ -993,7 +1034,8 @@ public class DefaultChannelPipelineTest {
} }
} }
@Test(timeout = 3000) @Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testHandlerRemovedExceptionFromChildHandlerIsPropagated() { public void testHandlerRemovedExceptionFromChildHandlerIsPropagated() {
final EventExecutorGroup group1 = new DefaultEventExecutorGroup(1); final EventExecutorGroup group1 = new DefaultEventExecutorGroup(1);
try { try {
@ -1016,7 +1058,8 @@ public class DefaultChannelPipelineTest {
} }
} }
@Test(timeout = 3000) @Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testHandlerAddedThrowsAndRemovedThrowsException() throws InterruptedException { public void testHandlerAddedThrowsAndRemovedThrowsException() throws InterruptedException {
final EventExecutorGroup group1 = new DefaultEventExecutorGroup(1); final EventExecutorGroup group1 = new DefaultEventExecutorGroup(1);
try { try {
@ -1054,7 +1097,8 @@ public class DefaultChannelPipelineTest {
} }
} }
@Test(timeout = 2000) @Test
@Timeout(value = 2000, unit = TimeUnit.MILLISECONDS)
public void testAddRemoveHandlerCalledOnceRegistered() throws Throwable { public void testAddRemoveHandlerCalledOnceRegistered() throws Throwable {
ChannelPipeline pipeline = new LocalChannel().pipeline(); ChannelPipeline pipeline = new LocalChannel().pipeline();
CallbackCheckHandler handler = new CallbackCheckHandler(); CallbackCheckHandler handler = new CallbackCheckHandler();
@ -1075,7 +1119,8 @@ public class DefaultChannelPipelineTest {
assertTrue(handler.removedHandler.get()); assertTrue(handler.removedHandler.get());
} }
@Test(timeout = 3000) @Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testAddReplaceHandlerCalledOnceRegistered() throws Throwable { public void testAddReplaceHandlerCalledOnceRegistered() throws Throwable {
ChannelPipeline pipeline = new LocalChannel().pipeline(); ChannelPipeline pipeline = new LocalChannel().pipeline();
CallbackCheckHandler handler = new CallbackCheckHandler(); CallbackCheckHandler handler = new CallbackCheckHandler();
@ -1109,7 +1154,8 @@ public class DefaultChannelPipelineTest {
assertTrue(handler2.removedHandler.get()); assertTrue(handler2.removedHandler.get());
} }
@Test(timeout = 3000) @Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testAddBefore() throws Throwable { public void testAddBefore() throws Throwable {
ChannelPipeline pipeline1 = new LocalChannel().pipeline(); ChannelPipeline pipeline1 = new LocalChannel().pipeline();
ChannelPipeline pipeline2 = new LocalChannel().pipeline(); ChannelPipeline pipeline2 = new LocalChannel().pipeline();
@ -1133,44 +1179,48 @@ public class DefaultChannelPipelineTest {
} }
} }
@Test(timeout = 3000) @Test
public void testAddInListenerNio() throws Throwable { @Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testAddInListenerNio() {
testAddInListener(new NioSocketChannel(), new NioEventLoopGroup(1)); testAddInListener(new NioSocketChannel(), new NioEventLoopGroup(1));
} }
@Test(timeout = 3000) @SuppressWarnings("deprecation")
public void testAddInListenerOio() throws Throwable { @Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testAddInListenerOio() {
testAddInListener(new OioSocketChannel(), new OioEventLoopGroup(1)); testAddInListener(new OioSocketChannel(), new OioEventLoopGroup(1));
} }
@Test(timeout = 3000) @Test
public void testAddInListenerLocal() throws Throwable { @Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testAddInListenerLocal() {
testAddInListener(new LocalChannel(), new DefaultEventLoopGroup(1)); testAddInListener(new LocalChannel(), new DefaultEventLoopGroup(1));
} }
private static void testAddInListener(Channel channel, EventLoopGroup group) throws Throwable { private static void testAddInListener(Channel channel, EventLoopGroup group) {
ChannelPipeline pipeline1 = channel.pipeline(); ChannelPipeline pipeline1 = channel.pipeline();
try { try {
final Object event = new Object(); final Object event = new Object();
final Promise<Object> promise = ImmediateEventExecutor.INSTANCE.newPromise(); final Promise<Object> promise = ImmediateEventExecutor.INSTANCE.newPromise();
group.register(pipeline1.channel()).addListener(new ChannelFutureListener() { group.register(pipeline1.channel()).addListener(new ChannelFutureListener() {
@Override @Override
public void operationComplete(ChannelFuture future) throws Exception { public void operationComplete(ChannelFuture future) {
ChannelPipeline pipeline = future.channel().pipeline(); ChannelPipeline pipeline = future.channel().pipeline();
final AtomicBoolean handlerAddedCalled = new AtomicBoolean(); final AtomicBoolean handlerAddedCalled = new AtomicBoolean();
pipeline.addLast(new ChannelInboundHandlerAdapter() { pipeline.addLast(new ChannelInboundHandlerAdapter() {
@Override @Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception { public void handlerAdded(ChannelHandlerContext ctx) {
handlerAddedCalled.set(true); handlerAddedCalled.set(true);
} }
@Override @Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
promise.setSuccess(event); promise.setSuccess(event);
} }
@Override @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
promise.setFailure(cause); promise.setFailure(cause);
} }
}); });
@ -1203,7 +1253,8 @@ public class DefaultChannelPipelineTest {
pipeline.addBefore("test", null, newHandler()); pipeline.addBefore("test", null, newHandler());
} }
@Test(timeout = 3000) @Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testUnorderedEventExecutor() throws Throwable { public void testUnorderedEventExecutor() throws Throwable {
ChannelPipeline pipeline1 = new LocalChannel().pipeline(); ChannelPipeline pipeline1 = new LocalChannel().pipeline();
EventExecutorGroup eventExecutors = new UnorderedThreadPoolEventExecutor(2); EventExecutorGroup eventExecutors = new UnorderedThreadPoolEventExecutor(2);
@ -1214,13 +1265,13 @@ public class DefaultChannelPipelineTest {
final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1);
pipeline1.addLast(eventExecutors, new ChannelInboundHandlerAdapter() { pipeline1.addLast(eventExecutors, new ChannelInboundHandlerAdapter() {
@Override @Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception { public void handlerAdded(ChannelHandlerContext ctx) {
// Just block one of the two threads. // Just block one of the two threads.
LockSupport.park(); LockSupport.park();
} }
@Override @Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
latch.countDown(); latch.countDown();
} }
}); });
@ -1272,8 +1323,9 @@ public class DefaultChannelPipelineTest {
group.shutdownGracefully(0, 0, TimeUnit.SECONDS); group.shutdownGracefully(0, 0, TimeUnit.SECONDS);
} }
@Test(timeout = 3000) @Test
public void testVoidPromiseNotify() throws Throwable { @Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testVoidPromiseNotify() {
ChannelPipeline pipeline1 = new LocalChannel().pipeline(); ChannelPipeline pipeline1 = new LocalChannel().pipeline();
EventLoopGroup defaultGroup = new DefaultEventLoopGroup(1); EventLoopGroup defaultGroup = new DefaultEventLoopGroup(1);
@ -1604,7 +1656,7 @@ public class DefaultChannelPipelineTest {
} }
private void assertCalled(String methodName, int mask) { private void assertCalled(String methodName, int mask) {
assertTrue(methodName + " was not called", (executionMask & mask) != 0); assertTrue((executionMask & mask) != 0, methodName + " was not called");
} }
} }
@ -1694,7 +1746,7 @@ public class DefaultChannelPipelineTest {
} }
private void assertCalled(String methodName, int mask) { private void assertCalled(String methodName, int mask) {
assertTrue(methodName + " was not called", (executionMask & mask) != 0); assertTrue((executionMask & mask) != 0, methodName + " was not called");
} }
} }
@ -1780,12 +1832,14 @@ public class DefaultChannelPipelineTest {
channel2.close().syncUninterruptibly(); channel2.close().syncUninterruptibly();
} }
@Test(timeout = 5000) @Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testHandlerAddedFailedButHandlerStillRemoved() throws InterruptedException { public void testHandlerAddedFailedButHandlerStillRemoved() throws InterruptedException {
testHandlerAddedFailedButHandlerStillRemoved0(false); testHandlerAddedFailedButHandlerStillRemoved0(false);
} }
@Test(timeout = 5000) @Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testHandlerAddedFailedButHandlerStillRemovedWithLaterRegister() throws InterruptedException { public void testHandlerAddedFailedButHandlerStillRemovedWithLaterRegister() throws InterruptedException {
testHandlerAddedFailedButHandlerStillRemoved0(true); testHandlerAddedFailedButHandlerStillRemoved0(true);
} }
@ -1841,12 +1895,14 @@ public class DefaultChannelPipelineTest {
} }
} }
@Test(timeout = 5000) @Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void handlerAddedStateUpdatedBeforeHandlerAddedDoneForceEventLoop() throws InterruptedException { public void handlerAddedStateUpdatedBeforeHandlerAddedDoneForceEventLoop() throws InterruptedException {
handlerAddedStateUpdatedBeforeHandlerAddedDone(true); handlerAddedStateUpdatedBeforeHandlerAddedDone(true);
} }
@Test(timeout = 5000) @Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void handlerAddedStateUpdatedBeforeHandlerAddedDoneOnCallingThread() throws InterruptedException { public void handlerAddedStateUpdatedBeforeHandlerAddedDoneOnCallingThread() throws InterruptedException {
handlerAddedStateUpdatedBeforeHandlerAddedDone(false); handlerAddedStateUpdatedBeforeHandlerAddedDone(false);
} }
@ -1930,7 +1986,7 @@ public class DefaultChannelPipelineTest {
} }
@Override @Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { public void handlerRemoved(ChannelHandlerContext ctx) {
if (!removedHandler.trySuccess(true)) { if (!removedHandler.trySuccess(true)) {
error.set(new AssertionError("handlerRemoved(...) called multiple times: " + ctx.name())); error.set(new AssertionError("handlerRemoved(...) called multiple times: " + ctx.name()));
} else if (addedHandler.getNow() == Boolean.FALSE) { } else if (addedHandler.getNow() == Boolean.FALSE) {
@ -1985,7 +2041,7 @@ public class DefaultChannelPipelineTest {
} }
@Override @Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { public void handlerRemoved(ChannelHandlerContext ctx) {
removedQueue.add(this); removedQueue.add(this);
checkExecutor(ctx); checkExecutor(ctx);
} }
@ -2026,7 +2082,7 @@ public class DefaultChannelPipelineTest {
} }
@Override @Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { public void handlerRemoved(ChannelHandlerContext ctx) {
assertExecutor(ctx, removedPromise); assertExecutor(ctx, removedPromise);
} }
@ -2058,7 +2114,7 @@ public class DefaultChannelPipelineTest {
} }
@Override @Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { public void handlerRemoved(ChannelHandlerContext ctx) {
error.set(new AssertionError()); error.set(new AssertionError());
} }
} }
@ -2156,8 +2212,8 @@ public class DefaultChannelPipelineTest {
} }
public void validate(boolean afterAdd, boolean afterRemove) { public void validate(boolean afterAdd, boolean afterRemove) {
assertEquals(name, afterAdd, this.afterAdd); assertEquals(afterAdd, this.afterAdd, name);
assertEquals(name, afterRemove, this.afterRemove); assertEquals(afterRemove, this.afterRemove, name);
} }
@Override @Override

View File

@ -17,22 +17,40 @@ package io.netty.channel;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.concurrent.ImmediateEventExecutor; import io.netty.util.concurrent.ImmediateEventExecutor;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class DefaultChannelPromiseTest { public class DefaultChannelPromiseTest {
@Test(expected = NullPointerException.class) @Test
public void testNullChannel() { public void testNullChannel() {
assertThrows(NullPointerException.class, new Executable() {
@Override
public void execute() {
new DefaultChannelPromise(null); new DefaultChannelPromise(null);
} }
});
}
@Test(expected = NullPointerException.class) @Test
public void testChannelWithNullExecutor() { public void testChannelWithNullExecutor() {
assertThrows(NullPointerException.class, new Executable() {
@Override
public void execute() {
new DefaultChannelPromise(new EmbeddedChannel(), null); new DefaultChannelPromise(new EmbeddedChannel(), null);
} }
});
}
@Test(expected = NullPointerException.class) @Test
public void testNullChannelWithExecutor() { public void testNullChannelWithExecutor() {
assertThrows(NullPointerException.class, new Executable() {
@Override
public void execute() {
new DefaultChannelPromise(null, ImmediateEventExecutor.INSTANCE); new DefaultChannelPromise(null, ImmediateEventExecutor.INSTANCE);
} }
});
}
} }

View File

@ -16,7 +16,7 @@
package io.netty.channel; package io.netty.channel;
import io.netty.util.internal.PlatformDependent; import io.netty.util.internal.PlatformDependent;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
@ -26,9 +26,9 @@ import java.io.RandomAccessFile;
import java.nio.channels.Channels; import java.nio.channels.Channels;
import java.nio.channels.WritableByteChannel; import java.nio.channels.WritableByteChannel;
import static org.junit.Assert.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.fail; import static org.junit.jupiter.api.Assertions.fail;
public class DefaultFileRegionTest { public class DefaultFileRegionTest {

View File

@ -17,7 +17,7 @@ package io.netty.channel;
import io.netty.util.concurrent.Future; import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener; import io.netty.util.concurrent.GenericFutureListener;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
public class DelegatingChannelPromiseNotifierTest { public class DelegatingChannelPromiseNotifierTest {

View File

@ -15,10 +15,13 @@
*/ */
package io.netty.channel; package io.netty.channel;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.mockito.Mockito; import org.mockito.Mockito;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class FailedChannelFutureTest { public class FailedChannelFutureTest {
@Test @Test
@ -31,8 +34,13 @@ public class FailedChannelFutureTest {
assertSame(e, future.cause()); assertSame(e, future.cause());
} }
@Test(expected = NullPointerException.class) @Test
public void shouldDisallowNullException() { public void shouldDisallowNullException() {
assertThrows(NullPointerException.class, new Executable() {
@Override
public void execute() {
new FailedChannelFuture(Mockito.mock(Channel.class), null, null); new FailedChannelFuture(Mockito.mock(Channel.class), null, null);
} }
});
}
} }

View File

@ -20,7 +20,8 @@ import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -29,7 +30,13 @@ import java.util.concurrent.atomic.AtomicReference;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
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;
import static org.junit.jupiter.api.Assertions.fail;
public class PendingWriteQueueTest { public class PendingWriteQueueTest {
@ -38,12 +45,12 @@ public class PendingWriteQueueTest {
assertWrite(new TestHandler() { assertWrite(new TestHandler() {
@Override @Override
public void flush(ChannelHandlerContext ctx) throws Exception { public void flush(ChannelHandlerContext ctx) throws Exception {
assertFalse("Should not be writable anymore", ctx.channel().isWritable()); assertFalse(ctx.channel().isWritable(), "Should not be writable anymore");
ChannelFuture future = queue.removeAndWrite(); ChannelFuture future = queue.removeAndWrite();
future.addListener(new ChannelFutureListener() { future.addListener(new ChannelFutureListener() {
@Override @Override
public void operationComplete(ChannelFuture future) throws Exception { public void operationComplete(ChannelFuture future) {
assertQueueEmpty(queue); assertQueueEmpty(queue);
} }
}); });
@ -57,12 +64,12 @@ public class PendingWriteQueueTest {
assertWrite(new TestHandler() { assertWrite(new TestHandler() {
@Override @Override
public void flush(ChannelHandlerContext ctx) throws Exception { public void flush(ChannelHandlerContext ctx) throws Exception {
assertFalse("Should not be writable anymore", ctx.channel().isWritable()); assertFalse(ctx.channel().isWritable(), "Should not be writable anymore");
ChannelFuture future = queue.removeAndWriteAll(); ChannelFuture future = queue.removeAndWriteAll();
future.addListener(new ChannelFutureListener() { future.addListener(new ChannelFutureListener() {
@Override @Override
public void operationComplete(ChannelFuture future) throws Exception { public void operationComplete(ChannelFuture future) {
assertQueueEmpty(queue); assertQueueEmpty(queue);
} }
}); });
@ -102,13 +109,13 @@ public class PendingWriteQueueTest {
final EmbeddedChannel channel = new EmbeddedChannel(new ChannelInboundHandlerAdapter() { final EmbeddedChannel channel = new EmbeddedChannel(new ChannelInboundHandlerAdapter() {
@Override @Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception { public void handlerAdded(ChannelHandlerContext ctx) {
ctxRef.set(ctx); ctxRef.set(ctx);
queueRef.set(new PendingWriteQueue(ctx)); queueRef.set(new PendingWriteQueue(ctx));
} }
@Override @Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception { public void channelWritabilityChanged(ChannelHandlerContext ctx) {
final PendingWriteQueue queue = queueRef.get(); final PendingWriteQueue queue = queueRef.get();
final ByteBuf msg = (ByteBuf) queue.current(); final ByteBuf msg = (ByteBuf) queue.current();
@ -212,7 +219,7 @@ public class PendingWriteQueueTest {
ChannelPromise promise = channel.newPromise(); ChannelPromise promise = channel.newPromise();
promise.addListener(new ChannelFutureListener() { promise.addListener(new ChannelFutureListener() {
@Override @Override
public void operationComplete(ChannelFuture future) throws Exception { public void operationComplete(ChannelFuture future) {
queue.removeAndFailAll(new IllegalStateException()); queue.removeAndFailAll(new IllegalStateException());
} }
}); });
@ -299,13 +306,13 @@ public class PendingWriteQueueTest {
final ChannelPromise promise3 = channel.newPromise(); final ChannelPromise promise3 = channel.newPromise();
promise3.addListener(new ChannelFutureListener() { promise3.addListener(new ChannelFutureListener() {
@Override @Override
public void operationComplete(ChannelFuture future) throws Exception { public void operationComplete(ChannelFuture future) {
failOrder.add(3); failOrder.add(3);
} }
}); });
promise.addListener(new ChannelFutureListener() { promise.addListener(new ChannelFutureListener() {
@Override @Override
public void operationComplete(ChannelFuture future) throws Exception { public void operationComplete(ChannelFuture future) {
failOrder.add(1); failOrder.add(1);
queue.add(3L, promise3); queue.add(3L, promise3);
} }
@ -315,7 +322,7 @@ public class PendingWriteQueueTest {
ChannelPromise promise2 = channel.newPromise(); ChannelPromise promise2 = channel.newPromise();
promise2.addListener(new ChannelFutureListener() { promise2.addListener(new ChannelFutureListener() {
@Override @Override
public void operationComplete(ChannelFuture future) throws Exception { public void operationComplete(ChannelFuture future) {
failOrder.add(2); failOrder.add(2);
} }
}); });
@ -341,7 +348,7 @@ public class PendingWriteQueueTest {
ChannelPromise promise = channel.newPromise(); ChannelPromise promise = channel.newPromise();
promise.addListener(new ChannelFutureListener() { promise.addListener(new ChannelFutureListener() {
@Override @Override
public void operationComplete(ChannelFuture future) throws Exception { public void operationComplete(ChannelFuture future) {
queue.removeAndWriteAll(); queue.removeAndWriteAll();
} }
}); });
@ -385,7 +392,7 @@ public class PendingWriteQueueTest {
public void channelActive(ChannelHandlerContext ctx) throws Exception { public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx); super.channelActive(ctx);
assertQueueEmpty(queue); assertQueueEmpty(queue);
assertTrue("Should be writable", ctx.channel().isWritable()); assertTrue(ctx.channel().isWritable(), "Should be writable");
} }
@Override @Override

View File

@ -22,12 +22,13 @@ import io.netty.channel.local.LocalAddress;
import io.netty.util.concurrent.Future; import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener; import io.netty.util.concurrent.GenericFutureListener;
import org.hamcrest.Matchers; import org.hamcrest.Matchers;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.nio.channels.ClosedChannelException; import java.nio.channels.ClosedChannelException;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
public class ReentrantChannelTest extends BaseChannelTest { public class ReentrantChannelTest extends BaseChannelTest {

View File

@ -18,13 +18,15 @@ package io.netty.channel;
import io.netty.buffer.DefaultByteBufHolder; import io.netty.buffer.DefaultByteBufHolder;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
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.assertTrue;
public class SimpleUserEventChannelHandlerTest { public class SimpleUserEventChannelHandlerTest {
@ -32,7 +34,7 @@ public class SimpleUserEventChannelHandlerTest {
private AllEventCatcher allEventCatcher; private AllEventCatcher allEventCatcher;
private EmbeddedChannel channel; private EmbeddedChannel channel;
@Before @BeforeEach
public void setUp() { public void setUp() {
fooEventCatcher = new FooEventCatcher(); fooEventCatcher = new FooEventCatcher();
allEventCatcher = new AllEventCatcher(); allEventCatcher = new AllEventCatcher();

View File

@ -21,9 +21,10 @@ import ch.qos.logback.core.Appender;
import io.netty.channel.local.LocalChannel; import io.netty.channel.local.LocalChannel;
import io.netty.util.concurrent.EventExecutor; import io.netty.util.concurrent.EventExecutor;
import org.hamcrest.MatcherAssert; import org.hamcrest.MatcherAssert;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.ArrayList; import java.util.ArrayList;
@ -42,7 +43,10 @@ import java.util.concurrent.atomic.AtomicLong;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
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.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
public class SingleThreadEventLoopTest { public class SingleThreadEventLoopTest {
@ -55,14 +59,14 @@ public class SingleThreadEventLoopTest {
private SingleThreadEventLoopB loopB; private SingleThreadEventLoopB loopB;
private SingleThreadEventLoopC loopC; private SingleThreadEventLoopC loopC;
@Before @BeforeEach
public void newEventLoop() { public void newEventLoop() {
loopA = new SingleThreadEventLoopA(); loopA = new SingleThreadEventLoopA();
loopB = new SingleThreadEventLoopB(); loopB = new SingleThreadEventLoopB();
loopC = new SingleThreadEventLoopC(); loopC = new SingleThreadEventLoopC();
} }
@After @AfterEach
public void stopEventLoop() { public void stopEventLoop() {
if (!loopA.isShuttingDown()) { if (!loopA.isShuttingDown()) {
loopA.shutdownGracefully(0, 0, TimeUnit.MILLISECONDS); loopA.shutdownGracefully(0, 0, TimeUnit.MILLISECONDS);
@ -170,12 +174,14 @@ public class SingleThreadEventLoopTest {
is(greaterThanOrEqualTo(TimeUnit.MILLISECONDS.toNanos(500)))); is(greaterThanOrEqualTo(TimeUnit.MILLISECONDS.toNanos(500))));
} }
@Test(timeout = 5000) @Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void scheduleTaskAtFixedRateA() throws Exception { public void scheduleTaskAtFixedRateA() throws Exception {
testScheduleTaskAtFixedRate(loopA); testScheduleTaskAtFixedRate(loopA);
} }
@Test(timeout = 5000) @Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void scheduleTaskAtFixedRateB() throws Exception { public void scheduleTaskAtFixedRateB() throws Exception {
testScheduleTaskAtFixedRate(loopB); testScheduleTaskAtFixedRate(loopB);
} }
@ -218,12 +224,14 @@ public class SingleThreadEventLoopTest {
} }
} }
@Test(timeout = 5000) @Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void scheduleLaggyTaskAtFixedRateA() throws Exception { public void scheduleLaggyTaskAtFixedRateA() throws Exception {
testScheduleLaggyTaskAtFixedRate(loopA); testScheduleLaggyTaskAtFixedRate(loopA);
} }
@Test(timeout = 5000) @Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void scheduleLaggyTaskAtFixedRateB() throws Exception { public void scheduleLaggyTaskAtFixedRateB() throws Exception {
testScheduleLaggyTaskAtFixedRate(loopB); testScheduleLaggyTaskAtFixedRate(loopB);
} }
@ -272,12 +280,14 @@ public class SingleThreadEventLoopTest {
} }
} }
@Test(timeout = 5000) @Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void scheduleTaskWithFixedDelayA() throws Exception { public void scheduleTaskWithFixedDelayA() throws Exception {
testScheduleTaskWithFixedDelay(loopA); testScheduleTaskWithFixedDelay(loopA);
} }
@Test(timeout = 5000) @Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void scheduleTaskWithFixedDelayB() throws Exception { public void scheduleTaskWithFixedDelayB() throws Exception {
testScheduleTaskWithFixedDelay(loopB); testScheduleTaskWithFixedDelay(loopB);
} }
@ -362,7 +372,8 @@ public class SingleThreadEventLoopTest {
assertEquals(NUM_TASKS, ranTasks.get()); assertEquals(NUM_TASKS, ranTasks.get());
} }
@Test(timeout = 10000) @Test
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public void testRegistrationAfterShutdown() throws Exception { public void testRegistrationAfterShutdown() throws Exception {
loopA.shutdown(); loopA.shutdown();
@ -389,7 +400,8 @@ public class SingleThreadEventLoopTest {
} }
} }
@Test(timeout = 10000) @Test
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public void testRegistrationAfterShutdown2() throws Exception { public void testRegistrationAfterShutdown2() throws Exception {
loopA.shutdown(); loopA.shutdown();
@ -428,7 +440,8 @@ public class SingleThreadEventLoopTest {
} }
} }
@Test(timeout = 5000) @Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testGracefulShutdownQuietPeriod() throws Exception { public void testGracefulShutdownQuietPeriod() throws Exception {
loopA.shutdownGracefully(1, Integer.MAX_VALUE, TimeUnit.SECONDS); loopA.shutdownGracefully(1, Integer.MAX_VALUE, TimeUnit.SECONDS);
// Keep Scheduling tasks for another 2 seconds. // Keep Scheduling tasks for another 2 seconds.
@ -450,7 +463,8 @@ public class SingleThreadEventLoopTest {
is(greaterThanOrEqualTo(TimeUnit.SECONDS.toNanos(1)))); is(greaterThanOrEqualTo(TimeUnit.SECONDS.toNanos(1))));
} }
@Test(timeout = 5000) @Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testGracefulShutdownTimeout() throws Exception { public void testGracefulShutdownTimeout() throws Exception {
loopA.shutdownGracefully(2, 2, TimeUnit.SECONDS); loopA.shutdownGracefully(2, 2, TimeUnit.SECONDS);
// Keep Scheduling tasks for another 3 seconds. // Keep Scheduling tasks for another 3 seconds.
@ -474,7 +488,8 @@ public class SingleThreadEventLoopTest {
assertThat(loopA.isShutdown(), is(true)); assertThat(loopA.isShutdown(), is(true));
} }
@Test(timeout = 10000) @Test
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
public void testOnEventLoopIteration() throws Exception { public void testOnEventLoopIteration() throws Exception {
CountingRunnable onIteration = new CountingRunnable(); CountingRunnable onIteration = new CountingRunnable();
loopC.executeAfterEventLoopIteration(onIteration); loopC.executeAfterEventLoopIteration(onIteration);
@ -487,7 +502,8 @@ public class SingleThreadEventLoopTest {
onIteration.getInvocationCount(), is(1)); onIteration.getInvocationCount(), is(1));
} }
@Test(timeout = 10000) @Test
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
public void testRemoveOnEventLoopIteration() throws Exception { public void testRemoveOnEventLoopIteration() throws Exception {
CountingRunnable onIteration1 = new CountingRunnable(); CountingRunnable onIteration1 = new CountingRunnable();
loopC.executeAfterEventLoopIteration(onIteration1); loopC.executeAfterEventLoopIteration(onIteration1);

View File

@ -15,10 +15,11 @@
*/ */
package io.netty.channel; package io.netty.channel;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class SucceededChannelFutureTest { public class SucceededChannelFutureTest {
@Test @Test

View File

@ -20,13 +20,14 @@ import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream; import io.netty.buffer.ByteBufOutputStream;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelId; import io.netty.channel.ChannelId;
import org.junit.Assert; import org.junit.jupiter.api.Test;
import org.junit.Test;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class EmbeddedChannelIdTest { public class EmbeddedChannelIdTest {
@Test @Test
@ -50,9 +51,9 @@ public class EmbeddedChannelIdTest {
inStream.close(); inStream.close();
} }
Assert.assertEquals(normalInstance, deserializedInstance); assertEquals(normalInstance, deserializedInstance);
Assert.assertEquals(normalInstance.hashCode(), deserializedInstance.hashCode()); assertEquals(normalInstance.hashCode(), deserializedInstance.hashCode());
Assert.assertEquals(0, normalInstance.compareTo(deserializedInstance)); assertEquals(0, normalInstance.compareTo(deserializedInstance));
} }
} }

View File

@ -15,12 +15,12 @@
*/ */
package io.netty.channel.embedded; package io.netty.channel.embedded;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.Assert.assertSame; import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.Assert.fail; import static org.junit.jupiter.api.Assertions.fail;
import java.nio.channels.ClosedChannelException; import java.nio.channels.ClosedChannelException;
import java.util.ArrayDeque; import java.util.ArrayDeque;
@ -31,7 +31,8 @@ import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
@ -87,7 +88,8 @@ public class EmbeddedChannelTest {
assertFalse(channel.finish()); assertFalse(channel.finish());
} }
@Test(timeout = 2000) @Test
@Timeout(value = 2000, unit = TimeUnit.MILLISECONDS)
public void promiseDoesNotInfiniteLoop() throws InterruptedException { public void promiseDoesNotInfiniteLoop() throws InterruptedException {
EmbeddedChannel channel = new EmbeddedChannel(); EmbeddedChannel channel = new EmbeddedChannel();
channel.closeFuture().addListener(new ChannelFutureListener() { channel.closeFuture().addListener(new ChannelFutureListener() {
@ -164,7 +166,8 @@ public class EmbeddedChannelTest {
assertTrue(future.isCancelled()); assertTrue(future.isCancelled());
} }
@Test(timeout = 3000) @Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testHandlerAddedExecutedInEventLoop() throws Throwable { public void testHandlerAddedExecutedInEventLoop() throws Throwable {
final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Throwable> error = new AtomicReference<Throwable>(); final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
@ -209,7 +212,8 @@ public class EmbeddedChannelTest {
} }
// See https://github.com/netty/netty/issues/4316. // See https://github.com/netty/netty/issues/4316.
@Test(timeout = 2000) @Test
@Timeout(value = 2000, unit = TimeUnit.MILLISECONDS)
public void testFireChannelInactiveAndUnregisteredOnClose() throws InterruptedException { public void testFireChannelInactiveAndUnregisteredOnClose() throws InterruptedException {
testFireChannelInactiveAndUnregistered(new Action() { testFireChannelInactiveAndUnregistered(new Action() {
@Override @Override
@ -225,7 +229,8 @@ public class EmbeddedChannelTest {
}); });
} }
@Test(timeout = 2000) @Test
@Timeout(value = 2000, unit = TimeUnit.MILLISECONDS)
public void testFireChannelInactiveAndUnregisteredOnDisconnect() throws InterruptedException { public void testFireChannelInactiveAndUnregisteredOnDisconnect() throws InterruptedException {
testFireChannelInactiveAndUnregistered(new Action() { testFireChannelInactiveAndUnregistered(new Action() {
@Override @Override
@ -586,7 +591,8 @@ public class EmbeddedChannelTest {
} }
} }
@Test(timeout = 5000) @Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testChannelInactiveFired() throws InterruptedException { public void testChannelInactiveFired() throws InterruptedException {
final AtomicBoolean inactive = new AtomicBoolean(); final AtomicBoolean inactive = new AtomicBoolean();
EmbeddedChannel channel = new EmbeddedChannel(new ChannelInboundHandlerAdapter() { EmbeddedChannel channel = new EmbeddedChannel(new ChannelInboundHandlerAdapter() {

View File

@ -23,7 +23,7 @@ import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.concurrent.GlobalEventExecutor; import io.netty.util.concurrent.GlobalEventExecutor;
import org.junit.Test; import org.junit.jupiter.api.Test;
public class DefaultChannelGroupTest { public class DefaultChannelGroupTest {

View File

@ -38,9 +38,11 @@ import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.Promise; import io.netty.util.concurrent.Promise;
import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory; import io.netty.util.internal.logging.InternalLoggerFactory;
import org.junit.AfterClass; import org.junit.jupiter.api.AfterAll;
import org.junit.BeforeClass; import org.junit.jupiter.api.BeforeAll;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.function.Executable;
import java.net.ConnectException; import java.net.ConnectException;
import java.nio.channels.ClosedChannelException; import java.nio.channels.ClosedChannelException;
@ -53,11 +55,12 @@ import static java.util.concurrent.TimeUnit.SECONDS;
import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.Assert.fail; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
public class LocalChannelTest { public class LocalChannelTest {
@ -69,14 +72,14 @@ public class LocalChannelTest {
private static EventLoopGroup group2; private static EventLoopGroup group2;
private static EventLoopGroup sharedGroup; private static EventLoopGroup sharedGroup;
@BeforeClass @BeforeAll
public static void beforeClass() { public static void beforeClass() {
group1 = new DefaultEventLoopGroup(2); group1 = new DefaultEventLoopGroup(2);
group2 = new DefaultEventLoopGroup(2); group2 = new DefaultEventLoopGroup(2);
sharedGroup = new DefaultEventLoopGroup(1); sharedGroup = new DefaultEventLoopGroup(1);
} }
@AfterClass @AfterAll
public static void afterClass() throws InterruptedException { public static void afterClass() throws InterruptedException {
Future<?> group1Future = group1.shutdownGracefully(0, 0, SECONDS); Future<?> group1Future = group1.shutdownGracefully(0, 0, SECONDS);
Future<?> group2Future = group2.shutdownGracefully(0, 0, SECONDS); Future<?> group2Future = group2.shutdownGracefully(0, 0, SECONDS);
@ -130,9 +133,9 @@ public class LocalChannelTest {
closeChannel(sc); closeChannel(sc);
sc.closeFuture().sync(); sc.closeFuture().sync();
assertNull(String.format( assertNull(LocalChannelRegistry.get(TEST_ADDRESS), String.format(
"Expected null, got channel '%s' for local address '%s'", "Expected null, got channel '%s' for local address '%s'",
LocalChannelRegistry.get(TEST_ADDRESS), TEST_ADDRESS), LocalChannelRegistry.get(TEST_ADDRESS)); LocalChannelRegistry.get(TEST_ADDRESS), TEST_ADDRESS));
} finally { } finally {
closeChannel(cc); closeChannel(cc);
closeChannel(sc); closeChannel(sc);
@ -283,7 +286,7 @@ public class LocalChannelTest {
} }
}); });
ChannelFuture future = bootstrap.connect(sc.localAddress()); ChannelFuture future = bootstrap.connect(sc.localAddress());
assertTrue("Connection should finish, not time out", future.await(200)); assertTrue(future.await(2000), "Connection should finish, not time out");
cc = future.channel(); cc = future.channel();
} finally { } finally {
closeChannel(cc); closeChannel(cc);
@ -803,7 +806,8 @@ public class LocalChannelTest {
} }
} }
@Test(timeout = 3000) @Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testConnectFutureBeforeChannelActive() throws Exception { public void testConnectFutureBeforeChannelActive() throws Exception {
Bootstrap cb = new Bootstrap(); Bootstrap cb = new Bootstrap();
ServerBootstrap sb = new ServerBootstrap(); ServerBootstrap sb = new ServerBootstrap();
@ -854,13 +858,18 @@ public class LocalChannelTest {
} }
} }
@Test(expected = ConnectException.class) @Test
public void testConnectionRefused() { public void testConnectionRefused() {
Bootstrap sb = new Bootstrap(); final Bootstrap sb = new Bootstrap();
sb.group(group1) sb.group(group1)
.channel(LocalChannel.class) .channel(LocalChannel.class)
.handler(new TestHandler()) .handler(new TestHandler());
.connect(LocalAddress.ANY).syncUninterruptibly(); assertThrows(ConnectException.class, new Executable() {
@Override
public void execute() {
sb.connect(LocalAddress.ANY).syncUninterruptibly();
}
});
} }
private static final class LatchChannelFutureListener extends CountDownLatch implements ChannelFutureListener { private static final class LatchChannelFutureListener extends CountDownLatch implements ChannelFutureListener {
@ -960,12 +969,14 @@ public class LocalChannelTest {
}); });
} }
@Test(timeout = 5000) @Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testAutoReadDisabledSharedGroup() throws Exception { public void testAutoReadDisabledSharedGroup() throws Exception {
testAutoReadDisabled(sharedGroup, sharedGroup); testAutoReadDisabled(sharedGroup, sharedGroup);
} }
@Test(timeout = 5000) @Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testAutoReadDisabledDifferentGroup() throws Exception { public void testAutoReadDisabledDifferentGroup() throws Exception {
testAutoReadDisabled(group1, group2); testAutoReadDisabled(group1, group2);
} }
@ -1023,22 +1034,26 @@ public class LocalChannelTest {
} }
} }
@Test(timeout = 5000) @Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testMaxMessagesPerReadRespectedWithAutoReadSharedGroup() throws Exception { public void testMaxMessagesPerReadRespectedWithAutoReadSharedGroup() throws Exception {
testMaxMessagesPerReadRespected(sharedGroup, sharedGroup, true); testMaxMessagesPerReadRespected(sharedGroup, sharedGroup, true);
} }
@Test(timeout = 5000) @Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testMaxMessagesPerReadRespectedWithoutAutoReadSharedGroup() throws Exception { public void testMaxMessagesPerReadRespectedWithoutAutoReadSharedGroup() throws Exception {
testMaxMessagesPerReadRespected(sharedGroup, sharedGroup, false); testMaxMessagesPerReadRespected(sharedGroup, sharedGroup, false);
} }
@Test(timeout = 5000) @Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testMaxMessagesPerReadRespectedWithAutoReadDifferentGroup() throws Exception { public void testMaxMessagesPerReadRespectedWithAutoReadDifferentGroup() throws Exception {
testMaxMessagesPerReadRespected(group1, group2, true); testMaxMessagesPerReadRespected(group1, group2, true);
} }
@Test(timeout = 5000) @Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testMaxMessagesPerReadRespectedWithoutAutoReadDifferentGroup() throws Exception { public void testMaxMessagesPerReadRespectedWithoutAutoReadDifferentGroup() throws Exception {
testMaxMessagesPerReadRespected(group1, group2, false); testMaxMessagesPerReadRespected(group1, group2, false);
} }
@ -1080,22 +1095,26 @@ public class LocalChannelTest {
} }
} }
@Test(timeout = 5000) @Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testServerMaxMessagesPerReadRespectedWithAutoReadSharedGroup() throws Exception { public void testServerMaxMessagesPerReadRespectedWithAutoReadSharedGroup() throws Exception {
testServerMaxMessagesPerReadRespected(sharedGroup, sharedGroup, true); testServerMaxMessagesPerReadRespected(sharedGroup, sharedGroup, true);
} }
@Test(timeout = 5000) @Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testServerMaxMessagesPerReadRespectedWithoutAutoReadSharedGroup() throws Exception { public void testServerMaxMessagesPerReadRespectedWithoutAutoReadSharedGroup() throws Exception {
testServerMaxMessagesPerReadRespected(sharedGroup, sharedGroup, false); testServerMaxMessagesPerReadRespected(sharedGroup, sharedGroup, false);
} }
@Test(timeout = 5000) @Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testServerMaxMessagesPerReadRespectedWithAutoReadDifferentGroup() throws Exception { public void testServerMaxMessagesPerReadRespectedWithAutoReadDifferentGroup() throws Exception {
testServerMaxMessagesPerReadRespected(group1, group2, true); testServerMaxMessagesPerReadRespected(group1, group2, true);
} }
@Test(timeout = 5000) @Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testServerMaxMessagesPerReadRespectedWithoutAutoReadDifferentGroup() throws Exception { public void testServerMaxMessagesPerReadRespectedWithoutAutoReadDifferentGroup() throws Exception {
testServerMaxMessagesPerReadRespected(group1, group2, false); testServerMaxMessagesPerReadRespected(group1, group2, false);
} }

View File

@ -24,11 +24,13 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.DefaultEventLoopGroup; import io.netty.channel.DefaultEventLoopGroup;
import io.netty.util.ReferenceCountUtil; import io.netty.util.ReferenceCountUtil;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.assertEquals;
public class LocalTransportThreadModelTest2 { public class LocalTransportThreadModelTest2 {
@ -36,7 +38,8 @@ public class LocalTransportThreadModelTest2 {
static final int messageCountPerRun = 4; static final int messageCountPerRun = 4;
@Test(timeout = 15000) @Test
@Timeout(value = 15000, unit = TimeUnit.MILLISECONDS)
public void testSocketReuse() throws InterruptedException { public void testSocketReuse() throws InterruptedException {
ServerBootstrap serverBootstrap = new ServerBootstrap(); ServerBootstrap serverBootstrap = new ServerBootstrap();
LocalHandler serverHandler = new LocalHandler("SERVER"); LocalHandler serverHandler = new LocalHandler("SERVER");

View File

@ -32,7 +32,8 @@ import io.netty.util.concurrent.DefaultThreadFactory;
import io.netty.util.concurrent.Future; import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.RejectedExecutionHandlers; import io.netty.util.concurrent.RejectedExecutionHandlers;
import io.netty.util.concurrent.ThreadPerTaskExecutor; import io.netty.util.concurrent.ThreadPerTaskExecutor;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
@ -51,7 +52,11 @@ import java.util.concurrent.atomic.AtomicReference;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.instanceOf;
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.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class NioEventLoopTest extends AbstractEventLoopTest { public class NioEventLoopTest extends AbstractEventLoopTest {
@ -156,7 +161,8 @@ public class NioEventLoopTest extends AbstractEventLoopTest {
} }
} }
@Test(timeout = 3000) @Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testSelectableChannel() throws Exception { public void testSelectableChannel() throws Exception {
NioEventLoopGroup group = new NioEventLoopGroup(1); NioEventLoopGroup group = new NioEventLoopGroup(1);
NioEventLoop loop = (NioEventLoop) group.next(); NioEventLoop loop = (NioEventLoop) group.next();
@ -269,7 +275,8 @@ public class NioEventLoopTest extends AbstractEventLoopTest {
} }
} }
@Test(timeout = 3000L) @Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testChannelsRegistered() throws Exception { public void testChannelsRegistered() throws Exception {
NioEventLoopGroup group = new NioEventLoopGroup(1); NioEventLoopGroup group = new NioEventLoopGroup(1);
final NioEventLoop loop = (NioEventLoop) group.next(); final NioEventLoop loop = (NioEventLoop) group.next();

View File

@ -15,8 +15,8 @@
*/ */
package io.netty.channel.nio; package io.netty.channel.nio;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
@ -24,7 +24,12 @@ import java.nio.channels.SelectionKey;
import java.util.Iterator; import java.util.Iterator;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
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.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
public class SelectedSelectionKeySetTest { public class SelectedSelectionKeySetTest {
@Mock @Mock
@ -35,7 +40,7 @@ public class SelectedSelectionKeySetTest {
@Mock @Mock
private SelectionKey mockKey3; private SelectionKey mockKey3;
@Before @BeforeEach
public void setup() { public void setup() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
} }

View File

@ -16,7 +16,7 @@
package io.netty.channel.socket; package io.netty.channel.socket;
import io.netty.util.NetUtil; import io.netty.util.NetUtil;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.net.InetAddress; import java.net.InetAddress;

View File

@ -17,14 +17,17 @@ package io.netty.channel.socket.nio;
import io.netty.channel.ChannelOption; import io.netty.channel.ChannelOption;
import io.netty.channel.nio.AbstractNioChannel; import io.netty.channel.nio.AbstractNioChannel;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.io.IOException; import java.io.IOException;
import java.net.SocketOption; import java.net.SocketOption;
import java.net.StandardSocketOptions; import java.net.StandardSocketOptions;
import java.nio.channels.NetworkChannel; import java.nio.channels.NetworkChannel;
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.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
public abstract class AbstractNioChannelTest<T extends AbstractNioChannel> { public abstract class AbstractNioChannelTest<T extends AbstractNioChannel> {

View File

@ -24,14 +24,14 @@ import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.DatagramChannel; import io.netty.channel.socket.DatagramChannel;
import io.netty.util.ReferenceCountUtil; import io.netty.util.ReferenceCountUtil;
import io.netty.util.concurrent.GlobalEventExecutor; import io.netty.util.concurrent.GlobalEventExecutor;
import org.junit.Assert; import org.junit.jupiter.api.Test;
import org.junit.Test;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.SocketOption; import java.net.SocketOption;
import java.net.StandardSocketOptions; import java.net.StandardSocketOptions;
import java.nio.channels.NetworkChannel; import java.nio.channels.NetworkChannel;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class NioDatagramChannelTest extends AbstractNioChannelTest<NioDatagramChannel> { public class NioDatagramChannelTest extends AbstractNioChannelTest<NioDatagramChannel> {
@ -58,7 +58,7 @@ public class NioDatagramChannelTest extends AbstractNioChannelTest<NioDatagramCh
.bind(new InetSocketAddress(0)).syncUninterruptibly().channel(); .bind(new InetSocketAddress(0)).syncUninterruptibly().channel();
channelGroup.add(datagramChannel); channelGroup.add(datagramChannel);
} }
Assert.assertEquals(100, channelGroup.size()); assertEquals(100, channelGroup.size());
} finally { } finally {
channelGroup.close().sync(); channelGroup.close().sync();
group.shutdownGracefully().sync(); group.shutdownGracefully().sync();

View File

@ -18,8 +18,8 @@ package io.netty.channel.socket.nio;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup; import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup;
import org.junit.Assert;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
@ -28,6 +28,9 @@ import java.net.StandardSocketOptions;
import java.nio.channels.NetworkChannel; import java.nio.channels.NetworkChannel;
import java.nio.channels.ServerSocketChannel; import java.nio.channels.ServerSocketChannel;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class NioServerSocketChannelTest extends AbstractNioChannelTest<NioServerSocketChannel> { public class NioServerSocketChannelTest extends AbstractNioChannelTest<NioServerSocketChannel> {
@Test @Test
@ -38,8 +41,8 @@ public class NioServerSocketChannelTest extends AbstractNioChannelTest<NioServer
try { try {
group.register(serverSocketChannel).syncUninterruptibly(); group.register(serverSocketChannel).syncUninterruptibly();
serverSocketChannel.bind(new InetSocketAddress(0)).syncUninterruptibly(); serverSocketChannel.bind(new InetSocketAddress(0)).syncUninterruptibly();
Assert.assertFalse(serverSocketChannel.closeOnReadError(new IOException())); assertFalse(serverSocketChannel.closeOnReadError(new IOException()));
Assert.assertTrue(serverSocketChannel.closeOnReadError(new IllegalArgumentException())); assertTrue(serverSocketChannel.closeOnReadError(new IllegalArgumentException()));
serverSocketChannel.close().syncUninterruptibly(); serverSocketChannel.close().syncUninterruptibly();
} finally { } finally {
group.shutdownGracefully(); group.shutdownGracefully();
@ -53,11 +56,11 @@ public class NioServerSocketChannelTest extends AbstractNioChannelTest<NioServer
try { try {
group.register(serverSocketChannel).syncUninterruptibly(); group.register(serverSocketChannel).syncUninterruptibly();
Channel channel = serverSocketChannel.bind(new InetSocketAddress(0)).syncUninterruptibly().channel(); Channel channel = serverSocketChannel.bind(new InetSocketAddress(0)).syncUninterruptibly().channel();
Assert.assertTrue(channel.isActive()); assertTrue(channel.isActive());
Assert.assertTrue(channel.isOpen()); assertTrue(channel.isOpen());
channel.close().syncUninterruptibly(); channel.close().syncUninterruptibly();
Assert.assertFalse(channel.isOpen()); assertFalse(channel.isOpen());
Assert.assertFalse(channel.isActive()); assertFalse(channel.isActive());
} finally { } finally {
group.shutdownGracefully(); group.shutdownGracefully();
} }

View File

@ -35,7 +35,8 @@ import io.netty.channel.socket.SocketChannel;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import io.netty.util.NetUtil; import io.netty.util.NetUtil;
import io.netty.util.internal.PlatformDependent; import io.netty.util.internal.PlatformDependent;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import java.io.DataInput; import java.io.DataInput;
import java.io.DataInputStream; import java.io.DataInputStream;
@ -52,10 +53,12 @@ import java.nio.channels.NetworkChannel;
import java.util.Queue; import java.util.Queue;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
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.assertNotSame;
public class NioSocketChannelTest extends AbstractNioChannelTest<NioSocketChannel> { public class NioSocketChannelTest extends AbstractNioChannelTest<NioSocketChannel> {
@ -160,12 +163,14 @@ public class NioSocketChannelTest extends AbstractNioChannelTest<NioSocketChanne
} }
// Test for https://github.com/netty/netty/issues/4805 // Test for https://github.com/netty/netty/issues/4805
@Test(timeout = 3000) @Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testChannelReRegisterReadSameEventLoop() throws Exception { public void testChannelReRegisterReadSameEventLoop() throws Exception {
testChannelReRegisterRead(true); testChannelReRegisterRead(true);
} }
@Test(timeout = 3000) @Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testChannelReRegisterReadDifferentEventLoop() throws Exception { public void testChannelReRegisterReadDifferentEventLoop() throws Exception {
testChannelReRegisterRead(false); testChannelReRegisterRead(false);
} }
@ -247,7 +252,8 @@ public class NioSocketChannelTest extends AbstractNioChannelTest<NioSocketChanne
} }
} }
@Test(timeout = 3000) @Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testShutdownOutputAndClose() throws IOException { public void testShutdownOutputAndClose() throws IOException {
NioEventLoopGroup group = new NioEventLoopGroup(1); NioEventLoopGroup group = new NioEventLoopGroup(1);
ServerSocket socket = new ServerSocket(); ServerSocket socket = new ServerSocket();