Skip to content

Commit

Permalink
Migrate servicetalk-transport-netty-internal tests to JUnit 5 (#1568) (
Browse files Browse the repository at this point in the history
…#1601)

Motivation:

- JUnit 5 leverages features from Java 8 or later, such as lambda functions, making tests more powerful and easier to maintain.
- JUnit 5 has added some very useful new features for describing, organizing, and executing tests. For instance, tests get better display names and can be organized hierarchically.
- JUnit 5 is organized into multiple libraries, so only the features you need are imported into your project. With build systems such as Maven and Gradle, including the right libraries is easy.
- JUnit 5 can use more than one extension at a time, which JUnit 4 could not (only one runner could be used at a time). This means you can easily combine the Spring extension with other extensions (such as your own custom extension).

Modifications:

- Unit tests have been migrated from JUnit 4 to JUnit 5

Result:

Module servicetalk-transport-netty-internal now runs tests using JUnit 5
  • Loading branch information
kashike authored Jun 2, 2021
1 parent bae5f7b commit e890ac9
Show file tree
Hide file tree
Showing 29 changed files with 548 additions and 625 deletions.
5 changes: 4 additions & 1 deletion servicetalk-transport-netty-internal/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,16 @@ dependencies {
testImplementation project(":servicetalk-concurrent-api-test")
testImplementation project(":servicetalk-concurrent-test-internal")
testImplementation "org.junit.jupiter:junit-jupiter-api:$junit5Version"
testImplementation "junit:junit:$junitVersion"
testImplementation "org.junit.jupiter:junit-jupiter-params:$junit5Version"
testImplementation "org.hamcrest:hamcrest-library:$hamcrestVersion"
testImplementation "org.mockito:mockito-core:$mockitoCoreVersion"
testImplementation "org.mockito:mockito-junit-jupiter:$mockitoCoreVersion"

testFixturesImplementation "com.google.code.findbugs:jsr305:$jsr305Version"
testFixturesImplementation "org.junit.jupiter:junit-jupiter-api:$junit5Version"
testFixturesImplementation "junit:junit:$junitVersion"
testFixturesImplementation "org.hamcrest:hamcrest-library:$hamcrestVersion"
testFixturesImplementation "org.mockito:mockito-core:$mockitoCoreVersion"

testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junit5Version"
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@
<Source name="~.*Test\.java"/>
<Bug pattern="RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT"/>
</Match>
<!-- jUnit5 requires @Nested test classes to be non-static -->
<Match>
<Source name="~.*Test\.java"/>
<Bug pattern="SIC_INNER_SHOULD_BE_STATIC"/>
</Match>
</FindBugsFilter>
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package io.servicetalk.transport.netty.internal;

import io.servicetalk.concurrent.internal.ServiceTalkTestTimeout;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
Expand All @@ -25,10 +23,8 @@
import io.netty.channel.EventLoop;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.local.LocalChannel;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.Timeout;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutionException;
Expand All @@ -41,15 +37,12 @@
import static org.hamcrest.Matchers.not;

public abstract class AbstractOutOfEventloopTest {
@Rule
public final Timeout timeout = new ServiceTalkTestTimeout();

protected Channel channel;
private EventLoopGroup eventLoopGroup;
protected BlockingQueue<Integer> pendingFlush;
protected BlockingQueue<Integer> written;

@Before
@BeforeEach
public void setUp() throws InterruptedException {
eventLoopGroup = new DefaultEventLoopGroup(2);
channel = new LocalChannel();
Expand Down Expand Up @@ -79,7 +72,7 @@ public void flush(ChannelHandlerContext ctx) {

public abstract void setup0();

@After
@AfterEach
public void tearDown() throws Exception {
written.clear();
channel.close().await(DEFAULT_TIMEOUT_SECONDS, SECONDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import io.servicetalk.concurrent.PublisherSource;
import io.servicetalk.concurrent.api.test.StepVerifiers;
import io.servicetalk.concurrent.internal.ServiceTalkTestTimeout;
import io.servicetalk.logging.api.LogLevel;
import io.servicetalk.transport.api.ConnectionInfo.Protocol;
import io.servicetalk.transport.netty.internal.NoopTransportObserver.NoopConnectionObserver;
Expand All @@ -26,10 +25,8 @@
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.handler.ssl.SslCloseCompletionEvent;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import static io.servicetalk.buffer.netty.BufferAllocators.DEFAULT_ALLOCATOR;
import static io.servicetalk.concurrent.api.Executors.immediate;
Expand All @@ -49,9 +46,6 @@ abstract class AbstractSslCloseNotifyAlertHandlingTest {
protected static final String BEGIN = "MSG_BEGIN";
protected static final String END = "MSG_END";

@Rule
public final Timeout timeout = new ServiceTalkTestTimeout();

protected final EmbeddedDuplexChannel channel;
protected final DefaultNettyConnection<String, String> conn;

Expand Down Expand Up @@ -86,7 +80,7 @@ public void write(final ChannelHandlerContext ctx, final Object msg, final Chann
.toFuture().get();
}

@After
@AfterEach
public void tearDown() throws Exception {
try {
// Make sure the connection and channel are closed after each test:
Expand All @@ -106,7 +100,7 @@ public void tearDown() throws Exception {
}

@Test
public void neverUsedIdleConnection() {
void neverUsedIdleConnection() {
closeNotifyAndVerifyClosing();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.After;
import org.junit.Before;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

import static io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION;
import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -41,15 +41,15 @@ public abstract class AbstractWriteTest {
protected CompletableSource.Subscriber completableSubscriber;
protected FailingWriteHandler failingWriteHandler;

@Before
@BeforeEach
public void setUp() throws Exception {
completableSubscriber = mock(CompletableSource.Subscriber.class);
failingWriteHandler = new FailingWriteHandler();
channel = new EmbeddedChannel(failingWriteHandler);
demandEstimator = mock(WriteDemandEstimator.class);
}

@After
@AfterEach
public void tearDown() throws Exception {
channel.finishAndReleaseAll();
channel.close().await();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import io.servicetalk.transport.api.HostAndPort;

import io.netty.util.NetUtil;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.net.InetSocketAddress;
import java.net.UnknownHostException;
Expand All @@ -28,13 +28,13 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThrows;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class BuilderUtilsTest {
class BuilderUtilsTest {

@Test
@SuppressWarnings("PMD.AvoidUsingHardCodedIP")
public void toResolvedInetSocketAddressFromIPv4() {
void toResolvedInetSocketAddressFromIPv4() {
final String localhostIp4Address = NetUtil.LOCALHOST4.getHostAddress();
InetSocketAddress address = toResolvedInetSocketAddress(HostAndPort.of(localhostIp4Address, 8080));
assertThat(address.isUnresolved(), is(false));
Expand All @@ -44,7 +44,7 @@ public void toResolvedInetSocketAddressFromIPv4() {

@Test
@SuppressWarnings("PMD.AvoidUsingHardCodedIP")
public void toResolvedInetSocketAddressFromIPv6() {
void toResolvedInetSocketAddressFromIPv6() {
final String localhostIp6Address = NetUtil.LOCALHOST6.getHostAddress();
InetSocketAddress address = toResolvedInetSocketAddress(HostAndPort.of(localhostIp6Address, 8080));
assertThat(address.isUnresolved(), is(false));
Expand All @@ -53,7 +53,7 @@ public void toResolvedInetSocketAddressFromIPv6() {
}

@Test
public void toResolvedInetSocketAddressFromUnresolved() {
void toResolvedInetSocketAddressFromUnresolved() {
Throwable t = assertThrows(IllegalArgumentException.class,
() -> toResolvedInetSocketAddress(HostAndPort.of("unresolved-hostname", 8080)));
assertThat(t.getCause(), instanceOf(UnknownHostException.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import io.servicetalk.concurrent.CompletableSource.Processor;
import io.servicetalk.concurrent.api.AsyncCloseable;
import io.servicetalk.concurrent.api.Completable;
import io.servicetalk.concurrent.internal.ServiceTalkTestTimeout;
import io.servicetalk.concurrent.test.internal.TestCompletableSubscriber;

import io.netty.channel.Channel;
Expand All @@ -28,14 +27,11 @@
import io.netty.channel.DefaultChannelId;
import io.netty.util.Attribute;
import io.netty.util.concurrent.GenericFutureListener;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.Timeout;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.mockito.junit.jupiter.MockitoExtension;

import static io.servicetalk.concurrent.api.AsyncCloseables.closeAsyncGracefully;
import static io.servicetalk.concurrent.api.Executors.immediate;
Expand All @@ -50,19 +46,14 @@
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class ChannelSetTest {

@Rule
public final Timeout timeout = new ServiceTalkTestTimeout();
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Rule
public final MockitoRule rule = MockitoJUnit.rule();
@ExtendWith(MockitoExtension.class)
class ChannelSetTest {

@Mock
private Channel channel;
Expand All @@ -81,20 +72,20 @@ public class ChannelSetTest {
private final Processor closeAsyncCompletable = newCompletableProcessor();
private GenericFutureListener<ChannelFuture> listener;

@Before
@BeforeEach
public void setupMocks() {
when(channel.id()).thenReturn(channelId);
when(channel.closeFuture()).thenReturn(channelCloseFuture);
when(channel.close()).then(invocation -> {
lenient().when(channel.close()).then(invocation -> {
listener.operationComplete(channelCloseFuture);
return channelCloseFuture;
});
when(channelCloseFuture.channel()).thenReturn(channel);
when(channel.pipeline()).thenReturn(channelPipeline);
when(channel.attr(eq(CHANNEL_CLOSEABLE_KEY))).thenReturn(mockClosableAttribute);
when(mockClosableAttribute.getAndSet(any())).thenReturn(nettyConnection);
when(nettyConnection.closeAsync()).thenReturn(fromSource(closeAsyncCompletable));
when(nettyConnection.closeAsyncGracefully()).thenReturn(fromSource(closeAsyncGracefullyCompletable));
lenient().when(channel.pipeline()).thenReturn(channelPipeline);
lenient().when(channel.attr(eq(CHANNEL_CLOSEABLE_KEY))).thenReturn(mockClosableAttribute);
lenient().when(mockClosableAttribute.getAndSet(any())).thenReturn(nettyConnection);
lenient().when(nettyConnection.closeAsync()).thenReturn(fromSource(closeAsyncCompletable));
lenient().when(nettyConnection.closeAsyncGracefully()).thenReturn(fromSource(closeAsyncGracefullyCompletable));
when(channelCloseFuture.addListener(any())).then((invocation) -> {
listener = invocation.getArgument(0);
return channelCloseFuture;
Expand All @@ -103,7 +94,7 @@ public void setupMocks() {
}

@Test
public void closeAsync() {
void closeAsync() {
Completable completable = fixture.closeAsync();
verify(channel, never()).close();
TestCompletableSubscriber subscriber = new TestCompletableSubscriber();
Expand All @@ -113,7 +104,7 @@ public void closeAsync() {
}

@Test
public void closeAsyncGracefullyWithNettyConnectionChannelHandler() throws Exception {
void closeAsyncGracefullyWithNettyConnectionChannelHandler() throws Exception {
Completable completable = closeAsyncGracefully(fixture, 100, SECONDS);
verify(nettyConnection, never()).closeAsyncGracefully();
TestCompletableSubscriber subscriber = new TestCompletableSubscriber();
Expand All @@ -128,7 +119,7 @@ public void closeAsyncGracefullyWithNettyConnectionChannelHandler() throws Excep
}

@Test
public void closeAsyncGracefullyWithoutNettyConnectionChannelHandler() {
void closeAsyncGracefullyWithoutNettyConnectionChannelHandler() {
when(mockClosableAttribute.getAndSet(any())).thenReturn(null);
Completable completable = closeAsyncGracefully(fixture, 100, SECONDS);
verify(channel, never()).close();
Expand All @@ -139,7 +130,7 @@ public void closeAsyncGracefullyWithoutNettyConnectionChannelHandler() {
}

@Test
public void testCloseAsyncGracefullyThenCloseAsync() throws Exception {
void testCloseAsyncGracefullyThenCloseAsync() throws Exception {
Completable gracefulCompletable = closeAsyncGracefully(fixture, 100, SECONDS);
Completable closeCompletable = fixture.closeAsync();

Expand All @@ -161,7 +152,7 @@ public void testCloseAsyncGracefullyThenCloseAsync() throws Exception {
}

@Test
public void testCloseAsyncThenCloseAsyncGracefully() throws Exception {
void testCloseAsyncThenCloseAsyncGracefully() throws Exception {
Completable closeCompletable = fixture.closeAsync();
Completable gracefulCompletable = closeAsyncGracefully(fixture, 100, SECONDS);

Expand All @@ -180,7 +171,7 @@ public void testCloseAsyncThenCloseAsyncGracefully() throws Exception {
}

@Test
public void testCloseAsyncGracefullyTwice() throws Exception {
void testCloseAsyncGracefullyTwice() throws Exception {
Completable gracefulCompletable1 = closeAsyncGracefully(fixture, 60, SECONDS);
Completable gracefulCompletable2 = closeAsyncGracefully(fixture, 60, SECONDS);

Expand All @@ -205,7 +196,7 @@ public void testCloseAsyncGracefullyTwice() throws Exception {
}

@Test
public void testCloseAsyncGracefullyTwiceTimesOut() throws Exception {
void testCloseAsyncGracefullyTwiceTimesOut() throws Exception {
Completable gracefulCompletable1 = closeAsyncGracefully(fixture, 100, MILLISECONDS);
Completable gracefulCompletable2 = closeAsyncGracefully(fixture, 1000, MILLISECONDS);

Expand All @@ -224,7 +215,7 @@ public void testCloseAsyncGracefullyTwiceTimesOut() throws Exception {
}

@Test
public void testCloseAsyncTwice() throws Exception {
void testCloseAsyncTwice() throws Exception {
Completable closeCompletable1 = fixture.closeAsync();
Completable closeCompletable2 = fixture.closeAsync();

Expand All @@ -243,7 +234,7 @@ public void testCloseAsyncTwice() throws Exception {
}

@Test
public void closeAsyncGracefullyClosesAfterTimeout() throws Exception {
void closeAsyncGracefullyClosesAfterTimeout() throws Exception {
Completable completable = closeAsyncGracefully(fixture, 100, MILLISECONDS);
TestCompletableSubscriber subscriber = new TestCompletableSubscriber();
toSource(completable).subscribe(subscriber);
Expand Down
Loading

0 comments on commit e890ac9

Please sign in to comment.