storj/pkg/overlay/cache_test.go

118 lines
2.8 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"
2018-12-17 18:47:26 +00:00
"math/rand"
"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"
2018-12-17 18:47:26 +00:00
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/statdb"
2018-11-30 13:40:13 +00:00
"storj.io/storj/pkg/storj"
"storj.io/storj/satellite/satellitedb"
"storj.io/storj/satellite/satellitedb/satellitedbtest"
"storj.io/storj/storage"
"storj.io/storj/storage/teststore"
)
func testCache(ctx context.Context, t *testing.T, store storage.KeyValueStore, sdb statdb.DB) {
2018-12-17 18:47:26 +00:00
valid1ID := storj.NodeID{}
valid2ID := storj.NodeID{}
missingID := storj.NodeID{}
_, _ = rand.Read(valid1ID[:])
_, _ = rand.Read(valid2ID[:])
_, _ = rand.Read(missingID[:])
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-17 18:47:26 +00:00
err := cache.Put(ctx, valid1ID, pb.Node{Id: valid1ID})
2018-11-16 16:31:14 +00:00
if err != nil {
t.Fatal(err)
}
2018-12-17 18:47:26 +00:00
err = cache.Put(ctx, valid2ID, pb.Node{Id: valid2ID})
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-12-17 18:47:26 +00:00
_, err := cache.Get(ctx, storj.NodeID{})
assert.Error(t, err)
assert.True(t, err == overlay.ErrEmptyNode)
valid1, err := cache.Get(ctx, valid1ID)
if assert.NoError(t, err) {
assert.Equal(t, valid1.Id, valid1ID)
}
valid2, err := cache.Get(ctx, valid2ID)
2018-12-17 18:47:26 +00:00
if assert.NoError(t, err) {
assert.Equal(t, valid2.Id, valid2ID)
}
2018-11-16 16:31:14 +00:00
2018-12-17 18:47:26 +00:00
invalid2, err := cache.Get(ctx, missingID)
2018-11-16 16:31:14 +00:00
assert.Error(t, err)
2018-12-17 18:47:26 +00:00
assert.True(t, err == overlay.ErrNodeNotFound)
2018-11-16 16:31:14 +00:00
assert.Nil(t, invalid2)
if storeClient, ok := store.(*teststore.Client); ok {
storeClient.ForceError++
_, 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
nodes, err := cache.GetAll(ctx, storj.NodeIDList{valid2ID, valid1ID, valid2ID})
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-12-17 18:47:26 +00:00
nodes, err = cache.GetAll(ctx, storj.NodeIDList{valid1ID, missingID})
assert.NoError(t, err)
assert.Equal(t, nodes[0].Id, valid1ID)
assert.Nil(t, nodes[1])
2018-11-16 16:31:14 +00:00
nodes, err = cache.GetAll(ctx, make(storj.NodeIDList, 2))
assert.NoError(t, err)
assert.Nil(t, nodes[0])
assert.Nil(t, nodes[1])
2018-11-16 16:31:14 +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++
_, 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
}
func TestCache_Masterdb(t *testing.T) {
2018-11-16 16:31:14 +00:00
ctx := testcontext.New(t)
defer ctx.Cleanup()
planet, err := testplanet.New(t, 1, 4, 0)
if err != nil {
t.Fatal(err)
}
defer ctx.Check(planet.Shutdown)
planet.Start(ctx)
satellitedbtest.Run(t, func(t *testing.T, db *satellitedb.DB) {
testCache(ctx, t, db.OverlayCache(), db.StatDB())
})
2018-11-16 16:31:14 +00:00
}