storj/pkg/overlay/cache_test.go

139 lines
3.2 KiB
Go
Raw Normal View History

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
2018-11-19 14:40:01 +00:00
package overlay_test
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
2018-11-16 16:31:14 +00:00
"storj.io/storj/internal/testcontext"
2018-11-19 14:40:01 +00:00
"storj.io/storj/internal/testplanet"
"storj.io/storj/pkg/overlay"
"storj.io/storj/pkg/pb"
"storj.io/storj/storage"
"storj.io/storj/storage/boltdb"
"storj.io/storj/storage/redis"
"storj.io/storj/storage/redis/redisserver"
"storj.io/storj/storage/teststore"
)
2018-11-19 14:40:01 +00:00
func testCache(ctx context.Context, t *testing.T, store storage.KeyValueStore) {
cache := overlay.Cache{DB: store}
2018-11-16 16:31:14 +00:00
2018-11-19 14:40:01 +00:00
{ // Put
err := cache.Put("valid1", pb.Node{Address: &pb.NodeAddress{Transport: pb.NodeTransport_TCP_TLS_GRPC, Address: "127.0.0.1:9001"}})
2018-11-16 16:31:14 +00:00
if err != nil {
t.Fatal(err)
}
2018-11-19 14:40:01 +00:00
err = cache.Put("valid2", pb.Node{Address: &pb.NodeAddress{Transport: pb.NodeTransport_TCP_TLS_GRPC, Address: "127.0.0.1:9002"}})
2018-11-16 16:31:14 +00:00
if err != nil {
t.Fatal(err)
}
2018-11-19 14:40:01 +00:00
}
2018-11-16 16:31:14 +00:00
2018-11-19 14:40:01 +00:00
{ // Get
valid2, err := cache.Get(ctx, "valid2")
2018-11-16 16:31:14 +00:00
if assert.NoError(t, err) {
assert.Equal(t, valid2.Address.Address, "127.0.0.1:9002")
}
2018-11-19 14:40:01 +00:00
invalid2, err := cache.Get(ctx, "invalid2")
2018-11-16 16:31:14 +00:00
assert.Error(t, err)
assert.Nil(t, invalid2)
if storeClient, ok := store.(*teststore.Client); ok {
storeClient.ForceError++
2018-11-19 14:40:01 +00:00
_, err := cache.Get(ctx, "valid1")
2018-11-16 16:31:14 +00:00
assert.Error(t, err)
}
2018-11-19 14:40:01 +00:00
}
2018-11-16 16:31:14 +00:00
2018-11-19 14:40:01 +00:00
{ // GetAll
nodes, err := cache.GetAll(ctx, []string{"valid2", "valid1", "valid2"})
2018-11-16 16:31:14 +00:00
if assert.NoError(t, err) {
assert.Equal(t, nodes[0].Address.Address, "127.0.0.1:9002")
assert.Equal(t, nodes[1].Address.Address, "127.0.0.1:9001")
assert.Equal(t, nodes[2].Address.Address, "127.0.0.1:9002")
}
2018-11-19 14:40:01 +00:00
nodes, err = cache.GetAll(ctx, []string{"valid1", "invalid"})
2018-11-16 16:31:14 +00:00
if assert.NoError(t, err) {
assert.Equal(t, nodes[0].Address.Address, "127.0.0.1:9001")
assert.Nil(t, nodes[1])
}
2018-11-19 14:40:01 +00:00
nodes, err = cache.GetAll(ctx, []string{"", ""})
2018-11-16 16:31:14 +00:00
if assert.NoError(t, err) {
assert.Nil(t, nodes[0])
assert.Nil(t, nodes[1])
}
2018-11-19 14:40:01 +00:00
_, err = cache.GetAll(ctx, []string{})
assert.True(t, overlay.OverlayError.Has(err))
2018-11-16 16:31:14 +00:00
if storeClient, ok := store.(*teststore.Client); ok {
storeClient.ForceError++
2018-11-19 14:40:01 +00:00
_, err := cache.GetAll(ctx, []string{"valid1", "valid2"})
2018-11-16 16:31:14 +00:00
assert.Error(t, err)
}
2018-11-19 14:40:01 +00:00
}
2018-11-16 16:31:14 +00:00
}
2018-11-19 14:40:01 +00:00
func TestCache_Redis(t *testing.T) {
2018-11-16 16:31:14 +00:00
ctx := testcontext.New(t)
defer ctx.Cleanup()
redisAddr, cleanup, err := redisserver.Start()
if err != nil {
t.Fatal(err)
}
defer cleanup()
store, err := redis.NewClient(redisAddr, "", 1)
if err != nil {
t.Fatal(err)
}
defer ctx.Check(store.Close)
2018-11-19 14:40:01 +00:00
testCache(ctx, t, store)
2018-11-16 16:31:14 +00:00
}
2018-11-19 14:40:01 +00:00
func TestCache_Bolt(t *testing.T) {
2018-11-16 16:31:14 +00:00
ctx := testcontext.New(t)
defer ctx.Cleanup()
client, err := boltdb.New(ctx.File("overlay.db"), "overlay")
if err != nil {
t.Fatal(err)
}
defer ctx.Check(client.Close)
2018-11-19 14:40:01 +00:00
testCache(ctx, t, client)
2018-11-16 16:31:14 +00:00
}
2018-11-19 14:40:01 +00:00
func TestCache_Store(t *testing.T) {
2018-11-16 16:31:14 +00:00
ctx := testcontext.New(t)
defer ctx.Cleanup()
2018-11-19 14:40:01 +00:00
testCache(ctx, t, teststore.New())
2018-11-16 16:31:14 +00:00
}
2018-11-19 14:40:01 +00:00
func TestCache_Refresh(t *testing.T) {
ctx := testcontext.New(t)
defer ctx.Cleanup()
2018-11-19 14:40:01 +00:00
planet, err := testplanet.New(t, 1, 30, 0)
2018-11-16 16:31:14 +00:00
if err != nil {
t.Fatal(err)
}
2018-11-19 14:40:01 +00:00
defer ctx.Check(planet.Shutdown)
2018-11-16 16:31:14 +00:00
2018-11-19 14:40:01 +00:00
planet.Start(ctx)
2018-11-19 14:40:01 +00:00
err = planet.Satellites[0].Overlay.Refresh(ctx)
assert.NoError(t, err)
}