Skip to content

Commit d049089

Browse files
committed
refactoring tests
1 parent d6e67af commit d049089

File tree

2 files changed

+33
-36
lines changed

2 files changed

+33
-36
lines changed

src/it/scala/io/iohk/ethereum/ledger/BlockImporterItSpec.scala

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import io.iohk.ethereum.ledger.Ledger.BlockResult
1919
import monix.execution.Scheduler
2020
import org.scalamock.scalatest.MockFactory
2121
import org.scalatest.BeforeAndAfterAll
22+
import org.scalatest.concurrent.Eventually.eventually
2223
import org.scalatest.flatspec.AsyncFlatSpecLike
2324
import org.scalatest.matchers.should.Matchers
2425

@@ -150,9 +151,8 @@ class BlockImporterItSpec
150151
blockImporter ! BlockImporter.Start
151152
blockImporter ! BlockFetcher.PickedBlocks(NonEmptyList.fromListUnsafe(newBranch))
152153

153-
Thread.sleep(1000)
154154
//because the blocks are not valid, we shouldn't reorganise, but at least stay with a current chain, and the best block of the current chain is oldBlock4
155-
blockchain.getBestBlock().get shouldEqual oldBlock4
155+
eventually { blockchain.getBestBlock().get shouldEqual oldBlock4 }
156156
}
157157

158158
it should "return a correct new best block after reorganising longer chain to a shorter one if its weight is bigger" in {
@@ -164,17 +164,16 @@ class BlockImporterItSpec
164164

165165
blockImporter ! BlockFetcher.PickedBlocks(NonEmptyList.fromListUnsafe(newBranch))
166166

167-
Thread.sleep(200)
168-
blockchain.getBestBlock().get shouldEqual newBlock3
167+
eventually { Thread.sleep(200); blockchain.getBestBlock().get shouldEqual newBlock3 }
169168
}
170169

171170
it should "return Unknown branch, in case of PickedBlocks with block that has a parent that's not in the chain" in {
172-
val newcomerBlock4: Block =
171+
val newBlock4ParentOldBlock3: Block =
173172
getBlock(genesisBlock.number + 4, difficulty = 104, parent = oldBlock3.header.hash)
174-
val newcomerWeight4Duplicate = oldWeight3.increase(newcomerBlock4.header)
173+
val newBlock4WeightParentOldBlock3 = oldWeight3.increase(newBlock4ParentOldBlock3.header)
175174

176175
//Block n5 with oldBlock4 as parent
177-
val newComerBlock5WithOldBlock4Parent: Block =
176+
val newBlock5ParentOldBlock4: Block =
178177
getBlock(
179178
genesisBlock.number + 5,
180179
difficulty = 108,
@@ -187,13 +186,12 @@ class BlockImporterItSpec
187186
blockchain.save(oldBlock4, Nil, oldWeight4, saveAsBestBlock = true)
188187
// simulation of node restart
189188
blockchain.saveBestKnownBlocks(blockchain.getBestBlockNumber() - 1)
190-
blockchain.save(newcomerBlock4, Nil, newcomerWeight4Duplicate, saveAsBestBlock = true)
189+
blockchain.save(newBlock4ParentOldBlock3, Nil, newBlock4WeightParentOldBlock3, saveAsBestBlock = true)
191190

192191
//not reorganising anymore until oldBlock4(not part of the chain anymore), no block/ommer validation when not part of the chain, resolveBranch is returning UnknownBranch
193-
blockImporter ! BlockFetcher.PickedBlocks(NonEmptyList.fromListUnsafe(List(newComerBlock5WithOldBlock4Parent)))
192+
blockImporter ! BlockFetcher.PickedBlocks(NonEmptyList.fromListUnsafe(List(newBlock5ParentOldBlock4)))
194193

195-
Thread.sleep(200)
196-
blockchain.getBestBlock().get shouldEqual newcomerBlock4
194+
eventually { blockchain.getBestBlock().get shouldEqual newBlock4ParentOldBlock3 }
197195
}
198196

199197
it should "switch to a branch with a checkpoint" in {
@@ -206,9 +204,8 @@ class BlockImporterItSpec
206204

207205
blockImporter ! BlockFetcher.PickedBlocks(NonEmptyList.fromListUnsafe(newBranch))
208206

209-
Thread.sleep(200)
210-
blockchain.getBestBlock().get shouldEqual oldBlock5WithCheckpoint
211-
blockchain.getLatestCheckpointBlockNumber() shouldEqual oldBlock5WithCheckpoint.header.number
207+
eventually { blockchain.getBestBlock().get shouldEqual oldBlock5WithCheckpoint }
208+
eventually { blockchain.getLatestCheckpointBlockNumber() shouldEqual oldBlock5WithCheckpoint.header.number }
212209
}
213210

214211
it should "switch to a branch with a newer checkpoint" in {
@@ -221,9 +218,8 @@ class BlockImporterItSpec
221218

222219
blockImporter ! BlockFetcher.PickedBlocks(NonEmptyList.fromListUnsafe(newBranch))
223220

224-
Thread.sleep(200)
225-
blockchain.getBestBlock().get shouldEqual newBlock4WithCheckpoint
226-
blockchain.getLatestCheckpointBlockNumber() shouldEqual newBlock4WithCheckpoint.header.number
221+
eventually { blockchain.getBestBlock().get shouldEqual newBlock4WithCheckpoint }
222+
eventually { blockchain.getLatestCheckpointBlockNumber() shouldEqual newBlock4WithCheckpoint.header.number }
227223
}
228224

229225
it should "return a correct checkpointed block after receiving a request for generating a new checkpoint" in {
@@ -242,8 +238,9 @@ class BlockImporterItSpec
242238

243239
val checkpointBlock = checkpointBlockGenerator.generate(newBlock5, Checkpoint(signatures))
244240

245-
Thread.sleep(1000)
246-
blockchain.getBestBlock().get shouldEqual checkpointBlock
247-
blockchain.getLatestCheckpointBlockNumber() shouldEqual newBlock5.header.number + 1
241+
eventually { Thread.sleep(1000); blockchain.getBestBlock().get shouldEqual checkpointBlock }
242+
eventually {
243+
Thread.sleep(1000); blockchain.getLatestCheckpointBlockNumber() shouldEqual newBlock5.header.number + 1
244+
}
248245
}
249246
}

src/test/scala/io/iohk/ethereum/domain/BlockchainSpec.scala

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,44 +26,44 @@ class BlockchainSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyCh
2626
val validBlock = Fixtures.Blocks.ValidBlock.block
2727
blockchain.storeBlock(validBlock).commit()
2828
val block = blockchain.getBlockByHash(validBlock.header.hash)
29-
assert(block.isDefined)
30-
assert(validBlock == block.get)
29+
block.isDefined should ===(true)
30+
validBlock should ===(block.get)
3131
val blockHeader = blockchain.getBlockHeaderByHash(validBlock.header.hash)
32-
assert(blockHeader.isDefined)
33-
assert(validBlock.header == blockHeader.get)
32+
blockHeader.isDefined should ===(true)
33+
validBlock.header should ===(blockHeader.get)
3434
val blockBody = blockchain.getBlockBodyByHash(validBlock.header.hash)
35-
assert(blockBody.isDefined)
36-
assert(validBlock.body == blockBody.get)
35+
blockBody.isDefined should ===(true)
36+
validBlock.body should ===(blockBody.get)
3737
}
3838

3939
it should "be able to store a block and retrieve it by number" in new EphemBlockchainTestSetup {
4040
val validBlock = Fixtures.Blocks.ValidBlock.block
4141
blockchain.storeBlock(validBlock).commit()
4242
val block = blockchain.getBlockByNumber(validBlock.header.number)
43-
assert(block.isDefined)
44-
assert(validBlock == block.get)
43+
block.isDefined should ===(true)
44+
validBlock should ===(block.get)
4545
}
4646

4747
it should "be able to do strict check of block existence in the chain" in new EphemBlockchainTestSetup {
4848
val validBlock = Fixtures.Blocks.ValidBlock.block
4949
blockchain.save(validBlock, Seq.empty, ChainWeight(100, 100), saveAsBestBlock = true)
50-
assert(blockchain.isInChain(validBlock.hash))
50+
blockchain.isInChain(validBlock.hash) === (false)
5151
// simulation of node restart
5252
blockchain.saveBestKnownBlocks(validBlock.header.number - 1)
53-
assert(!blockchain.isInChain(validBlock.hash))
53+
blockchain.isInChain(validBlock.hash) should ===(false)
5454
}
5555

5656
it should "be able to query a stored blockHeader by it's number" in new EphemBlockchainTestSetup {
5757
val validHeader = Fixtures.Blocks.ValidBlock.header
5858
blockchain.storeBlockHeader(validHeader).commit()
5959
val header = blockchain.getBlockHeaderByNumber(validHeader.number)
60-
assert(header.isDefined)
61-
assert(validHeader == header.get)
60+
header.isDefined should ===(true)
61+
validHeader should ===(header.get)
6262
}
6363

6464
it should "not return a value if not stored" in new EphemBlockchainTestSetup {
65-
assert(blockchain.getBlockByNumber(Fixtures.Blocks.ValidBlock.header.number).isEmpty)
66-
assert(blockchain.getBlockByHash(Fixtures.Blocks.ValidBlock.header.hash).isEmpty)
65+
blockchain.getBlockByNumber(Fixtures.Blocks.ValidBlock.header.number).isEmpty should ===(true)
66+
blockchain.getBlockByHash(Fixtures.Blocks.ValidBlock.header.hash).isEmpty should ===(true)
6767
}
6868

6969
it should "be able to store a block with checkpoint and retrieve it and checkpoint" in new EphemBlockchainTestSetup {
@@ -75,8 +75,8 @@ class BlockchainSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyCh
7575
blockchain.save(validBlock, Seq.empty, ChainWeight(0, 0), saveAsBestBlock = true)
7676

7777
val retrievedBlock = blockchain.getBlockByHash(validBlock.header.hash)
78-
assert(retrievedBlock.isDefined)
79-
assert(validBlock == retrievedBlock.get)
78+
retrievedBlock.isDefined should ===(true)
79+
validBlock should ===(retrievedBlock.get)
8080

8181
blockchain.getLatestCheckpointBlockNumber() should ===(validBlock.number)
8282
blockchain.getBestBlockNumber() should ===(validBlock.number)

0 commit comments

Comments
 (0)