From 47cfe074f06cfcd920546f832b97217d173fb9e3 Mon Sep 17 00:00:00 2001 From: Anton Evangelatov Date: Tue, 3 Apr 2018 16:58:08 +0200 Subject: [PATCH] swarm/storage: NewRandomChunk method rather than FakeChunk --- swarm/storage/memstore_lrucache_test.go | 35 +++++++++++++++++++++---- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/swarm/storage/memstore_lrucache_test.go b/swarm/storage/memstore_lrucache_test.go index 73f795117a..b391ef174e 100644 --- a/swarm/storage/memstore_lrucache_test.go +++ b/swarm/storage/memstore_lrucache_test.go @@ -17,8 +17,11 @@ package storage import ( + "crypto/rand" + "encoding/binary" "io/ioutil" "os" + "sync" "testing" "github.com/ethereum/go-ethereum/log" @@ -55,8 +58,8 @@ func TestMemStoreAndLDBStore(t *testing.T) { memStore := NewMemStore(ldb, defaultCacheCapacity) tests := []struct { - n int // number of chunks to push to memStore - chunkSize int64 // size of chunk (by default in Swarm - 4096) + n int // number of chunks to push to memStore + chunkSize uint64 // size of chunk (by default in Swarm - 4096) }{ { n: 1, @@ -70,6 +73,10 @@ func TestMemStoreAndLDBStore(t *testing.T) { n: 9999, chunkSize: 4096, }, + { + n: 20001, + chunkSize: 4096, + }, { n: 80001, chunkSize: 4096, @@ -80,11 +87,9 @@ func TestMemStoreAndLDBStore(t *testing.T) { var chunks []*Chunk for i := 0; i < tt.n; i++ { - chunks = append(chunks, NewChunk(nil, make(chan bool))) + chunks = append(chunks, NewRandomChunk(tt.chunkSize)) } - FakeChunk(tt.chunkSize, tt.n, chunks) - for i := 0; i < tt.n; i++ { go ldb.Put(chunks[i]) memStore.Put(chunks[i]) @@ -110,3 +115,23 @@ func TestMemStoreAndLDBStore(t *testing.T) { } } } + +func NewRandomChunk(chunkSize uint64) *Chunk { + c := &Chunk{ + Key: make([]byte, 32), + ReqC: nil, + SData: make([]byte, chunkSize), + dbStoredC: make(chan bool), + dbStoredMu: &sync.Mutex{}, + } + + rand.Read(c.SData) + + binary.LittleEndian.PutUint64(c.SData[:8], chunkSize) + + hasher := MakeHashFunc(SHA3Hash)() + hasher.Write(c.SData) + copy(c.Key, hasher.Sum(nil)) + + return c +}