@@ -48,17 +48,8 @@ class SyncControllerSpec
48
48
with Eventually
49
49
with NormalPatience {
50
50
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 ._
62
53
syncController ! SyncProtocol .Start
63
54
64
55
val handshakedPeers = HandshakedPeers (twoAcceptedPeers)
@@ -73,32 +64,35 @@ class SyncControllerSpec
73
64
}
74
65
}
75
66
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)
78
71
79
- syncController ! SyncProtocol .Start
72
+ syncController ! SyncProtocol .Start
80
73
81
- val handshakedPeers = HandshakedPeers (singlePeer)
74
+ val handshakedPeers = HandshakedPeers (singlePeer)
82
75
83
- val newBlocks =
84
- getHeaders(defaultStateBeforeNodeRestart.bestBlockHeaderNumber + 1 , syncConfig.blockHeadersPerRequest)
76
+ val newBlocks =
77
+ getHeaders(defaultStateBeforeNodeRestart.bestBlockHeaderNumber + 1 , syncConfig.blockHeadersPerRequest)
85
78
86
- setupAutoPilot(etcPeerManager, handshakedPeers, defaultPivotBlockHeader, BlockchainData (newBlocks))
79
+ setupAutoPilot(etcPeerManager, handshakedPeers, defaultPivotBlockHeader, BlockchainData (newBlocks))
87
80
88
- val watcher = TestProbe ()
89
- watcher.watch(syncController)
81
+ val watcher = TestProbe ()
82
+ watcher.watch(syncController)
90
83
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
+ }
99
92
}
100
93
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 ._
102
96
startWithState(defaultStateBeforeNodeRestart)
103
97
104
98
syncController ! SyncProtocol .Start
@@ -128,13 +122,14 @@ class SyncControllerSpec
128
122
}
129
123
}
130
124
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 {
133
127
override val blockHeaderValidator : BlockHeaderValidator = { (blockHeader, getBlockHeaderByHash) =>
134
128
Left (HeaderPoWError )
135
129
}
136
130
}
137
- ) {
131
+ ) { testSetup =>
132
+ import testSetup ._
138
133
startWithState(
139
134
defaultStateBeforeNodeRestart.copy(nextBlockToFullyValidate =
140
135
defaultStateBeforeNodeRestart.bestBlockHeaderNumber + 1
@@ -163,7 +158,8 @@ class SyncControllerSpec
163
158
}
164
159
}
165
160
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 ._
167
163
startWithState(defaultStateBeforeNodeRestart)
168
164
169
165
syncController ! SyncProtocol .Start
@@ -192,7 +188,8 @@ class SyncControllerSpec
192
188
}
193
189
}
194
190
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 ._
196
193
197
194
startWithState(defaultStateBeforeNodeRestart)
198
195
@@ -226,7 +223,7 @@ class SyncControllerSpec
226
223
}
227
224
}
228
225
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 {
230
227
override val blockHeaderValidator : BlockHeaderValidator = { (blockHeader, getBlockHeaderByHash) =>
231
228
{
232
229
if (blockHeader.number != 399500 + 10 ) {
@@ -236,7 +233,8 @@ class SyncControllerSpec
236
233
}
237
234
}
238
235
}
239
- }) {
236
+ }) { testSetup =>
237
+ import testSetup ._
240
238
startWithState(defaultStateBeforeNodeRestart)
241
239
242
240
syncController ! SyncProtocol .Start
@@ -275,7 +273,8 @@ class SyncControllerSpec
275
273
}
276
274
}
277
275
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 ._
279
278
startWithState(defaultStateBeforeNodeRestart)
280
279
syncController ! SyncProtocol .Start
281
280
@@ -309,7 +308,8 @@ class SyncControllerSpec
309
308
}
310
309
}
311
310
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 ._
313
313
startWithState(defaultStateBeforeNodeRestart)
314
314
syncController ! SyncProtocol .Start
315
315
@@ -356,7 +356,8 @@ class SyncControllerSpec
356
356
}
357
357
}
358
358
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 ._
360
361
361
362
startWithState(defaultStateBeforeNodeRestart)
362
363
@@ -387,7 +388,8 @@ class SyncControllerSpec
387
388
}
388
389
}
389
390
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 ._
391
393
startWithState(defaultStateBeforeNodeRestart)
392
394
393
395
syncController ! SyncProtocol .Start
@@ -448,7 +450,6 @@ class SyncControllerSpec
448
450
}
449
451
450
452
class TestSetup (
451
- blocksForWhichLedgerFails : Seq [BigInt ] = Nil ,
452
453
_validators : Validators = new Mocks .MockValidatorsAlwaysSucceed
453
454
) extends EphemBlockchainTestSetup
454
455
with TestSyncPeers
@@ -458,7 +459,8 @@ class SyncControllerSpec
458
459
var stateDownloadStarted = false
459
460
460
461
// + 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" ))
462
464
463
465
override lazy val vm : VMImpl = new VMImpl
464
466
@@ -734,5 +736,16 @@ class SyncControllerSpec
734
736
def someTimePasses () =
735
737
testScheduler.timePasses(3000 .millis)
736
738
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
+ }
737
750
}
738
751
}
0 commit comments