Skip to content

Commit

Permalink
Updated intermittently failing test replacing a busy loop with code t…
Browse files Browse the repository at this point in the history
…hat waits on an internal future for more robustness. (#5033)
  • Loading branch information
spericas authored Oct 3, 2022
1 parent 54285db commit 7121859
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ private void scheduleHalf() {
}, delayMillis, TimeUnit.MILLISECONDS));
}

ScheduledFuture<Boolean> schedule() {
return schedule.get();
}

private void resetCounters() {
results.reset();
successCounter.set(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
package io.helidon.nima.faulttolerance;

import java.time.Duration;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.junit.jupiter.api.Test;

Expand All @@ -26,10 +30,10 @@

class CircuitBreakerTest {

private static final long BUSY_WAIT_SLEEP = 100;
private static final long WAIT_TIMEOUT_MILLIS = 2000;

@Test
void testCircuitBreaker() throws InterruptedException {
void testCircuitBreaker() throws InterruptedException, ExecutionException, TimeoutException {
CircuitBreaker breaker = CircuitBreaker.builder()
.volume(10)
.errorRatio(20)
Expand All @@ -53,13 +57,8 @@ void testCircuitBreaker() throws InterruptedException {
assertThat(breaker.state(), is(CircuitBreaker.State.OPEN));

// need to wait until half open
int count = 0;
while (count++ < 10) {
Thread.sleep(BUSY_WAIT_SLEEP);
if (breaker.state() == CircuitBreaker.State.HALF_OPEN) {
break;
}
}
ScheduledFuture<Boolean> schedule = ((CircuitBreakerImpl) breaker).schedule();
schedule.get(WAIT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);

assertThat(breaker.state(), is(CircuitBreaker.State.HALF_OPEN));

Expand Down

0 comments on commit 7121859

Please sign in to comment.