Skip to content

Commit 3ff04fd

Browse files
author
Jaap van der Plas
committed
ETCM-147 use explicitly triggered scheduler for SyncControllerSpec
1 parent 2f61024 commit 3ff04fd

File tree

2 files changed

+58
-29
lines changed

2 files changed

+58
-29
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include "application.conf"
2+
3+
akka.scheduler.implementation = "akka.testkit.ExplicitlyTriggeredScheduler"

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

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,23 @@ import org.scalatest.time.{Seconds, Span}
3535

3636
import scala.concurrent.Await
3737
import scala.concurrent.duration._
38+
import com.typesafe.config.ConfigFactory
39+
import akka.testkit.ExplicitlyTriggeredScheduler
40+
import io.iohk.ethereum.NormalPatience
3841

3942
// 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 {
4150

4251
implicit var system: ActorSystem = _
4352

4453
before {
45-
system = ActorSystem("SyncControllerSpec_System")
54+
system = ActorSystem("SyncControllerSpec_System", ConfigFactory.load("explicit-scheduler"))
4655
}
4756

4857
after {
@@ -56,7 +65,8 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
5665

5766
setupAutoPilot(etcPeerManager, handshakedPeers, defaultPivotBlockHeader, BlockchainData(Seq()))
5867

59-
eventually(timeout = eventuallyTimeOut) {
68+
eventually {
69+
someTimePasses()
6070
val syncState = storagesInstance.storages.fastSyncStateStorage.getSyncState().get
6171
syncState.bestBlockHeaderNumber shouldBe 0
6272
syncState.pivotBlock == defaultPivotBlockHeader
@@ -78,7 +88,8 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
7888
val watcher = TestProbe()
7989
watcher.watch(syncController)
8090

81-
eventually(timeout = eventuallyTimeOut) {
91+
eventually {
92+
someTimePasses()
8293
//switch to regular download
8394
val children = syncController.children
8495
assert(storagesInstance.storages.appStateStorage.isFastSyncDone())
@@ -107,7 +118,8 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
107118
failedReceiptsTries = 1
108119
)
109120

110-
eventually(timeout = eventuallyTimeOut) {
121+
eventually {
122+
someTimePasses()
111123
assert(storagesInstance.storages.appStateStorage.isFastSyncDone())
112124
//switch to regular download
113125
val children = syncController.children
@@ -141,7 +153,8 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
141153
val watcher = TestProbe()
142154
watcher.watch(syncController)
143155

144-
eventually(timeout = eventuallyTimeOut) {
156+
eventually {
157+
someTimePasses()
145158
val syncState = storagesInstance.storages.fastSyncStateStorage.getSyncState().get
146159
syncState.bestBlockHeaderNumber shouldBe (defaultStateBeforeNodeRestart.bestBlockHeaderNumber - syncConfig.fastSyncBlockValidationN)
147160
syncState.nextBlockToFullyValidate shouldBe (defaultStateBeforeNodeRestart.bestBlockHeaderNumber - syncConfig.fastSyncBlockValidationN + 1)
@@ -169,7 +182,8 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
169182
val watcher = TestProbe()
170183
watcher.watch(syncController)
171184

172-
eventually(timeout = eventuallyTimeOut) {
185+
eventually {
186+
someTimePasses()
173187
val syncState = storagesInstance.storages.fastSyncStateStorage.getSyncState().get
174188
syncState.bestBlockHeaderNumber shouldBe (defaultStateBeforeNodeRestart.bestBlockHeaderNumber - syncConfig.fastSyncBlockValidationN)
175189
syncState.nextBlockToFullyValidate shouldBe (defaultStateBeforeNodeRestart.bestBlockHeaderNumber - syncConfig.fastSyncBlockValidationN + 1)
@@ -200,12 +214,14 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
200214
implicit val ec = system.dispatcher
201215
system.scheduler.scheduleAtFixedRate(0.seconds, 0.1.second, fast, futureHeadersMessage)
202216

203-
eventually(timeout = eventuallyTimeOut) {
217+
eventually {
218+
someTimePasses()
204219
storagesInstance.storages.fastSyncStateStorage.getSyncState().get.pivotBlock shouldBe defaultPivotBlockHeader
205220
}
206221

207222
// even though we receive this future headers fast sync should finish
208-
eventually(timeout = eventuallyTimeOut) {
223+
eventually {
224+
someTimePasses()
209225
assert(storagesInstance.storages.appStateStorage.isFastSyncDone())
210226
}
211227
}
@@ -238,7 +254,8 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
238254

239255
val autopilot = setupAutoPilot(etcPeerManager, handshakedPeers, defaultPivotBlockHeader, BlockchainData(newBlocks))
240256

241-
eventually(timeout = eventuallyTimeOut) {
257+
eventually {
258+
littleTimePasses()
242259
storagesInstance.storages.fastSyncStateStorage.getSyncState().get.pivotBlock shouldBe defaultPivotBlockHeader
243260
}
244261

@@ -247,7 +264,8 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
247264
val watcher = TestProbe()
248265
watcher.watch(syncController)
249266

250-
eventually(timeout = eventuallyTimeOut) {
267+
eventually {
268+
someTimePasses()
251269
val syncState = storagesInstance.storages.fastSyncStateStorage.getSyncState().get
252270
syncState.pivotBlock shouldBe newPivot
253271
syncState.safeDownloadTarget shouldEqual newPivot.number + syncConfig.fastSyncBlockValidationX
@@ -278,13 +296,15 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
278296
val pilot =
279297
setupAutoPilot(etcPeerManager, staleHandshakedPeers, staleHeader, BlockchainData(newBlocks), onlyPivot = true)
280298

281-
eventually(timeout = eventuallyTimeOut) {
299+
eventually {
300+
someTimePasses()
282301
storagesInstance.storages.fastSyncStateStorage.getSyncState().get.pivotBlockUpdateFailures shouldBe 1
283302
}
284303

285304
pilot.updateAutoPilot(freshHandshakedPeers, freshHeader, BlockchainData(newBlocks), onlyPivot = true)
286305

287-
eventually(timeout = eventuallyTimeOut) {
306+
eventually {
307+
someTimePasses()
288308
storagesInstance.storages.fastSyncStateStorage.getSyncState().get.pivotBlock shouldBe defaultPivotBlockHeader
289309
}
290310
}
@@ -303,7 +323,8 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
303323
val newBlocks = getHeaders(defaultStateBeforeNodeRestart.bestBlockHeaderNumber + 1, 50)
304324

305325
val pilot = setupAutoPilot(etcPeerManager, freshHandshakedPeers, freshHeader, BlockchainData(newBlocks))
306-
eventually(timeout = longeventuallyTimeOut) {
326+
eventually {
327+
someTimePasses()
307328
storagesInstance.storages.fastSyncStateStorage
308329
.getSyncState()
309330
.get
@@ -317,14 +338,16 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
317338
// set up new received header previously received header will need update
318339
pilot.updateAutoPilot(freshHandshakedPeers1, freshHeader1, BlockchainData(newBlocks))
319340

320-
eventually(timeout = longeventuallyTimeOut) {
341+
eventually {
342+
someTimePasses()
321343
storagesInstance.storages.fastSyncStateStorage
322344
.getSyncState()
323345
.get
324346
.bestBlockHeaderNumber shouldBe freshHeader1.number + syncConfig.fastSyncBlockValidationX
325347
}
326348

327-
eventually(timeout = longeventuallyTimeOut) {
349+
eventually {
350+
someTimePasses()
328351
assert(storagesInstance.storages.appStateStorage.isFastSyncDone())
329352
//switch to regular download
330353
val children = syncController.children
@@ -354,7 +377,8 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
354377
failedBodiesTries = 1
355378
)
356379

357-
eventually(timeout = eventuallyTimeOut) {
380+
eventually {
381+
someTimePasses()
358382
assert(storagesInstance.storages.appStateStorage.isFastSyncDone())
359383
//switch to regular download
360384
val children = syncController.children
@@ -382,7 +406,8 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
382406
)
383407

384408
// choose first pivot and as it is fresh enough start state sync
385-
eventually(timeout = eventuallyTimeOut) {
409+
eventually {
410+
someTimePasses()
386411
val syncState = storagesInstance.storages.fastSyncStateStorage.getSyncState().get
387412
syncState.isBlockchainWorkFinished shouldBe true
388413
syncState.updatingPivotBlock shouldBe false
@@ -400,7 +425,8 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
400425
)
401426

402427
// sync to new pivot
403-
eventually(timeout = eventuallyTimeOut) {
428+
eventually {
429+
someTimePasses()
404430
val syncState = storagesInstance.storages.fastSyncStateStorage.getSyncState().get
405431
syncState.pivotBlock shouldBe newPivot
406432
}
@@ -411,7 +437,8 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
411437
val watcher = TestProbe()
412438
watcher.watch(syncController)
413439

414-
eventually(timeout = longeventuallyTimeOut) {
440+
eventually {
441+
someTimePasses()
415442
//switch to regular download
416443
val children = syncController.children
417444
assert(storagesInstance.storages.appStateStorage.isFastSyncDone())
@@ -430,8 +457,6 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
430457
@volatile
431458
var stateDownloadStarted = false
432459

433-
val eventuallyTimeOut: Timeout = Timeout(Span(10, Seconds))
434-
val longeventuallyTimeOut = Timeout(Span(30, Seconds))
435460
//+ cake overrides
436461
override implicit lazy val system: ActorSystem = SyncControllerSpec.this.system
437462

@@ -493,8 +518,6 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
493518

494519
blockchain.storeChainWeight(baseBlockHeader.parentHash, ChainWeight.zero).commit()
495520

496-
val startDelayMillis = 200
497-
498521
case class BlockchainData(
499522
headers: Map[BigInt, BlockHeader],
500523
bodies: Map[ByteString, BlockBody],
@@ -703,10 +726,13 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
703726
storagesInstance.storages.fastSyncStateStorage.putSyncState(state)
704727
}
705728

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+
711737
}
712738
}

0 commit comments

Comments
 (0)