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
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
}