swarm/storage: update test for MemStore and LDBStore

This commit is contained in:
Anton Evangelatov 2018-04-03 14:27:51 +02:00
parent 4809d94999
commit 1d54933cd1

View file

@ -17,7 +17,6 @@
package storage package storage
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"testing" "testing"
@ -31,7 +30,6 @@ func newLDBStore(t *testing.T) (*LDBStore, func()) {
t.Fatal(err) t.Fatal(err)
} }
log.Trace("memstore.tempdir", "dir", dir) log.Trace("memstore.tempdir", "dir", dir)
fmt.Println(dir)
db, err := NewLDBStore(dir, MakeHashFunc(SHA3Hash), defaultDbCapacity, testPoFunc) db, err := NewLDBStore(dir, MakeHashFunc(SHA3Hash), defaultDbCapacity, testPoFunc)
if err != nil { if err != nil {
@ -39,6 +37,7 @@ func newLDBStore(t *testing.T) (*LDBStore, func()) {
} }
cleanup := func() { cleanup := func() {
db.Close()
err := os.RemoveAll(dir) err := os.RemoveAll(dir)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -48,26 +47,31 @@ func newLDBStore(t *testing.T) (*LDBStore, func()) {
return db, cleanup return db, cleanup
} }
func newMemStore(t *testing.T) (*MemStore, func()) { func TestMemStoreAndLDBStore(t *testing.T) {
ldb, cleanup := newLDBStore(t) ldb, cleanup := newLDBStore(t)
ldb.setCapacity(singletonSwarmDbCapacity) ldb.setCapacity(singletonSwarmDbCapacity)
memstore := NewMemStore(ldb, defaultCacheCapacity)
return memstore, cleanup
}
func TestMemStore(t *testing.T) {
log.Root().SetHandler(log.LvlFilterHandler(log.LvlTrace, log.StreamHandler(os.Stderr, log.TerminalFormat(true))))
memStore, cleanup := newMemStore(t)
defer cleanup() defer cleanup()
memStore := NewMemStore(ldb, defaultCacheCapacity)
tests := []struct { tests := []struct {
n int // number of chunks to push to memStore n int // number of chunks to push to memStore
chunkSize int64 // size of chunk (by default in Swarm - 4096) chunkSize int64 // size of chunk (by default in Swarm - 4096)
}{ }{
{ {
n: 10001, n: 1,
chunkSize: 4096,
},
{
n: 201,
chunkSize: 4096,
},
{
n: 20001,
chunkSize: 4096,
},
{
n: 50001,
chunkSize: 4096, chunkSize: 4096,
}, },
} }
@ -82,14 +86,27 @@ func TestMemStore(t *testing.T) {
FakeChunk(tt.chunkSize, tt.n, chunks) FakeChunk(tt.chunkSize, tt.n, chunks)
for i := 0; i < tt.n; i++ { for i := 0; i < tt.n; i++ {
go ldb.Put(chunks[i])
memStore.Put(chunks[i]) memStore.Put(chunks[i])
} }
for i := 0; i < tt.n; i++ { for i := 0; i < tt.n; i++ {
_, err := memStore.Get(chunks[i].Key) _, err := memStore.Get(chunks[i].Key)
if err != nil { if err != nil {
t.Fatal(err) if err == ErrChunkNotFound {
_, err := ldb.Get(chunks[i].Key)
if err != nil {
t.Fatal(err)
}
} else {
t.Fatal(err)
}
} }
} }
// wait for all chunks to be stored before ending the test are cleaning up
for i := 0; i < tt.n; i++ {
<-chunks[i].dbStoredC
}
} }
} }