Skip to content

Commit 04fc825

Browse files
author
Jaap van der Plas
committed
ETCM-147 refactor test fixture to remove var
1 parent 3ff04fd commit 04fc825

File tree

1 file changed

+55
-42
lines changed

1 file changed

+55
-42
lines changed

src/test/scala/io/iohk/ethereum/blockchain/sync/SyncControllerSpec.scala

Lines changed: 55 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,8 @@ class SyncControllerSpec
4848
with Eventually
4949
with NormalPatience {
5050

51-
implicit var system: ActorSystem = _
52-
53-
before {
54-
system = ActorSystem("SyncControllerSpec_System", ConfigFactory.load("explicit-scheduler"))
55-
}
56-
57-
after {
58-
Await.result(system.terminate(), 10.seconds)
59-
}
60-
61-
"SyncController" should "download pivot block and request block headers" in new TestSetup() {
51+
"SyncController" should "download pivot block and request block headers" in withTestSetup() { testSetup =>
52+
import testSetup._
6253
syncController ! SyncProtocol.Start
6354

6455
val handshakedPeers = HandshakedPeers(twoAcceptedPeers)
@@ -73,32 +64,35 @@ class SyncControllerSpec
7364
}
7465
}
7566

76-
it should "download better pivot block, request state, blocks and finish when downloaded" in new TestSetup() {
77-
startWithState(defaultStateBeforeNodeRestart)
67+
it should "download better pivot block, request state, blocks and finish when downloaded" in withTestSetup() {
68+
testSetup =>
69+
import testSetup._
70+
startWithState(defaultStateBeforeNodeRestart)
7871

79-
syncController ! SyncProtocol.Start
72+
syncController ! SyncProtocol.Start
8073

81-
val handshakedPeers = HandshakedPeers(singlePeer)
74+
val handshakedPeers = HandshakedPeers(singlePeer)
8275

83-
val newBlocks =
84-
getHeaders(defaultStateBeforeNodeRestart.bestBlockHeaderNumber + 1, syncConfig.blockHeadersPerRequest)
76+
val newBlocks =
77+
getHeaders(defaultStateBeforeNodeRestart.bestBlockHeaderNumber + 1, syncConfig.blockHeadersPerRequest)
8578

86-
setupAutoPilot(etcPeerManager, handshakedPeers, defaultPivotBlockHeader, BlockchainData(newBlocks))
79+
setupAutoPilot(etcPeerManager, handshakedPeers, defaultPivotBlockHeader, BlockchainData(newBlocks))
8780

88-
val watcher = TestProbe()
89-
watcher.watch(syncController)
81+
val watcher = TestProbe()
82+
watcher.watch(syncController)
9083

91-
eventually {
92-
someTimePasses()
93-
//switch to regular download
94-
val children = syncController.children
95-
assert(storagesInstance.storages.appStateStorage.isFastSyncDone())
96-
assert(children.exists(ref => ref.path.name == "regular-sync"))
97-
assert(blockchain.getBestBlockNumber() == defaultPivotBlockHeader.number)
98-
}
84+
eventually {
85+
someTimePasses()
86+
//switch to regular download
87+
val children = syncController.children
88+
assert(storagesInstance.storages.appStateStorage.isFastSyncDone())
89+
assert(children.exists(ref => ref.path.name == "regular-sync"))
90+
assert(blockchain.getBestBlockNumber() == defaultPivotBlockHeader.number)
91+
}
9992
}
10093

101-
it should "gracefully handle receiving empty receipts while syncing" in new TestSetup() {
94+
it should "gracefully handle receiving empty receipts while syncing" in withTestSetup() { testSetup =>
95+
import testSetup._
10296
startWithState(defaultStateBeforeNodeRestart)
10397

10498
syncController ! SyncProtocol.Start
@@ -128,13 +122,14 @@ class SyncControllerSpec
128122
}
129123
}
130124

131-
it should "handle blocks that fail validation" in new TestSetup(
132-
_validators = new Mocks.MockValidatorsAlwaysSucceed {
125+
it should "handle blocks that fail validation" in withTestSetup(
126+
new Mocks.MockValidatorsAlwaysSucceed {
133127
override val blockHeaderValidator: BlockHeaderValidator = { (blockHeader, getBlockHeaderByHash) =>
134128
Left(HeaderPoWError)
135129
}
136130
}
137-
) {
131+
) { testSetup =>
132+
import testSetup._
138133
startWithState(
139134
defaultStateBeforeNodeRestart.copy(nextBlockToFullyValidate =
140135
defaultStateBeforeNodeRestart.bestBlockHeaderNumber + 1
@@ -163,7 +158,8 @@ class SyncControllerSpec
163158
}
164159
}
165160

166-
it should "rewind fast-sync state if received header have no known parent" in new TestSetup() {
161+
it should "rewind fast-sync state if received header have no known parent" in withTestSetup() { testSetup =>
162+
import testSetup._
167163
startWithState(defaultStateBeforeNodeRestart)
168164

169165
syncController ! SyncProtocol.Start
@@ -192,7 +188,8 @@ class SyncControllerSpec
192188
}
193189
}
194190

195-
it should "not change best block after receiving faraway block" in new TestSetup {
191+
it should "not change best block after receiving faraway block" in withTestSetup() { testSetup =>
192+
import testSetup._
196193

197194
startWithState(defaultStateBeforeNodeRestart)
198195

@@ -226,7 +223,7 @@ class SyncControllerSpec
226223
}
227224
}
228225

229-
it should "update pivot block if pivot fail" in new TestSetup(_validators = new Mocks.MockValidatorsAlwaysSucceed {
226+
it should "update pivot block if pivot fail" in withTestSetup(new Mocks.MockValidatorsAlwaysSucceed {
230227
override val blockHeaderValidator: BlockHeaderValidator = { (blockHeader, getBlockHeaderByHash) =>
231228
{
232229
if (blockHeader.number != 399500 + 10) {
@@ -236,7 +233,8 @@ class SyncControllerSpec
236233
}
237234
}
238235
}
239-
}) {
236+
}) { testSetup =>
237+
import testSetup._
240238
startWithState(defaultStateBeforeNodeRestart)
241239

242240
syncController ! SyncProtocol.Start
@@ -275,7 +273,8 @@ class SyncControllerSpec
275273
}
276274
}
277275

278-
it should "not process, out of date new pivot block" in new TestSetup() {
276+
it should "not process, out of date new pivot block" in withTestSetup() { testSetup =>
277+
import testSetup._
279278
startWithState(defaultStateBeforeNodeRestart)
280279
syncController ! SyncProtocol.Start
281280

@@ -309,7 +308,8 @@ class SyncControllerSpec
309308
}
310309
}
311310

312-
it should "start state download only when pivot block is fresh enough" in new TestSetup() {
311+
it should "start state download only when pivot block is fresh enough" in withTestSetup() { testSetup =>
312+
import testSetup._
313313
startWithState(defaultStateBeforeNodeRestart)
314314
syncController ! SyncProtocol.Start
315315

@@ -356,7 +356,8 @@ class SyncControllerSpec
356356
}
357357
}
358358

359-
it should "re-enqueue block bodies when empty response is received" in new TestSetup {
359+
it should "re-enqueue block bodies when empty response is received" in withTestSetup() { testSetup =>
360+
import testSetup._
360361

361362
startWithState(defaultStateBeforeNodeRestart)
362363

@@ -387,7 +388,8 @@ class SyncControllerSpec
387388
}
388389
}
389390

390-
it should "update pivot block during state sync if it goes stale" in new TestSetup() {
391+
it should "update pivot block during state sync if it goes stale" in withTestSetup() { testSetup =>
392+
import testSetup._
391393
startWithState(defaultStateBeforeNodeRestart)
392394

393395
syncController ! SyncProtocol.Start
@@ -448,7 +450,6 @@ class SyncControllerSpec
448450
}
449451

450452
class TestSetup(
451-
blocksForWhichLedgerFails: Seq[BigInt] = Nil,
452453
_validators: Validators = new Mocks.MockValidatorsAlwaysSucceed
453454
) extends EphemBlockchainTestSetup
454455
with TestSyncPeers
@@ -458,7 +459,8 @@ class SyncControllerSpec
458459
var stateDownloadStarted = false
459460

460461
//+ cake overrides
461-
override implicit lazy val system: ActorSystem = SyncControllerSpec.this.system
462+
override implicit lazy val system: ActorSystem =
463+
ActorSystem("SyncControllerSpec_System", ConfigFactory.load("explicit-scheduler"))
462464

463465
override lazy val vm: VMImpl = new VMImpl
464466

@@ -734,5 +736,16 @@ class SyncControllerSpec
734736
def someTimePasses() =
735737
testScheduler.timePasses(3000.millis)
736738

739+
def cleanup(): Unit =
740+
Await.result(system.terminate(), 10.seconds)
741+
}
742+
743+
def withTestSetup(validators: Validators = new Mocks.MockValidatorsAlwaysSucceed)(test: TestSetup => Any): Unit = {
744+
val testSetup = new TestSetup(validators)
745+
try {
746+
test(testSetup)
747+
} finally {
748+
testSetup.cleanup()
749+
}
737750
}
738751
}

0 commit comments

Comments
 (0)