From 3fc6eb2dd41d29c020d68984f6089f775cb8bcae Mon Sep 17 00:00:00 2001 From: Caesar Ralf Date: Tue, 4 Jul 2023 10:22:25 +0200 Subject: [PATCH 1/2] Add java 17 to LTS --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 1b89096..f04da41 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -17,7 +17,7 @@ jobs: # Test on each Java LTS version strategy: matrix: - java_version: [8, 11] + java_version: [8, 11, 17] steps: - uses: actions/checkout@v2 with: From fd824dc6ccd3a93521d22c94511a40d114360df2 Mon Sep 17 00:00:00 2001 From: Caesar Ralf Date: Tue, 4 Jul 2023 11:36:13 +0200 Subject: [PATCH 2/2] Updates tests to JUnit5 --- pom.xml | 14 +- .../futures/CompletableFuturesTest.java | 931 +++++++++--------- .../futures/ConcurrencyReducerTest.java | 53 +- .../com/spotify/futures/FunctionsTest.java | 36 +- .../futures/jmh/AllAsListBenchmark.java | 1 - 5 files changed, 534 insertions(+), 501 deletions(-) diff --git a/pom.xml b/pom.xml index fc2895a..8175943 100644 --- a/pom.xml +++ b/pom.xml @@ -86,15 +86,15 @@ - junit - junit - 4.13.1 + org.junit.jupiter + junit-jupiter + 5.9.3 test org.hamcrest - java-hamcrest - 2.0.0.0 + hamcrest-core + 2.2 test @@ -112,13 +112,13 @@ org.mockito mockito-core - 1.10.19 + 4.11.0 test org.jmock jmock - 2.8.2 + 2.12.0 test diff --git a/src/test/java/com/spotify/futures/CompletableFuturesTest.java b/src/test/java/com/spotify/futures/CompletableFuturesTest.java index 338f638..23bf97f 100644 --- a/src/test/java/com/spotify/futures/CompletableFuturesTest.java +++ b/src/test/java/com/spotify/futures/CompletableFuturesTest.java @@ -2,7 +2,7 @@ * -\-\- * completable-futures * -- - * Copyright (C) 2016 - 2020 Spotify AB + * Copyright (C) 2016 - 2023 Spotify AB * -- * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,31 +19,6 @@ */ package com.spotify.futures; -import java.util.HashMap; -import java.util.Map; -import org.hamcrest.CustomTypeSafeMatcher; -import org.hamcrest.Matcher; -import org.jmock.lib.concurrent.DeterministicScheduler; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import java.time.Duration; -import java.util.List; -import java.util.Optional; -import java.util.concurrent.CancellationException; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CompletionStage; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ScheduledFuture; -import java.util.function.Supplier; -import java.util.stream.Stream; -import java.util.concurrent.CompletionException; -import java.util.concurrent.TimeoutException; - import static com.spotify.futures.CompletableFutures.allAsList; import static com.spotify.futures.CompletableFutures.allAsMap; import static com.spotify.futures.CompletableFutures.combine; @@ -68,117 +43,120 @@ import static java.util.concurrent.TimeUnit.SECONDS; import static java.util.function.Function.identity; import static java.util.stream.Collectors.toList; -import static org.hamcrest.Matchers.both; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasEntry; -import static org.hamcrest.Matchers.hasProperty; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.core.Is.is; -import static org.hamcrest.core.Is.isA; import static org.hamcrest.core.IsNot.not; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.fail; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyLong; -import static org.mockito.Matchers.eq; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.time.Duration; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.CancellationException; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.concurrent.CompletionStage; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeoutException; +import java.util.function.Supplier; +import java.util.stream.Stream; +import org.hamcrest.CustomTypeSafeMatcher; +import org.hamcrest.Matcher; +import org.jmock.lib.concurrent.DeterministicScheduler; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + public class CompletableFuturesTest { private DeterministicScheduler executor; - @Rule - public ExpectedException exception = ExpectedException.none(); - - @Before + @BeforeEach public void setUp() { executor = new DeterministicScheduler(); } @Test - public void allAsList_empty() throws Exception { + public void allAsList_empty() { final List> input = emptyList(); assertThat(allAsList(input), completesTo(emptyList())); } @Test - public void allAsList_one() throws Exception { + public void allAsList_one() { final String value = "a"; final List> input = singletonList(completedFuture(value)); assertThat(allAsList(input), completesTo(singletonList(value))); } @Test - public void allAsList_multiple() throws Exception { + public void allAsList_multiple() { final List values = asList("a", "b", "c"); - final List> input = values.stream() - .map(CompletableFuture::completedFuture) - .collect(toList()); + final List> input = + values.stream().map(CompletableFuture::completedFuture).collect(toList()); assertThat(allAsList(input), completesTo(values)); } @Test - public void allAsList_exceptional() throws Exception { - final RuntimeException ex = new RuntimeException("boom"); - final List> input = asList( - completedFuture("a"), - exceptionallyCompletedFuture(ex), - completedFuture("b") - ); + public void allAsList_exceptional() { + final RuntimeException boom = new RuntimeException("boom"); + final List> input = + asList(completedFuture("a"), exceptionallyCompletedFuture(boom), completedFuture("b")); - exception.expectCause(is(ex)); - allAsList(input).get(); + final ExecutionException e = assertThrows(ExecutionException.class, allAsList(input)::get); + assertThat(e.getCause(), is(equalTo(boom))); } @Test public void allAsList_exceptional_failFast() { final CompletableFuture incomplete = incompleteFuture(); - final CompletableFuture failed = - exceptionallyCompletedFuture(new TimeoutException()); - final List> input = - asList(incomplete, failed); - - try { - allAsList(input).join(); - fail("Expected exception being thrown."); - } catch (Exception e) { - assertThat(e, instanceOf(CompletionException.class)); - assertThat(e.getCause(), instanceOf(TimeoutException.class)); - } + final CompletableFuture failed = exceptionallyCompletedFuture(new TimeoutException()); + final List> input = asList(incomplete, failed); + + final CompletionException e = + assertThrows(CompletionException.class, () -> allAsList(input).join()); + assertThat(e.getCause(), instanceOf(TimeoutException.class)); } @Test - public void allAsList_null() throws Exception { - exception.expect(NullPointerException.class); - allAsList(null); + public void allAsList_null() { + assertThrows(NullPointerException.class, () -> allAsList(null)); } @Test - public void allAsList_containsNull() throws Exception { - final List> input = asList( - completedFuture("a"), - null, - completedFuture("b") - ); + public void allAsList_containsNull() { + final List> input = + asList(completedFuture("a"), null, completedFuture("b")); - exception.expect(NullPointerException.class); - allAsList(input); + assertThrows(NullPointerException.class, () -> allAsList(input)); } @Test - public void allAsMap_empty() throws Exception { + public void allAsMap_empty() { final Map> input = emptyMap(); assertThat(allAsMap(input), completesTo(emptyMap())); } @Test - public void allAsMap_one() throws Exception { + public void allAsMap_one() { final String key = "1"; final String value = "a"; final Map> input = singletonMap(key, completedFuture(value)); @@ -186,140 +164,127 @@ public void allAsMap_one() throws Exception { } @Test - public void allAsMap_multiple() throws Exception { + public void allAsMap_multiple() { final List keys = asList("1", "2", "3"); final List values = asList("a", "b", "c"); - final List> stagedValues = values.stream() - .map(CompletableFuture::completedFuture) - .collect(toList()); + final List> stagedValues = + values.stream().map(CompletableFuture::completedFuture).collect(toList()); final Map> input = asMap(keys, stagedValues); assertThat(allAsMap(input), completesTo(asMap(keys, values))); } @Test public void allAsMap_exceptional() throws Exception { - final RuntimeException ex = new RuntimeException("boom"); final List keys = asList("1", "2", "3"); - final List> values = asList( - completedFuture("a"), - exceptionallyCompletedFuture(ex), - completedFuture("b") - ); - Map input = asMap(keys, values); - exception.expectCause(is(ex)); - allAsMap(input).get(); + final RuntimeException boom = new RuntimeException("boom"); + final List> values = + asList(completedFuture("a"), exceptionallyCompletedFuture(boom), completedFuture("b")); + Map> input = asMap(keys, values); + final ExecutionException e = assertThrows(ExecutionException.class, allAsMap(input)::get); + assertThat(e.getCause(), is(equalTo(boom))); } @Test - public void allAsMap_null() throws Exception { - exception.expect(NullPointerException.class); - allAsMap(null); + public void allAsMap_null() { + assertThrows(NullPointerException.class, () -> allAsMap(null)); } @Test - public void allAsMap_valueContainsNull() throws Exception { + public void allAsMap_valueContainsNull() { final List keys = asList("1", "2", "3"); - final List> values = asList( - completedFuture("a"), - null, - completedFuture("b") - ); + final List> values = + asList(completedFuture("a"), null, completedFuture("b")); final Map> input = asMap(keys, values); - exception.expect(NullPointerException.class); - allAsMap(input); + assertThrows(NullPointerException.class, () -> allAsMap(input)); } @Test - public void allAsMap_keyContainsNull() throws Exception { + public void allAsMap_keyContainsNull() { final List keys = asList("1", null, "3"); final List values = asList("a", "b", "c"); - final List> stagedValues = values.stream() - .map(CompletableFuture::completedFuture) - .collect(toList());; + final List> stagedValues = + values.stream().map(CompletableFuture::completedFuture).collect(toList()); + ; final Map> input = asMap(keys, stagedValues); assertThat(allAsMap(input), completesTo(asMap(keys, values))); } @Test - public void successfulAsList_exceptionalAndNull() throws Exception { - final List> input = asList( - completedFuture("a"), - exceptionallyCompletedFuture(new RuntimeException("boom")), - completedFuture(null), - completedFuture("d") - ); + public void successfulAsList_exceptionalAndNull() { + final List> input = + asList( + completedFuture("a"), + exceptionallyCompletedFuture(new RuntimeException("boom")), + completedFuture(null), + completedFuture("d")); final List expected = asList("a", "default", null, "d"); assertThat(successfulAsList(input, t -> "default"), completesTo(expected)); } @Test - public void getCompleted_done() throws Exception { + public void getCompleted_done() { final CompletionStage future = completedFuture("hello"); assertThat(getCompleted(future), is("hello")); } @Test - public void getCompleted_exceptional() throws Exception { + public void getCompleted_exceptional() { final Exception ex = new Exception("boom"); final CompletionStage future = exceptionallyCompletedFuture(ex); - exception.expectCause(is(ex)); - getCompleted(future); + final Exception e = assertThrows(Exception.class, () -> getCompleted(future)); + assertThat(e.getCause(), is(equalTo(ex))); } @Test - public void getCompleted_nilResult() throws Exception { + public void getCompleted_nilResult() { final CompletableFuture future = completedFuture(null); assertNull(getCompleted(future)); } @Test - public void getCompleted_pending() throws Exception { + public void getCompleted_pending() { final CompletionStage future = new CompletableFuture<>(); - exception.expect(IllegalStateException.class); - getCompleted(future); + assertThrows(IllegalStateException.class, () -> getCompleted(future)); } @Test - public void getException_completedExceptionally() throws Exception { + public void getException_completedExceptionally() { final Exception ex = new Exception("boom"); final CompletionStage future = exceptionallyCompletedFuture(ex); assertThat(getException(future), is(ex)); } @Test - public void getException_completedNormally() throws Exception { + public void getException_completedNormally() { final CompletionStage future = completedFuture("hello"); - exception.expect(IllegalStateException.class); - getException(future); + assertThrows(IllegalStateException.class, () -> getException(future)); } @Test - public void getException_pending() throws Exception { + public void getException_pending() { final CompletionStage future = new CompletableFuture<>(); - exception.expect(IllegalStateException.class); - getException(future); + assertThrows(IllegalStateException.class, () -> getException(future)); } @Test - public void getException_cancelled() throws Exception { + public void getException_cancelled() { final CompletionStage future = new CompletableFuture<>(); future.toCompletableFuture().cancel(true); - exception.expect(CancellationException.class); - getException(future); + assertThrows(CancellationException.class, () -> getException(future)); } @Test - public void getException_returnsNullIfImplementationDoesNotThrow() throws Exception { + public void getException_returnsNullIfImplementationDoesNotThrow() { final CompletableFuture future = new NonThrowingFuture<>(); future.completeExceptionally(new NullPointerException()); assertNull(getException(future)); } @Test - public void exceptionallyCompletedFuture_completed() throws Exception { + public void exceptionallyCompletedFuture_completed() { final CompletableFuture future = exceptionallyCompletedFuture(new Exception("boom")); assertThat(future.isCompletedExceptionally(), is(true)); } @@ -329,21 +294,18 @@ public void exceptionallyCompletedFuture_throws() throws Exception { final Exception ex = new Exception("boom"); final CompletableFuture future = exceptionallyCompletedFuture(ex); - exception.expectCause(is(ex)); - future.get(); + final ExecutionException e = assertThrows(ExecutionException.class, future::get); + assertThat(e.getCause(), is(equalTo(ex))); } @Test - public void exceptionallyCompletedFuture_null() throws Exception { - exception.expect(NullPointerException.class); - exceptionallyCompletedFuture(null); + public void exceptionallyCompletedFuture_null() { + assertThrows(NullPointerException.class, () -> exceptionallyCompletedFuture(null)); } @Test public void joinList_empty() throws Exception { - final List result = Stream.>of() - .collect(joinList()) - .get(); + final List result = Stream.>of().collect(joinList()).get(); assertThat(result, not(nullValue())); assertThat(result, hasSize(0)); @@ -351,9 +313,7 @@ public void joinList_empty() throws Exception { @Test public void joinList_one() throws Exception { - final List result = Stream.of(completedFuture("a")) - .collect(joinList()) - .get(); + final List result = Stream.of(completedFuture("a")).collect(joinList()).get(); assertThat(result, hasSize(1)); assertThat(result, contains("a")); @@ -364,9 +324,7 @@ public void joinList_two() throws Exception { final CompletableFuture a = completedFuture("hello"); final CompletableFuture b = completedFuture("world"); - final List result = Stream.of(a, b) - .collect(joinList()) - .get(); + final List result = Stream.of(a, b).collect(joinList()).get(); assertThat(result, contains("hello", "world")); } @@ -376,9 +334,7 @@ public void joinList_mixedStageTypes() throws Exception { final CompletionStage a = completedFuture("hello"); final CompletableFuture b = completedFuture("world"); - final List result = Stream.of(a, b) - .collect(joinList()) - .get(); + final List result = Stream.of(a, b).collect(joinList()).get(); assertThat(result, contains("hello", "world")); } @@ -390,9 +346,7 @@ public void joinList_mixedValueTypes() throws Exception { final Stream> s = Stream.of(a, b); - final List result = s - .collect(joinList()) - .get(); + final List result = s.collect(joinList()).get(); assertThat(result, contains(3, 4L)); } @@ -404,25 +358,23 @@ public void joinList_exceptional() throws Exception { final CompletableFuture> result = Stream.of(a, b).collect(joinList()); - exception.expectCause(is(ex)); - result.get(); + final ExecutionException e = assertThrows(ExecutionException.class, result::get); + assertThat(e.getCause(), is(equalTo(ex))); } @Test - public void joinList_containsNull() throws Exception { + public void joinList_containsNull() { final CompletableFuture a = completedFuture("hello"); final CompletableFuture b = null; final Stream> stream = Stream.of(a, b); - exception.expect(NullPointerException.class); - stream.collect(joinList()); + assertThrows(NullPointerException.class, () -> stream.collect(joinList())); } @Test public void joinMap_empty() throws Exception { - final Map result = Stream.of() - .collect(joinMap(identity(), CompletableFuture::completedFuture)) - .get(); + final Map result = + Stream.of().collect(joinMap(identity(), CompletableFuture::completedFuture)).get(); assertThat(result, not(nullValue())); assertThat(result.values(), hasSize(0)); @@ -430,9 +382,8 @@ public void joinMap_empty() throws Exception { @Test public void joinMap_one() throws Exception { - final Map result = Stream.of("a") - .collect(joinMap(identity(), k -> completedFuture(k + "v"))) - .get(); + final Map result = + Stream.of("a").collect(joinMap(identity(), k -> completedFuture(k + "v"))).get(); assertThat(result.values(), hasSize(1)); assertThat(result.values(), contains("av")); @@ -441,7 +392,8 @@ public void joinMap_one() throws Exception { @Test public void joinMap_two() throws Exception { - final Map result = Stream.of("hello", "world") + final Map result = + Stream.of("hello", "world") .collect(joinMap(e -> "k " + e, e -> completedFuture("v " + e))) .get(); assertThat(result.entrySet(), hasSize(2)); @@ -455,25 +407,26 @@ public void joinMap_exceptional() throws Exception { final CompletableFuture a = completedFuture("hello"); final CompletableFuture b = exceptionallyCompletedFuture(ex); - final CompletableFuture> result = Stream.of("a", "b") - .collect(joinMap(identity(), v -> v.equals("a") ? a : b)); + final CompletableFuture> result = + Stream.of("a", "b").collect(joinMap(identity(), v -> v.equals("a") ? a : b)); - exception.expectCause(is(ex)); - result.get(); + final ExecutionException e = assertThrows(ExecutionException.class, result::get); + assertThat(e.getCause(), is(equalTo(ex))); } @Test - public void joinMap_containsNull() throws Exception { + public void joinMap_containsNull() { final CompletableFuture a = completedFuture("hello"); final CompletableFuture b = null; final Stream stream = Stream.of("a", "b"); - exception.expect(NullPointerException.class); - stream.collect(joinMap(identity(), v -> v.equals("a") ? a : b)); + assertThrows( + NullPointerException.class, + () -> stream.collect(joinMap(identity(), v -> v.equals("a") ? a : b))); } @Test - public void dereference_completed() throws Exception { + public void dereference_completed() { final CompletionStage future = completedFuture("hello"); final CompletionStage dereferenced = dereference(completedFuture(future)); @@ -481,25 +434,27 @@ public void dereference_completed() throws Exception { } @Test - public void dereference_exceptional() throws Exception { + public void dereference_exceptional() { final IllegalArgumentException ex = new IllegalArgumentException(); final CompletionStage future = exceptionallyCompletedFuture(ex); final CompletionStage dereferenced = dereference(completedFuture(future)); - exception.expectCause(is(ex)); - getCompleted(dereferenced); + final CompletionException e = + assertThrows(CompletionException.class, () -> getCompleted(dereferenced)); + assertThat(e.getCause(), is(equalTo(ex))); } @Test - public void dereference_null() throws Exception { + public void dereference_null() { final CompletionStage dereferenced = dereference(completedFuture(null)); - exception.expectCause(isA(NullPointerException.class)); - getCompleted(dereferenced); + final CompletionException e = + assertThrows(CompletionException.class, () -> getCompleted(dereferenced)); + assertThat(e.getCause(), is(instanceOf(NullPointerException.class))); } @Test - public void exceptionallyCompose_complete() throws Exception { + public void exceptionallyCompose_complete() { final CompletionStage future = exceptionallyCompletedFuture(new Exception("boom")); final CompletableFuture fallback = completedFuture("hello"); @@ -509,19 +464,20 @@ public void exceptionallyCompose_complete() throws Exception { } @Test - public void exceptionallyCompose_exceptional() throws Exception { + public void exceptionallyCompose_exceptional() { final CompletionStage future = exceptionallyCompletedFuture(new Exception("boom")); final IllegalStateException fallbackException = new IllegalStateException(); final CompletableFuture fallback = exceptionallyCompletedFuture(fallbackException); final CompletionStage composed = exceptionallyCompose(future, throwable -> fallback); - exception.expectCause(is(fallbackException)); - getCompleted(composed); + final CompletionException e = + assertThrows(CompletionException.class, () -> getCompleted(composed)); + assertThat(e.getCause(), is(equalTo(fallbackException))); } @Test - public void exceptionallyCompose_unused() throws Exception { + public void exceptionallyCompose_unused() { final CompletionStage future = completedFuture("hello"); final IllegalStateException fallbackException = new IllegalStateException(); final CompletableFuture fallback = exceptionallyCompletedFuture(fallbackException); @@ -531,30 +487,35 @@ public void exceptionallyCompose_unused() throws Exception { } @Test - public void exceptionallyCompose_throws() throws Exception { + public void exceptionallyCompose_throws() { final CompletionStage future = exceptionallyCompletedFuture(new Exception("boom")); final IllegalStateException ex = new IllegalStateException(); - final CompletionStage composed = exceptionallyCompose(future, throwable -> { - throw ex; - }); + final CompletionStage composed = + exceptionallyCompose( + future, + throwable -> { + throw ex; + }); - exception.expectCause(is(ex)); - getCompleted(composed); + final CompletionException e = + assertThrows(CompletionException.class, () -> getCompleted(composed)); + assertThat(e.getCause(), is(equalTo(ex))); } @Test - public void exceptionallyCompose_returnsNull() throws Exception { + public void exceptionallyCompose_returnsNull() { final CompletionStage future = exceptionallyCompletedFuture(new Exception("boom")); final CompletionStage composed = exceptionallyCompose(future, throwable -> null); - exception.expectCause(isA(NullPointerException.class)); - getCompleted(composed); + final CompletionException e = + assertThrows(CompletionException.class, () -> getCompleted(composed)); + assertThat(e.getCause(), is(instanceOf(NullPointerException.class))); } @Test - public void handleCompose_completed() throws Exception { + public void handleCompose_completed() { final CompletionStage future = exceptionallyCompletedFuture(new Exception("boom")); final CompletionStage composed = @@ -564,369 +525,427 @@ public void handleCompose_completed() throws Exception { } @Test - public void handleCompose_failure() throws Exception { + public void handleCompose_failure() { final CompletionStage future = exceptionallyCompletedFuture(new Exception("boom")); final IllegalStateException ex = new IllegalStateException(); final CompletionStage composed = handleCompose(future, (s, t) -> exceptionallyCompletedFuture(ex)); - exception.expectCause(is(ex)); - getCompleted(composed); + final CompletionException e = + assertThrows(CompletionException.class, () -> getCompleted(composed)); + assertThat(e.getCause(), is(equalTo(ex))); } @Test - public void handleCompose_throws() throws Exception { + public void handleCompose_throws() { final CompletionStage future = exceptionallyCompletedFuture(new Exception("boom")); final IllegalStateException ex = new IllegalStateException(); - final CompletionStage composed = handleCompose(future, (s, throwable) -> { throw ex; }); + final CompletionStage composed = + handleCompose( + future, + (s, throwable) -> { + throw ex; + }); - exception.expectCause(is(ex)); - getCompleted(composed); + final CompletionException e = + assertThrows(CompletionException.class, () -> getCompleted(composed)); + assertThat(e.getCause(), is(equalTo(ex))); } @Test - public void handleCompose_returnsNull() throws Exception { + public void handleCompose_returnsNull() { final CompletionStage future = exceptionallyCompletedFuture(new Exception("boom")); final CompletionStage composed = handleCompose(future, (s, throwable) -> null); - exception.expectCause(isA(NullPointerException.class)); - getCompleted(composed); + final CompletionException e = + assertThrows(CompletionException.class, () -> getCompleted(composed)); + assertThat(e.getCause(), is(instanceOf(NullPointerException.class))); } @Test - public void combine2_completed() throws Exception { - final CompletionStage future = combine( - completedFuture("a"), completedFuture("b"), - (a, b) -> a + b); + public void combine2_completed() { + final CompletionStage future = + combine(completedFuture("a"), completedFuture("b"), (a, b) -> a + b); assertThat(future, completesTo("ab")); } @Test - public void combine2_exceptional() throws Exception { - final CompletionStage future = combine( - completedFuture("a"), - exceptionallyCompletedFuture(new IllegalStateException()), - (a, b) -> a + b); + public void combine2_exceptional() { + final CompletionStage future = + combine( + completedFuture("a"), + exceptionallyCompletedFuture(new IllegalStateException()), + (a, b) -> a + b); - exception.expectCause(isA(IllegalStateException.class)); - getCompleted(future); + final CompletionException e = + assertThrows(CompletionException.class, () -> getCompleted(future)); + assertThat(e.getCause(), is(instanceOf(IllegalStateException.class))); } @Test - public void combine3_completed() throws Exception { - final CompletionStage future = combine( - completedFuture("a"), completedFuture("b"), completedFuture("c"), - (a, b, c) -> a + b + c); + public void combine3_completed() { + final CompletionStage future = + combine( + completedFuture("a"), + completedFuture("b"), + completedFuture("c"), + (a, b, c) -> a + b + c); assertThat(future, completesTo("abc")); } @Test - public void combine3_exceptional() throws Exception { - final CompletionStage future = combine( - completedFuture("a"), completedFuture("b"), - exceptionallyCompletedFuture(new IllegalStateException()), - (a, b, c) -> a + b + c); + public void combine3_exceptional() { + final CompletionStage future = + combine( + completedFuture("a"), + completedFuture("b"), + exceptionallyCompletedFuture(new IllegalStateException()), + (a, b, c) -> a + b + c); - exception.expectCause(isA(IllegalStateException.class)); - getCompleted(future); + final CompletionException e = + assertThrows(CompletionException.class, () -> getCompleted(future)); + assertThat(e.getCause(), is(instanceOf(IllegalStateException.class))); } @Test - public void combine4_completed() throws Exception { - final CompletionStage future = combine( - completedFuture("a"), completedFuture("b"), completedFuture("c"), - completedFuture("d"), - (a, b, c, d) -> a + b + c + d); + public void combine4_completed() { + final CompletionStage future = + combine( + completedFuture("a"), + completedFuture("b"), + completedFuture("c"), + completedFuture("d"), + (a, b, c, d) -> a + b + c + d); assertThat(future, completesTo("abcd")); } @Test - public void combine4_exceptional() throws Exception { - final CompletionStage future = combine( - completedFuture("a"), completedFuture("b"), completedFuture("c"), - exceptionallyCompletedFuture(new IllegalStateException()), - (a, b, c, d)-> a + b + c + d); + public void combine4_exceptional() { + final CompletionStage future = + combine( + completedFuture("a"), + completedFuture("b"), + completedFuture("c"), + exceptionallyCompletedFuture(new IllegalStateException()), + (a, b, c, d) -> a + b + c + d); - exception.expectCause(isA(IllegalStateException.class)); - getCompleted(future); + final CompletionException e = + assertThrows(CompletionException.class, () -> getCompleted(future)); + assertThat(e.getCause(), is(instanceOf(IllegalStateException.class))); } @Test - public void combine4_incomplete() throws Exception { - final CompletionStage future = combine( - completedFuture("a"), completedFuture("b"), completedFuture("c"), - incompleteFuture(), + public void combine4_incomplete() { + final CompletionStage future = + combine( + completedFuture("a"), + completedFuture("b"), + completedFuture("c"), + incompleteFuture(), (a, b, c, d) -> a + b + c + d); - exception.expect(isA(IllegalStateException.class)); - getCompleted(future); + assertThrows(IllegalStateException.class, () -> getCompleted(future)); } @Test - public void combine5_completed() throws Exception { - final CompletionStage future = combine( - completedFuture("a"), completedFuture("b"), completedFuture("c"), - completedFuture("d"), completedFuture("e"), - (a, b, c, d, e) -> a + b + c + d + e); + public void combine5_completed() { + final CompletionStage future = + combine( + completedFuture("a"), + completedFuture("b"), + completedFuture("c"), + completedFuture("d"), + completedFuture("e"), + (a, b, c, d, e) -> a + b + c + d + e); assertThat(future, completesTo("abcde")); } @Test - public void combine5_exceptional() throws Exception { - final CompletionStage future = combine( - completedFuture("a"), completedFuture("b"), completedFuture("c"), - completedFuture("d"), - exceptionallyCompletedFuture(new IllegalStateException()), - (a, b, c, d, e)-> a + b + c + d + e); + public void combine5_exceptional() { + final CompletionStage future = + combine( + completedFuture("a"), + completedFuture("b"), + completedFuture("c"), + completedFuture("d"), + exceptionallyCompletedFuture(new IllegalStateException()), + (a, b, c, d, e) -> a + b + c + d + e); - exception.expectCause(isA(IllegalStateException.class)); - getCompleted(future); + final CompletionException e = + assertThrows(CompletionException.class, () -> getCompleted(future)); + assertThat(e.getCause(), is(instanceOf(IllegalStateException.class))); } @Test - public void combine5_incomplete() throws Exception { - final CompletionStage future = combine( - completedFuture("a"), completedFuture("b"), completedFuture("c"), - completedFuture("d"), - incompleteFuture(), - (a, b, c, d, e) -> a + b + c + d + e); - exception.expect(isA(IllegalStateException.class)); - getCompleted(future); + public void combine5_incomplete() { + final CompletionStage future = + combine( + completedFuture("a"), + completedFuture("b"), + completedFuture("c"), + completedFuture("d"), + incompleteFuture(), + (a, b, c, d, e) -> a + b + c + d + e); + assertThrows(IllegalStateException.class, () -> getCompleted(future)); } @Test - public void combine6_completed() throws Exception { - final CompletionStage future = combine( - completedFuture("a"), completedFuture("b"), completedFuture("c"), - completedFuture("d"), completedFuture("e"), completedFuture("f"), - (a, b, c, d, e, f) -> a + b + c + d + e + f); + public void combine6_completed() { + final CompletionStage future = + combine( + completedFuture("a"), + completedFuture("b"), + completedFuture("c"), + completedFuture("d"), + completedFuture("e"), + completedFuture("f"), + (a, b, c, d, e, f) -> a + b + c + d + e + f); assertThat(future, completesTo("abcdef")); } @Test - public void combine6_exceptional() throws Exception { - final CompletionStage future = combine( - completedFuture("a"), completedFuture("b"), completedFuture("c"), - completedFuture("d"), completedFuture("e"), - exceptionallyCompletedFuture(new IllegalStateException()), - (a, b, c, d, e, f)-> a + b + c + d + e + f); + public void combine6_exceptional() { + final CompletionStage future = + combine( + completedFuture("a"), + completedFuture("b"), + completedFuture("c"), + completedFuture("d"), + completedFuture("e"), + exceptionallyCompletedFuture(new IllegalStateException()), + (a, b, c, d, e, f) -> a + b + c + d + e + f); - exception.expectCause(isA(IllegalStateException.class)); - getCompleted(future); + final CompletionException e = + assertThrows(CompletionException.class, () -> getCompleted(future)); + assertThat(e.getCause(), is(instanceOf(IllegalStateException.class))); } @Test - public void combine6_incomplete() throws Exception { - final CompletionStage future = combine( - completedFuture("a"), completedFuture("b"), completedFuture("c"), - completedFuture("d"), completedFuture("e"), - incompleteFuture(), - (a, b, c, d, e, f) -> a + b + c + d + e + f); - exception.expect(isA(IllegalStateException.class)); - getCompleted(future); + public void combine6_incomplete() { + final CompletionStage future = + combine( + completedFuture("a"), + completedFuture("b"), + completedFuture("c"), + completedFuture("d"), + completedFuture("e"), + incompleteFuture(), + (a, b, c, d, e, f) -> a + b + c + d + e + f); + assertThrows(IllegalStateException.class, () -> getCompleted(future)); } @Test - public void combineFutures2_completed() throws Exception { - final CompletionStage future = combineFutures( - completedFuture("a"), - completedFuture("b"), - (a, b) -> completedFuture(a + b)); + public void combineFutures2_completed() { + final CompletionStage future = + combineFutures( + completedFuture("a"), completedFuture("b"), (a, b) -> completedFuture(a + b)); assertThat(future, completesTo("ab")); } @Test - public void combineFutures2_incomplete() throws Exception { - final CompletionStage future = combineFutures( - completedFuture("a"), - incompleteFuture(), - (a, b) -> completedFuture(a + b)); + public void combineFutures2_incomplete() { + final CompletionStage future = + combineFutures(completedFuture("a"), incompleteFuture(), (a, b) -> completedFuture(a + b)); - exception.expect(isA(IllegalStateException.class)); - getCompleted(future); + assertThrows(IllegalStateException.class, () -> getCompleted(future)); } @Test - public void combineFutures2_exceptional() throws Exception { - final CompletionStage future = combineFutures( - completedFuture("a"), - exceptionallyCompletedFuture(new IllegalStateException()), - (a, b) -> completedFuture(a + b)); + public void combineFutures2_exceptional() { + final CompletionStage future = + combineFutures( + completedFuture("a"), + exceptionallyCompletedFuture(new IllegalStateException()), + (a, b) -> completedFuture(a + b)); - exception.expectCause(isA(IllegalStateException.class)); - getCompleted(future); + final CompletionException e = + assertThrows(CompletionException.class, () -> getCompleted(future)); + assertThat(e.getCause(), is(instanceOf(IllegalStateException.class))); } @Test - public void combineFutures3_completed() throws Exception { - final CompletionStage future = combineFutures( - completedFuture("a"), - completedFuture("b"), - completedFuture("c"), - (a, b, c) -> completedFuture(a + b + c)); + public void combineFutures3_completed() { + final CompletionStage future = + combineFutures( + completedFuture("a"), + completedFuture("b"), + completedFuture("c"), + (a, b, c) -> completedFuture(a + b + c)); assertThat(future, completesTo("abc")); } @Test - public void combineFutures3_incomplete() throws Exception { - final CompletionStage future = combineFutures( - completedFuture("a"), - completedFuture("b"), - incompleteFuture(), - (a, b, c) -> completedFuture(a + b + c)); + public void combineFutures3_incomplete() { + final CompletionStage future = + combineFutures( + completedFuture("a"), + completedFuture("b"), + incompleteFuture(), + (a, b, c) -> completedFuture(a + b + c)); - exception.expect(isA(IllegalStateException.class)); - getCompleted(future); + assertThrows(IllegalStateException.class, () -> getCompleted(future)); } @Test - public void combineFutures3_exceptional() throws Exception { - final CompletionStage future = combineFutures( - completedFuture("a"), - completedFuture("b"), - exceptionallyCompletedFuture(new IllegalStateException()), - (a, b, c) -> completedFuture(a + b + c)); + public void combineFutures3_exceptional() { + final CompletionStage future = + combineFutures( + completedFuture("a"), + completedFuture("b"), + exceptionallyCompletedFuture(new IllegalStateException()), + (a, b, c) -> completedFuture(a + b + c)); - exception.expectCause(isA(IllegalStateException.class)); - getCompleted(future); + final CompletionException e = + assertThrows(CompletionException.class, () -> getCompleted(future)); + assertThat(e.getCause(), is(instanceOf(IllegalStateException.class))); } @Test - public void combineFutures4_completed() throws Exception { - final CompletionStage future = combineFutures( - completedFuture("a"), - completedFuture("b"), - completedFuture("c"), - completedFuture("d"), - (a, b, c, d) -> completedFuture(a + b + c + d)); + public void combineFutures4_completed() { + final CompletionStage future = + combineFutures( + completedFuture("a"), + completedFuture("b"), + completedFuture("c"), + completedFuture("d"), + (a, b, c, d) -> completedFuture(a + b + c + d)); assertThat(future, completesTo("abcd")); } @Test - public void combineFutures4_incomplete() throws Exception { - final CompletionStage future = combineFutures( - completedFuture("a"), - completedFuture("b"), - completedFuture("c"), - incompleteFuture(), - (a, b, c, d) -> completedFuture(a + b + c + d)); + public void combineFutures4_incomplete() { + final CompletionStage future = + combineFutures( + completedFuture("a"), + completedFuture("b"), + completedFuture("c"), + incompleteFuture(), + (a, b, c, d) -> completedFuture(a + b + c + d)); - exception.expect(isA(IllegalStateException.class)); - getCompleted(future); + assertThrows(IllegalStateException.class, () -> getCompleted(future)); } @Test - public void combineFutures4_exceptional() throws Exception { - final CompletionStage future = combineFutures( - completedFuture("a"), - completedFuture("b"), - completedFuture("c"), - exceptionallyCompletedFuture(new IllegalStateException()), - (a, b, c, d) -> completedFuture(a + b + c + d)); + public void combineFutures4_exceptional() { + final CompletionStage future = + combineFutures( + completedFuture("a"), + completedFuture("b"), + completedFuture("c"), + exceptionallyCompletedFuture(new IllegalStateException()), + (a, b, c, d) -> completedFuture(a + b + c + d)); - exception.expectCause(isA(IllegalStateException.class)); - getCompleted(future); + final CompletionException e = + assertThrows(CompletionException.class, () -> getCompleted(future)); + assertThat(e.getCause(), is(instanceOf(IllegalStateException.class))); } @Test - public void combineFutures5_completed() throws Exception { - final CompletionStage future = combineFutures( - completedFuture("a"), - completedFuture("b"), - completedFuture("c"), - completedFuture("d"), - completedFuture("e"), - (a, b, c, d, e) -> completedFuture(a + b + c + d + e)); + public void combineFutures5_completed() { + final CompletionStage future = + combineFutures( + completedFuture("a"), + completedFuture("b"), + completedFuture("c"), + completedFuture("d"), + completedFuture("e"), + (a, b, c, d, e) -> completedFuture(a + b + c + d + e)); assertThat(future, completesTo("abcde")); } @Test - public void combineFutures5_incomplete() throws Exception { - final CompletionStage future = combineFutures( - completedFuture("a"), - completedFuture("b"), - completedFuture("c"), - completedFuture("d"), - incompleteFuture(), - (a, b, c, d, e) -> completedFuture(a + b + c + d + e)); + public void combineFutures5_incomplete() { + final CompletionStage future = + combineFutures( + completedFuture("a"), + completedFuture("b"), + completedFuture("c"), + completedFuture("d"), + incompleteFuture(), + (a, b, c, d, e) -> completedFuture(a + b + c + d + e)); - exception.expect(isA(IllegalStateException.class)); - getCompleted(future); + assertThrows(IllegalStateException.class, () -> getCompleted(future)); } @Test - public void combineFutures5_exceptional() throws Exception { - final CompletionStage future = combineFutures( - completedFuture("a"), - completedFuture("b"), - completedFuture("c"), - completedFuture("d"), - exceptionallyCompletedFuture(new IllegalStateException()), - (a, b, c, d, e) -> completedFuture(a + b + c + d + e)); + public void combineFutures5_exceptional() { + final CompletionStage future = + combineFutures( + completedFuture("a"), + completedFuture("b"), + completedFuture("c"), + completedFuture("d"), + exceptionallyCompletedFuture(new IllegalStateException()), + (a, b, c, d, e) -> completedFuture(a + b + c + d + e)); - exception.expectCause(isA(IllegalStateException.class)); - getCompleted(future); + final CompletionException e = + assertThrows(CompletionException.class, () -> getCompleted(future)); + assertThat(e.getCause(), is(instanceOf(IllegalStateException.class))); } @Test - public void combineFutures6_completed() throws Exception { - final CompletionStage future = combineFutures( - completedFuture("a"), - completedFuture("b"), - completedFuture("c"), - completedFuture("d"), - completedFuture("e"), - completedFuture("f"), - (a, b, c, d, e, f) -> completedFuture(a + b + c + d + e + f)); + public void combineFutures6_completed() { + final CompletionStage future = + combineFutures( + completedFuture("a"), + completedFuture("b"), + completedFuture("c"), + completedFuture("d"), + completedFuture("e"), + completedFuture("f"), + (a, b, c, d, e, f) -> completedFuture(a + b + c + d + e + f)); assertThat(future, completesTo("abcdef")); } @Test - public void combineFutures6_incomplete() throws Exception { - final CompletionStage future = combineFutures( - completedFuture("a"), - completedFuture("b"), - completedFuture("c"), - completedFuture("d"), - completedFuture("e"), - incompleteFuture(), - (a, b, c, d, e, f) -> completedFuture(a + b + c + d + e + f)); + public void combineFutures6_incomplete() { + final CompletionStage future = + combineFutures( + completedFuture("a"), + completedFuture("b"), + completedFuture("c"), + completedFuture("d"), + completedFuture("e"), + incompleteFuture(), + (a, b, c, d, e, f) -> completedFuture(a + b + c + d + e + f)); - exception.expect(isA(IllegalStateException.class)); - getCompleted(future); + assertThrows(IllegalStateException.class, () -> getCompleted(future)); } @Test - public void combineFutures6_exceptional() throws Exception { - final CompletionStage future = combineFutures( - completedFuture("a"), - completedFuture("b"), - completedFuture("c"), - completedFuture("d"), - completedFuture("e"), - exceptionallyCompletedFuture(new IllegalStateException()), - (a, b, c, d, e, f) -> completedFuture(a + b + c + d + e + f)); + public void combineFutures6_exceptional() { + final CompletionStage future = + combineFutures( + completedFuture("a"), + completedFuture("b"), + completedFuture("c"), + completedFuture("d"), + completedFuture("e"), + exceptionallyCompletedFuture(new IllegalStateException()), + (a, b, c, d, e, f) -> completedFuture(a + b + c + d + e + f)); - exception.expectCause(isA(IllegalStateException.class)); - getCompleted(future); + final CompletionException e = + assertThrows(CompletionException.class, () -> getCompleted(future)); + assertThat(e.getCause(), is(instanceOf(IllegalStateException.class))); } @Test public void combineVararg_success() { final CompletionStage first = completedFuture("a"); final CompletionStage second = completedFuture("b"); - final CompletionStage future = combine(combined -> combined.get(first) + combined.get(second), first, second); + final CompletionStage future = + combine(combined -> combined.get(first) + combined.get(second), first, second); assertEquals("ab", getCompleted(future)); } @@ -935,7 +954,8 @@ public void combineVararg_success() { public void combineList_success() { final CompletionStage first = completedFuture("a"); final CompletionStage second = completedFuture("b"); - final CompletionStage future = combine(combined -> combined.get(first) + combined.get(second), asList(first, second)); + final CompletionStage future = + combine(combined -> combined.get(first) + combined.get(second), asList(first, second)); assertEquals("ab", getCompleted(future)); } @@ -945,27 +965,33 @@ public void combineList_nullValues_success() { final CompletionStage first = completedFuture(null); final CompletionStage future = combine(combined -> combined.get(first), first); - assertEquals(null, getCompleted(future)); + assertNull(getCompleted(future)); } @Test public void combineVararg_exceptional() { final CompletionStage first = completedFuture("a"); - final CompletionStage second = exceptionallyCompletedFuture(new IllegalStateException()); - final CompletionStage future = combine(combined -> combined.get(first) + combined.get(second), first, second); + final CompletionStage second = + exceptionallyCompletedFuture(new IllegalStateException()); + final CompletionStage future = + combine(combined -> combined.get(first) + combined.get(second), first, second); - exception.expectCause(isA(IllegalStateException.class)); - getCompleted(future); + final CompletionException e = + assertThrows(CompletionException.class, () -> getCompleted(future)); + assertThat(e.getCause(), is(instanceOf(IllegalStateException.class))); } @Test public void combineList_exceptional() { final CompletionStage first = completedFuture("a"); - final CompletionStage second = exceptionallyCompletedFuture(new IllegalStateException()); - final CompletionStage future = combine(combined -> combined.get(first) + combined.get(second), asList(first, second)); + final CompletionStage second = + exceptionallyCompletedFuture(new IllegalStateException()); + final CompletionStage future = + combine(combined -> combined.get(first) + combined.get(second), asList(first, second)); - exception.expectCause(isA(IllegalStateException.class)); - getCompleted(future); + final CompletionException e = + assertThrows(CompletionException.class, () -> getCompleted(future)); + assertThat(e.getCause(), is(instanceOf(IllegalStateException.class))); } @Test @@ -973,10 +999,15 @@ public void combineVararg_misuse() { final CompletionStage first = completedFuture("a"); final CompletionStage second = completedFuture("b"); final CompletionStage third = completedFuture("c"); - final CompletionStage future = combine(combined -> combined.get(first) + combined.get(second) + combined.get(third), first, second); + final CompletionStage future = + combine( + combined -> combined.get(first) + combined.get(second) + combined.get(third), + first, + second); - exception.expectCause(isA(IllegalArgumentException.class)); - getCompleted(future); + final CompletionException e = + assertThrows(CompletionException.class, () -> getCompleted(future)); + assertThat(e.getCause(), is(instanceOf(IllegalArgumentException.class))); } @Test @@ -984,24 +1015,27 @@ public void combineList_misuse() { final CompletionStage first = completedFuture("a"); final CompletionStage second = completedFuture("b"); final CompletionStage third = completedFuture("c"); - final CompletionStage future = combine(combined -> combined.get(first) + combined.get(second) + combined.get(third), asList(first, second)); + final CompletionStage future = + combine( + combined -> combined.get(first) + combined.get(second) + combined.get(third), + asList(first, second)); - exception.expectCause(isA(IllegalArgumentException.class)); - getCompleted(future); + final CompletionException e = + assertThrows(CompletionException.class, () -> getCompleted(future)); + assertThat(e.getCause(), is(instanceOf(IllegalArgumentException.class))); } @Test public void ctor_preventInstantiation() throws Exception { - exception.expect(both(isA(InvocationTargetException.class)) - .and(hasProperty("cause", isA(IllegalAccessError.class)))); - final Constructor ctor = CompletableFutures.class.getDeclaredConstructor(); ctor.setAccessible(true); - ctor.newInstance(); + final InvocationTargetException e = + assertThrows(InvocationTargetException.class, ctor::newInstance); + assertThat(e.getCause(), is(instanceOf(IllegalAccessError.class))); } @Test - public void poll_done() throws Exception { + public void poll_done() { final Supplier> supplier = () -> Optional.of("done"); final CompletableFuture future = poll(supplier, Duration.ofMillis(2), executor); @@ -1010,7 +1044,7 @@ public void poll_done() throws Exception { } @Test - public void poll_twice() throws Exception { + public void poll_twice() { final List> results = asList(Optional.empty(), Optional.of("done")); final Supplier> supplier = results.iterator()::next; final CompletableFuture future = poll(supplier, Duration.ofMillis(2), executor); @@ -1028,23 +1062,26 @@ public void poll_taskReturnsNull() throws Exception { final CompletableFuture future = poll(supplier, Duration.ofMillis(2), executor); executor.runNextPendingCommand(); - exception.expectCause(isA(NullPointerException.class)); - future.get(); + final ExecutionException e = assertThrows(ExecutionException.class, future::get); + assertThat(e.getCause(), is(instanceOf(NullPointerException.class))); } @Test public void poll_taskThrows() throws Exception { final RuntimeException ex = new RuntimeException("boom"); - final Supplier> supplier = () -> {throw ex;}; + final Supplier> supplier = + () -> { + throw ex; + }; final CompletableFuture future = poll(supplier, Duration.ofMillis(2), executor); executor.runNextPendingCommand(); - exception.expectCause(is(ex)); - future.get(); + final ExecutionException e = assertThrows(ExecutionException.class, future::get); + assertThat(e.getCause(), is(equalTo(ex))); } @Test - public void poll_scheduled() throws Exception { + public void poll_scheduled() { final ScheduledExecutorService executor = mock(ScheduledExecutorService.class); final Supplier> supplier = () -> Optional.of("hello"); poll(supplier, Duration.ofMillis(2), executor); @@ -1054,7 +1091,7 @@ public void poll_scheduled() throws Exception { @Test @SuppressWarnings("unchecked") - public void poll_resultFutureCanceled() throws Exception { + public void poll_resultFutureCanceled() { final ScheduledFuture scheduledFuture = mock(ScheduledFuture.class); final ScheduledExecutorService executor = mock(ScheduledExecutorService.class); when(executor.scheduleAtFixedRate(any(), anyLong(), anyLong(), any())) @@ -1067,7 +1104,7 @@ public void poll_resultFutureCanceled() throws Exception { } @Test - public void poll_notRunningAfterCancel() throws Exception { + public void poll_notRunningAfterCancel() { final CompletableFuture future = poll(Optional::empty, Duration.ofMillis(2), executor); future.cancel(true); @@ -1085,7 +1122,8 @@ private static Matcher> completesTo(final T expected) { } private static Matcher> completesTo(final Matcher expected) { - return new CustomTypeSafeMatcher>("completes to " + String.valueOf(expected)) { + return new CustomTypeSafeMatcher>( + "completes to " + String.valueOf(expected)) { @Override protected boolean matchesSafely(CompletionStage item) { try { @@ -1099,7 +1137,7 @@ protected boolean matchesSafely(CompletionStage item) { } private static Map asMap(final List keys, final List input) { - Map map = new HashMap(); + Map map = new HashMap<>(); for (int i = 0; i < keys.size(); i++) { map.put(keys.get(i), input.get(i)); } @@ -1115,5 +1153,4 @@ public T join() { return super.join(); } } - } diff --git a/src/test/java/com/spotify/futures/ConcurrencyReducerTest.java b/src/test/java/com/spotify/futures/ConcurrencyReducerTest.java index db11b83..0501290 100644 --- a/src/test/java/com/spotify/futures/ConcurrencyReducerTest.java +++ b/src/test/java/com/spotify/futures/ConcurrencyReducerTest.java @@ -2,7 +2,7 @@ * -\-\- * completable-futures * -- - * Copyright (C) 2016 - 2020 Spotify AB + * Copyright (C) 2016 - 2023 Spotify AB * -- * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,9 +22,10 @@ import static com.spotify.futures.CompletableFutures.getException; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.instanceOf; -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.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.ArrayList; import java.util.List; @@ -34,24 +35,24 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Supplier; -import org.junit.Test; +import org.junit.jupiter.api.Test; public class ConcurrencyReducerTest { - @Test(expected = IllegalArgumentException.class) - public void testTooLowConcurrency() throws Exception { - ConcurrencyReducer.create(0, 10); + @Test + public void testTooLowConcurrency() { + assertThrows(IllegalArgumentException.class, () -> ConcurrencyReducer.create(0, 10)); } - @Test(expected = IllegalArgumentException.class) - public void testTooLowQueueSize() throws Exception { - ConcurrencyReducer.create(10, 0); + @Test + public void testTooLowQueueSize() { + assertThrows(IllegalArgumentException.class, () -> ConcurrencyReducer.create(10, 0)); } - @Test(expected = NullPointerException.class) - public void testNullJob() throws Exception { + @Test + public void testNullJob() { final ConcurrencyReducer limiter = ConcurrencyReducer.create(1, 10); - limiter.add(null); + assertThrows(NullPointerException.class, () -> limiter.add(null)); } @Test() @@ -65,7 +66,7 @@ public void testVoidJob() { } @Test - public void testJobReturnsNull() throws Exception { + public void testJobReturnsNull() { final ConcurrencyReducer limiter = ConcurrencyReducer.create(1, 10); final CompletableFuture response = limiter.add(job(null)); assertTrue(response.isDone()); @@ -74,10 +75,11 @@ public void testJobReturnsNull() throws Exception { } @Test - public void testJobThrows() throws Exception { + public void testJobThrows() { final ConcurrencyReducer limiter = ConcurrencyReducer.create(1, 10); final CompletableFuture response = - limiter.add(() -> { + limiter.add( + () -> { throw new IllegalStateException(); }); @@ -87,10 +89,11 @@ public void testJobThrows() throws Exception { } @Test - public void testJobReturnsFailure() throws Exception { + public void testJobReturnsFailure() { final ConcurrencyReducer limiter = ConcurrencyReducer.create(1, 10); final CompletionStage response = - limiter.add(job(CompletableFutures.exceptionallyCompletedFuture(new IllegalStateException()))); + limiter.add( + job(CompletableFutures.exceptionallyCompletedFuture(new IllegalStateException()))); assertTrue(response.toCompletableFuture().isDone()); final Throwable exception = getException(response); @@ -98,7 +101,7 @@ public void testJobReturnsFailure() throws Exception { } @Test - public void testCancellation() throws Exception { + public void testCancellation() { final ConcurrencyReducer limiter = ConcurrencyReducer.create(2, 10); final CompletableFuture request1 = new CompletableFuture<>(); final CompletableFuture request2 = new CompletableFuture<>(); @@ -144,7 +147,7 @@ public void testCancellation() throws Exception { } @Test - public void testSimple() throws Exception { + public void testSimple() { final ConcurrencyReducer limiter = ConcurrencyReducer.create(2, 10); final CompletableFuture request1 = new CompletableFuture<>(); final CompletableFuture request2 = new CompletableFuture<>(); @@ -181,7 +184,7 @@ public void testSimple() throws Exception { } @Test - public void testLongRunning() throws Exception { + public void testLongRunning() { final AtomicInteger activeCount = new AtomicInteger(); final AtomicInteger maxCount = new AtomicInteger(); final int queueSize = 11; @@ -213,7 +216,7 @@ public void testLongRunning() throws Exception { } @Test - public void testQueueSize() throws Exception { + public void testQueueSize() { final ConcurrencyReducer limiter = ConcurrencyReducer.create(10, 10); for (int i = 0; i < 20; i++) { limiter.add(job(new CompletableFuture<>())); @@ -226,7 +229,7 @@ public void testQueueSize() throws Exception { } @Test - public void testQueueSizeCounter() throws Exception { + public void testQueueSizeCounter() { final CompletableFuture future = new CompletableFuture<>(); final ConcurrencyReducer limiter = ConcurrencyReducer.create(10, 10); @@ -264,7 +267,7 @@ public CountingJob(Supplier activeCount, AtomicInteger maxCount) { } @Override - public CompletionStage call() throws Exception { + public CompletionStage call() { if (activeCount.get() > maxCount.get()) { maxCount.set(activeCount.get()); } diff --git a/src/test/java/com/spotify/futures/FunctionsTest.java b/src/test/java/com/spotify/futures/FunctionsTest.java index 7465503..ea9ddfe 100644 --- a/src/test/java/com/spotify/futures/FunctionsTest.java +++ b/src/test/java/com/spotify/futures/FunctionsTest.java @@ -2,7 +2,7 @@ * -\-\- * completable-futures * -- - * Copyright (C) 2016 - 2020 Spotify AB + * Copyright (C) 2016 - 2023 Spotify AB * -- * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,65 +21,59 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.junit.jupiter.api.Test; public class FunctionsTest { - @Rule - public ExpectedException exception = ExpectedException.none(); - @Test public void function3_andThen() { final Function3 f = (a, b, c) -> 1; - assertThat(f.andThen(i -> i+1).apply("", "", ""), is(2)); + assertThat(f.andThen(i -> i + 1).apply("", "", ""), is(2)); } @Test public void function3_andThenNull() { final Function3 f = (a, b, c) -> 1; - exception.expect(NullPointerException.class); - f.andThen(null); + assertThrows(NullPointerException.class, () -> f.andThen(null)); } @Test public void function4_andThen() { final Function4 f = (a, b, c, d) -> 1; - assertThat(f.andThen(i -> i+1).apply("", "", "", ""), is(2)); + assertThat(f.andThen(i -> i + 1).apply("", "", "", ""), is(2)); } @Test public void function4_andThenNull() { final Function4 f = (a, b, c, d) -> 1; - exception.expect(NullPointerException.class); - f.andThen(null); + assertThrows(NullPointerException.class, () -> f.andThen(null)); } @Test public void function5_andThen() { final Function5 f = (a, b, c, d, e) -> 1; - assertThat(f.andThen(i -> i+1).apply("", "", "", "", ""), is(2)); + assertThat(f.andThen(i -> i + 1).apply("", "", "", "", ""), is(2)); } @Test public void function5_andThenNull() { final Function5 f = (a, b, c, d, e) -> 1; - exception.expect(NullPointerException.class); - f.andThen(null); + assertThrows(NullPointerException.class, () -> f.andThen(null)); } @Test public void function6_andThen() { - final Function6 ff = (a, b, c, d, e, f) -> 1; - assertThat(ff.andThen(i -> i+1).apply("", "", "", "", "", ""), is(2)); + final Function6 ff = + (a, b, c, d, e, f) -> 1; + assertThat(ff.andThen(i -> i + 1).apply("", "", "", "", "", ""), is(2)); } @Test public void function6_andThenNull() { - final Function6 ff = (a, b, c, d, e, f) -> 1; - exception.expect(NullPointerException.class); - ff.andThen(null); + final Function6 ff = + (a, b, c, d, e, f) -> 1; + assertThrows(NullPointerException.class, () -> ff.andThen(null)); } } diff --git a/src/test/java/com/spotify/futures/jmh/AllAsListBenchmark.java b/src/test/java/com/spotify/futures/jmh/AllAsListBenchmark.java index e9eee03..5a1eba6 100644 --- a/src/test/java/com/spotify/futures/jmh/AllAsListBenchmark.java +++ b/src/test/java/com/spotify/futures/jmh/AllAsListBenchmark.java @@ -42,7 +42,6 @@ import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; -@SuppressWarnings("unused") @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS) public class AllAsListBenchmark {