github.com/jincm/wesharechain@v0.0.0-20210122032815-1537409ce26a/chain/swarm/network/simulation/bucket_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package simulation 18 19 import ( 20 "sync" 21 "testing" 22 23 "github.com/ethereum/go-ethereum/node" 24 "github.com/ethereum/go-ethereum/p2p/simulations/adapters" 25 ) 26 27 // TestServiceBucket tests all bucket functionality using subtests. 28 // It constructs a simulation of two nodes by adding items to their buckets 29 // in ServiceFunc constructor, then by SetNodeItem. Testing UpNodesItems 30 // is done by stopping one node and validating availability of its items. 31 func TestServiceBucket(t *testing.T) { 32 testKey := "Key" 33 testValue := "Value" 34 35 sim := New(map[string]ServiceFunc{ 36 "noop": func(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) { 37 b.Store(testKey, testValue+ctx.Config.ID.String()) 38 return newNoopService(), nil, nil 39 }, 40 }) 41 defer sim.Close() 42 43 id1, err := sim.AddNode() 44 if err != nil { 45 t.Fatal(err) 46 } 47 48 id2, err := sim.AddNode() 49 if err != nil { 50 t.Fatal(err) 51 } 52 53 t.Run("ServiceFunc bucket Store", func(t *testing.T) { 54 v, ok := sim.NodeItem(id1, testKey) 55 if !ok { 56 t.Fatal("bucket item not found") 57 } 58 s, ok := v.(string) 59 if !ok { 60 t.Fatal("bucket item value is not string") 61 } 62 if s != testValue+id1.String() { 63 t.Fatalf("expected %q, got %q", testValue+id1.String(), s) 64 } 65 66 v, ok = sim.NodeItem(id2, testKey) 67 if !ok { 68 t.Fatal("bucket item not found") 69 } 70 s, ok = v.(string) 71 if !ok { 72 t.Fatal("bucket item value is not string") 73 } 74 if s != testValue+id2.String() { 75 t.Fatalf("expected %q, got %q", testValue+id2.String(), s) 76 } 77 }) 78 79 customKey := "anotherKey" 80 customValue := "anotherValue" 81 82 t.Run("SetNodeItem", func(t *testing.T) { 83 sim.SetNodeItem(id1, customKey, customValue) 84 85 v, ok := sim.NodeItem(id1, customKey) 86 if !ok { 87 t.Fatal("bucket item not found") 88 } 89 s, ok := v.(string) 90 if !ok { 91 t.Fatal("bucket item value is not string") 92 } 93 if s != customValue { 94 t.Fatalf("expected %q, got %q", customValue, s) 95 } 96 97 _, ok = sim.NodeItem(id2, customKey) 98 if ok { 99 t.Fatal("bucket item should not be found") 100 } 101 }) 102 103 if err := sim.StopNode(id2); err != nil { 104 t.Fatal(err) 105 } 106 107 t.Run("UpNodesItems", func(t *testing.T) { 108 items := sim.UpNodesItems(testKey) 109 110 v, ok := items[id1] 111 if !ok { 112 t.Errorf("node 1 item not found") 113 } 114 s, ok := v.(string) 115 if !ok { 116 t.Fatal("node 1 item value is not string") 117 } 118 if s != testValue+id1.String() { 119 t.Fatalf("expected %q, got %q", testValue+id1.String(), s) 120 } 121 122 _, ok = items[id2] 123 if ok { 124 t.Errorf("node 2 item should not be found") 125 } 126 }) 127 128 t.Run("NodeItems", func(t *testing.T) { 129 items := sim.NodesItems(testKey) 130 131 v, ok := items[id1] 132 if !ok { 133 t.Errorf("node 1 item not found") 134 } 135 s, ok := v.(string) 136 if !ok { 137 t.Fatal("node 1 item value is not string") 138 } 139 if s != testValue+id1.String() { 140 t.Fatalf("expected %q, got %q", testValue+id1.String(), s) 141 } 142 143 v, ok = items[id2] 144 if !ok { 145 t.Errorf("node 2 item not found") 146 } 147 s, ok = v.(string) 148 if !ok { 149 t.Fatal("node 1 item value is not string") 150 } 151 if s != testValue+id2.String() { 152 t.Fatalf("expected %q, got %q", testValue+id2.String(), s) 153 } 154 }) 155 }