2018-06-13 19:22:32 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-11-19 14:40:01 +00:00
|
|
|
package overlay_test
|
2018-06-13 19:22:32 +01:00
|
|
|
|
|
|
|
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"
|
2018-11-30 13:40:13 +00:00
|
|
|
"storj.io/storj/internal/teststorj"
|
2018-11-19 14:40:01 +00:00
|
|
|
"storj.io/storj/pkg/overlay"
|
2018-12-04 20:18:26 +00:00
|
|
|
"storj.io/storj/pkg/statdb"
|
2018-11-30 13:40:13 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-06-13 19:22:32 +01:00
|
|
|
"storj.io/storj/storage"
|
|
|
|
"storj.io/storj/storage/boltdb"
|
|
|
|
"storj.io/storj/storage/redis"
|
2018-09-07 08:33:33 +01:00
|
|
|
"storj.io/storj/storage/redis/redisserver"
|
2018-09-11 08:27:12 +01:00
|
|
|
"storj.io/storj/storage/teststore"
|
2018-06-13 19:22:32 +01:00
|
|
|
)
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
var (
|
|
|
|
valid1ID = teststorj.NodeIDFromString("valid1")
|
|
|
|
valid2ID = teststorj.NodeIDFromString("valid2")
|
|
|
|
invalid1ID = teststorj.NodeIDFromString("invalid1")
|
|
|
|
invalid2ID = teststorj.NodeIDFromString("invalid2")
|
|
|
|
)
|
|
|
|
|
2018-12-04 20:18:26 +00:00
|
|
|
func testCache(ctx context.Context, t *testing.T, store storage.KeyValueStore, sdb *statdb.StatDB) {
|
|
|
|
cache := overlay.Cache{DB: store, StatDB: sdb}
|
2018-11-16 16:31:14 +00:00
|
|
|
|
2018-11-19 14:40:01 +00:00
|
|
|
{ // Put
|
2018-12-04 20:18:26 +00:00
|
|
|
err := cache.Put(ctx, valid1ID, *teststorj.MockNode("valid1"))
|
2018-11-16 16:31:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-12-04 20:18:26 +00:00
|
|
|
err = cache.Put(ctx, valid2ID, *teststorj.MockNode("valid2"))
|
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
|
2018-11-29 18:39:27 +00:00
|
|
|
valid2, err := cache.Get(ctx, valid2ID)
|
2018-12-04 20:18:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, valid2.Id, valid2ID)
|
2018-11-16 16:31:14 +00:00
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
invalid2, err := cache.Get(ctx, invalid2ID)
|
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-29 18:39:27 +00:00
|
|
|
_, err := cache.Get(ctx, valid1ID)
|
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
|
2018-11-29 18:39:27 +00:00
|
|
|
nodes, err := cache.GetAll(ctx, storj.NodeIDList{valid2ID, valid1ID, valid2ID})
|
2018-12-04 20:18:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, nodes[0].Id, valid2ID)
|
|
|
|
assert.Equal(t, nodes[1].Id, valid1ID)
|
|
|
|
assert.Equal(t, nodes[2].Id, valid2ID)
|
2018-11-16 16:31:14 +00:00
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
nodes, err = cache.GetAll(ctx, storj.NodeIDList{valid1ID, invalid1ID})
|
2018-12-04 20:18:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, nodes[0].Id, valid1ID)
|
|
|
|
assert.Nil(t, nodes[1])
|
2018-11-16 16:31:14 +00:00
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
nodes, err = cache.GetAll(ctx, make(storj.NodeIDList, 2))
|
2018-12-04 20:18:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Nil(t, nodes[0])
|
|
|
|
assert.Nil(t, nodes[1])
|
2018-11-16 16:31:14 +00:00
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
_, err = cache.GetAll(ctx, storj.NodeIDList{})
|
2018-11-19 14:40:01 +00:00
|
|
|
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-29 18:39:27 +00:00
|
|
|
_, err := cache.GetAll(ctx, storj.NodeIDList{valid1ID, valid2ID})
|
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()
|
|
|
|
|
2018-12-04 20:18:26 +00:00
|
|
|
planet, err := testplanet.New(t, 1, 4, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer ctx.Check(planet.Shutdown)
|
|
|
|
planet.Start(ctx)
|
|
|
|
sdb := planet.Satellites[0].StatDB
|
|
|
|
|
2018-11-16 16:31:14 +00:00
|
|
|
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-12-04 20:18:26 +00:00
|
|
|
testCache(ctx, t, store, sdb)
|
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()
|
|
|
|
|
2018-12-04 20:18:26 +00:00
|
|
|
planet, err := testplanet.New(t, 1, 4, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer ctx.Check(planet.Shutdown)
|
|
|
|
planet.Start(ctx)
|
|
|
|
sdb := planet.Satellites[0].StatDB
|
|
|
|
|
2018-11-16 16:31:14 +00:00
|
|
|
client, err := boltdb.New(ctx.File("overlay.db"), "overlay")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer ctx.Check(client.Close)
|
|
|
|
|
2018-12-04 20:18:26 +00:00
|
|
|
testCache(ctx, t, client, sdb)
|
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-12-04 20:18:26 +00:00
|
|
|
planet, err := testplanet.New(t, 1, 4, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer ctx.Check(planet.Shutdown)
|
|
|
|
planet.Start(ctx)
|
|
|
|
sdb := planet.Satellites[0].StatDB
|
|
|
|
|
|
|
|
testCache(ctx, t, teststore.New(), sdb)
|
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-08-07 00:32:47 +01:00
|
|
|
|
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-08-07 00:32:47 +01:00
|
|
|
|
2018-11-19 14:40:01 +00:00
|
|
|
err = planet.Satellites[0].Overlay.Refresh(ctx)
|
2018-08-07 00:32:47 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|