This repository has been archived by the owner on Apr 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 115
/
IODBSpecification.scala
89 lines (76 loc) · 2.65 KB
/
IODBSpecification.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package hybrid.history
import examples.commons._
import examples.hybrid.blocks.{HybridBlock, PosBlock, PowBlock}
import hybrid.HybridGenerators
import io.iohk.iodb.{ByteArrayWrapper, LSMStore}
import org.scalacheck.Gen
import org.scalatest.matchers.should.Matchers
import org.scalatest.{fixture, Outcome}
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks
import scorex.core.utils.ScorexEncoding
import scorex.testkit.utils.FileUtils
import scorex.util.ModifierId
@SuppressWarnings(Array("org.wartremover.warts.TraversableOps"))
class IODBSpecification extends fixture.PropSpec
with ScalaCheckPropertyChecks
with Matchers
with HybridGenerators
with FileUtils
with ScorexEncoding {
type FixtureParam = LSMStore
override def withFixture(test: OneArgTest): Outcome = {
val fixture = new LSMStore(createTempDir)
try test(fixture)
finally {}
}
property("Rollback should not touch keys before") { blocksStorage =>
def writeTx(tx: SimpleBoxTransaction): Unit = {
// val boxIdsToRemove: Iterable[ByteArrayWrapper] = tx.boxIdsToOpen.map(id => ByteArrayWrapper(id))
val boxIdsToRemove: Iterable[ByteArrayWrapper] = Seq()
val boxesToAdd: Iterable[(ByteArrayWrapper, ByteArrayWrapper)] = tx.newBoxes
.map(b => (ByteArrayWrapper(b.id), ByteArrayWrapper(b.bytes))).toList
blocksStorage.update(idToBAW(tx.id), boxIdsToRemove, boxesToAdd)
}
def checkTx(tx: SimpleBoxTransaction): Unit = {
tx.newBoxes.foreach(b => require(blocksStorage.get(ByteArrayWrapper(b.id)).isDefined))
}
forAll(Gen.nonEmptyListOf(simpleBoxTransactionGen)) { txs =>
whenever(txs.length >= 2) {
val head = txs.head
writeTx(head)
checkTx(head)
txs.tail.foreach(tx => writeTx(tx))
txs.foreach(tx => checkTx(tx))
blocksStorage.rollback(idToBAW(head.id))
checkTx(head)
}
}
}
property("writeBlock() test") { blocksStorage =>
def writeBlock(b: HybridBlock): Unit = {
val typeByte = b match {
case _: PowBlock =>
PowBlock.ModifierTypeId
case _: PosBlock =>
PosBlock.ModifierTypeId
}
blocksStorage.update(
idToBAW(b.id),
Seq(),
Seq(idToBAW(b.id) -> ByteArrayWrapper(typeByte +: b.bytes)))
}
var ids: Seq[ModifierId] = Seq()
forAll(powBlockGen) { block =>
ids = block.id +: ids
writeBlock(block)
blocksStorage.get(idToBAW(block.id)).isDefined shouldBe true
}
ids.foreach { id =>
blocksStorage.get(idToBAW(id)) match {
case None =>
throw new Error(s"Id ${encoder.encodeId(id)} not found")
case Some(_) => ()
}
}
}
}