Skip to content

Commit

Permalink
Replace Thread.sleep() with await().untilAsserted().
Browse files Browse the repository at this point in the history
  • Loading branch information
ammachado committed Sep 16, 2024
1 parent 81ce005 commit b6c022c
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions context/src/test/java/io/opentelemetry/context/ContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ void scheduleCallable() {
}

@Test
void scheduleAtFixedRate() throws Exception {
void scheduleAtFixedRate() {
LongAdder longAdder = new LongAdder();
Runnable runnable =
() -> {
Expand All @@ -676,14 +676,18 @@ void scheduleAtFixedRate() throws Exception {
};
Future<?> future = wrapped.scheduleAtFixedRate(runnable, 1L, 2L, TimeUnit.NANOSECONDS);
assertThat(future).isNotNull();
Thread.sleep(5L);
future.cancel(true);
await().await().untilAsserted(() -> {
if (!future.isCancelled()) {
future.cancel(true);
}
assertThat(longAdder.intValue()).isGreaterThan(1);
});
assertThat(longAdder.intValue()).isGreaterThan(1);
assertThat(value).hasValue("cat");
}

@Test
void scheduleWithFixedDelay() throws Exception {
void scheduleWithFixedDelay() {
LongAdder longAdder = new LongAdder();
Runnable runnable =
() -> {
Expand All @@ -692,9 +696,12 @@ void scheduleWithFixedDelay() throws Exception {
};
Future<?> future = wrapped.scheduleWithFixedDelay(runnable, 1L, 2L, TimeUnit.NANOSECONDS);
assertThat(future).isNotNull();
Thread.sleep(5L);
future.cancel(true);
assertThat(longAdder.intValue()).isGreaterThan(1);
await().await().untilAsserted(() -> {
if (!future.isCancelled()) {
future.cancel(true);
}
assertThat(longAdder.intValue()).isGreaterThan(1);
});
assertThat(value).hasValue("cat");
}
}
Expand Down

0 comments on commit b6c022c

Please sign in to comment.