Skip to content

Commit

Permalink
introduce awaitility for polling
Browse files Browse the repository at this point in the history
  • Loading branch information
atakavci committed Oct 14, 2024
1 parent 41348d5 commit 53b4f7d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 33 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>4.2.2</version>
<scope>test</scope>
</dependency>

<!-- circuit breaker / failover -->
<dependency>
<groupId>io.github.resilience4j</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.awaitility.Awaitility.await;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.util.AssertUtil;

public abstract class UnifiedJedisClientSideCacheTestBase {

Expand Down Expand Up @@ -37,7 +40,8 @@ public void simple() {
control.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
control.del("foo");
AssertUtil.tryAssert(() -> jedis.get("foo"), null, 50, 100);
await().atMost(5, TimeUnit.SECONDS).pollInterval(50, TimeUnit.MILLISECONDS)
.until(() -> jedis.get("foo") == null);
}
}

Expand All @@ -51,7 +55,8 @@ public void simpleWithSimpleMap() {
assertEquals(1, cache.getSize());
control.del("foo");
assertEquals(1, cache.getSize());
AssertUtil.tryAssert(() -> jedis.get("foo"), null, 50, 100);
await().atMost(5, TimeUnit.SECONDS).pollInterval(50, TimeUnit.MILLISECONDS)
.until(() -> jedis.get("foo") == null);
assertEquals(1, cache.getSize());
}
}
Expand All @@ -63,7 +68,8 @@ public void flushAll() {
control.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
control.flushAll();
AssertUtil.tryAssert(() -> jedis.get("foo"), null, 50, 100);
await().atMost(5, TimeUnit.SECONDS).pollInterval(50, TimeUnit.MILLISECONDS)
.until(() -> jedis.get("foo") == null);
}
}

Expand All @@ -77,7 +83,8 @@ public void flushAllWithSimpleMap() {
assertEquals(1, cache.getSize());
control.flushAll();
assertEquals(1, cache.getSize());
AssertUtil.tryAssert(() -> jedis.get("foo"), null, 50, 100);
await().atMost(5, TimeUnit.SECONDS).pollInterval(50, TimeUnit.MILLISECONDS)
.until(() -> jedis.get("foo") == null);
assertEquals(1, cache.getSize());
}
}
Expand All @@ -88,7 +95,7 @@ public void cacheNotEmptyTest() {
Cache cache = jedis.getCache();
control.set("foo", "bar");
assertEquals(0, cache.getSize());
AssertUtil.tryAssert(() -> jedis.get("foo"), "bar", 50, 100);
assertEquals("bar", (jedis.get("foo")));
assertEquals(1, cache.getSize());
}
}
Expand Down
27 changes: 0 additions & 27 deletions src/test/java/redis/clients/jedis/util/AssertUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import org.junit.ComparisonFailure;
import redis.clients.jedis.RedisProtocol;

Expand Down Expand Up @@ -149,29 +147,4 @@ private static void assertPipelineSyncAllSet(Set<?> expected, Set<?> actual) {
assertEquals(expected, actual);
}
}

public static <T> void tryAssert(Callable<T> callable, T expected, long interval, int iterations) {
int attempts = 0;

while (attempts++ < iterations) {

T result;
try {
result = callable.call();
} catch (Exception e) {
throw new AssertionError("Callable threw an exception", e);
}

if (Objects.equals(expected, result)) {
return;
}

try {
TimeUnit.MILLISECONDS.sleep(interval);
} catch (InterruptedException e) {
throw new AssertionError("Thread was interrupted", e);
}
}
throw new AssertionError("tryAssert failed after " + iterations + " attempts");
}
}

0 comments on commit 53b4f7d

Please sign in to comment.