diff options
author | Balint Gabor <balint.g@gmail.com> | 2018-09-13 17:42:19 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-13 17:42:19 +0800 |
commit | 3ff2f756368f2d8ec0d1d9d25f6ba9cdabd7383e (patch) | |
tree | 62a2896b3b824449595272f0b92dda877ba1c58d /swarm/storage/localstore_test.go | |
parent | ff3a5d24d2e40fd66f7813173e9cfc31144f3c53 (diff) | |
download | go-tangerine-3ff2f756368f2d8ec0d1d9d25f6ba9cdabd7383e.tar.gz go-tangerine-3ff2f756368f2d8ec0d1d9d25f6ba9cdabd7383e.tar.zst go-tangerine-3ff2f756368f2d8ec0d1d9d25f6ba9cdabd7383e.zip |
swarm: Chunk refactor (#17659)
Co-authored-by: Janos Guljas <janos@resenje.org>
Co-authored-by: Balint Gabor <balint.g@gmail.com>
Co-authored-by: Anton Evangelatov <anton.evangelatov@gmail.com>
Co-authored-by: Viktor TrĂ³n <viktor.tron@gmail.com>
Diffstat (limited to 'swarm/storage/localstore_test.go')
-rw-r--r-- | swarm/storage/localstore_test.go | 68 |
1 files changed, 47 insertions, 21 deletions
diff --git a/swarm/storage/localstore_test.go b/swarm/storage/localstore_test.go index ae62218fe..814d270d3 100644 --- a/swarm/storage/localstore_test.go +++ b/swarm/storage/localstore_test.go @@ -17,11 +17,12 @@ package storage import ( + "context" "io/ioutil" "os" "testing" - "github.com/ethereum/go-ethereum/swarm/chunk" + ch "github.com/ethereum/go-ethereum/swarm/chunk" ) var ( @@ -50,29 +51,29 @@ func TestValidator(t *testing.T) { chunks := GenerateRandomChunks(259, 2) goodChunk := chunks[0] badChunk := chunks[1] - copy(badChunk.SData, goodChunk.SData) + copy(badChunk.Data(), goodChunk.Data()) - PutChunks(store, goodChunk, badChunk) - if err := goodChunk.GetErrored(); err != nil { + errs := putChunks(store, goodChunk, badChunk) + if errs[0] != nil { t.Fatalf("expected no error on good content address chunk in spite of no validation, but got: %s", err) } - if err := badChunk.GetErrored(); err != nil { + if errs[1] != nil { t.Fatalf("expected no error on bad content address chunk in spite of no validation, but got: %s", err) } // add content address validator and check puts // bad should fail, good should pass store.Validators = append(store.Validators, NewContentAddressValidator(hashfunc)) - chunks = GenerateRandomChunks(chunk.DefaultSize, 2) + chunks = GenerateRandomChunks(ch.DefaultSize, 2) goodChunk = chunks[0] badChunk = chunks[1] - copy(badChunk.SData, goodChunk.SData) + copy(badChunk.Data(), goodChunk.Data()) - PutChunks(store, goodChunk, badChunk) - if err := goodChunk.GetErrored(); err != nil { + errs = putChunks(store, goodChunk, badChunk) + if errs[0] != nil { t.Fatalf("expected no error on good content address chunk with content address validator only, but got: %s", err) } - if err := badChunk.GetErrored(); err == nil { + if errs[1] == nil { t.Fatal("expected error on bad content address chunk with content address validator only, but got nil") } @@ -81,16 +82,16 @@ func TestValidator(t *testing.T) { var negV boolTestValidator store.Validators = append(store.Validators, negV) - chunks = GenerateRandomChunks(chunk.DefaultSize, 2) + chunks = GenerateRandomChunks(ch.DefaultSize, 2) goodChunk = chunks[0] badChunk = chunks[1] - copy(badChunk.SData, goodChunk.SData) + copy(badChunk.Data(), goodChunk.Data()) - PutChunks(store, goodChunk, badChunk) - if err := goodChunk.GetErrored(); err != nil { + errs = putChunks(store, goodChunk, badChunk) + if errs[0] != nil { t.Fatalf("expected no error on good content address chunk with content address validator only, but got: %s", err) } - if err := badChunk.GetErrored(); err == nil { + if errs[1] == nil { t.Fatal("expected error on bad content address chunk with content address validator only, but got nil") } @@ -99,18 +100,19 @@ func TestValidator(t *testing.T) { var posV boolTestValidator = true store.Validators = append(store.Validators, posV) - chunks = GenerateRandomChunks(chunk.DefaultSize, 2) + chunks = GenerateRandomChunks(ch.DefaultSize, 2) goodChunk = chunks[0] badChunk = chunks[1] - copy(badChunk.SData, goodChunk.SData) + copy(badChunk.Data(), goodChunk.Data()) - PutChunks(store, goodChunk, badChunk) - if err := goodChunk.GetErrored(); err != nil { + errs = putChunks(store, goodChunk, badChunk) + if errs[0] != nil { t.Fatalf("expected no error on good content address chunk with content address validator only, but got: %s", err) } - if err := badChunk.GetErrored(); err != nil { - t.Fatalf("expected no error on bad content address chunk with content address validator only, but got: %s", err) + if errs[1] != nil { + t.Fatalf("expected no error on bad content address chunk in spite of no validation, but got: %s", err) } + } type boolTestValidator bool @@ -118,3 +120,27 @@ type boolTestValidator bool func (self boolTestValidator) Validate(addr Address, data []byte) bool { return bool(self) } + +// putChunks adds chunks to localstore +// It waits for receive on the stored channel +// It logs but does not fail on delivery error +func putChunks(store *LocalStore, chunks ...Chunk) []error { + i := 0 + f := func(n int64) Chunk { + chunk := chunks[i] + i++ + return chunk + } + _, errs := put(store, len(chunks), f) + return errs +} + +func put(store *LocalStore, n int, f func(i int64) Chunk) (hs []Address, errs []error) { + for i := int64(0); i < int64(n); i++ { + chunk := f(ch.DefaultSize) + err := store.Put(context.TODO(), chunk) + errs = append(errs, err) + hs = append(hs, chunk.Address()) + } + return hs, errs +} |