From 0bfeef8a910ab268db48e0191f500fe78842be51 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Sat, 5 Apr 2025 05:10:27 +0200 Subject: [PATCH] fix ProjectionSnapshotRepositoryInMemory.excludeTheHeadBySize --- .../projection/ProjectionSnapshotRepositoryInMemory.kt | 10 ++-------- .../event/projection/GameStateRepositoryTest.kt | 6 ++---- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/eventDemo/libs/event/projection/ProjectionSnapshotRepositoryInMemory.kt b/src/main/kotlin/eventDemo/libs/event/projection/ProjectionSnapshotRepositoryInMemory.kt index db82251..dcbbf87 100644 --- a/src/main/kotlin/eventDemo/libs/event/projection/ProjectionSnapshotRepositoryInMemory.kt +++ b/src/main/kotlin/eventDemo/libs/event/projection/ProjectionSnapshotRepositoryInMemory.kt @@ -144,14 +144,8 @@ class ProjectionSnapshotRepositoryInMemory, P : Projection, ID */ private fun FilteredList

.excludeTheHeadBySize(): FilteredList

{ // filter if size exceeds the limit - if (size > snapshotCacheConfig.maxSnapshotCacheSize) { - val numberToRemove = size - snapshotCacheConfig.maxSnapshotCacheSize - if (numberToRemove > 0) { - return sortedBy { it.first.lastEventVersion } - .takeLast(numberToRemove) - } - } - return this + return sortedBy { it.first.lastEventVersion } + .dropLast(snapshotCacheConfig.maxSnapshotCacheSize) } /** diff --git a/src/test/kotlin/eventDemo/business/event/projection/GameStateRepositoryTest.kt b/src/test/kotlin/eventDemo/business/event/projection/GameStateRepositoryTest.kt index 938dc8d..beaadbe 100644 --- a/src/test/kotlin/eventDemo/business/event/projection/GameStateRepositoryTest.kt +++ b/src/test/kotlin/eventDemo/business/event/projection/GameStateRepositoryTest.kt @@ -11,9 +11,8 @@ import io.kotest.assertions.nondeterministic.eventually import io.kotest.assertions.nondeterministic.eventuallyConfig import io.kotest.core.spec.style.FunSpec import io.kotest.matchers.collections.shouldHaveSize -import io.kotest.matchers.comparables.shouldBeGreaterThan import io.kotest.matchers.equals.shouldBeEqual -import io.kotest.matchers.ints.shouldBeLessThan +import io.kotest.matchers.shouldBe import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.joinAll @@ -148,8 +147,7 @@ class GameStateRepositoryTest : lastEventVersion shouldBeEqual 1000 players shouldHaveSize 1000 } - repo.count(aggregateId) shouldBeGreaterThan 20 - repo.count(aggregateId) shouldBeLessThan 30 + repo.count(aggregateId) shouldBe 119 } } }