storj/pkg/kademlia/queue_test.go
Bryan White 2a0c4e60d2
preparing for use of customtype gogo extension with NodeID type (#693)
* preparing for use of `customtype` gogo extension with `NodeID` type

* review changes

* preparing for use of `customtype` gogo extension with `NodeID` type

* review changes

* wip

* tests passing

* wip fixing tests

* more wip test fixing

* remove NodeIDList from proto files

* linter fixes

* linter fixes

* linter/review fixes

* more freaking linter fixes

* omg just kill me - linterrrrrrrr

* travis linter, i will muder you and your family in your sleep

* goimports everything - burn in hell travis

* goimports update

* go mod tidy
2018-11-29 19:39:27 +01:00

40 lines
1.1 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package kademlia
import (
"math/big"
"testing"
"github.com/stretchr/testify/assert"
"storj.io/storj/internal/teststorj"
"storj.io/storj/pkg/pb"
)
func TestXorQueue(t *testing.T) {
target := teststorj.NodeIDFromBytes([]byte{1})
testValues := []byte{3, 6, 7, 8} // 0011, 0110, 0111, 1000
expectedPriority := []int{2, 6, 7, 9} // 0010=>2, 0111=>7, 0110=>6, 1001=>9
expectedIds := []byte{3, 7, 6, 8}
nodes := make([]*pb.Node, len(testValues))
for i, v := range testValues {
nodes[i] = &pb.Node{Id: teststorj.NodeIDFromBytes([]byte{v})}
}
// populate queue
pq := NewXorQueue(3)
pq.Insert(target, nodes)
// make sure we remove as many things as the queue should hold
assert.Equal(t, pq.Len(), 3)
for i := 0; pq.Len() > 0; i++ {
node, priority := pq.Closest()
assert.Equal(t, *big.NewInt(int64(expectedPriority[i])), priority)
assert.Equal(t, []byte{expectedIds[i]}, node.Id[:1])
}
// test that reading beyong length returns nil
node, _ := pq.Closest()
assert.Nil(t, node)
}