java - 如何等待Redis缓存缓存信息
问题描述
我正在使用 spring-data-redis 并尝试使用 junit 来测试我的缓存逻辑。测试用例偶尔会起作用。我猜如果缓存逻辑在调用第二个方法调用之前完成,那么它会起作用,否则它会失败。如果有些人遇到过类似的问题,我想了解他们是如何做到的。截至目前,我正在使用 thread.sleep() 但正在寻找替代方案。
@Test
public void getUserById() {
User user = new User("name", "1234");
when(userRepository.findbyId("1234")).thenReturn(Optional.ofNullable(user));
// first method call
User user1 = userService.findbyId("1234");
assertThat(user.getName()).isEqualTo(user1.getName());
assertThat(user.getId).isEqualTo(user1.getId());
// sleeping the thread so to provide caching aspect sufficient time
// to cache the information
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// second method call, expecting cache to work.
userCache = userService.findbyId("1234");
verify(userRepository, never()).findbyId("1234");
assertThat(user.getName()).isEqualTo(userCache.getName());
assertThat(user.getId).isEqualTo(userCache.getId());
}
解决方案
等待很短时间的运行时问题在分布式系统中非常常见。为了弥补等待测试断言过长的需要,有一个名为Awaitility的小工具。
有了这个,你基本上可以通过多次查询断言来做一个更聪明的等待,在一定的时间间隔内,直到达到给定的超时(……等等)。
关于你的例子,试试这个:
Awaitility.await()
.pollInterval(new Duration(1, TimeUnit.SECONDS))
.atMost(new Duration(10, TimeUnit.SECONDS))
.untilAsserted(() ->
User user1 = userService.findbyId("1234");
assertThat(user1.getName()).isEqualTo(user.getName());
关于您问题的另一部分,在集成测试中,您实际上可以对您的实例执行某种预热,Redis
或者如果您有容器化集成测试(例如 Docker),您可以在其上触发一些第一个请求或等待某个条件之前从你的测试开始。
推荐阅读
- sql - 在 SQL 中获取单个记录 id
- ruby-on-rails - 当我需要很长时间加载页面时,如何在 Ruby Watir 中停止页面加载并关闭浏览器?
- symfony - jms 序列化程序组在子实体中不起作用
- python - 只需键入名称即可使文件可执行
- apache-spark - 从 Dataproc 写入 BigQuery 时在哪里可以找到错误?
- python - 求解 ODE 时出错:函数及其雅可比矩阵必须是可调用函数
- java - 如何知道 ListView 项是否使用两行?
- apache-spark - Is it better to partition by time stamp or year,month,day, hour
- javascript - 如何有条件地应用 CSS 类?
- flutter - 如何使用 firebase_auth(电话身份验证)在颤振应用程序中添加短信自动验证?