[bluetooth] Try to make tests more stable (#9304)

Signed-off-by: Fabian Wolter <github@fabian-wolter.de>
This commit is contained in:
Fabian Wolter 2020-12-09 15:07:55 +01:00 committed by GitHub
parent 34a9c5482a
commit aba5e2c996
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -35,6 +35,7 @@ import org.openhab.core.common.NamedThreadFactory;
*/ */
class RetryFutureTest { class RetryFutureTest {
private static final int TIMEOUT_MS = 1000;
private ScheduledExecutorService scheduler; private ScheduledExecutorService scheduler;
@BeforeEach @BeforeEach
@ -52,17 +53,17 @@ class RetryFutureTest {
} }
@Test @Test
void callWithRetryNormal() throws InterruptedException { void callWithRetryNormal() {
Future<String> retryFuture = RetryFuture.callWithRetry(() -> "test", scheduler); Future<String> retryFuture = RetryFuture.callWithRetry(() -> "test", scheduler);
try { try {
assertEquals("test", retryFuture.get(100, TimeUnit.MILLISECONDS)); assertEquals("test", retryFuture.get(TIMEOUT_MS, TimeUnit.MILLISECONDS));
} catch (InterruptedException | ExecutionException | TimeoutException e) { } catch (InterruptedException | ExecutionException | TimeoutException e) {
fail(e); fail(e);
} }
} }
@Test @Test
void callWithRetry1() throws InterruptedException { void callWithRetry1() {
AtomicInteger visitCount = new AtomicInteger(); AtomicInteger visitCount = new AtomicInteger();
Future<String> retryFuture = RetryFuture.callWithRetry(() -> { Future<String> retryFuture = RetryFuture.callWithRetry(() -> {
if (visitCount.getAndIncrement() == 0) { if (visitCount.getAndIncrement() == 0) {
@ -71,14 +72,14 @@ class RetryFutureTest {
return "test"; return "test";
}, scheduler); }, scheduler);
try { try {
assertEquals("test", retryFuture.get(100, TimeUnit.MILLISECONDS)); assertEquals("test", retryFuture.get(TIMEOUT_MS, TimeUnit.MILLISECONDS));
} catch (InterruptedException | ExecutionException | TimeoutException e) { } catch (InterruptedException | ExecutionException | TimeoutException e) {
fail(e); fail(e);
} }
} }
@Test @Test
void composeWithRetryNormal() throws InterruptedException { void composeWithRetryNormal() {
CompletableFuture<?> composedFuture = new CompletableFuture<>(); CompletableFuture<?> composedFuture = new CompletableFuture<>();
Future<?> retryFuture = RetryFuture.composeWithRetry(() -> { Future<?> retryFuture = RetryFuture.composeWithRetry(() -> {
@ -87,7 +88,7 @@ class RetryFutureTest {
}, scheduler); }, scheduler);
try { try {
retryFuture.get(100, TimeUnit.MILLISECONDS); retryFuture.get(TIMEOUT_MS, TimeUnit.MILLISECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) { } catch (InterruptedException | ExecutionException | TimeoutException e) {
fail(e); fail(e);
} }
@ -95,7 +96,7 @@ class RetryFutureTest {
} }
@Test @Test
void composeWithRetryThrow() throws InterruptedException { void composeWithRetryThrow() {
CompletableFuture<?> composedFuture = new CompletableFuture<>(); CompletableFuture<?> composedFuture = new CompletableFuture<>();
Future<?> retryFuture = RetryFuture.composeWithRetry(() -> { Future<?> retryFuture = RetryFuture.composeWithRetry(() -> {
@ -104,7 +105,7 @@ class RetryFutureTest {
}, scheduler); }, scheduler);
try { try {
retryFuture.get(100, TimeUnit.MILLISECONDS); retryFuture.get(TIMEOUT_MS, TimeUnit.MILLISECONDS);
} catch (InterruptedException | TimeoutException e) { } catch (InterruptedException | TimeoutException e) {
fail(e); fail(e);
} catch (ExecutionException ex) { } catch (ExecutionException ex) {
@ -126,7 +127,7 @@ class RetryFutureTest {
}, scheduler); }, scheduler);
try { try {
assertEquals("test", retryFuture.get(100, TimeUnit.MILLISECONDS)); assertEquals("test", retryFuture.get(TIMEOUT_MS, TimeUnit.MILLISECONDS));
} catch (InterruptedException | ExecutionException | TimeoutException e) { } catch (InterruptedException | ExecutionException | TimeoutException e) {
fail(e); fail(e);
} }
@ -135,7 +136,7 @@ class RetryFutureTest {
} }
@Test @Test
void composeWithRetry1Cancel() throws InterruptedException { void composeWithRetry1Cancel() {
CountDownLatch latch = new CountDownLatch(1); CountDownLatch latch = new CountDownLatch(1);
AtomicInteger visitCount = new AtomicInteger(); AtomicInteger visitCount = new AtomicInteger();
CompletableFuture<String> composedFuture = new CompletableFuture<>(); CompletableFuture<String> composedFuture = new CompletableFuture<>();
@ -148,7 +149,7 @@ class RetryFutureTest {
}, scheduler); }, scheduler);
try { try {
if (!latch.await(100, TimeUnit.MILLISECONDS)) { if (!latch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
fail("Timeout while waiting for latch"); fail("Timeout while waiting for latch");
} }
Thread.sleep(1); Thread.sleep(1);