Skip to content

Commit 99ca4b6

Browse files
committed
eth/fetcher: clean up test assertions
1 parent b53f701 commit 99ca4b6

File tree

1 file changed

+49
-111
lines changed

1 file changed

+49
-111
lines changed

eth/fetcher/fetcher_test.go

Lines changed: 49 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,37 @@ func (f *fetcherTester) makeFetcher(blocks map[common.Hash]*types.Block) blockRe
159159
}
160160
}
161161

162+
// verifyImportEvent verifies that one single event arrive on an import channel.
163+
func verifyImportEvent(t *testing.T, imported chan *types.Block) {
164+
select {
165+
case <-imported:
166+
case <-time.After(time.Second):
167+
t.Fatalf("import timeout")
168+
}
169+
}
170+
171+
// verifyImportCount verifies that exactly count number of events arrive on an
172+
// import hook channel.
173+
func verifyImportCount(t *testing.T, imported chan *types.Block, count int) {
174+
for i := 0; i < count; i++ {
175+
select {
176+
case <-imported:
177+
case <-time.After(time.Second):
178+
t.Fatalf("block %d: import timeout", i)
179+
}
180+
}
181+
verifyImportDone(t, imported)
182+
}
183+
184+
// verifyImportDone verifies that no more events are arriving on an import channel.
185+
func verifyImportDone(t *testing.T, imported chan *types.Block) {
186+
select {
187+
case <-imported:
188+
t.Fatalf("extra block imported")
189+
case <-time.After(50 * time.Millisecond):
190+
}
191+
}
192+
162193
// Tests that a fetcher accepts block announcements and initiates retrievals for
163194
// them, successfully importing into the local chain.
164195
func TestSequentialAnnouncements(t *testing.T) {
@@ -176,18 +207,9 @@ func TestSequentialAnnouncements(t *testing.T) {
176207

177208
for i := len(hashes) - 2; i >= 0; i-- {
178209
tester.fetcher.Notify("valid", hashes[i], time.Now().Add(-arriveTimeout), fetcher)
179-
180-
select {
181-
case <-imported:
182-
case <-time.After(time.Second):
183-
t.Fatalf("block %d: import timeout", len(hashes)-i)
184-
}
185-
}
186-
select {
187-
case <-imported:
188-
t.Fatalf("extra block imported")
189-
case <-time.After(50 * time.Millisecond):
210+
verifyImportEvent(t, imported)
190211
}
212+
verifyImportDone(t, imported)
191213
}
192214

193215
// Tests that if blocks are announced by multiple peers (or even the same buggy
@@ -216,17 +238,10 @@ func TestConcurrentAnnouncements(t *testing.T) {
216238
tester.fetcher.Notify("second", hashes[i], time.Now().Add(-arriveTimeout+time.Millisecond), wrapper)
217239
tester.fetcher.Notify("second", hashes[i], time.Now().Add(-arriveTimeout-time.Millisecond), wrapper)
218240

219-
select {
220-
case <-imported:
221-
case <-time.After(time.Second):
222-
t.Fatalf("block %d: import timeout", len(hashes)-i)
223-
}
224-
}
225-
select {
226-
case <-imported:
227-
t.Fatalf("extra block imported")
228-
case <-time.After(50 * time.Millisecond):
241+
verifyImportEvent(t, imported)
229242
}
243+
verifyImportDone(t, imported)
244+
230245
// Make sure no blocks were retrieved twice
231246
if int(counter) != targetBlocks {
232247
t.Fatalf("retrieval count mismatch: have %v, want %v", counter, targetBlocks)
@@ -259,18 +274,7 @@ func TestOverlappingAnnouncements(t *testing.T) {
259274
}
260275
}
261276
// Wait for all the imports to complete and check count
262-
for i := 0; i < len(hashes)-1; i++ {
263-
select {
264-
case <-imported:
265-
case <-time.After(time.Second):
266-
t.Fatalf("block %d: import timeout", i)
267-
}
268-
}
269-
select {
270-
case <-imported:
271-
t.Fatalf("extra block imported")
272-
case <-time.After(50 * time.Millisecond):
273-
}
277+
verifyImportCount(t, imported, len(hashes)-1)
274278
}
275279

276280
// Tests that announces already being retrieved will not be duplicated.
@@ -334,19 +338,7 @@ func TestRandomArrivalImport(t *testing.T) {
334338
}
335339
// Finally announce the skipped entry and check full import
336340
tester.fetcher.Notify("valid", hashes[skip], time.Now().Add(-arriveTimeout), fetcher)
337-
338-
for i := 0; i < len(hashes)-1; i++ {
339-
select {
340-
case <-imported:
341-
case <-time.After(time.Second):
342-
t.Fatalf("block %d: import timeout", i)
343-
}
344-
}
345-
select {
346-
case <-imported:
347-
t.Fatalf("extra block imported")
348-
case <-time.After(50 * time.Millisecond):
349-
}
341+
verifyImportCount(t, imported, len(hashes)-1)
350342
}
351343

352344
// Tests that direct block enqueues (due to block propagation vs. hash announce)
@@ -372,19 +364,7 @@ func TestQueueGapFill(t *testing.T) {
372364
}
373365
// Fill the missing block directly as if propagated
374366
tester.fetcher.Enqueue("valid", blocks[hashes[skip]])
375-
376-
for i := 0; i < len(hashes)-1; i++ {
377-
select {
378-
case <-imported:
379-
case <-time.After(time.Second):
380-
t.Fatalf("block %d: import timeout", i)
381-
}
382-
}
383-
select {
384-
case <-imported:
385-
t.Fatalf("extra block imported")
386-
case <-time.After(50 * time.Millisecond):
387-
}
367+
verifyImportCount(t, imported, len(hashes)-1)
388368
}
389369

390370
// Tests that blocks arriving from various sources (multiple propagations, hash
@@ -419,16 +399,8 @@ func TestImportDeduplication(t *testing.T) {
419399

420400
// Fill the missing block directly as if propagated, and check import uniqueness
421401
tester.fetcher.Enqueue("valid", blocks[hashes[1]])
422-
for done := false; !done; {
423-
select {
424-
case <-imported:
425-
case <-time.After(50 * time.Millisecond):
426-
done = true
427-
}
428-
}
429-
if imported := len(tester.blocks); imported != 3 {
430-
t.Fatalf("synchronised block mismatch: have %v, want %v", imported, 3)
431-
}
402+
verifyImportCount(t, imported, 2)
403+
432404
if counter != 2 {
433405
t.Fatalf("import invocation count mismatch: have %v, want %v", counter, 2)
434406
}
@@ -491,32 +463,14 @@ func TestHashMemoryExhaustionAttack(t *testing.T) {
491463
t.Fatalf("queued announce count mismatch: have %d, want %d", len(tester.fetcher.announced), hashLimit+maxQueueDist)
492464
}
493465
// Wait for fetches to complete
494-
for i := 0; i < maxQueueDist; i++ {
495-
select {
496-
case <-imported:
497-
case <-time.After(time.Second):
498-
t.Fatalf("block %d: import timeout", i)
499-
}
500-
}
501-
select {
502-
case <-imported:
503-
t.Fatalf("extra block imported")
504-
case <-time.After(50 * time.Millisecond):
505-
}
466+
verifyImportCount(t, imported, maxQueueDist)
467+
506468
// Feed the remaining valid hashes to ensure DOS protection state remains clean
507469
for i := len(hashes) - maxQueueDist - 2; i >= 0; i-- {
508470
tester.fetcher.Notify("valid", hashes[i], time.Now().Add(-arriveTimeout), valid)
509-
select {
510-
case <-imported:
511-
case <-time.After(time.Second):
512-
t.Fatalf("block %d: import timeout", len(hashes)-i)
513-
}
514-
}
515-
select {
516-
case <-imported:
517-
t.Fatalf("extra block imported")
518-
case <-time.After(50 * time.Millisecond):
471+
verifyImportEvent(t, imported)
519472
}
473+
verifyImportDone(t, imported)
520474
}
521475

522476
// Tests that blocks sent to the fetcher (either through propagation or via hash
@@ -559,28 +513,12 @@ func TestBlockMemoryExhaustionAttack(t *testing.T) {
559513
}
560514
// Insert the missing piece (and sanity check the import)
561515
tester.fetcher.Enqueue("valid", blocks[hashes[len(hashes)-2]])
562-
for i := 0; i < maxQueueDist; i++ {
563-
select {
564-
case <-imported:
565-
case <-time.After(time.Second):
566-
t.Fatalf("block %d: import timeout", i)
567-
}
568-
}
569-
select {
570-
case <-imported:
571-
t.Fatalf("extra block imported")
572-
case <-time.After(50 * time.Millisecond):
573-
}
516+
verifyImportCount(t, imported, maxQueueDist)
517+
574518
// Insert the remaining blocks in chunks to ensure clean DOS protection
575519
for i := maxQueueDist; i < len(hashes)-1; i++ {
576520
tester.fetcher.Enqueue("valid", blocks[hashes[len(hashes)-2-i]])
577-
select {
578-
case <-imported:
579-
case <-time.After(time.Second):
580-
t.Fatalf("block %d: import timeout", len(hashes)-i)
581-
}
582-
}
583-
if imported := len(tester.blocks); imported != len(hashes) {
584-
t.Fatalf("synchronised block mismatch: have %v, want %v", imported, len(hashes))
521+
verifyImportEvent(t, imported)
585522
}
523+
verifyImportDone(t, imported)
586524
}

0 commit comments

Comments
 (0)