Skip to content

Commit 5d9b8f1

Browse files
committed
Made the test more uniform
1 parent 3c28757 commit 5d9b8f1

1 file changed

Lines changed: 27 additions & 22 deletions

File tree

eventual-cassandra/src/test/scala/com/evolution/kafka/journal/eventual/cassandra/ReplicatedCassandraTest.scala

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ class ReplicatedCassandraTest extends AnyFunSuite with Matchers {
4040
private val recordId = RecordId(UUID.fromString("13131313-1313-4313-9313-131313131313"))
4141
private val record = eventRecordOf(SeqNr.min, partitionOffset)
4242

43-
private def eventRecordOf(seqNr: SeqNr, partitionOffset: PartitionOffset) = {
44-
val event = EventRecord(
43+
private def eventOf(seqNr: SeqNr, partitionOffset: PartitionOffset): EventRecord[EventualPayloadAndType] = {
44+
EventRecord(
4545
event = Event[EventualPayloadAndType](seqNr),
4646
timestamp = timestamp0,
4747
partitionOffset = partitionOffset,
@@ -50,11 +50,14 @@ class ReplicatedCassandraTest extends AnyFunSuite with Matchers {
5050
metadata = RecordMetadata(HeaderMetadata(Json.obj(("key", "value")).some), PayloadMetadata.empty),
5151
headers = Headers(("key", "value")),
5252
)
53-
JournalRecord(event, recordId.some)
5453
}
5554

56-
private def event(seqNr: Int, offset: Int) = {
57-
eventRecordOf(SeqNr.unsafe(seqNr), PartitionOffset(Partition.min, Offset.unsafe(offset))).event
55+
private def eventOf(seqNr: SeqNr, offset: Offset): EventRecord[EventualPayloadAndType] = {
56+
eventOf(seqNr, PartitionOffset(Partition.min, offset))
57+
}
58+
59+
private def eventRecordOf(seqNr: SeqNr, partitionOffset: PartitionOffset): JournalRecord = {
60+
JournalRecord(eventOf(seqNr, partitionOffset), recordId.some)
5861
}
5962

6063
for {
@@ -2002,10 +2005,10 @@ class ReplicatedCassandraTest extends AnyFunSuite with Matchers {
20022005

20032006
test(s"detect a journal fork appended in a later batch, $suffix") {
20042007
val key = Key("id", topic0)
2005-
val event0 = event(seqNr = 1, offset = 1)
2008+
val event0 = eventOf(SeqNr.unsafe(1), Offset.unsafe(1))
20062009
// the stale write of the previous incarnation of the entity, landed after `event0` was already
20072010
// replicated: same `seqNr`, higher offset, different node and Cassandra timestamp
2008-
val event1 = event(seqNr = 1, offset = 2).copy(origin = Some(origin1), timestamp = timestamp1)
2011+
val event1 = eventOf(SeqNr.unsafe(1), Offset.unsafe(2)).copy(origin = Some(origin1), timestamp = timestamp1)
20092012

20102013
val program = for {
20112014
_ <- journal.append(key, Partition.min, Offset.unsafe(1), timestamp0, none, Nel.of(event0))
@@ -2033,8 +2036,8 @@ class ReplicatedCassandraTest extends AnyFunSuite with Matchers {
20332036
val key = Key("id", topic0)
20342037
// `Batch.of` merges consecutive appends without comparing their `seqNr`s, so both branches of
20352038
// a fork can arrive in a single `append`
2036-
val event0 = event(seqNr = 1, offset = 1)
2037-
val event1 = event(seqNr = 1, offset = 2).copy(origin = Some(origin1), timestamp = timestamp1)
2039+
val event0 = eventOf(SeqNr.unsafe(1), Offset.unsafe(1))
2040+
val event1 = eventOf(SeqNr.unsafe(1), Offset.unsafe(2)).copy(origin = Some(origin1), timestamp = timestamp1)
20382041

20392042
val program = journal
20402043
.append(key, Partition.min, Offset.unsafe(2), timestamp1, none, Nel.of(event0, event1))
@@ -2052,12 +2055,12 @@ class ReplicatedCassandraTest extends AnyFunSuite with Matchers {
20522055

20532056
test(s"detect a journal fork which regresses `seqNr` by more than one event, $suffix") {
20542057
val key = Key("id", topic0)
2055-
val event0 = event(seqNr = 1, offset = 1)
2056-
val event1 = event(seqNr = 2, offset = 2)
2058+
val event0 = eventOf(SeqNr.unsafe(1), Offset.unsafe(1))
2059+
val event1 = eventOf(SeqNr.unsafe(2), Offset.unsafe(2))
20572060
// the dead incarnation had both events in flight, so the live one replicated past the `seqNr`
20582061
// this one duplicates. Nothing here proves seqNr 1 is occupied - the head says 2 - so it is
20592062
// reported as suspected only, which is also what a legitimate out-of-order append looks like
2060-
val event2 = event(seqNr = 1, offset = 3).copy(origin = Some(origin1), timestamp = timestamp1)
2063+
val event2 = eventOf(SeqNr.unsafe(1), Offset.unsafe(3)).copy(origin = Some(origin1), timestamp = timestamp1)
20612064

20622065
val program = for {
20632066
_ <- journal.append(key, Partition.min, Offset.unsafe(2), timestamp0, none, Nel.of(event0, event1))
@@ -2078,9 +2081,9 @@ class ReplicatedCassandraTest extends AnyFunSuite with Matchers {
20782081

20792082
test(s"do not report a re-delivered batch as a journal fork, $suffix") {
20802083
val key = Key("id", topic0)
2081-
val event0 = event(seqNr = 1, offset = 1)
2082-
val event1 = event(seqNr = 2, offset = 2)
2083-
val event2 = event(seqNr = 3, offset = 3)
2084+
val event0 = eventOf(SeqNr.unsafe(1), Offset.unsafe(1))
2085+
val event1 = eventOf(SeqNr.unsafe(2), Offset.unsafe(2))
2086+
val event2 = eventOf(SeqNr.unsafe(3), Offset.unsafe(3))
20842087

20852088
val program = for {
20862089
_ <- journal.append(key, Partition.min, Offset.unsafe(2), timestamp0, none, Nel.of(event0, event1))
@@ -2107,10 +2110,11 @@ class ReplicatedCassandraTest extends AnyFunSuite with Matchers {
21072110

21082111
test(s"do not report a purged and recreated journal as a journal fork, $suffix") {
21092112
val key = Key("id", topic0)
2110-
val event0 = event(seqNr = 1, offset = 1)
2111-
val event1 = event(seqNr = 2, offset = 2)
2113+
val event0 = eventOf(SeqNr.unsafe(1), Offset.unsafe(1))
2114+
val event1 = eventOf(SeqNr.unsafe(2), Offset.unsafe(2))
21122115
// after a purge the journal legitimately restarts from `SeqNr.min`
2113-
val event2 = event(seqNr = 1, offset = 4).copy(timestamp = timestamp1)
2116+
val event2 =
2117+
eventOf(SeqNr.unsafe(1), Offset.unsafe(4)).copy(timestamp = timestamp1)
21142118

21152119
val program = for {
21162120
_ <- journal.append(key, Partition.min, Offset.unsafe(1), timestamp0, none, Nel.of(event0))
@@ -2128,12 +2132,13 @@ class ReplicatedCassandraTest extends AnyFunSuite with Matchers {
21282132

21292133
test(s"`delete` does not reset `seqNr`, so it neither hides nor fakes a journal fork, $suffix") {
21302134
val key = Key("id", topic0)
2131-
val event0 = event(seqNr = 1, offset = 1)
2132-
val event1 = event(seqNr = 2, offset = 2)
2133-
val event2 = event(seqNr = 3, offset = 4)
2135+
val event0 = eventOf(SeqNr.unsafe(1), Offset.unsafe(1))
2136+
val event1 = eventOf(SeqNr.unsafe(2), Offset.unsafe(2))
2137+
val event2 = eventOf(SeqNr.unsafe(3), Offset.unsafe(4))
21342138
// a duplicate of an already deleted `seqNr`: still reported, but no recovery can trip over it
21352139
// because the row it duplicates is gone
2136-
val event3 = event(seqNr = 2, offset = 5).copy(timestamp = timestamp1)
2140+
val event3 =
2141+
eventOf(SeqNr.unsafe(2), Offset.unsafe(5)).copy(timestamp = timestamp1)
21372142

21382143
val program = for {
21392144
_ <- journal.append(key, Partition.min, Offset.unsafe(2), timestamp0, none, Nel.of(event0, event1))

0 commit comments

Comments
 (0)