aboutsummaryrefslogtreecommitdiffstats
path: root/swarm/storage/mock/test
diff options
context:
space:
mode:
authorJanoš Guljaš <janos@users.noreply.github.com>2019-02-23 17:47:33 +0800
committerViktor Trón <viktor.tron@gmail.com>2019-02-23 17:47:33 +0800
commit64d10c08726af33048e8eeb8df257628a3944870 (patch)
tree00002e147d650e1d6ec571731bbf900e77d9e307 /swarm/storage/mock/test
parent02c28046a04ebf649af5d1b2a702d0da1c8a2a39 (diff)
downloadgo-tangerine-64d10c08726af33048e8eeb8df257628a3944870.tar
go-tangerine-64d10c08726af33048e8eeb8df257628a3944870.tar.gz
go-tangerine-64d10c08726af33048e8eeb8df257628a3944870.tar.bz2
go-tangerine-64d10c08726af33048e8eeb8df257628a3944870.tar.lz
go-tangerine-64d10c08726af33048e8eeb8df257628a3944870.tar.xz
go-tangerine-64d10c08726af33048e8eeb8df257628a3944870.tar.zst
go-tangerine-64d10c08726af33048e8eeb8df257628a3944870.zip
swarm: mock store listings (#19157)
* swarm/storage/mock: implement listings methods for mem and rpc stores * swarm/storage/mock/rpc: add comments and newTestStore helper function * swarm/storage/mock/mem: add missing comments * swarm/storage/mock: add comments to new types and constants * swarm/storage/mock/db: implement listings for mock/db global store * swarm/storage/mock/test: add comments for MockStoreListings * swarm/storage/mock/explorer: initial implementation * cmd/swarm/global-store: add chunk explorer * cmd/swarm/global-store: add chunk explorer tests * swarm/storage/mock/explorer: add tests * swarm/storage/mock/explorer: add swagger api definition * swarm/storage/mock/explorer: not-zero test values for invalid addr and key * swarm/storage/mock/explorer: test wildcard cors origin * swarm/storage/mock/db: renames based on Fabio's suggestions * swarm/storage/mock/explorer: add more comments to testHandler function * cmd/swarm/global-store: terminate subprocess with Kill in tests
Diffstat (limited to 'swarm/storage/mock/test')
-rw-r--r--swarm/storage/mock/test/test.go118
1 files changed, 118 insertions, 0 deletions
diff --git a/swarm/storage/mock/test/test.go b/swarm/storage/mock/test/test.go
index 69828b144..cc837f0b7 100644
--- a/swarm/storage/mock/test/test.go
+++ b/swarm/storage/mock/test/test.go
@@ -20,6 +20,7 @@ package test
import (
"bytes"
+ "encoding/binary"
"fmt"
"io"
"strconv"
@@ -170,6 +171,123 @@ func MockStore(t *testing.T, globalStore mock.GlobalStorer, n int) {
})
}
+// MockStoreListings tests global store methods Keys, Nodes, NodeKeys and KeyNodes.
+// It uses a provided globalstore to put chunks for n number of node addresses
+// and to validate that methods are returning the right responses.
+func MockStoreListings(t *testing.T, globalStore mock.GlobalStorer, n int) {
+ addrs := make([]common.Address, n)
+ for i := 0; i < n; i++ {
+ addrs[i] = common.HexToAddress(strconv.FormatInt(int64(i)+1, 16))
+ }
+ type chunk struct {
+ key []byte
+ data []byte
+ }
+ const chunksPerNode = 5
+ keys := make([][]byte, n*chunksPerNode)
+ for i := 0; i < n*chunksPerNode; i++ {
+ b := make([]byte, 8)
+ binary.BigEndian.PutUint64(b, uint64(i))
+ keys[i] = b
+ }
+
+ // keep track of keys on every node
+ nodeKeys := make(map[common.Address][][]byte)
+ // keep track of nodes that store particular key
+ keyNodes := make(map[string][]common.Address)
+ for i := 0; i < chunksPerNode; i++ {
+ // put chunks for every address
+ for j := 0; j < n; j++ {
+ addr := addrs[j]
+ key := keys[(i*n)+j]
+ err := globalStore.Put(addr, key, []byte("data"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ nodeKeys[addr] = append(nodeKeys[addr], key)
+ keyNodes[string(key)] = append(keyNodes[string(key)], addr)
+ }
+
+ // test Keys method
+ var startKey []byte
+ var gotKeys [][]byte
+ for {
+ keys, err := globalStore.Keys(startKey, 0)
+ if err != nil {
+ t.Fatal(err)
+ }
+ gotKeys = append(gotKeys, keys.Keys...)
+ if keys.Next == nil {
+ break
+ }
+ startKey = keys.Next
+ }
+ wantKeys := keys[:(i+1)*n]
+ if fmt.Sprint(gotKeys) != fmt.Sprint(wantKeys) {
+ t.Fatalf("got #%v keys %v, want %v", i+1, gotKeys, wantKeys)
+ }
+
+ // test Nodes method
+ var startNode *common.Address
+ var gotNodes []common.Address
+ for {
+ nodes, err := globalStore.Nodes(startNode, 0)
+ if err != nil {
+ t.Fatal(err)
+ }
+ gotNodes = append(gotNodes, nodes.Addrs...)
+ if nodes.Next == nil {
+ break
+ }
+ startNode = nodes.Next
+ }
+ wantNodes := addrs
+ if fmt.Sprint(gotNodes) != fmt.Sprint(wantNodes) {
+ t.Fatalf("got #%v nodes %v, want %v", i+1, gotNodes, wantNodes)
+ }
+
+ // test NodeKeys method
+ for addr, wantKeys := range nodeKeys {
+ var startKey []byte
+ var gotKeys [][]byte
+ for {
+ keys, err := globalStore.NodeKeys(addr, startKey, 0)
+ if err != nil {
+ t.Fatal(err)
+ }
+ gotKeys = append(gotKeys, keys.Keys...)
+ if keys.Next == nil {
+ break
+ }
+ startKey = keys.Next
+ }
+ if fmt.Sprint(gotKeys) != fmt.Sprint(wantKeys) {
+ t.Fatalf("got #%v %s node keys %v, want %v", i+1, addr.Hex(), gotKeys, wantKeys)
+ }
+ }
+
+ // test KeyNodes method
+ for key, wantNodes := range keyNodes {
+ var startNode *common.Address
+ var gotNodes []common.Address
+ for {
+ nodes, err := globalStore.KeyNodes([]byte(key), startNode, 0)
+ if err != nil {
+ t.Fatal(err)
+ }
+ gotNodes = append(gotNodes, nodes.Addrs...)
+ if nodes.Next == nil {
+ break
+ }
+ startNode = nodes.Next
+ }
+ if fmt.Sprint(gotNodes) != fmt.Sprint(wantNodes) {
+ t.Fatalf("got #%v %x key nodes %v, want %v", i+1, []byte(key), gotNodes, wantNodes)
+ }
+ }
+ }
+}
+
// ImportExport saves chunks to the outStore, exports them to the tar archive,
// imports tar archive to the inStore and checks if all chunks are imported correctly.
func ImportExport(t *testing.T, outStore, inStore mock.GlobalStorer, n int) {