Skip to content

Commit

Permalink
[bluetooth] Try to make tests more stable (openhab#9304)
Browse files Browse the repository at this point in the history
Signed-off-by: Fabian Wolter <[email protected]>
  • Loading branch information
fwolter authored Dec 9, 2020
1 parent 34a9c54 commit aba5e2c
Showing 1 changed file with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
*/
class RetryFutureTest {

private static final int TIMEOUT_MS = 1000;
private ScheduledExecutorService scheduler;

@BeforeEach
Expand All @@ -52,17 +53,17 @@ public void cleanup() {
}

@Test
void callWithRetryNormal() throws InterruptedException {
void callWithRetryNormal() {
Future<String> retryFuture = RetryFuture.callWithRetry(() -> "test", scheduler);
try {
assertEquals("test", retryFuture.get(100, TimeUnit.MILLISECONDS));
assertEquals("test", retryFuture.get(TIMEOUT_MS, TimeUnit.MILLISECONDS));
} catch (InterruptedException | ExecutionException | TimeoutException e) {
fail(e);
}
}

@Test
void callWithRetry1() throws InterruptedException {
void callWithRetry1() {
AtomicInteger visitCount = new AtomicInteger();
Future<String> retryFuture = RetryFuture.callWithRetry(() -> {
if (visitCount.getAndIncrement() == 0) {
Expand All @@ -71,14 +72,14 @@ void callWithRetry1() throws InterruptedException {
return "test";
}, scheduler);
try {
assertEquals("test", retryFuture.get(100, TimeUnit.MILLISECONDS));
assertEquals("test", retryFuture.get(TIMEOUT_MS, TimeUnit.MILLISECONDS));
} catch (InterruptedException | ExecutionException | TimeoutException e) {
fail(e);
}
}

@Test
void composeWithRetryNormal() throws InterruptedException {
void composeWithRetryNormal() {
CompletableFuture<?> composedFuture = new CompletableFuture<>();

Future<?> retryFuture = RetryFuture.composeWithRetry(() -> {
Expand All @@ -87,15 +88,15 @@ void composeWithRetryNormal() throws InterruptedException {
}, scheduler);

try {
retryFuture.get(100, TimeUnit.MILLISECONDS);
retryFuture.get(TIMEOUT_MS, TimeUnit.MILLISECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
fail(e);
}
assertTrue(composedFuture.isDone());
}

@Test
void composeWithRetryThrow() throws InterruptedException {
void composeWithRetryThrow() {
CompletableFuture<?> composedFuture = new CompletableFuture<>();

Future<?> retryFuture = RetryFuture.composeWithRetry(() -> {
Expand All @@ -104,7 +105,7 @@ void composeWithRetryThrow() throws InterruptedException {
}, scheduler);

try {
retryFuture.get(100, TimeUnit.MILLISECONDS);
retryFuture.get(TIMEOUT_MS, TimeUnit.MILLISECONDS);
} catch (InterruptedException | TimeoutException e) {
fail(e);
} catch (ExecutionException ex) {
Expand All @@ -126,7 +127,7 @@ void composeWithRetry1() throws InterruptedException {
}, scheduler);

try {
assertEquals("test", retryFuture.get(100, TimeUnit.MILLISECONDS));
assertEquals("test", retryFuture.get(TIMEOUT_MS, TimeUnit.MILLISECONDS));
} catch (InterruptedException | ExecutionException | TimeoutException e) {
fail(e);
}
Expand All @@ -135,7 +136,7 @@ void composeWithRetry1() throws InterruptedException {
}

@Test
void composeWithRetry1Cancel() throws InterruptedException {
void composeWithRetry1Cancel() {
CountDownLatch latch = new CountDownLatch(1);
AtomicInteger visitCount = new AtomicInteger();
CompletableFuture<String> composedFuture = new CompletableFuture<>();
Expand All @@ -148,7 +149,7 @@ void composeWithRetry1Cancel() throws InterruptedException {
}, scheduler);

try {
if (!latch.await(100, TimeUnit.MILLISECONDS)) {
if (!latch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
fail("Timeout while waiting for latch");
}
Thread.sleep(1);
Expand Down

0 comments on commit aba5e2c

Please sign in to comment.