mocker_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright 2017 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 simulations simulates p2p networks.
  17. // A mocker simulates starting and stopping real nodes in a network.
  18. package simulations
  19. import (
  20. "encoding/json"
  21. "net/http"
  22. "net/url"
  23. "strconv"
  24. "sync"
  25. "testing"
  26. "time"
  27. "github.com/ethereum/go-ethereum/p2p/enode"
  28. )
  29. func TestMocker(t *testing.T) {
  30. //start the simulation HTTP server
  31. _, s := testHTTPServer(t)
  32. defer s.Close()
  33. //create a client
  34. client := NewClient(s.URL)
  35. //start the network
  36. err := client.StartNetwork()
  37. if err != nil {
  38. t.Fatalf("Could not start test network: %s", err)
  39. }
  40. //stop the network to terminate
  41. defer func() {
  42. err = client.StopNetwork()
  43. if err != nil {
  44. t.Fatalf("Could not stop test network: %s", err)
  45. }
  46. }()
  47. //get the list of available mocker types
  48. resp, err := http.Get(s.URL + "/mocker")
  49. if err != nil {
  50. t.Fatalf("Could not get mocker list: %s", err)
  51. }
  52. defer resp.Body.Close()
  53. if resp.StatusCode != 200 {
  54. t.Fatalf("Invalid Status Code received, expected 200, got %d", resp.StatusCode)
  55. }
  56. //check the list is at least 1 in size
  57. var mockerlist []string
  58. err = json.NewDecoder(resp.Body).Decode(&mockerlist)
  59. if err != nil {
  60. t.Fatalf("Error decoding JSON mockerlist: %s", err)
  61. }
  62. if len(mockerlist) < 1 {
  63. t.Fatalf("No mockers available")
  64. }
  65. nodeCount := 10
  66. var wg sync.WaitGroup
  67. events := make(chan *Event, 10)
  68. var opts SubscribeOpts
  69. sub, err := client.SubscribeNetwork(events, opts)
  70. defer sub.Unsubscribe()
  71. // wait until all nodes are started and connected
  72. // store every node up event in a map (value is irrelevant, mimic Set datatype)
  73. nodemap := make(map[enode.ID]bool)
  74. nodesComplete := false
  75. connCount := 0
  76. wg.Add(1)
  77. go func() {
  78. defer wg.Done()
  79. for connCount < (nodeCount-1)*2 {
  80. select {
  81. case event := <-events:
  82. if isNodeUp(event) {
  83. //add the correspondent node ID to the map
  84. nodemap[event.Node.Config.ID] = true
  85. //this means all nodes got a nodeUp event, so we can continue the test
  86. if len(nodemap) == nodeCount {
  87. nodesComplete = true
  88. }
  89. } else if event.Conn != nil && nodesComplete {
  90. connCount += 1
  91. }
  92. case <-time.After(30 * time.Second):
  93. t.Errorf("Timeout waiting for nodes being started up!")
  94. return
  95. }
  96. }
  97. }()
  98. //take the last element of the mockerlist as the default mocker-type to ensure one is enabled
  99. mockertype := mockerlist[len(mockerlist)-1]
  100. //still, use hardcoded "probabilistic" one if available ;)
  101. for _, m := range mockerlist {
  102. if m == "probabilistic" {
  103. mockertype = m
  104. break
  105. }
  106. }
  107. //start the mocker with nodeCount number of nodes
  108. resp, err = http.PostForm(s.URL+"/mocker/start", url.Values{"mocker-type": {mockertype}, "node-count": {strconv.Itoa(nodeCount)}})
  109. if err != nil {
  110. t.Fatalf("Could not start mocker: %s", err)
  111. }
  112. if resp.StatusCode != 200 {
  113. t.Fatalf("Invalid Status Code received for starting mocker, expected 200, got %d", resp.StatusCode)
  114. }
  115. wg.Wait()
  116. //check there are nodeCount number of nodes in the network
  117. nodesInfo, err := client.GetNodes()
  118. if err != nil {
  119. t.Fatalf("Could not get nodes list: %s", err)
  120. }
  121. if len(nodesInfo) != nodeCount {
  122. t.Fatalf("Expected %d number of nodes, got: %d", nodeCount, len(nodesInfo))
  123. }
  124. //stop the mocker
  125. resp, err = http.Post(s.URL+"/mocker/stop", "", nil)
  126. if err != nil {
  127. t.Fatalf("Could not stop mocker: %s", err)
  128. }
  129. if resp.StatusCode != 200 {
  130. t.Fatalf("Invalid Status Code received for stopping mocker, expected 200, got %d", resp.StatusCode)
  131. }
  132. //reset the network
  133. _, err = http.Post(s.URL+"/reset", "", nil)
  134. if err != nil {
  135. t.Fatalf("Could not reset network: %s", err)
  136. }
  137. //now the number of nodes in the network should be zero
  138. nodesInfo, err = client.GetNodes()
  139. if err != nil {
  140. t.Fatalf("Could not get nodes list: %s", err)
  141. }
  142. if len(nodesInfo) != 0 {
  143. t.Fatalf("Expected empty list of nodes, got: %d", len(nodesInfo))
  144. }
  145. }
  146. func isNodeUp(event *Event) bool {
  147. return event.Node != nil && event.Node.Up()
  148. }