@@ -348,36 +348,37 @@ class EventsBySliceSpec
348348 val timestamp1 = queryWithCache.latestEventTimestamp(entityType, slice, slice).futureValue
349349 timestamp1 shouldBe expectedTimestamp1
350350
351- persister ! PersistWithAck (" e2" , probe.ref)
352- probe.expectMessage(Done )
353-
354- // second query will return cached result (when still within TTL of 2 seconds)
351+ // second query will return same result within TTL; note that the cache update is a side effect of
352+ // the first query's future, so it may or may not have been applied yet - we avoid persisting new
353+ // events between the two calls so the result is the same whether the cache was hit or not
355354 val timestamp2 = queryWithCache.latestEventTimestamp(entityType, slice, slice).futureValue
356355 timestamp2 shouldBe timestamp1
357356
358357 // after clearing cache, will fetch the latest timestamp
358+ persister ! PersistWithAck (" e2" , probe.ref)
359+ probe.expectMessage(Done )
359360 queryWithCache.clearLatestEventTimestampCache()
360361 val expectedTimestamp3 = queryWithCache.timestampOf(pid, 2L ).futureValue
361362 val timestamp3 = queryWithCache.latestEventTimestamp(entityType, slice, slice).futureValue
362363 timestamp3 shouldBe expectedTimestamp3
363364
365+ // make sure cached value has expired
364366 persister ! PersistWithAck (" e3" , probe.ref)
365367 probe.expectMessage(Done )
366-
367- // make sure cached value has expired
368368 Thread .sleep(2000 )
369369
370370 // new result fetched from database after cache expiry
371371 val expectedTimestamp4 = queryWithCache.timestampOf(pid, 3L ).futureValue
372372 val timestamp4 = queryWithCache.latestEventTimestamp(entityType, slice, slice).futureValue
373373 timestamp4 shouldBe expectedTimestamp4
374374
375- persister ! PersistWithAck (" e4" , probe.ref)
376- probe.expectMessage(Done )
377-
378- // next query will return cached result again (when still within TTL of 2 seconds)
375+ // next query will return same result within TTL; same reasoning as above - cache update is a side
376+ // effect so we avoid persisting new events between the two calls
379377 val timestamp5 = queryWithCache.latestEventTimestamp(entityType, slice, slice).futureValue
380378 timestamp5 shouldBe timestamp4
379+
380+ persister ! PersistWithAck (" e4" , probe.ref)
381+ probe.expectMessage(Done )
381382 }
382383
383384 " support LoadEventQuery" in new Setup {
0 commit comments