diff options
author | Péter Szilágyi <peterke@gmail.com> | 2015-06-16 20:35:15 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2015-06-18 20:56:07 +0800 |
commit | 8b64e041d6c41d76994510fdd8bb42ad7c5be5aa (patch) | |
tree | 516c496d83faebf47596b66aa89401479536a737 /eth/fetcher/fetcher_test.go | |
parent | 2a1b722d048d00401084c37a5ca12612f1dd5fcd (diff) | |
download | dexon-8b64e041d6c41d76994510fdd8bb42ad7c5be5aa.tar.gz dexon-8b64e041d6c41d76994510fdd8bb42ad7c5be5aa.tar.zst dexon-8b64e041d6c41d76994510fdd8bb42ad7c5be5aa.zip |
eth/fetcher: add test to check for duplicate downloads
Diffstat (limited to 'eth/fetcher/fetcher_test.go')
-rw-r--r-- | eth/fetcher/fetcher_test.go | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/eth/fetcher/fetcher_test.go b/eth/fetcher/fetcher_test.go index e29f9c7cd..44e99f30f 100644 --- a/eth/fetcher/fetcher_test.go +++ b/eth/fetcher/fetcher_test.go @@ -200,3 +200,41 @@ func TestOverlappingAnnouncements(t *testing.T) { t.Fatalf("synchronised block mismatch: have %v, want %v", imported, targetBlocks+1) } } + +// Tests that announces already being retrieved will not be duplicated. +func TestPendingDeduplication(t *testing.T) { + // Create a hash and corresponding block + hashes := createHashes(1, knownHash) + blocks := createBlocksFromHashes(hashes) + + // Assemble a tester with a built in counter and delayed fetcher + tester := newTester() + fetcher := tester.makeFetcher(blocks) + + delay := 50 * time.Millisecond + counter := uint32(0) + wrapper := func(hashes []common.Hash) error { + atomic.AddUint32(&counter, uint32(len(hashes))) + + // Simulate a long running fetch + go func() { + time.Sleep(delay) + fetcher(hashes) + }() + return nil + } + // Announce the same block many times until it's fetched (wait for any pending ops) + for !tester.hasBlock(hashes[0]) { + tester.fetcher.Notify("repeater", hashes[0], time.Now().Add(-arriveTimeout), wrapper) + time.Sleep(time.Millisecond) + } + time.Sleep(delay) + + // Check that all blocks were imported and none fetched twice + if imported := len(tester.ownBlocks); imported != 2 { + t.Fatalf("synchronised block mismatch: have %v, want %v", imported, 2) + } + if int(counter) != 1 { + t.Fatalf("retrieval count mismatch: have %v, want %v", counter, 1) + } +} |