Skip to content

Commit 0aa4dcb

Browse files
committed
[Kaizen] Unflake BlockFetcherSpec
1 parent da11b98 commit 0aa4dcb

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

src/test/scala/io/iohk/ethereum/blockchain/sync/regular/BlockFetcherSpec.scala

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package io.iohk.ethereum.blockchain.sync.regular
33
import java.net.InetSocketAddress
44

55
import akka.actor.ActorSystem
6-
import akka.actor.testkit.typed.scaladsl.ScalaTestWithActorTestKit
6+
import akka.actor.testkit.typed.scaladsl.ActorTestKit
77
import akka.actor.typed.ActorRef
88
import akka.actor.typed.scaladsl.adapter._
9+
import akka.testkit.TestKit
910
import akka.testkit.TestProbe
1011

1112
import scala.concurrent.ExecutionContext.Implicits.global
@@ -41,7 +42,7 @@ import io.iohk.ethereum.network.p2p.messages.ETH62._
4142
import io.iohk.ethereum.security.SecureRandomBuilder
4243
import io.iohk.ethereum.utils.Config
4344

44-
class BlockFetcherSpec extends ScalaTestWithActorTestKit() with AnyFreeSpecLike with Matchers with SecureRandomBuilder {
45+
class BlockFetcherSpec extends AnyFreeSpecLike with Matchers with SecureRandomBuilder {
4546

4647
"BlockFetcher" - {
4748

@@ -76,6 +77,7 @@ class BlockFetcherSpec extends ScalaTestWithActorTestKit() with AnyFreeSpecLike
7677
peersClient.send(refExpectingReply, PeersClient.Response(fakePeer, secondGetBlockHeadersResponse))
7778

7879
peersClient.expectMsgPF() { case PeersClient.Request(msg, _, _) if msg == firstGetBlockHeadersRequest => () }
80+
shutdownActorSystem()
7981
}
8082

8183
"should not requests headers upon invalidation while a request is already in progress, should resume after failure in response" in new TestSetup {
@@ -111,6 +113,7 @@ class BlockFetcherSpec extends ScalaTestWithActorTestKit() with AnyFreeSpecLike
111113

112114
peersClient.expectMsgClass(classOf[BlacklistPeer])
113115
peersClient.expectMsgPF() { case PeersClient.Request(msg, _, _) if msg == firstGetBlockHeadersRequest => () }
116+
shutdownActorSystem()
114117
}
115118

116119
"should not enqueue requested blocks if the received bodies do not match" in new TestSetup {
@@ -129,6 +132,7 @@ class BlockFetcherSpec extends ScalaTestWithActorTestKit() with AnyFreeSpecLike
129132
// Fetcher should not enqueue any new block
130133
importer.send(blockFetcher.toClassic, PickBlocks(syncConfig.blocksBatchSize, importer.ref))
131134
importer.expectNoMessage(100.millis)
135+
shutdownActorSystem()
132136
}
133137

134138
"should be able to handle block bodies received in several parts" in new TestSetup {
@@ -161,6 +165,7 @@ class BlockFetcherSpec extends ScalaTestWithActorTestKit() with AnyFreeSpecLike
161165
importer.expectMsgPF() { case BlockFetcher.PickedBlocks(blocks) =>
162166
blocks.map(_.hash).toList shouldEqual firstBlocksBatch.map(_.hash)
163167
}
168+
shutdownActorSystem()
164169
}
165170

166171
"should stop requesting, without blacklist the peer, in case empty bodies are received" in new TestSetup {
@@ -190,6 +195,7 @@ class BlockFetcherSpec extends ScalaTestWithActorTestKit() with AnyFreeSpecLike
190195
importer.expectMsgPF() { case BlockFetcher.PickedBlocks(blocks) =>
191196
blocks.map(_.hash).toList shouldEqual subChain1.map(_.hash)
192197
}
198+
shutdownActorSystem()
193199
}
194200

195201
"should ensure blocks passed to importer are always forming chain" in new TestSetup {
@@ -210,14 +216,20 @@ class BlockFetcherSpec extends ScalaTestWithActorTestKit() with AnyFreeSpecLike
210216
skip = 0,
211217
reverse = false
212218
)
213-
// Save the reference to respond to the ask pattern on fetcher
214-
val refForAnswerSecondHeaderReq = peersClient.expectMsgPF() {
215-
case PeersClient.Request(msg, _, _) if msg == secondGetBlockHeadersRequest => peersClient.lastSender
219+
220+
val msgs = peersClient.receiveWhile() {
221+
// Save the reference to respond to the ask pattern on fetcher
222+
case PeersClient.Request(`secondGetBlockHeadersRequest`, _, _) =>
223+
(secondGetBlockHeadersRequest, peersClient.lastSender)
224+
// First bodies request
225+
case PeersClient.Request(`firstGetBlockBodiesRequest`, _, _) =>
226+
(firstGetBlockBodiesRequest, peersClient.lastSender)
216227
}
217228

218-
// First bodies request
219-
val refForAnswerFirstBodiesReq = peersClient.expectMsgPF() {
220-
case PeersClient.Request(msg, _, _) if msg == firstGetBlockBodiesRequest => peersClient.lastSender
229+
val (refForAnswerSecondHeaderReq, refForAnswerFirstBodiesReq) = msgs match {
230+
case Seq((`secondGetBlockHeadersRequest`, s1), (`firstGetBlockBodiesRequest`, s2)) => (s1, s2)
231+
case Seq((`firstGetBlockBodiesRequest`, s2), (`secondGetBlockHeadersRequest`, s1)) => (s1, s2)
232+
case _ => fail("missing body or header request")
221233
}
222234

223235
// Block 16 is mined (we could have reached this stage due to invalidation messages sent to the fetcher)
@@ -249,9 +261,9 @@ class BlockFetcherSpec extends ScalaTestWithActorTestKit() with AnyFreeSpecLike
249261
importer.send(blockFetcher.toClassic, PickBlocks(syncConfig.blocksBatchSize, importer.ref))
250262
importer.expectMsgPF() { case BlockFetcher.PickedBlocks(blocks) =>
251263
val headers = blocks.map(_.header).toList
252-
253264
assert(HeadersSeq.areChain(headers))
254265
}
266+
shutdownActorSystem()
255267
}
256268

257269
"should properly handle a request timeout" in new TestSetup {
@@ -268,11 +280,13 @@ class BlockFetcherSpec extends ScalaTestWithActorTestKit() with AnyFreeSpecLike
268280
Thread.sleep((syncConfig.peerResponseTimeout + 2.seconds).toMillis)
269281

270282
peersClient.expectMsgPF() { case PeersClient.Request(`firstGetBlockHeadersRequest`, _, _) => () }
283+
shutdownActorSystem()
271284
}
272285
}
273286

274287
trait TestSetup extends TestSyncConfig {
275288
val as: ActorSystem = ActorSystem("BlockFetcherSpec_System")
289+
val atks: ActorTestKit = ActorTestKit(as.toTyped)
276290

277291
val time = new VirtualTime
278292

@@ -295,7 +309,7 @@ class BlockFetcherSpec extends ScalaTestWithActorTestKit() with AnyFreeSpecLike
295309
val fakePeerActor: TestProbe = TestProbe()(as)
296310
val fakePeer: Peer = Peer(PeerId("fakePeer"), new InetSocketAddress("127.0.0.1", 9000), fakePeerActor.ref, false)
297311

298-
lazy val blockFetcher: ActorRef[BlockFetcher.FetchCommand] = spawn(
312+
lazy val blockFetcher: ActorRef[BlockFetcher.FetchCommand] = atks.spawn(
299313
BlockFetcher(
300314
peersClient.ref,
301315
peerEventBus.ref,
@@ -318,6 +332,11 @@ class BlockFetcherSpec extends ScalaTestWithActorTestKit() with AnyFreeSpecLike
318332
)
319333
}
320334

335+
def shutdownActorSystem(): Unit = {
336+
atks.shutdownTestKit()
337+
TestKit.shutdownActorSystem(as, verifySystemShutdown = true)
338+
}
339+
321340
// Sending a far away block as a NewBlock message
322341
// Currently BlockFetcher only downloads first block-headers-per-request blocks without this
323342
def triggerFetching(startingNumber: BigInt = 1000): Unit = {

0 commit comments

Comments
 (0)