github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/swarm/network/simulation/simulation_test.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 12:09:48</date> 10 //</624342674534567936> 11 12 // 13 // 14 // 15 // 16 // 17 // 18 // 19 // 20 // 21 // 22 // 23 // 24 // 25 // 26 // 27 28 package simulation 29 30 import ( 31 "context" 32 "errors" 33 "flag" 34 "sync" 35 "testing" 36 "time" 37 38 "github.com/ethereum/go-ethereum/log" 39 "github.com/ethereum/go-ethereum/node" 40 "github.com/ethereum/go-ethereum/p2p" 41 "github.com/ethereum/go-ethereum/p2p/simulations/adapters" 42 "github.com/ethereum/go-ethereum/rpc" 43 colorable "github.com/mattn/go-colorable" 44 ) 45 46 var ( 47 loglevel = flag.Int("loglevel", 2, "verbosity of logs") 48 ) 49 50 func init() { 51 flag.Parse() 52 log.PrintOrigins(true) 53 log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(*loglevel), log.StreamHandler(colorable.NewColorableStderr(), log.TerminalFormat(true)))) 54 } 55 56 // 57 func TestRun(t *testing.T) { 58 sim := New(noopServiceFuncMap) 59 defer sim.Close() 60 61 t.Run("call", func(t *testing.T) { 62 expect := "something" 63 var got string 64 r := sim.Run(context.Background(), func(ctx context.Context, sim *Simulation) error { 65 got = expect 66 return nil 67 }) 68 69 if r.Error != nil { 70 t.Errorf("unexpected error: %v", r.Error) 71 } 72 if got != expect { 73 t.Errorf("expected %q, got %q", expect, got) 74 } 75 }) 76 77 t.Run("cancellation", func(t *testing.T) { 78 ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond) 79 defer cancel() 80 81 r := sim.Run(ctx, func(ctx context.Context, sim *Simulation) error { 82 time.Sleep(time.Second) 83 return nil 84 }) 85 86 if r.Error != context.DeadlineExceeded { 87 t.Errorf("unexpected error: %v", r.Error) 88 } 89 }) 90 91 t.Run("context value and duration", func(t *testing.T) { 92 ctx := context.WithValue(context.Background(), "hey", "there") 93 sleep := 50 * time.Millisecond 94 95 r := sim.Run(ctx, func(ctx context.Context, sim *Simulation) error { 96 if ctx.Value("hey") != "there" { 97 return errors.New("expected context value not passed") 98 } 99 time.Sleep(sleep) 100 return nil 101 }) 102 103 if r.Error != nil { 104 t.Errorf("unexpected error: %v", r.Error) 105 } 106 if r.Duration < sleep { 107 t.Errorf("reported run duration less then expected: %s", r.Duration) 108 } 109 }) 110 } 111 112 // 113 func TestClose(t *testing.T) { 114 var mu sync.Mutex 115 var cleanupCount int 116 117 sleep := 50 * time.Millisecond 118 119 sim := New(map[string]ServiceFunc{ 120 "noop": func(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) { 121 return newNoopService(), func() { 122 time.Sleep(sleep) 123 mu.Lock() 124 defer mu.Unlock() 125 cleanupCount++ 126 }, nil 127 }, 128 }) 129 130 nodeCount := 30 131 132 _, err := sim.AddNodes(nodeCount) 133 if err != nil { 134 t.Fatal(err) 135 } 136 137 var upNodeCount int 138 for _, n := range sim.Net.GetNodes() { 139 if n.Up { 140 upNodeCount++ 141 } 142 } 143 if upNodeCount != nodeCount { 144 t.Errorf("all nodes should be up, insted only %v are up", upNodeCount) 145 } 146 147 sim.Close() 148 149 if cleanupCount != nodeCount { 150 t.Errorf("number of cleanups expected %v, got %v", nodeCount, cleanupCount) 151 } 152 153 upNodeCount = 0 154 for _, n := range sim.Net.GetNodes() { 155 if n.Up { 156 upNodeCount++ 157 } 158 } 159 if upNodeCount != 0 { 160 t.Errorf("all nodes should be down, insted %v are up", upNodeCount) 161 } 162 } 163 164 // 165 func TestDone(t *testing.T) { 166 sim := New(noopServiceFuncMap) 167 sleep := 50 * time.Millisecond 168 timeout := 2 * time.Second 169 170 start := time.Now() 171 go func() { 172 time.Sleep(sleep) 173 sim.Close() 174 }() 175 176 select { 177 case <-time.After(timeout): 178 t.Error("done channel closing timed out") 179 case <-sim.Done(): 180 if d := time.Since(start); d < sleep { 181 t.Errorf("done channel closed sooner then expected: %s", d) 182 } 183 } 184 } 185 186 // 187 var noopServiceFuncMap = map[string]ServiceFunc{ 188 "noop": noopServiceFunc, 189 } 190 191 // 192 func noopServiceFunc(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) { 193 return newNoopService(), nil, nil 194 } 195 196 // 197 // 198 type noopService struct{} 199 200 func newNoopService() node.Service { 201 return &noopService{} 202 } 203 204 func (t *noopService) Protocols() []p2p.Protocol { 205 return []p2p.Protocol{} 206 } 207 208 func (t *noopService) APIs() []rpc.API { 209 return []rpc.API{} 210 } 211 212 func (t *noopService) Start(server *p2p.Server) error { 213 return nil 214 } 215 216 func (t *noopService) Stop() error { 217 return nil 218 } 219