queue_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package downloader
  2. import (
  3. "testing"
  4. "github.com/ethereum/go-ethereum/common"
  5. "github.com/ethereum/go-ethereum/core/types"
  6. "gopkg.in/fatih/set.v0"
  7. )
  8. func createHashSet(hashes []common.Hash) *set.Set {
  9. hset := set.New()
  10. for _, hash := range hashes {
  11. hset.Add(hash)
  12. }
  13. return hset
  14. }
  15. func createBlocksFromHashSet(hashes *set.Set) []*types.Block {
  16. blocks := make([]*types.Block, hashes.Size())
  17. var i int
  18. hashes.Each(func(v interface{}) bool {
  19. blocks[i] = createBlock(i, common.Hash{}, v.(common.Hash))
  20. i++
  21. return true
  22. })
  23. return blocks
  24. }
  25. func TestChunking(t *testing.T) {
  26. queue := newQueue()
  27. peer1 := newPeer("peer1", common.Hash{}, nil, nil)
  28. peer2 := newPeer("peer2", common.Hash{}, nil, nil)
  29. // 99 + 1 (1 == known genesis hash)
  30. hashes := createHashes(0, 99)
  31. queue.Insert(hashes)
  32. chunk1 := queue.Reserve(peer1, 99)
  33. if chunk1 == nil {
  34. t.Errorf("chunk1 is nil")
  35. t.FailNow()
  36. }
  37. chunk2 := queue.Reserve(peer2, 99)
  38. if chunk2 == nil {
  39. t.Errorf("chunk2 is nil")
  40. t.FailNow()
  41. }
  42. if len(chunk1.Hashes) != 99 {
  43. t.Error("expected chunk1 hashes to be 99, got", len(chunk1.Hashes))
  44. }
  45. if len(chunk2.Hashes) != 1 {
  46. t.Error("expected chunk1 hashes to be 1, got", len(chunk2.Hashes))
  47. }
  48. }