Take async cache writes into consideration

Since Spring Data Redis 4.0 and when using Lettuce, entries may be
written to the cache asynchronously. This means that they may not
be immediately visible to a subsequent read. This commit adapts the
test to use Awaitility to wait for the expected entry to become
visible in the cache.

Closes gh-50356
This commit is contained in:
Andy Wilkinson
2026-05-07 15:42:50 +01:00
parent 7cfd94a2b0
commit 5257df1bb1
@@ -16,7 +16,10 @@
package smoketest.cache;
import java.time.Duration;
import com.redis.testcontainers.RedisContainer;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
@@ -52,9 +55,11 @@ class SampleCacheApplicationRedisTests {
countries.clear(); // Simple test assuming the cache is empty
assertThat(countries.get("BE")).isNull();
Country be = this.countryRepository.findByCode("BE");
ValueWrapper belgium = countries.get("BE");
assertThat(belgium).isNotNull();
assertThat((Country) belgium.get()).isEqualTo(be);
Awaitility.waitAtMost(Duration.ofSeconds(10)).untilAsserted(() -> {
ValueWrapper belgium = countries.get("BE");
assertThat(belgium).isNotNull();
assertThat((Country) belgium.get()).isEqualTo(be);
});
}
}