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