2015-05-03 07:09:33 -05:00
|
|
|
package downloader
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"gopkg.in/fatih/set.v0"
|
|
|
|
)
|
|
|
|
|
|
|
|
func createHashSet(hashes []common.Hash) *set.Set {
|
|
|
|
hset := set.New()
|
|
|
|
|
|
|
|
for _, hash := range hashes {
|
|
|
|
hset.Add(hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
return hset
|
|
|
|
}
|
|
|
|
|
|
|
|
func createBlocksFromHashSet(hashes *set.Set) []*types.Block {
|
|
|
|
blocks := make([]*types.Block, hashes.Size())
|
|
|
|
|
|
|
|
var i int
|
|
|
|
hashes.Each(func(v interface{}) bool {
|
|
|
|
blocks[i] = createBlock(i, common.Hash{}, v.(common.Hash))
|
|
|
|
i++
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
return blocks
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChunking(t *testing.T) {
|
2015-05-06 07:32:53 -05:00
|
|
|
queue := newQueue()
|
2015-05-03 07:09:33 -05:00
|
|
|
peer1 := newPeer("peer1", common.Hash{}, nil, nil)
|
|
|
|
peer2 := newPeer("peer2", common.Hash{}, nil, nil)
|
|
|
|
|
|
|
|
// 99 + 1 (1 == known genesis hash)
|
|
|
|
hashes := createHashes(0, 99)
|
2015-05-06 07:32:53 -05:00
|
|
|
queue.Insert(hashes)
|
2015-05-03 07:09:33 -05:00
|
|
|
|
2015-05-06 07:32:53 -05:00
|
|
|
chunk1 := queue.Reserve(peer1, 99)
|
2015-05-03 07:09:33 -05:00
|
|
|
if chunk1 == nil {
|
|
|
|
t.Errorf("chunk1 is nil")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2015-05-06 07:32:53 -05:00
|
|
|
chunk2 := queue.Reserve(peer2, 99)
|
2015-05-03 07:09:33 -05:00
|
|
|
if chunk2 == nil {
|
|
|
|
t.Errorf("chunk2 is nil")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2015-05-06 07:32:53 -05:00
|
|
|
if len(chunk1.Hashes) != 99 {
|
|
|
|
t.Error("expected chunk1 hashes to be 99, got", len(chunk1.Hashes))
|
2015-05-03 07:09:33 -05:00
|
|
|
}
|
|
|
|
|
2015-05-06 07:32:53 -05:00
|
|
|
if len(chunk2.Hashes) != 1 {
|
|
|
|
t.Error("expected chunk1 hashes to be 1, got", len(chunk2.Hashes))
|
2015-05-03 07:09:33 -05:00
|
|
|
}
|
|
|
|
}
|