@@ -35,14 +35,23 @@ import org.scalatest.time.{Seconds, Span}
35
35
36
36
import scala .concurrent .Await
37
37
import scala .concurrent .duration ._
38
+ import com .typesafe .config .ConfigFactory
39
+ import akka .testkit .ExplicitlyTriggeredScheduler
40
+ import io .iohk .ethereum .NormalPatience
38
41
39
42
// scalastyle:off file.size.limit
40
- class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter with MockFactory with Eventually {
43
+ class SyncControllerSpec
44
+ extends AnyFlatSpec
45
+ with Matchers
46
+ with BeforeAndAfter
47
+ with MockFactory
48
+ with Eventually
49
+ with NormalPatience {
41
50
42
51
implicit var system : ActorSystem = _
43
52
44
53
before {
45
- system = ActorSystem (" SyncControllerSpec_System" )
54
+ system = ActorSystem (" SyncControllerSpec_System" , ConfigFactory .load( " explicit-scheduler " ) )
46
55
}
47
56
48
57
after {
@@ -56,7 +65,8 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
56
65
57
66
setupAutoPilot(etcPeerManager, handshakedPeers, defaultPivotBlockHeader, BlockchainData (Seq ()))
58
67
59
- eventually(timeout = eventuallyTimeOut) {
68
+ eventually {
69
+ someTimePasses()
60
70
val syncState = storagesInstance.storages.fastSyncStateStorage.getSyncState().get
61
71
syncState.bestBlockHeaderNumber shouldBe 0
62
72
syncState.pivotBlock == defaultPivotBlockHeader
@@ -78,7 +88,8 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
78
88
val watcher = TestProbe ()
79
89
watcher.watch(syncController)
80
90
81
- eventually(timeout = eventuallyTimeOut) {
91
+ eventually {
92
+ someTimePasses()
82
93
// switch to regular download
83
94
val children = syncController.children
84
95
assert(storagesInstance.storages.appStateStorage.isFastSyncDone())
@@ -107,7 +118,8 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
107
118
failedReceiptsTries = 1
108
119
)
109
120
110
- eventually(timeout = eventuallyTimeOut) {
121
+ eventually {
122
+ someTimePasses()
111
123
assert(storagesInstance.storages.appStateStorage.isFastSyncDone())
112
124
// switch to regular download
113
125
val children = syncController.children
@@ -141,7 +153,8 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
141
153
val watcher = TestProbe ()
142
154
watcher.watch(syncController)
143
155
144
- eventually(timeout = eventuallyTimeOut) {
156
+ eventually {
157
+ someTimePasses()
145
158
val syncState = storagesInstance.storages.fastSyncStateStorage.getSyncState().get
146
159
syncState.bestBlockHeaderNumber shouldBe (defaultStateBeforeNodeRestart.bestBlockHeaderNumber - syncConfig.fastSyncBlockValidationN)
147
160
syncState.nextBlockToFullyValidate shouldBe (defaultStateBeforeNodeRestart.bestBlockHeaderNumber - syncConfig.fastSyncBlockValidationN + 1 )
@@ -169,7 +182,8 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
169
182
val watcher = TestProbe ()
170
183
watcher.watch(syncController)
171
184
172
- eventually(timeout = eventuallyTimeOut) {
185
+ eventually {
186
+ someTimePasses()
173
187
val syncState = storagesInstance.storages.fastSyncStateStorage.getSyncState().get
174
188
syncState.bestBlockHeaderNumber shouldBe (defaultStateBeforeNodeRestart.bestBlockHeaderNumber - syncConfig.fastSyncBlockValidationN)
175
189
syncState.nextBlockToFullyValidate shouldBe (defaultStateBeforeNodeRestart.bestBlockHeaderNumber - syncConfig.fastSyncBlockValidationN + 1 )
@@ -200,12 +214,14 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
200
214
implicit val ec = system.dispatcher
201
215
system.scheduler.scheduleAtFixedRate(0 .seconds, 0.1 .second, fast, futureHeadersMessage)
202
216
203
- eventually(timeout = eventuallyTimeOut) {
217
+ eventually {
218
+ someTimePasses()
204
219
storagesInstance.storages.fastSyncStateStorage.getSyncState().get.pivotBlock shouldBe defaultPivotBlockHeader
205
220
}
206
221
207
222
// even though we receive this future headers fast sync should finish
208
- eventually(timeout = eventuallyTimeOut) {
223
+ eventually {
224
+ someTimePasses()
209
225
assert(storagesInstance.storages.appStateStorage.isFastSyncDone())
210
226
}
211
227
}
@@ -238,7 +254,8 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
238
254
239
255
val autopilot = setupAutoPilot(etcPeerManager, handshakedPeers, defaultPivotBlockHeader, BlockchainData (newBlocks))
240
256
241
- eventually(timeout = eventuallyTimeOut) {
257
+ eventually {
258
+ littleTimePasses()
242
259
storagesInstance.storages.fastSyncStateStorage.getSyncState().get.pivotBlock shouldBe defaultPivotBlockHeader
243
260
}
244
261
@@ -247,7 +264,8 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
247
264
val watcher = TestProbe ()
248
265
watcher.watch(syncController)
249
266
250
- eventually(timeout = eventuallyTimeOut) {
267
+ eventually {
268
+ someTimePasses()
251
269
val syncState = storagesInstance.storages.fastSyncStateStorage.getSyncState().get
252
270
syncState.pivotBlock shouldBe newPivot
253
271
syncState.safeDownloadTarget shouldEqual newPivot.number + syncConfig.fastSyncBlockValidationX
@@ -278,13 +296,15 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
278
296
val pilot =
279
297
setupAutoPilot(etcPeerManager, staleHandshakedPeers, staleHeader, BlockchainData (newBlocks), onlyPivot = true )
280
298
281
- eventually(timeout = eventuallyTimeOut) {
299
+ eventually {
300
+ someTimePasses()
282
301
storagesInstance.storages.fastSyncStateStorage.getSyncState().get.pivotBlockUpdateFailures shouldBe 1
283
302
}
284
303
285
304
pilot.updateAutoPilot(freshHandshakedPeers, freshHeader, BlockchainData (newBlocks), onlyPivot = true )
286
305
287
- eventually(timeout = eventuallyTimeOut) {
306
+ eventually {
307
+ someTimePasses()
288
308
storagesInstance.storages.fastSyncStateStorage.getSyncState().get.pivotBlock shouldBe defaultPivotBlockHeader
289
309
}
290
310
}
@@ -303,7 +323,8 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
303
323
val newBlocks = getHeaders(defaultStateBeforeNodeRestart.bestBlockHeaderNumber + 1 , 50 )
304
324
305
325
val pilot = setupAutoPilot(etcPeerManager, freshHandshakedPeers, freshHeader, BlockchainData (newBlocks))
306
- eventually(timeout = longeventuallyTimeOut) {
326
+ eventually {
327
+ someTimePasses()
307
328
storagesInstance.storages.fastSyncStateStorage
308
329
.getSyncState()
309
330
.get
@@ -317,14 +338,16 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
317
338
// set up new received header previously received header will need update
318
339
pilot.updateAutoPilot(freshHandshakedPeers1, freshHeader1, BlockchainData (newBlocks))
319
340
320
- eventually(timeout = longeventuallyTimeOut) {
341
+ eventually {
342
+ someTimePasses()
321
343
storagesInstance.storages.fastSyncStateStorage
322
344
.getSyncState()
323
345
.get
324
346
.bestBlockHeaderNumber shouldBe freshHeader1.number + syncConfig.fastSyncBlockValidationX
325
347
}
326
348
327
- eventually(timeout = longeventuallyTimeOut) {
349
+ eventually {
350
+ someTimePasses()
328
351
assert(storagesInstance.storages.appStateStorage.isFastSyncDone())
329
352
// switch to regular download
330
353
val children = syncController.children
@@ -354,7 +377,8 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
354
377
failedBodiesTries = 1
355
378
)
356
379
357
- eventually(timeout = eventuallyTimeOut) {
380
+ eventually {
381
+ someTimePasses()
358
382
assert(storagesInstance.storages.appStateStorage.isFastSyncDone())
359
383
// switch to regular download
360
384
val children = syncController.children
@@ -382,7 +406,8 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
382
406
)
383
407
384
408
// choose first pivot and as it is fresh enough start state sync
385
- eventually(timeout = eventuallyTimeOut) {
409
+ eventually {
410
+ someTimePasses()
386
411
val syncState = storagesInstance.storages.fastSyncStateStorage.getSyncState().get
387
412
syncState.isBlockchainWorkFinished shouldBe true
388
413
syncState.updatingPivotBlock shouldBe false
@@ -400,7 +425,8 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
400
425
)
401
426
402
427
// sync to new pivot
403
- eventually(timeout = eventuallyTimeOut) {
428
+ eventually {
429
+ someTimePasses()
404
430
val syncState = storagesInstance.storages.fastSyncStateStorage.getSyncState().get
405
431
syncState.pivotBlock shouldBe newPivot
406
432
}
@@ -411,7 +437,8 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
411
437
val watcher = TestProbe ()
412
438
watcher.watch(syncController)
413
439
414
- eventually(timeout = longeventuallyTimeOut) {
440
+ eventually {
441
+ someTimePasses()
415
442
// switch to regular download
416
443
val children = syncController.children
417
444
assert(storagesInstance.storages.appStateStorage.isFastSyncDone())
@@ -430,8 +457,6 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
430
457
@ volatile
431
458
var stateDownloadStarted = false
432
459
433
- val eventuallyTimeOut : Timeout = Timeout (Span (10 , Seconds ))
434
- val longeventuallyTimeOut = Timeout (Span (30 , Seconds ))
435
460
// + cake overrides
436
461
override implicit lazy val system : ActorSystem = SyncControllerSpec .this .system
437
462
@@ -493,8 +518,6 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
493
518
494
519
blockchain.storeChainWeight(baseBlockHeader.parentHash, ChainWeight .zero).commit()
495
520
496
- val startDelayMillis = 200
497
-
498
521
case class BlockchainData (
499
522
headers : Map [BigInt , BlockHeader ],
500
523
bodies : Map [ByteString , BlockBody ],
@@ -703,10 +726,13 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
703
726
storagesInstance.storages.fastSyncStateStorage.putSyncState(state)
704
727
}
705
728
706
- def persistState (): Unit = {
707
- Thread .sleep(300 )
708
- syncController.getSingleChild(" fast-sync" ) ! FastSync .PersistSyncState
709
- Thread .sleep(300 )
710
- }
729
+ private def testScheduler = system.scheduler.asInstanceOf [ExplicitlyTriggeredScheduler ]
730
+
731
+ def littleTimePasses () =
732
+ testScheduler.timePasses(100 .millis)
733
+
734
+ def someTimePasses () =
735
+ testScheduler.timePasses(3000 .millis)
736
+
711
737
}
712
738
}
0 commit comments