swarm/storage: NewRandomChunk method rather than FakeChunk

This commit is contained in:
Anton Evangelatov 2018-04-03 16:58:08 +02:00
parent fa8acc90fb
commit 47cfe074f0

View file

@ -17,8 +17,11 @@
package storage package storage
import ( import (
"crypto/rand"
"encoding/binary"
"io/ioutil" "io/ioutil"
"os" "os"
"sync"
"testing" "testing"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
@ -56,7 +59,7 @@ func TestMemStoreAndLDBStore(t *testing.T) {
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 uint64 // size of chunk (by default in Swarm - 4096)
}{ }{
{ {
n: 1, n: 1,
@ -70,6 +73,10 @@ func TestMemStoreAndLDBStore(t *testing.T) {
n: 9999, n: 9999,
chunkSize: 4096, chunkSize: 4096,
}, },
{
n: 20001,
chunkSize: 4096,
},
{ {
n: 80001, n: 80001,
chunkSize: 4096, chunkSize: 4096,
@ -80,11 +87,9 @@ func TestMemStoreAndLDBStore(t *testing.T) {
var chunks []*Chunk var chunks []*Chunk
for i := 0; i < tt.n; i++ { 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++ { for i := 0; i < tt.n; i++ {
go ldb.Put(chunks[i]) go ldb.Put(chunks[i])
memStore.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
}