From 498fee3d2841bb211b50c42bf14f7f33e0f6bae5 Mon Sep 17 00:00:00 2001 From: atakavci <58048133+atakavci@users.noreply.github.com> Date: Thu, 17 Oct 2024 16:24:23 +0300 Subject: [PATCH] Fix flaky tests with 'await' (#3972) * fix flaky test - dumy set-get to gain time * adding same commands for 'simple' * introdue tryAssert in CSC tests * remove leftovers * introduce awaitility for polling * nit * fix pipelining test untilasserted --- pom.xml | 7 +++++++ .../redis/clients/jedis/PipeliningTest.java | 5 ++++- .../UnifiedJedisClientSideCacheTestBase.java | 18 ++++++++++-------- .../redis/clients/jedis/util/AssertUtil.java | 2 -- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/pom.xml b/pom.xml index 2b8b56f20b..4b839b8b9b 100644 --- a/pom.xml +++ b/pom.xml @@ -143,6 +143,13 @@ test + + org.awaitility + awaitility + 4.2.2 + test + + io.github.resilience4j diff --git a/src/test/java/redis/clients/jedis/PipeliningTest.java b/src/test/java/redis/clients/jedis/PipeliningTest.java index 7df000c7a3..fede809686 100644 --- a/src/test/java/redis/clients/jedis/PipeliningTest.java +++ b/src/test/java/redis/clients/jedis/PipeliningTest.java @@ -20,12 +20,14 @@ import java.util.Map; import java.util.Set; import java.util.UUID; +import java.util.concurrent.TimeUnit; import org.hamcrest.Matcher; import org.hamcrest.Matchers; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; +import org.awaitility.Awaitility; import redis.clients.jedis.commands.ProtocolCommand; import redis.clients.jedis.commands.jedis.JedisCommandsTestBase; @@ -342,7 +344,8 @@ public void waitAof() { try (Jedis j = new Jedis(endpoint.getHostAndPort())) { j.auth(endpoint.getPassword()); - assertEquals("aof", j.get("wait")); + Awaitility.await().atMost(5, TimeUnit.SECONDS).pollInterval(50, TimeUnit.MILLISECONDS) + .untilAsserted(() -> assertEquals("aof", j.get("wait"))); } } diff --git a/src/test/java/redis/clients/jedis/csc/UnifiedJedisClientSideCacheTestBase.java b/src/test/java/redis/clients/jedis/csc/UnifiedJedisClientSideCacheTestBase.java index 388113307b..e07bf578ee 100644 --- a/src/test/java/redis/clients/jedis/csc/UnifiedJedisClientSideCacheTestBase.java +++ b/src/test/java/redis/clients/jedis/csc/UnifiedJedisClientSideCacheTestBase.java @@ -3,9 +3,11 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertNull; +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; @@ -39,7 +41,8 @@ public void simple() { control.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); control.del("foo"); - assertNull(jedis.get("foo")); + await().atMost(5, TimeUnit.SECONDS).pollInterval(50, TimeUnit.MILLISECONDS) + .untilAsserted(() -> assertNull(jedis.get("foo"))); } } @@ -53,9 +56,8 @@ public void simpleWithSimpleMap() { assertEquals(1, cache.getSize()); control.del("foo"); assertEquals(1, cache.getSize()); - assertNull(jedis.get("foo")); - assertEquals(1, cache.getSize()); - assertNull(jedis.get("foo")); + await().atMost(5, TimeUnit.SECONDS).pollInterval(50, TimeUnit.MILLISECONDS) + .untilAsserted(() -> assertNull(jedis.get("foo"))); assertEquals(1, cache.getSize()); } } @@ -67,7 +69,8 @@ public void flushAll() { control.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); control.flushAll(); - assertNull(jedis.get("foo")); + await().atMost(5, TimeUnit.SECONDS).pollInterval(50, TimeUnit.MILLISECONDS) + .untilAsserted(() -> assertNull(jedis.get("foo"))); } } @@ -81,9 +84,8 @@ public void flushAllWithSimpleMap() { assertEquals(1, cache.getSize()); control.flushAll(); assertEquals(1, cache.getSize()); - assertNull(jedis.get("foo")); - assertEquals(1, cache.getSize()); - assertNull(jedis.get("foo")); + await().atMost(5, TimeUnit.SECONDS).pollInterval(50, TimeUnit.MILLISECONDS) + .untilAsserted(() -> assertNull(jedis.get("foo"))); assertEquals(1, cache.getSize()); } } diff --git a/src/test/java/redis/clients/jedis/util/AssertUtil.java b/src/test/java/redis/clients/jedis/util/AssertUtil.java index 110be7e48e..474b34e74b 100644 --- a/src/test/java/redis/clients/jedis/util/AssertUtil.java +++ b/src/test/java/redis/clients/jedis/util/AssertUtil.java @@ -7,10 +7,8 @@ import java.util.Collection; import java.util.Iterator; import java.util.List; -import java.util.Map; import java.util.Objects; import java.util.Set; - import org.junit.ComparisonFailure; import redis.clients.jedis.RedisProtocol;