dee2c137c8
* slowly but surely * hardcode ID for tests so we can get predictable results * skipping bad test * removing tests of bkad * wip * new algorithm for worker * clean up * remove skipped test * changes * uncomment * fixed conflicts * maybe done ? * cleanup * boot bkad * wip * cleanup * undo change * fixes * wip * wip * moving nodeID around * wip * wip * fixes * fixes after merge * added TODO * fixed tests post identity * linter fixes * wip * PR review comments * wip * fixing tests * fix tests * force db directory * bad test * fixes race condition * small cleanups * adding db folder * testing * wip * cleanup * cleanup * linters * export Restrict * add timeout * testing * linters * forgot one * moar fixes from master merge * PR comments * moar PR comments * removed stun flag * remove duplicate declaration * remove old tests * remove timeout * fix tests * missed one * changed StringToID >> IDFromString * PR comments * stupid linter * moevd overlay mock * fixed merge conflicts * fixes * linter
63 lines
1.1 KiB
Go
63 lines
1.1 KiB
Go
// Copyright (C) 2018 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package kademlia
|
|
|
|
import (
|
|
"container/heap"
|
|
"math/big"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"storj.io/storj/pkg/pb"
|
|
)
|
|
|
|
func TestPriorityQueue(t *testing.T) {
|
|
cases := []struct {
|
|
target *big.Int
|
|
nodes map[string]*pb.Node
|
|
pq PriorityQueue
|
|
expected []int
|
|
}{
|
|
{
|
|
target: func() *big.Int {
|
|
i, ok := new(big.Int).SetString("0001", 2)
|
|
assert.True(t, ok)
|
|
return i
|
|
}(),
|
|
nodes: map[string]*pb.Node{
|
|
"1001": &pb.Node{Id: "1001"},
|
|
"0100": &pb.Node{Id: "0100"},
|
|
"1100": &pb.Node{Id: "1100"},
|
|
"0010": &pb.Node{Id: "0010"},
|
|
},
|
|
pq: make(PriorityQueue, 4),
|
|
expected: []int{3, 5, 8, 13},
|
|
},
|
|
}
|
|
|
|
for _, v := range cases {
|
|
i := 0
|
|
for id, value := range v.nodes {
|
|
bn, ok := new(big.Int).SetString(id, 2)
|
|
assert.True(t, ok)
|
|
v.pq[i] = &Item{
|
|
value: value,
|
|
priority: new(big.Int).Xor(v.target, bn),
|
|
index: i,
|
|
}
|
|
i++
|
|
}
|
|
heap.Init(&v.pq)
|
|
|
|
i = 0
|
|
for v.pq.Len() > 0 {
|
|
item := heap.Pop(&v.pq).(*Item)
|
|
assert.Equal(t, big.NewInt(int64(v.expected[i])), item.priority)
|
|
i++
|
|
}
|
|
|
|
}
|
|
|
|
}
|