storj/pkg/kademlia/replacement_cache_test.go
Jennifer Li Johnson b5447c6608
Routing table tests (#1270)
This PR includes a new package called testrouting, which implements a very algorithmically slow but hopefully easy-to-keep-operationally-correct in-memory routing table. The routing table also supports writing out its current structure as a DOT graph for visualization. testrouting is primarily meant to help in coming up with generic routing table integration tests.

This PR also includes a new routing table integration test suite that runs against all current routing table implementations. Our existing routing table passes a lot of the tests, but not all of them, still debugging why. I have confirmed the tests should pass with the visualization graphs though.
2019-02-22 13:39:29 -05:00

61 lines
2.2 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information
package kademlia
import (
"testing"
"github.com/stretchr/testify/assert"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/teststorj"
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/storj"
)
func TestAddToReplacementCache(t *testing.T) {
ctx := testcontext.New(t)
defer ctx.Cleanup()
rt := createRoutingTable(storj.NodeID{244, 255})
defer ctx.Check(rt.Close)
kadBucketID := bucketID{255, 255}
node1 := teststorj.MockNode(string([]byte{233, 255}))
rt.addToReplacementCache(kadBucketID, node1)
assert.Equal(t, []*pb.Node{node1}, rt.replacementCache[kadBucketID])
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)
assert.Equal(t, []*pb.Node{node1}, rt.replacementCache[kadBucketID])
assert.Equal(t, []*pb.Node{node2, node3}, rt.replacementCache[kadBucketID2])
rt.addToReplacementCache(kadBucketID2, node4)
assert.Equal(t, []*pb.Node{node3, node4}, rt.replacementCache[kadBucketID2])
}
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])
}