MCPcopy Create free account
hub / github.com/CPChain/chain / TestPendingDeduplication

Function TestPendingDeduplication

protocols/cpc/fetcher/fetcher_test.go:362–397  ·  view source on GitHub ↗

Tests that announces already being retrieved will not be duplicated.

(t *testing.T)

Source from the content-addressed store, hash-verified

360
361// Tests that announces already being retrieved will not be duplicated.
362func TestPendingDeduplication(t *testing.T) {
363 // Create a hash and corresponding block
364 hashes, blocks := makeChain(1, 0, genesis)
365
366 // Assemble a tester with a built in counter and delayed fetcher
367 tester := newTester()
368 headerFetcher := tester.makeHeaderFetcher("repeater", blocks, -gatherSlack)
369 bodyFetcher := tester.makeBodyFetcher("repeater", blocks, 0)
370
371 delay := 50 * time.Millisecond
372 counter := uint32(0)
373 headerWrapper := func(hash common.Hash) error {
374 atomic.AddUint32(&counter, 1)
375
376 // Simulate a long running fetch
377 go func() {
378 time.Sleep(delay)
379 headerFetcher(hash)
380 }()
381 return nil
382 }
383 // Announce the same block many times until it's fetched (wait for any pending ops)
384 for tester.getBlock(hashes[0]) == nil {
385 tester.fetcher.Notify("repeater", hashes[0], 1, time.Now().Add(-arriveTimeout), headerWrapper, bodyFetcher)
386 time.Sleep(time.Millisecond)
387 }
388 time.Sleep(delay)
389
390 // Check that all blocks were imported and none fetched twice
391 if imported := len(tester.blocks); imported != 2 {
392 t.Fatalf("synchronised block mismatch: have %v, want %v", imported, 2)
393 }
394 if int(counter) != 1 {
395 t.Fatalf("retrieval count mismatch: have %v, want %v", counter, 1)
396 }
397}
398
399// Tests that announcements retrieved in a random order are cached and eventually
400// imported when all the gaps are filled in.

Callers

nothing calls this directly

Calls 9

makeChainFunction · 0.85
newTesterFunction · 0.85
makeHeaderFetcherMethod · 0.80
makeBodyFetcherMethod · 0.80
SleepMethod · 0.80
AddMethod · 0.65
NowMethod · 0.65
getBlockMethod · 0.45
NotifyMethod · 0.45

Tested by

no test coverage detected