simulation_test.go 5.1 KB

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