Skip to content

Commit 9fa173e

Browse files
committed
Fixed wrong snapshot usage
1 parent d77a28f commit 9fa173e

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

modules/backend/src/main/scala/eventsourcing/RepositoryReader.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ object RepositoryReader {
5454
.flatMap {
5555
case Some(last) =>
5656
journal
57-
.readStreamAfter(streamId, last.version)
57+
.readStreamAfter(streamId, last.version - 1)
5858
.through(scanState(last))
5959
.compile
6060
.lastOrError
61+
6162
case None => history(streamId).compile.lastOrError
6263
}
6364

modules/backend/src/test/scala/eventsourcing/RepositoryReaderSuite.scala

+35
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,41 @@ class RepositoryReaderSuite extends CatsEffectSuite {
6969
)
7070
)
7171
}
72+
73+
test("Repository reader uses snapshot") {
74+
val snapshot: SnapshotReader[IO, Long] = ConstantSnapshotStore(45, 9)
75+
val journal: JournalReader[IO, Int] = JournalReaderStub(data)
76+
77+
val r = RepositoryReader(journal, snapshot)
78+
79+
r.get("sut").assertEquals(AggregateState.Valid(55, 10)) >>
80+
r.history("sut")
81+
.compile
82+
.toList
83+
.assertEquals(
84+
data.scanLeft(initial)((s, e) =>
85+
AggregateState.Valid(s.state + e.payload, s.version + 1)
86+
)
87+
)
88+
}
89+
90+
test("Repository reader uses wrong snapshots too!") {
91+
val snapshot: SnapshotReader[IO, Long] = ConstantSnapshotStore(100, 9)
92+
val journal: JournalReader[IO, Int] = JournalReaderStub(data)
93+
94+
val r = RepositoryReader(journal, snapshot)
95+
96+
r.get("sut").assertEquals(AggregateState.Valid(110, 10)) >>
97+
r.history("sut")
98+
.compile
99+
.toList
100+
.assertEquals(
101+
data.scanLeft(initial)((s, e) =>
102+
AggregateState.Valid(s.state + e.payload, s.version + 1)
103+
)
104+
)
105+
}
106+
72107
}
73108

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

0 commit comments

Comments
 (0)