storj/pkg/piecestore/rpc/server/psdb/psdb_test.go

217 lines
4.2 KiB
Go
Raw Normal View History

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package psdb
import (
2018-09-08 16:34:55 +01:00
"bytes"
"io/ioutil"
"os"
"path/filepath"
2018-09-08 16:34:55 +01:00
"strconv"
"testing"
"github.com/gogo/protobuf/proto"
_ "github.com/mattn/go-sqlite3"
pb "storj.io/storj/protos/piecestore"
"golang.org/x/net/context"
)
var ctx = context.Background()
2018-09-08 16:34:55 +01:00
const concurrency = 10
2018-09-08 16:34:55 +01:00
func openTest(t testing.TB) (*DB, func()) {
tmpdir, err := ioutil.TempDir("", "storj-psdb")
if err != nil {
2018-09-08 16:34:55 +01:00
t.Fatal(err)
}
2018-09-08 16:34:55 +01:00
dbpath := filepath.Join(tmpdir, "psdb.db")
2018-09-08 16:34:55 +01:00
db, err := Open(ctx, "", dbpath)
if err != nil {
2018-09-08 16:34:55 +01:00
t.Fatal(err)
}
2018-09-08 16:34:55 +01:00
return db, func() {
err := db.Close()
if err != nil {
t.Fatal(err)
}
2018-09-08 16:34:55 +01:00
err = os.RemoveAll(tmpdir)
if err != nil {
t.Fatal(err)
}
}
}
2018-09-08 16:34:55 +01:00
func TestHappyPath(t *testing.T) {
db, cleanup := openTest(t)
defer cleanup()
2018-09-08 16:34:55 +01:00
type TTL struct {
ID string
Expiration int64
}
2018-09-08 16:34:55 +01:00
tests := []TTL{
{ID: "", Expiration: 0},
{ID: "\x00", Expiration: ^int64(0)},
{ID: "test", Expiration: 666},
}
2018-09-08 16:34:55 +01:00
t.Run("Add", func(t *testing.T) {
for P := 0; P < concurrency; P++ {
t.Run("#"+strconv.Itoa(P), func(t *testing.T) {
t.Parallel()
for _, ttl := range tests {
err := db.AddTTLToDB(ttl.ID, ttl.Expiration)
if err != nil {
t.Fatal(err)
}
}
})
}
2018-09-08 16:34:55 +01:00
})
2018-09-08 16:34:55 +01:00
t.Run("Get", func(t *testing.T) {
for P := 0; P < concurrency; P++ {
t.Run("#"+strconv.Itoa(P), func(t *testing.T) {
t.Parallel()
for _, ttl := range tests {
expiration, err := db.GetTTLByID(ttl.ID)
if err != nil {
t.Fatal(err)
}
if ttl.Expiration != expiration {
t.Fatalf("expected %d got %d", ttl.Expiration, expiration)
}
}
})
}
})
2018-09-08 16:34:55 +01:00
t.Run("Delete", func(t *testing.T) {
for P := 0; P < concurrency; P++ {
t.Run("Delete", func(t *testing.T) {
t.Parallel()
for _, ttl := range tests {
err := db.DeleteTTLByID(ttl.ID)
if err != nil {
t.Fatal(err)
}
}
})
}
2018-09-08 16:34:55 +01:00
})
2018-09-08 16:34:55 +01:00
t.Run("Get Deleted", func(t *testing.T) {
for P := 0; P < concurrency; P++ {
t.Run("#"+strconv.Itoa(P), func(t *testing.T) {
t.Parallel()
for _, ttl := range tests {
expiration, err := db.GetTTLByID(ttl.ID)
if err == nil {
t.Fatal(err)
}
if expiration != 0 {
t.Fatalf("expected expiration 0 got %d", expiration)
}
}
})
}
})
2018-09-08 16:34:55 +01:00
bandwidthAllocation := func(total int64) []byte {
return serialize(t, &pb.RenterBandwidthAllocation_Data{
PayerAllocation: &pb.PayerBandwidthAllocation{},
Total: total,
})
}
2018-09-08 16:34:55 +01:00
//TODO: use better data
allocationTests := []*pb.RenterBandwidthAllocation{
{
2018-09-08 16:34:55 +01:00
Signature: []byte("signed by test"),
Data: bandwidthAllocation(0),
},
{
Signature: []byte("signed by sigma"),
Data: bandwidthAllocation(10),
},
{
Signature: []byte("signed by sigma"),
Data: bandwidthAllocation(98),
},
{
Signature: []byte("signed by test"),
Data: bandwidthAllocation(3),
},
}
2018-09-08 16:34:55 +01:00
t.Run("Bandwidth Allocation", func(t *testing.T) {
for P := 0; P < concurrency; P++ {
t.Run("#"+strconv.Itoa(P), func(t *testing.T) {
t.Parallel()
for _, test := range allocationTests {
err := db.WriteBandwidthAllocToDB(test)
if err != nil {
t.Fatal(err)
}
agreements, err := db.GetBandwidthAllocationBySignature(test.Signature)
if err != nil {
t.Fatal(err)
}
found := false
for _, agreement := range agreements {
if bytes.Equal(agreement, test.Data) {
found = true
break
}
}
if !found {
t.Fatal("did not find added bandwidth allocation")
}
}
2018-09-08 16:34:55 +01:00
})
}
})
}
2018-09-08 16:34:55 +01:00
func BenchmarkWriteBandwidthAllocation(b *testing.B) {
db, cleanup := openTest(b)
defer cleanup()
2018-09-08 16:34:55 +01:00
const WritesPerLoop = 10
2018-09-08 16:34:55 +01:00
data := serialize(b, &pb.RenterBandwidthAllocation_Data{
PayerAllocation: &pb.PayerBandwidthAllocation{},
Total: 156,
})
2018-09-08 16:34:55 +01:00
b.RunParallel(func(b *testing.PB) {
for b.Next() {
for i := 0; i < WritesPerLoop; i++ {
_ = db.WriteBandwidthAllocToDB(&pb.RenterBandwidthAllocation{
Signature: []byte("signed by test"),
Data: data,
})
}
}
2018-09-08 16:34:55 +01:00
})
}
2018-09-08 16:34:55 +01:00
func serialize(t testing.TB, v proto.Message) []byte {
data, err := proto.Marshal(v)
if err != nil {
t.Fatal(err)
}
return data
}