2018-10-08 16:09:37 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package kademlia
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-11-29 18:39:27 +00:00
|
|
|
|
|
|
|
"storj.io/storj/internal/teststorj"
|
2018-10-08 16:09:37 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
|
|
|
)
|
|
|
|
|
2018-10-24 13:24:47 +01:00
|
|
|
func TestXorQueue(t *testing.T) {
|
2018-11-29 18:39:27 +00:00
|
|
|
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}
|
2018-10-08 16:09:37 +01:00
|
|
|
|
2018-10-24 13:24:47 +01:00
|
|
|
nodes := make([]*pb.Node, len(testValues))
|
2018-11-29 18:39:27 +00:00
|
|
|
for i, v := range testValues {
|
|
|
|
nodes[i] = &pb.Node{Id: teststorj.NodeIDFromBytes([]byte{v})}
|
2018-10-08 16:09:37 +01:00
|
|
|
}
|
2018-11-29 18:39:27 +00:00
|
|
|
// populate queue
|
2018-10-24 13:24:47 +01:00
|
|
|
pq := NewXorQueue(3)
|
2018-11-29 18:39:27 +00:00
|
|
|
pq.Insert(target, nodes)
|
|
|
|
// make sure we remove as many things as the queue should hold
|
2018-10-24 13:24:47 +01:00
|
|
|
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)
|
2018-11-29 18:39:27 +00:00
|
|
|
assert.Equal(t, []byte{expectedIds[i]}, node.Id[:1])
|
2018-10-24 13:24:47 +01:00
|
|
|
}
|
2018-11-29 18:39:27 +00:00
|
|
|
// test that reading beyong length returns nil
|
2018-10-24 13:24:47 +01:00
|
|
|
node, _ := pq.Closest()
|
|
|
|
assert.Nil(t, node)
|
2018-10-08 16:09:37 +01:00
|
|
|
}
|