Skip to content

Commit

Permalink
Migrate tests in servicetalk-concurrent-api module from junit4 to j…
Browse files Browse the repository at this point in the history
…unit5 (#1415)

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).

[migrating-from-junit-4-to-junit-5-important-differences-and-benefits](https://blogs.oracle.com/javamagazine/migrating-from-junit-4-to-junit-5-important-differences-and-benefits)

Modifications:
   Updated unit tests 
   `ServiceTalkTestTimeout` junit4 Rule functionality is replaced with 
    - global timeout for all tests methods - `junit.jupiter.execution.timeout.default`
    - junit5 `@Timeout` annotation 
    - custom junit5 `TimeoutTracingInfoExtension`

Result:
   module concurrent-api is running tests using junit5 engine
  • Loading branch information
danfaer authored Mar 17, 2021
1 parent bc810aa commit d5cdaac
Show file tree
Hide file tree
Showing 186 changed files with 2,291 additions and 2,226 deletions.
7 changes: 6 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright © 2018-2019 Apple Inc. and the ServiceTalk project authors
# Copyright © 2018-2019, 2021 Apple Inc. and the ServiceTalk project authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -36,6 +36,7 @@ jsonUnitVersion=2.8.1

jmhCoreVersion=1.28
junitVersion=4.13.2
junit5Version=5.7.1
testngVersion=6.14.3
hamcrestVersion=1.3
mockitoCoreVersion=2.28.2
Expand Down Expand Up @@ -68,3 +69,7 @@ shadowPluginVersion=4.0.4
# https://issues.sonatype.org/browse/MVNCENTRAL-5276
# https://issues.apache.org/jira/browse/INFRA-14923
systemProp.org.gradle.internal.publish.checksums.insecure=true

# Test properties
# expected format for timeout: <number>[ns|μs|ms|s|m|h|d])
junit5DefaultTimeout=30s
16 changes: 14 additions & 2 deletions servicetalk-concurrent-api/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2018-2020 Apple Inc. and the ServiceTalk project authors
* Copyright © 2018-2021 Apple Inc. and the ServiceTalk project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,14 @@

apply plugin: "io.servicetalk.servicetalk-gradle-plugin-internal-library"

test {
useJUnitPlatform()
def junit5TimeoutParamName = "junit.jupiter.execution.timeout.default"
def junit5Timeout = System.getProperty(junit5TimeoutParamName, "$junit5DefaultTimeout")
systemProperty junit5TimeoutParamName, "$junit5Timeout"
systemProperty "junit.jupiter.extensions.autodetection.enabled", "true"
}

dependencies {
api project(":servicetalk-concurrent")

Expand All @@ -28,15 +36,19 @@ dependencies {
testImplementation testFixtures(project(":servicetalk-concurrent-internal"))
testImplementation project(":servicetalk-concurrent-test-internal")
testImplementation project(":servicetalk-test-resources")
testImplementation "junit:junit:$junitVersion"
testImplementation "org.junit.jupiter:junit-jupiter-api:$junit5Version"
testImplementation "org.junit.jupiter:junit-jupiter-params:$junit5Version"
testImplementation "org.hamcrest:hamcrest-library:$hamcrestVersion"
testImplementation "org.mockito:mockito-core:$mockitoCoreVersion"

testFixturesImplementation testFixtures(project(":servicetalk-concurrent-internal"))
testFixturesImplementation project(":servicetalk-utils-internal")
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"
testFixturesImplementation "org.slf4j:slf4j-api:$slf4jVersion"

testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junit5Version"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2020 Apple Inc. and the ServiceTalk project authors
* Copyright © 2020-2021 Apple Inc. and the ServiceTalk project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,8 +17,8 @@

import io.servicetalk.concurrent.Cancellable;

import org.junit.ClassRule;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -31,8 +31,8 @@
import static org.mockito.Mockito.verify;

public abstract class AbstractCompositeCancellableTest<T extends Cancellable> {
@ClassRule
public static final ExecutorRule<Executor> EXECUTOR_RULE = ExecutorRule.newRule();
@RegisterExtension
static final ExecutorExtension<Executor> EXECUTOR_RULE = ExecutorExtension.withCachedExecutor();

protected abstract T newCompositeCancellable();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2019 Apple Inc. and the ServiceTalk project authors
* Copyright © 2019, 2021 Apple Inc. and the ServiceTalk project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,11 +16,9 @@
package io.servicetalk.concurrent.api;

import io.servicetalk.concurrent.Cancellable;
import io.servicetalk.concurrent.internal.ServiceTalkTestTimeout;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.mockito.Mockito;

import java.util.concurrent.CancellationException;
Expand All @@ -38,17 +36,15 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

public abstract class AbstractToFutureTest<T> {

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

@Rule
public final ExecutorRule<Executor> exec = ExecutorRule.newRule();
@RegisterExtension
public final ExecutorExtension<Executor> exec = ExecutorExtension.withCachedExecutor();

protected final Cancellable mockCancellable = Mockito.mock(Cancellable.class);

Expand Down Expand Up @@ -151,11 +147,11 @@ public void testFailed() throws Exception {
verify(mockCancellable, never()).cancel();
}

@Test(expected = NullPointerException.class)
@Test
public void testFailedWithNull() {
Future<T> future = toFuture();
assertThat(future.isDone(), is(false));
failSource(null);
assertThrows(NullPointerException.class, () -> failSource(null));
}

@Test
Expand Down Expand Up @@ -188,11 +184,11 @@ public void testFailedAfterGetWithTimeout() throws Exception {
verify(mockCancellable, never()).cancel();
}

@Test(expected = TimeoutException.class)
public void testGetTimeoutException() throws Exception {
@Test
public void testGetTimeoutException() {
Future<T> future = toFuture();
assertThat(future.isDone(), is(false));
assertThat(future.get(10, MILLISECONDS), is(expectedResult()));
assertThrows(TimeoutException.class, () -> future.get(10, MILLISECONDS));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2018 Apple Inc. and the ServiceTalk project authors
* Copyright © 2018, 2021 Apple Inc. and the ServiceTalk project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,21 +16,16 @@
package io.servicetalk.concurrent.api;

import io.servicetalk.concurrent.api.AsyncContextMap.Key;
import io.servicetalk.concurrent.internal.ServiceTalkTestTimeout;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
import org.junit.jupiter.api.Test;

import java.util.concurrent.atomic.AtomicReference;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.fail;

public class AsyncContextDisableTest {
@Rule
public final Timeout timeout = new ServiceTalkTestTimeout();
private static final Key<String> K1 = Key.newKey("k1");

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2020 Apple Inc. and the ServiceTalk project authors
* Copyright © 2020-2021 Apple Inc. and the ServiceTalk project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,28 +15,21 @@
*/
package io.servicetalk.concurrent.api;

import io.servicetalk.concurrent.internal.ServiceTalkTestTimeout;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
import org.junit.jupiter.api.Test;

import java.util.concurrent.TimeoutException;

import static io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThrows;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.verifyZeroInteractions;

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

private final DefaultBlockingProcessorSignalsHolder<Integer> buffer;
@SuppressWarnings("unchecked")
private final ProcessorSignalsConsumer<Integer> consumer = mock(ProcessorSignalsConsumer.class);
Expand All @@ -55,8 +48,8 @@ public void consumeItem() throws Exception {

@Test
public void consumeEmpty() {
assertThrows("Unexpected consume when empty.", TimeoutException.class,
() -> buffer.consume(consumer, 1, MILLISECONDS));
assertThrows(TimeoutException.class,
() -> buffer.consume(consumer, 1, MILLISECONDS), "Unexpected consume when empty.");
verifyZeroInteractions(consumer);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2020 Apple Inc. and the ServiceTalk project authors
* Copyright © 2020-2021 Apple Inc. and the ServiceTalk project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,13 +16,10 @@
package io.servicetalk.concurrent.api;

import io.servicetalk.concurrent.api.BufferStrategy.Accumulator;
import io.servicetalk.concurrent.internal.ServiceTalkTestTimeout;

import org.junit.After;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.time.Duration;
import java.util.ArrayList;
Expand All @@ -49,9 +46,6 @@
import static org.mockito.Mockito.when;

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

private final BlockingQueue<TestCompletable> timers = new LinkedBlockingDeque<>();
private final Executor executor = mock(Executor.class);
private final java.util.concurrent.ExecutorService jdkExecutor = Executors.newCachedThreadPool();
Expand All @@ -64,12 +58,12 @@ public BufferStrategiesTest() {
}));
}

@After
public void tearDown() throws Exception {
@AfterEach
public void tearDown() {
jdkExecutor.shutdownNow();
}

@Ignore("https://github.com/apple/servicetalk/issues/1259")
@Disabled("https://github.com/apple/servicetalk/issues/1259")
@Test
public void sizeOrDurationConcurrent() throws Exception {
final int maxBoundaries = 1_000;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2018 Apple Inc. and the ServiceTalk project authors
* Copyright © 2018, 2021 Apple Inc. and the ServiceTalk project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,9 +17,9 @@

import io.servicetalk.concurrent.Cancellable;

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

import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2020 Apple Inc. and the ServiceTalk project authors
* Copyright © 2020-2021 Apple Inc. and the ServiceTalk project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,8 +17,8 @@

import io.servicetalk.concurrent.Cancellable;

import org.junit.ClassRule;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -35,15 +35,15 @@
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.isIn;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
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.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

public class ClosableConcurrentStackTest {
@ClassRule
public static final ExecutorRule<Executor> EXECUTOR_RULE = ExecutorRule.newRule();
@RegisterExtension
static final ExecutorExtension<Executor> EXECUTOR_RULE = ExecutorExtension.withCachedExecutor();

@Test
public void singleThreadPushClose() {
Expand Down Expand Up @@ -119,8 +119,8 @@ public void concurrentPushRemove() throws Exception {
} catch (Exception e) {
throw new AssertionError(e);
}
assertTrue("failed for index: " + finalI, stack.push(finalI));
assertTrue("failed for index: " + finalI, stack.relaxedRemove(finalI));
assertTrue(stack.push(finalI), () -> "failed for index: " + finalI);
assertTrue(stack.relaxedRemove(finalI), () -> "failed for index: " + finalI);
}));
}

Expand All @@ -146,7 +146,7 @@ public void concurrentPushRemoveDifferentThread() throws Exception {
}
stack.push(finalI);
}).concat(EXECUTOR_RULE.executor().submit(() ->
assertTrue("failed for index: " + finalI, stack.relaxedRemove(finalI)))));
assertTrue(stack.relaxedRemove(finalI), () -> "failed for index: " + finalI))));
}

Future<Void> future = mergeAll(completableList, itemCount).toFuture();
Expand All @@ -170,7 +170,7 @@ public void concurrentClosePushRemove() throws Exception {
} catch (Exception e) {
throw new AssertionError(e);
}
assertTrue("failed for index: " + finalI, stack.push(c));
assertTrue(stack.push(c), () -> "failed for index: " + finalI);
return c;
}));
}
Expand Down
Loading

0 comments on commit d5cdaac

Please sign in to comment.