2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-08-17 20:11:46 +01:00
|
|
|
// See LICENSE for copying information
|
|
|
|
|
|
|
|
package kademlia
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-09-18 05:39:06 +01:00
|
|
|
|
2019-02-22 18:39:29 +00:00
|
|
|
"storj.io/storj/internal/testcontext"
|
2018-11-29 18:39:27 +00:00
|
|
|
"storj.io/storj/internal/teststorj"
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2019-01-02 18:57:11 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-08-17 20:11:46 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAddToReplacementCache(t *testing.T) {
|
2019-02-22 18:39:29 +00:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
rt := createRoutingTable(storj.NodeID{244, 255})
|
|
|
|
defer ctx.Check(rt.Close)
|
2019-01-02 18:57:11 +00:00
|
|
|
|
|
|
|
kadBucketID := bucketID{255, 255}
|
2018-11-29 18:39:27 +00:00
|
|
|
node1 := teststorj.MockNode(string([]byte{233, 255}))
|
2018-08-17 20:11:46 +01:00
|
|
|
rt.addToReplacementCache(kadBucketID, node1)
|
2018-11-29 18:39:27 +00:00
|
|
|
assert.Equal(t, []*pb.Node{node1}, rt.replacementCache[kadBucketID])
|
2019-01-02 18:57:11 +00:00
|
|
|
kadBucketID2 := bucketID{127, 255}
|
2018-11-29 18:39:27 +00:00
|
|
|
node2 := teststorj.MockNode(string([]byte{100, 255}))
|
|
|
|
node3 := teststorj.MockNode(string([]byte{90, 255}))
|
|
|
|
node4 := teststorj.MockNode(string([]byte{80, 255}))
|
2018-08-17 20:11:46 +01:00
|
|
|
rt.addToReplacementCache(kadBucketID2, node2)
|
|
|
|
rt.addToReplacementCache(kadBucketID2, node3)
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
assert.Equal(t, []*pb.Node{node1}, rt.replacementCache[kadBucketID])
|
|
|
|
assert.Equal(t, []*pb.Node{node2, node3}, rt.replacementCache[kadBucketID2])
|
2018-08-17 20:11:46 +01:00
|
|
|
rt.addToReplacementCache(kadBucketID2, node4)
|
2018-11-29 18:39:27 +00:00
|
|
|
assert.Equal(t, []*pb.Node{node3, node4}, rt.replacementCache[kadBucketID2])
|
2018-08-17 20:11:46 +01:00
|
|
|
}
|
2019-02-22 18:39:29 +00:00
|
|
|
|
|
|
|
func TestRemoveFromReplacementCache(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
rt := createRoutingTableWith(storj.NodeID{244, 255}, routingTableOpts{cacheSize: 3})
|
|
|
|
defer ctx.Check(rt.Close)
|
|
|
|
|
|
|
|
kadBucketID2 := bucketID{127, 255}
|
|
|
|
node2 := teststorj.MockNode(string([]byte{100, 255}))
|
|
|
|
node3 := teststorj.MockNode(string([]byte{90, 255}))
|
|
|
|
node4 := teststorj.MockNode(string([]byte{80, 255}))
|
|
|
|
rt.addToReplacementCache(kadBucketID2, node2)
|
|
|
|
rt.addToReplacementCache(kadBucketID2, node3)
|
|
|
|
rt.addToReplacementCache(kadBucketID2, node4)
|
|
|
|
assert.Equal(t, []*pb.Node{node2, node3, node4}, rt.replacementCache[kadBucketID2])
|
|
|
|
rt.removeFromReplacementCache(kadBucketID2, node3)
|
|
|
|
assert.Equal(t, []*pb.Node{node2, node4}, rt.replacementCache[kadBucketID2])
|
|
|
|
rt.removeFromReplacementCache(kadBucketID2, node2)
|
|
|
|
assert.Equal(t, []*pb.Node{node4}, rt.replacementCache[kadBucketID2])
|
|
|
|
rt.removeFromReplacementCache(kadBucketID2, node4)
|
|
|
|
assert.Equal(t, []*pb.Node{}, rt.replacementCache[kadBucketID2])
|
|
|
|
}
|