Skip to content

Commit

Permalink
flaky test
Browse files Browse the repository at this point in the history
  • Loading branch information
osiegmar committed Oct 15, 2023
1 parent 90c2777 commit 82af705
Showing 1 changed file with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -113,23 +115,25 @@ void validatePoolSize() {

@Test
void infinitePollWaitTime() throws InterruptedException {
final Duration waitTime = Duration.ofMillis(200);

final SimpleObjectPool<MyPooledObject> pool =
new SimpleObjectPool<>(factory, 1, -1, 100, 0);

try (pool) {
final MyPooledObject o1 = pool.borrowObject();
final long before = System.nanoTime();
final Instant before = Instant.now();

Executors.newScheduledThreadPool(1)
.schedule(() -> pool.returnObject(o1), 200, TimeUnit.MILLISECONDS);
.schedule(() -> pool.returnObject(o1), waitTime.toMillis(), TimeUnit.MILLISECONDS);

final MyPooledObject o2 = pool.borrowObject();
final long after = System.nanoTime();
final Instant after = Instant.now();

final long elapsedTimeMillis = TimeUnit.NANOSECONDS.toMillis(after - before);
final Duration elapsedTime = Duration.between(before, after);

assertThat(o2).isNotNull();
assertThat(elapsedTimeMillis).isGreaterThan(200);
assertThat(elapsedTime).isGreaterThanOrEqualTo(waitTime);
}
}

Expand Down

0 comments on commit 82af705

Please sign in to comment.