simulation_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 simulation
  17. import (
  18. "context"
  19. "errors"
  20. "flag"
  21. "sync"
  22. "testing"
  23. "time"
  24. "github.com/ethereum/go-ethereum/log"
  25. "github.com/ethereum/go-ethereum/node"
  26. "github.com/ethereum/go-ethereum/p2p"
  27. "github.com/ethereum/go-ethereum/p2p/simulations/adapters"
  28. "github.com/ethereum/go-ethereum/rpc"
  29. colorable "github.com/mattn/go-colorable"
  30. )
  31. var (
  32. loglevel = flag.Int("loglevel", 2, "verbosity of logs")
  33. )
  34. func init() {
  35. flag.Parse()
  36. log.PrintOrigins(true)
  37. log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(*loglevel), log.StreamHandler(colorable.NewColorableStderr(), log.TerminalFormat(true))))
  38. }
  39. // TestRun tests if Run method calls RunFunc and if it handles context properly.
  40. func TestRun(t *testing.T) {
  41. sim := New(noopServiceFuncMap)
  42. defer sim.Close()
  43. t.Run("call", func(t *testing.T) {
  44. expect := "something"
  45. var got string
  46. r := sim.Run(context.Background(), func(ctx context.Context, sim *Simulation) error {
  47. got = expect
  48. return nil
  49. })
  50. if r.Error != nil {
  51. t.Errorf("unexpected error: %v", r.Error)
  52. }
  53. if got != expect {
  54. t.Errorf("expected %q, got %q", expect, got)
  55. }
  56. })
  57. t.Run("cancelation", func(t *testing.T) {
  58. ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
  59. defer cancel()
  60. r := sim.Run(ctx, func(ctx context.Context, sim *Simulation) error {
  61. time.Sleep(time.Second)
  62. return nil
  63. })
  64. if r.Error != context.DeadlineExceeded {
  65. t.Errorf("unexpected error: %v", r.Error)
  66. }
  67. })
  68. t.Run("context value and duration", func(t *testing.T) {
  69. ctx := context.WithValue(context.Background(), "hey", "there")
  70. sleep := 50 * time.Millisecond
  71. r := sim.Run(ctx, func(ctx context.Context, sim *Simulation) error {
  72. if ctx.Value("hey") != "there" {
  73. return errors.New("expected context value not passed")
  74. }
  75. time.Sleep(sleep)
  76. return nil
  77. })
  78. if r.Error != nil {
  79. t.Errorf("unexpected error: %v", r.Error)
  80. }
  81. if r.Duration < sleep {
  82. t.Errorf("reported run duration less then expected: %s", r.Duration)
  83. }
  84. })
  85. }
  86. // TestClose tests are Close method triggers all close functions and are all nodes not up anymore.
  87. func TestClose(t *testing.T) {
  88. var mu sync.Mutex
  89. var cleanupCount int
  90. sleep := 50 * time.Millisecond
  91. sim := New(map[string]ServiceFunc{
  92. "noop": func(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) {
  93. return newNoopService(), func() {
  94. time.Sleep(sleep)
  95. mu.Lock()
  96. defer mu.Unlock()
  97. cleanupCount++
  98. }, nil
  99. },
  100. })
  101. nodeCount := 30
  102. _, err := sim.AddNodes(nodeCount)
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. var upNodeCount int
  107. for _, n := range sim.Net.GetNodes() {
  108. if n.Up {
  109. upNodeCount++
  110. }
  111. }
  112. if upNodeCount != nodeCount {
  113. t.Errorf("all nodes should be up, insted only %v are up", upNodeCount)
  114. }
  115. sim.Close()
  116. if cleanupCount != nodeCount {
  117. t.Errorf("number of cleanups expected %v, got %v", nodeCount, cleanupCount)
  118. }
  119. upNodeCount = 0
  120. for _, n := range sim.Net.GetNodes() {
  121. if n.Up {
  122. upNodeCount++
  123. }
  124. }
  125. if upNodeCount != 0 {
  126. t.Errorf("all nodes should be down, insted %v are up", upNodeCount)
  127. }
  128. }
  129. // TestDone checks if Close method triggers the closing of done channel.
  130. func TestDone(t *testing.T) {
  131. sim := New(noopServiceFuncMap)
  132. sleep := 50 * time.Millisecond
  133. timeout := 2 * time.Second
  134. start := time.Now()
  135. go func() {
  136. time.Sleep(sleep)
  137. sim.Close()
  138. }()
  139. select {
  140. case <-time.After(timeout):
  141. t.Error("done channel closing timmed out")
  142. case <-sim.Done():
  143. if d := time.Since(start); d < sleep {
  144. t.Errorf("done channel closed sooner then expected: %s", d)
  145. }
  146. }
  147. }
  148. // a helper map for usual services that do not do anyting
  149. var noopServiceFuncMap = map[string]ServiceFunc{
  150. "noop": noopServiceFunc,
  151. }
  152. // a helper function for most basic noop service
  153. func noopServiceFunc(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) {
  154. return newNoopService(), nil, nil
  155. }
  156. // noopService is the service that does not do anything
  157. // but implements node.Service interface.
  158. type noopService struct{}
  159. func newNoopService() node.Service {
  160. return &noopService{}
  161. }
  162. func (t *noopService) Protocols() []p2p.Protocol {
  163. return []p2p.Protocol{}
  164. }
  165. func (t *noopService) APIs() []rpc.API {
  166. return []rpc.API{}
  167. }
  168. func (t *noopService) Start(server *p2p.Server) error {
  169. return nil
  170. }
  171. func (t *noopService) Stop() error {
  172. return nil
  173. }