overlay_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. package main
  17. import (
  18. "context"
  19. "encoding/json"
  20. "fmt"
  21. "io/ioutil"
  22. "net/http"
  23. "net/http/httptest"
  24. "net/url"
  25. "testing"
  26. "time"
  27. "github.com/ethereum/go-ethereum/p2p/enode"
  28. "github.com/ethereum/go-ethereum/p2p/simulations"
  29. "github.com/ethereum/go-ethereum/swarm/log"
  30. )
  31. var (
  32. nodeCount = 10
  33. )
  34. //This test is used to test the overlay simulation.
  35. //As the simulation is executed via a main, it is easily missed on changes
  36. //An automated test will prevent that
  37. //The test just connects to the simulations, starts the network,
  38. //starts the mocker, gets the number of nodes, and stops it again.
  39. //It also provides a documentation on the steps needed by frontends
  40. //to use the simulations
  41. func TestOverlaySim(t *testing.T) {
  42. //start the simulation
  43. log.Info("Start simulation backend")
  44. //get the simulation networ; needed to subscribe for up events
  45. net := newSimulationNetwork()
  46. //create the overlay simulation
  47. sim := newOverlaySim(net)
  48. //create a http test server with it
  49. srv := httptest.NewServer(sim)
  50. defer srv.Close()
  51. log.Debug("Http simulation server started. Start simulation network")
  52. //start the simulation network (initialization of simulation)
  53. resp, err := http.Post(srv.URL+"/start", "application/json", nil)
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. defer resp.Body.Close()
  58. if resp.StatusCode != http.StatusOK {
  59. t.Fatalf("Expected Status Code %d, got %d", http.StatusOK, resp.StatusCode)
  60. }
  61. log.Debug("Start mocker")
  62. //start the mocker, needs a node count and an ID
  63. resp, err = http.PostForm(srv.URL+"/mocker/start",
  64. url.Values{
  65. "node-count": {fmt.Sprintf("%d", nodeCount)},
  66. "mocker-type": {simulations.GetMockerList()[0]},
  67. })
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. defer resp.Body.Close()
  72. if resp.StatusCode != http.StatusOK {
  73. reason, err := ioutil.ReadAll(resp.Body)
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. t.Fatalf("Expected Status Code %d, got %d, response body %s", http.StatusOK, resp.StatusCode, string(reason))
  78. }
  79. //variables needed to wait for nodes being up
  80. var upCount int
  81. trigger := make(chan enode.ID)
  82. //wait for all nodes to be up
  83. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  84. defer cancel()
  85. //start watching node up events...
  86. go watchSimEvents(net, ctx, trigger)
  87. //...and wait until all expected up events (nodeCount) have been received
  88. LOOP:
  89. for {
  90. select {
  91. case <-trigger:
  92. //new node up event received, increase counter
  93. upCount++
  94. //all expected node up events received
  95. if upCount == nodeCount {
  96. break LOOP
  97. }
  98. case <-ctx.Done():
  99. t.Fatalf("Timed out waiting for up events")
  100. }
  101. }
  102. //at this point we can query the server
  103. log.Info("Get number of nodes")
  104. //get the number of nodes
  105. resp, err = http.Get(srv.URL + "/nodes")
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. defer resp.Body.Close()
  110. if resp.StatusCode != http.StatusOK {
  111. t.Fatalf("err %s", resp.Status)
  112. }
  113. b, err := ioutil.ReadAll(resp.Body)
  114. if err != nil {
  115. t.Fatal(err)
  116. }
  117. //unmarshal number of nodes from JSON response
  118. var nodesArr []simulations.Node
  119. err = json.Unmarshal(b, &nodesArr)
  120. if err != nil {
  121. t.Fatal(err)
  122. }
  123. //check if number of nodes received is same as sent
  124. if len(nodesArr) != nodeCount {
  125. t.Fatal(fmt.Errorf("Expected %d number of nodes, got %d", nodeCount, len(nodesArr)))
  126. }
  127. //need to let it run for a little while, otherwise stopping it immediately can crash due running nodes
  128. //wanting to connect to already stopped nodes
  129. time.Sleep(1 * time.Second)
  130. log.Info("Stop the network")
  131. //stop the network
  132. resp, err = http.Post(srv.URL+"/stop", "application/json", nil)
  133. if err != nil {
  134. t.Fatal(err)
  135. }
  136. defer resp.Body.Close()
  137. if resp.StatusCode != http.StatusOK {
  138. t.Fatalf("err %s", resp.Status)
  139. }
  140. log.Info("Reset the network")
  141. //reset the network (removes all nodes and connections)
  142. resp, err = http.Post(srv.URL+"/reset", "application/json", nil)
  143. if err != nil {
  144. t.Fatal(err)
  145. }
  146. defer resp.Body.Close()
  147. if resp.StatusCode != http.StatusOK {
  148. t.Fatalf("err %s", resp.Status)
  149. }
  150. }
  151. //watch for events so we know when all nodes are up
  152. func watchSimEvents(net *simulations.Network, ctx context.Context, trigger chan enode.ID) {
  153. events := make(chan *simulations.Event)
  154. sub := net.Events().Subscribe(events)
  155. defer sub.Unsubscribe()
  156. for {
  157. select {
  158. case ev := <-events:
  159. //only catch node up events
  160. if ev.Type == simulations.EventTypeNode {
  161. if ev.Node.Up() {
  162. log.Debug("got node up event", "event", ev, "node", ev.Node.Config.ID)
  163. select {
  164. case trigger <- ev.Node.Config.ID:
  165. case <-ctx.Done():
  166. return
  167. }
  168. }
  169. }
  170. case <-ctx.Done():
  171. return
  172. }
  173. }
  174. }