github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/network/simulations/overlay_test.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 // 10 // 11 // 12 // 13 // 14 // 15 // 16 // 17 // 18 // 19 // 20 // 21 // 22 // 23 // 24 package main 25 26 import ( 27 "context" 28 "encoding/json" 29 "fmt" 30 "io/ioutil" 31 "net/http" 32 "net/http/httptest" 33 "net/url" 34 "testing" 35 "time" 36 37 "github.com/ethereum/go-ethereum/p2p/discover" 38 "github.com/ethereum/go-ethereum/p2p/simulations" 39 "github.com/ethereum/go-ethereum/swarm/log" 40 ) 41 42 var ( 43 nodeCount = 16 44 ) 45 46 // 47 // 48 // 49 // 50 // 51 // 52 // 53 func TestOverlaySim(t *testing.T) { 54 t.Skip("Test is flaky, see: https:// 55 // 56 log.Info("Start simulation backend") 57 // 58 net := newSimulationNetwork() 59 // 60 sim := newOverlaySim(net) 61 // 62 srv := httptest.NewServer(sim) 63 defer srv.Close() 64 65 log.Debug("Http simulation server started. Start simulation network") 66 // 67 resp, err := http.Post(srv.URL+"/start", "application/json", nil) 68 if err != nil { 69 t.Fatal(err) 70 } 71 defer resp.Body.Close() 72 if resp.StatusCode != http.StatusOK { 73 t.Fatalf("Expected Status Code %d, got %d", http.StatusOK, resp.StatusCode) 74 } 75 76 log.Debug("Start mocker") 77 // 78 resp, err = http.PostForm(srv.URL+"/mocker/start", 79 url.Values{ 80 "node-count": {fmt.Sprintf("%d", nodeCount)}, 81 "mocker-type": {simulations.GetMockerList()[0]}, 82 }) 83 if err != nil { 84 t.Fatal(err) 85 } 86 defer resp.Body.Close() 87 if resp.StatusCode != http.StatusOK { 88 reason, err := ioutil.ReadAll(resp.Body) 89 if err != nil { 90 t.Fatal(err) 91 } 92 t.Fatalf("Expected Status Code %d, got %d, response body %s", http.StatusOK, resp.StatusCode, string(reason)) 93 } 94 95 // 96 var upCount int 97 trigger := make(chan discover.NodeID) 98 99 // 100 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) 101 defer cancel() 102 103 // 104 go watchSimEvents(net, ctx, trigger) 105 106 // 107 LOOP: 108 for { 109 select { 110 case <-trigger: 111 // 112 upCount++ 113 // 114 if upCount == nodeCount { 115 break LOOP 116 } 117 case <-ctx.Done(): 118 t.Fatalf("Timed out waiting for up events") 119 } 120 121 } 122 123 // 124 log.Info("Get number of nodes") 125 // 126 resp, err = http.Get(srv.URL + "/nodes") 127 if err != nil { 128 t.Fatal(err) 129 } 130 131 defer resp.Body.Close() 132 if resp.StatusCode != http.StatusOK { 133 t.Fatalf("err %s", resp.Status) 134 } 135 b, err := ioutil.ReadAll(resp.Body) 136 if err != nil { 137 t.Fatal(err) 138 } 139 140 // 141 var nodesArr []simulations.Node 142 err = json.Unmarshal(b, &nodesArr) 143 if err != nil { 144 t.Fatal(err) 145 } 146 147 // 148 if len(nodesArr) != nodeCount { 149 t.Fatal(fmt.Errorf("Expected %d number of nodes, got %d", nodeCount, len(nodesArr))) 150 } 151 152 // 153 // 154 time.Sleep(1 * time.Second) 155 156 log.Info("Stop the network") 157 // 158 resp, err = http.Post(srv.URL+"/stop", "application/json", nil) 159 if err != nil { 160 t.Fatal(err) 161 } 162 defer resp.Body.Close() 163 if resp.StatusCode != http.StatusOK { 164 t.Fatalf("err %s", resp.Status) 165 } 166 167 log.Info("Reset the network") 168 // 169 resp, err = http.Post(srv.URL+"/reset", "application/json", nil) 170 if err != nil { 171 t.Fatal(err) 172 } 173 defer resp.Body.Close() 174 if resp.StatusCode != http.StatusOK { 175 t.Fatalf("err %s", resp.Status) 176 } 177 } 178 179 // 180 func watchSimEvents(net *simulations.Network, ctx context.Context, trigger chan discover.NodeID) { 181 events := make(chan *simulations.Event) 182 sub := net.Events().Subscribe(events) 183 defer sub.Unsubscribe() 184 185 for { 186 select { 187 case ev := <-events: 188 // 189 if ev.Type == simulations.EventTypeNode { 190 if ev.Node.Up { 191 log.Debug("got node up event", "event", ev, "node", ev.Node.Config.ID) 192 select { 193 case trigger <- ev.Node.Config.ID: 194 case <-ctx.Done(): 195 return 196 } 197 } 198 } 199 case <-ctx.Done(): 200 return 201 } 202 } 203 }