Skip to content

Commit

Permalink
Fixed wrong snapshot usage
Browse files Browse the repository at this point in the history
  • Loading branch information
hnaderi committed Sep 12, 2024
1 parent d77a28f commit 9fa173e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ object RepositoryReader {
.flatMap {
case Some(last) =>
journal
.readStreamAfter(streamId, last.version)
.readStreamAfter(streamId, last.version - 1)
.through(scanState(last))
.compile
.lastOrError

case None => history(streamId).compile.lastOrError
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,41 @@ class RepositoryReaderSuite extends CatsEffectSuite {
)
)
}

test("Repository reader uses snapshot") {
val snapshot: SnapshotReader[IO, Long] = ConstantSnapshotStore(45, 9)
val journal: JournalReader[IO, Int] = JournalReaderStub(data)

val r = RepositoryReader(journal, snapshot)

r.get("sut").assertEquals(AggregateState.Valid(55, 10)) >>
r.history("sut")
.compile
.toList
.assertEquals(
data.scanLeft(initial)((s, e) =>
AggregateState.Valid(s.state + e.payload, s.version + 1)
)
)
}

test("Repository reader uses wrong snapshots too!") {
val snapshot: SnapshotReader[IO, Long] = ConstantSnapshotStore(100, 9)
val journal: JournalReader[IO, Int] = JournalReaderStub(data)

val r = RepositoryReader(journal, snapshot)

r.get("sut").assertEquals(AggregateState.Valid(110, 10)) >>
r.history("sut")
.compile
.toList
.assertEquals(
data.scanLeft(initial)((s, e) =>
AggregateState.Valid(s.state + e.payload, s.version + 1)
)
)
}

}

class JournalReaderStub[E](data: Stream[IO, EventMessage[E]])
Expand Down

0 comments on commit 9fa173e

Please sign in to comment.