github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/swarm/network/simulation/bucket_test.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 19:16:43</date> 10 //</624450114211024896> 11 12 13 package simulation 14 15 import ( 16 "sync" 17 "testing" 18 19 "github.com/ethereum/go-ethereum/node" 20 "github.com/ethereum/go-ethereum/p2p/simulations/adapters" 21 ) 22 23 //TestServiceBucket使用子测试测试所有Bucket功能。 24 //它通过向两个节点的bucket中添加项来构造两个节点的模拟。 25 //在servicefunc构造函数中,然后通过setnodeitem。测试upnodesitems 26 //通过停止一个节点并验证其项的可用性来完成。 27 func TestServiceBucket(t *testing.T) { 28 testKey := "Key" 29 testValue := "Value" 30 31 sim := New(map[string]ServiceFunc{ 32 "noop": func(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) { 33 b.Store(testKey, testValue+ctx.Config.ID.String()) 34 return newNoopService(), nil, nil 35 }, 36 }) 37 defer sim.Close() 38 39 id1, err := sim.AddNode() 40 if err != nil { 41 t.Fatal(err) 42 } 43 44 id2, err := sim.AddNode() 45 if err != nil { 46 t.Fatal(err) 47 } 48 49 t.Run("ServiceFunc bucket Store", func(t *testing.T) { 50 v, ok := sim.NodeItem(id1, testKey) 51 if !ok { 52 t.Fatal("bucket item not found") 53 } 54 s, ok := v.(string) 55 if !ok { 56 t.Fatal("bucket item value is not string") 57 } 58 if s != testValue+id1.String() { 59 t.Fatalf("expected %q, got %q", testValue+id1.String(), s) 60 } 61 62 v, ok = sim.NodeItem(id2, testKey) 63 if !ok { 64 t.Fatal("bucket item not found") 65 } 66 s, ok = v.(string) 67 if !ok { 68 t.Fatal("bucket item value is not string") 69 } 70 if s != testValue+id2.String() { 71 t.Fatalf("expected %q, got %q", testValue+id2.String(), s) 72 } 73 }) 74 75 customKey := "anotherKey" 76 customValue := "anotherValue" 77 78 t.Run("SetNodeItem", func(t *testing.T) { 79 sim.SetNodeItem(id1, customKey, customValue) 80 81 v, ok := sim.NodeItem(id1, customKey) 82 if !ok { 83 t.Fatal("bucket item not found") 84 } 85 s, ok := v.(string) 86 if !ok { 87 t.Fatal("bucket item value is not string") 88 } 89 if s != customValue { 90 t.Fatalf("expected %q, got %q", customValue, s) 91 } 92 93 _, ok = sim.NodeItem(id2, customKey) 94 if ok { 95 t.Fatal("bucket item should not be found") 96 } 97 }) 98 99 if err := sim.StopNode(id2); err != nil { 100 t.Fatal(err) 101 } 102 103 t.Run("UpNodesItems", func(t *testing.T) { 104 items := sim.UpNodesItems(testKey) 105 106 v, ok := items[id1] 107 if !ok { 108 t.Errorf("node 1 item not found") 109 } 110 s, ok := v.(string) 111 if !ok { 112 t.Fatal("node 1 item value is not string") 113 } 114 if s != testValue+id1.String() { 115 t.Fatalf("expected %q, got %q", testValue+id1.String(), s) 116 } 117 118 _, ok = items[id2] 119 if ok { 120 t.Errorf("node 2 item should not be found") 121 } 122 }) 123 124 t.Run("NodeItems", func(t *testing.T) { 125 items := sim.NodesItems(testKey) 126 127 v, ok := items[id1] 128 if !ok { 129 t.Errorf("node 1 item not found") 130 } 131 s, ok := v.(string) 132 if !ok { 133 t.Fatal("node 1 item value is not string") 134 } 135 if s != testValue+id1.String() { 136 t.Fatalf("expected %q, got %q", testValue+id1.String(), s) 137 } 138 139 v, ok = items[id2] 140 if !ok { 141 t.Errorf("node 2 item not found") 142 } 143 s, ok = v.(string) 144 if !ok { 145 t.Fatal("node 1 item value is not string") 146 } 147 if s != testValue+id2.String() { 148 t.Fatalf("expected %q, got %q", testValue+id2.String(), s) 149 } 150 }) 151 } 152