Skip to content

Commit

Permalink
Replace deprecated Awaitable.await(long, TimeUnit) on Awaitable.await…
Browse files Browse the repository at this point in the history
…(Duration) (#8334)
  • Loading branch information
Captain1653 committed Feb 14, 2024
1 parent cf23fd4 commit eea7460
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2021 Oracle and/or its affiliates.
* Copyright (c) 2020, 2024 Oracle and/or its affiliates.
*
* 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 @@ -35,6 +35,7 @@

import org.junit.jupiter.api.Test;

import static java.time.Duration.ofMillis;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
Expand All @@ -59,7 +60,7 @@ void sameInstanceCallbacks() throws ExecutionException, InterruptedException {

future.thenAccept(whenCompleteFuture::complete);

future.await(100, TimeUnit.MILLISECONDS);
future.await(ofMillis(100));

assertThat("Peek needs to be invoked at await!", peekFuture.isDone(), is(true));
assertThat(peekFuture.get(), is(equalTo("1")));
Expand All @@ -79,7 +80,7 @@ void lazyCSConversion() throws ExecutionException, InterruptedException {

single.whenComplete((s, throwable) -> whenCompleteFuture.complete(s));

single.await(100, TimeUnit.MILLISECONDS);
single.await(ofMillis(100));

assertThat("Peek needs to be invoked at await!", peekFuture.isDone(), is(true));
assertThat(peekFuture.get(), is(equalTo("1")));
Expand Down Expand Up @@ -115,7 +116,7 @@ void callbackOrderSingle() {
})
.whenComplete((s, throwable) -> result.add(7));

awaitable.await(SAFE_WAIT_MILLIS, TimeUnit.MILLISECONDS);
awaitable.await(ofMillis(SAFE_WAIT_MILLIS));
assertThat(result, equalTo(IntStream.rangeClosed(1, 7).boxed().collect(Collectors.toList())));
}

Expand Down Expand Up @@ -148,7 +149,7 @@ void callbackOrderMulti() {
})
.whenComplete((s, throwable) -> result.add(8));

awaitable.await(SAFE_WAIT_MILLIS, TimeUnit.MILLISECONDS);
awaitable.await(ofMillis(SAFE_WAIT_MILLIS));
assertThat(result, equalTo(IntStream.rangeClosed(1, 8).boxed().collect(Collectors.toList())));
}

Expand Down Expand Up @@ -195,7 +196,7 @@ void forEachAwaitTimeout() {
AtomicLong sum = new AtomicLong();
testMulti()
.forEach(sum::addAndGet)
.await(SAFE_WAIT_MILLIS, TimeUnit.MILLISECONDS);
.await(ofMillis(SAFE_WAIT_MILLIS));
assertThat(sum.get(), equalTo(EXPECTED_SUM));
}

Expand All @@ -218,7 +219,7 @@ void forEachCancel() throws Exception {
void forEachAwaitTimeoutNegative() {
assertThrows(CompletionException.class, () -> testMulti()
.forEach(TestConsumer.noop())
.await(10, TimeUnit.MILLISECONDS));
.await(ofMillis(10)));
}

@Test
Expand All @@ -228,12 +229,12 @@ void singleAwait() {

@Test
void singleAwaitTimeout() {
assertThat(testSingle().await(SAFE_WAIT_MILLIS, TimeUnit.MILLISECONDS), equalTo(EXPECTED_SUM));
assertThat(testSingle().await(ofMillis(SAFE_WAIT_MILLIS)), equalTo(EXPECTED_SUM));
}

@Test
void singleAwaitTimeoutNegative() {
assertThrows(CompletionException.class, () -> testSingle().await(10, TimeUnit.MILLISECONDS));
assertThrows(CompletionException.class, () -> testSingle().await(ofMillis(10)));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2021 Oracle and/or its affiliates.
* Copyright (c) 2017, 2024 Oracle and/or its affiliates.
*
* 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 @@ -29,6 +29,7 @@

import org.junit.jupiter.api.Test;

import static java.time.Duration.ofSeconds;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
Expand Down Expand Up @@ -280,7 +281,7 @@ void flatMapping() {
.forEach(unused -> cnt.incrementAndGet());

try {
promise.await(10, TimeUnit.SECONDS);
promise.await(ofSeconds(10));
assertThat(cnt.get(), is(equalTo(STREAM_SIZE)));
} finally {
exec.shutdown();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
* Copyright (c) 2022, 2024 Oracle and/or its affiliates.
*
* 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 @@ -28,6 +28,7 @@
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

import static java.time.Duration.ofMillis;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.is;

Expand Down Expand Up @@ -58,7 +59,7 @@ void multiIgnorePeek() {
Multi.just(1, 2, 3)
.peek(result::add)
.ignoreElements()
.await(200, TimeUnit.MILLISECONDS);
.await(ofMillis(200));

assertThat(result, Matchers.contains(1, 2, 3));
}
Expand All @@ -69,7 +70,7 @@ void singleIgnorePeek() {
Single.just(3)
.peek(result::set)
.ignoreElement()
.await(200, TimeUnit.MILLISECONDS);
.await(ofMillis(200));

assertThat(result.get(), Matchers.is(3));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
* Copyright (c) 2021, 2024 Oracle and/or its affiliates.
*
* 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 @@ -29,6 +29,8 @@
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;

import static java.time.Duration.ofMillis;
import static java.time.Duration.ofSeconds;
import static org.hamcrest.MatcherAssert.assertThat;

public class MultiFlatMapCompletionStageTest {
Expand All @@ -50,7 +52,7 @@ void singleThread() {
List<Integer> result = Multi.just(1, 2, 3, 4)
.flatMapCompletionStage(CompletableFuture::completedFuture)
.collectList()
.await(100, TimeUnit.MILLISECONDS);
.await(ofMillis(100));

assertThat(result, Matchers.contains(1, 2, 3, 4));
}
Expand All @@ -62,7 +64,7 @@ void voidCs() {
.map(Throwable.class::cast)
.onErrorResume(Function.identity())
.first()
.await(100, TimeUnit.MILLISECONDS);
.await(ofMillis(100));

assertThat(result, Matchers.instanceOf(NullPointerException.class));
}
Expand Down Expand Up @@ -95,7 +97,7 @@ void multiThread() {
}
}, exec))
.collectList()
.await(2, TimeUnit.SECONDS);
.await(ofSeconds(2));

assertThat(result, Matchers.contains(10, 0, 8, 1));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2023 Oracle and/or its affiliates.
* Copyright (c) 2020, 2024 Oracle and/or its affiliates.
*
* 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,8 +15,6 @@
*/
package io.helidon.common.reactive;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
Expand All @@ -31,6 +29,7 @@
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;

import static java.time.Duration.ofMillis;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasSize;
Expand Down Expand Up @@ -311,7 +310,7 @@ public void multi() throws ExecutionException, InterruptedException {
.flatMap(MultiFlatMapPublisherTest::asyncFlowPublisher, MAX_CONCURRENCY, false, PREFETCH)
.distinct()
.collectList()
.await(800, TimeUnit.MILLISECONDS), hasSize(EXPECTED_EMISSION_COUNT));
.await(ofMillis(800)), hasSize(EXPECTED_EMISSION_COUNT));
}

private static Flow.Publisher<? extends String> asyncFlowPublisher(Integer i) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2021 Oracle and/or its affiliates.
* Copyright (c) 2017, 2024 Oracle and/or its affiliates.
*
* 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 @@ -44,6 +44,7 @@
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import static java.time.Duration.ofSeconds;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
Expand All @@ -70,7 +71,7 @@ void testReadAllData() {
// assert
byte[] bytes = Multi.create(publisher)
.collect(new BufferCollector())
.await(5, TimeUnit.SECONDS);
.await(ofSeconds(5));

assertThat(bytes.length, is(TEST_DATA_SIZE));
assertByteSequence(bytes);
Expand All @@ -92,7 +93,7 @@ void testChunky() throws InterruptedException {
// assert
byte[] bytes = Multi.create(publisher)
.collect(new BufferCollector())
.await(5, TimeUnit.SECONDS);
.await(ofSeconds(5));

assertThat(bytes.length, is(TEST_DATA_SIZE));
assertByteSequence(bytes);
Expand All @@ -119,7 +120,7 @@ void testChunkyWithCustomExecutor() {
// assert
byte[] bytes = Multi.create(publisher)
.collect(new BufferCollector())
.await(5, TimeUnit.SECONDS);
.await(ofSeconds(5));

assertThat(bytes.length, is(TEST_DATA_SIZE));
assertByteSequence(bytes);
Expand Down Expand Up @@ -148,7 +149,7 @@ void testChunkyNoDelay() {
// assert
byte[] bytes = Multi.create(publisher)
.collect(new BufferCollector())
.await(5, TimeUnit.SECONDS);
.await(ofSeconds(5));

assertThat(bytes.length, is(TEST_DATA_SIZE));
assertByteSequence(bytes);
Expand All @@ -170,7 +171,7 @@ void testOnClosedChannel() {
try {
Multi.create(publisher)
.collect(new BufferCollector())
.await(5, TimeUnit.SECONDS);
.await(ofSeconds(5));
fail("Did not throw expected CompletionException!");
} catch (CompletionException e) {
assertThat(e.getCause(), instanceOf(ClosedChannelException.class));
Expand Down Expand Up @@ -201,7 +202,7 @@ void testOnClosedInProgress() throws Exception {
// immediately close the channel, so we fail reading
pc.close();

CompletionException c = assertThrows(CompletionException.class, () -> data.await(5, TimeUnit.SECONDS));
CompletionException c = assertThrows(CompletionException.class, () -> data.await(ofSeconds(5)));
assertThat(c.getCause(), instanceOf(ClosedChannelException.class));

MultiFromByteChannel multi = (MultiFromByteChannel) publisher;
Expand Down Expand Up @@ -272,7 +273,7 @@ void testNegativeDelay() {
try {
Multi.create(publisher)
.collect(new BufferCollector())
.await(5, TimeUnit.SECONDS);
.await(ofSeconds(5));

fail("Did not throw expected CompletionException!");
} catch (CompletionException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2021 Oracle and/or its affiliates.
* Copyright (c) 2020, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,7 +26,6 @@
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;

Expand All @@ -36,6 +35,8 @@
import org.junit.jupiter.api.RepetitionInfo;
import org.junit.jupiter.api.Test;

import static java.time.Duration.ofMillis;
import static java.time.Duration.ofSeconds;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

Expand Down Expand Up @@ -68,7 +69,7 @@ public void testInputStream() {
return list;
})
.collectList()
.await(100, TimeUnit.MILLISECONDS);
.await(ofMillis(100));
assertThat(result, equalTo(toList(initialArray)));
}

Expand Down Expand Up @@ -111,7 +112,7 @@ private void longString(Function<InputStream, Multi<ByteBuffer>> pubCreator) {
.map(CharBuffer::toString)
.map(CharSequence.class::cast)
.collectStream(Collectors.joining())
.await(100, TimeUnit.SECONDS);
.await(ofSeconds(100));

assertThat(result, equalTo(expected));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2021 Oracle and/or its affiliates.
* Copyright (c) 2020, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,7 +20,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletionException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
Expand All @@ -32,6 +31,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static java.time.Duration.ofMillis;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -133,7 +133,7 @@ void testLogWithError() throws IOException {


assertThat(
assertThrows(CompletionException.class, () -> single.await(300, TimeUnit.MILLISECONDS))
assertThrows(CompletionException.class, () -> single.await(ofMillis(300)))
.getCause()
.getMessage(),
equalTo("BOOM!"));
Expand Down

0 comments on commit eea7460

Please sign in to comment.