mocker_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 mokcer 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/discover"
  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[discover.NodeID]bool)
  74. wg.Add(1)
  75. nodesComplete := false
  76. connCount := 0
  77. go func() {
  78. for {
  79. select {
  80. case event := <-events:
  81. //if the event is a node Up event only
  82. if event.Node != nil && event.Node.Up {
  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. //wait for 3s as the mocker will need time to connect the nodes
  89. //time.Sleep( 3 *time.Second)
  90. }
  91. } else if event.Conn != nil && nodesComplete {
  92. connCount += 1
  93. if connCount == (nodeCount-1)*2 {
  94. wg.Done()
  95. return
  96. }
  97. }
  98. case <-time.After(30 * time.Second):
  99. wg.Done()
  100. t.Fatalf("Timeout waiting for nodes being started up!")
  101. }
  102. }
  103. }()
  104. //take the last element of the mockerlist as the default mocker-type to ensure one is enabled
  105. mockertype := mockerlist[len(mockerlist)-1]
  106. //still, use hardcoded "probabilistic" one if available ;)
  107. for _, m := range mockerlist {
  108. if m == "probabilistic" {
  109. mockertype = m
  110. break
  111. }
  112. }
  113. //start the mocker with nodeCount number of nodes
  114. resp, err = http.PostForm(s.URL+"/mocker/start", url.Values{"mocker-type": {mockertype}, "node-count": {strconv.Itoa(nodeCount)}})
  115. if err != nil {
  116. t.Fatalf("Could not start mocker: %s", err)
  117. }
  118. if resp.StatusCode != 200 {
  119. t.Fatalf("Invalid Status Code received for starting mocker, expected 200, got %d", resp.StatusCode)
  120. }
  121. wg.Wait()
  122. //check there are nodeCount number of nodes in the network
  123. nodes_info, err := client.GetNodes()
  124. if err != nil {
  125. t.Fatalf("Could not get nodes list: %s", err)
  126. }
  127. if len(nodes_info) != nodeCount {
  128. t.Fatalf("Expected %d number of nodes, got: %d", nodeCount, len(nodes_info))
  129. }
  130. //stop the mocker
  131. resp, err = http.Post(s.URL+"/mocker/stop", "", nil)
  132. if err != nil {
  133. t.Fatalf("Could not stop mocker: %s", err)
  134. }
  135. if resp.StatusCode != 200 {
  136. t.Fatalf("Invalid Status Code received for stopping mocker, expected 200, got %d", resp.StatusCode)
  137. }
  138. //reset the network
  139. _, err = http.Post(s.URL+"/reset", "", nil)
  140. if err != nil {
  141. t.Fatalf("Could not reset network: %s", err)
  142. }
  143. //now the number of nodes in the network should be zero
  144. nodes_info, err = client.GetNodes()
  145. if err != nil {
  146. t.Fatalf("Could not get nodes list: %s", err)
  147. }
  148. if len(nodes_info) != 0 {
  149. t.Fatalf("Expected empty list of nodes, got: %d", len(nodes_info))
  150. }
  151. }