github.com/turingchain2020/turingchain@v1.1.21/client/mock_system_test.go (about) 1 // Copyright Turing Corp. 2018 All Rights Reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package client_test 6 7 import ( 8 "flag" 9 "time" 10 11 "github.com/turingchain2020/turingchain/client" 12 "github.com/turingchain2020/turingchain/common/log" 13 "github.com/turingchain2020/turingchain/queue" 14 "github.com/turingchain2020/turingchain/rpc" 15 "github.com/turingchain2020/turingchain/types" 16 ) 17 18 var ( 19 configPath = flag.String("f", "../cmd/turingchain/turingchain.test.toml", "configfile") 20 21 jrpcaddr = "localhost:9671" 22 jrpcsite = "http://localhost:9671" 23 grpcaddr = "localhost:9672" 24 grpcsite = "localhost:9672" 25 ) 26 27 func init() { 28 cfg, sub := types.InitCfg(*configPath) 29 println(sub) 30 // change rpc bind address 31 cfg.RPC.JrpcBindAddr = jrpcaddr 32 cfg.RPC.GrpcBindAddr = grpcaddr 33 rpc.InitCfg(cfg.RPC) 34 log.SetLogLevel("crit") 35 } 36 37 type MockLive interface { 38 OnStartup(m *mockSystem) 39 OnStop() 40 } 41 42 type mockSystem struct { 43 grpcMock MockLive 44 jrpcMock MockLive 45 q *mockQueue 46 chain *mockBlockChain 47 mem *mockMempool 48 wallet *mockWallet 49 p2p *mockP2P 50 consensus *mockConsensus 51 store *mockStore 52 execs *mockExecs 53 } 54 55 type mockClient struct { 56 c queue.Client 57 } 58 59 func (mock *mockClient) Send(msg *queue.Message, waitReply bool) error { 60 if msg.Topic == "error" { 61 return types.ErrInvalidParam 62 } 63 return mock.c.Send(msg, waitReply) 64 } 65 66 func (mock *mockClient) SendTimeout(msg *queue.Message, waitReply bool, timeout time.Duration) error { 67 return mock.c.SendTimeout(msg, waitReply, timeout) 68 } 69 70 func (mock *mockClient) Wait(msg *queue.Message) (*queue.Message, error) { 71 return mock.c.Wait(msg) 72 } 73 74 func (mock *mockClient) Reply(msg *queue.Message) { 75 mock.c.Reply(msg) 76 } 77 78 func (mock *mockClient) WaitTimeout(msg *queue.Message, timeout time.Duration) (*queue.Message, error) { 79 return mock.c.WaitTimeout(msg, timeout) 80 } 81 82 func (mock *mockClient) Recv() chan *queue.Message { 83 return mock.c.Recv() 84 } 85 86 func (mock *mockClient) Sub(topic string) { 87 mock.c.Sub(topic) 88 } 89 90 func (mock *mockClient) Close() { 91 mock.c.Close() 92 } 93 94 func (mock *mockClient) CloseQueue() (*types.Reply, error) { 95 mock.c.CloseQueue() 96 return &types.Reply{IsOk: true, Msg: []byte("Ok")}, nil 97 } 98 99 func (mock *mockClient) NewMessage(topic string, ty int64, data interface{}) *queue.Message { 100 return mock.c.NewMessage(topic, ty, data) 101 } 102 103 func (mock *mockClient) FreeMessage(msg ...*queue.Message) { 104 } 105 106 func (mock *mockClient) GetConfig() *types.TuringchainConfig { 107 return mock.c.GetConfig() 108 } 109 110 func (mock *mockClient) Clone() queue.Client { 111 clone := mockClient{} 112 clone.c = mock.c 113 return &clone 114 } 115 116 type mockQueue struct { 117 q queue.Queue 118 } 119 120 func (mock *mockQueue) Close() { 121 mock.q.Close() 122 } 123 124 func (mock *mockQueue) Start() { 125 mock.q.Start() 126 } 127 128 func (mock *mockQueue) Client() queue.Client { 129 client := mockClient{} 130 client.c = mock.q.Client() 131 return &client 132 } 133 134 func (mock *mockQueue) Name() string { 135 return mock.q.Name() 136 } 137 138 func (mock *mockSystem) startup(size int) client.QueueProtocolAPI { 139 140 var q = queue.New("channel") 141 q.SetConfig(types.NewTuringchainConfig(types.GetDefaultCfgstring())) 142 queue := &mockQueue{q: q} 143 chain := &mockBlockChain{} 144 chain.SetQueueClient(q) 145 mem := &mockMempool{} 146 mem.SetQueueClient(q) 147 wallet := &mockWallet{} 148 wallet.SetQueueClient(q) 149 p2p := &mockP2P{} 150 p2p.SetQueueClient(q) 151 consensus := &mockConsensus{} 152 consensus.SetQueueClient(q) 153 store := &mockStore{} 154 store.SetQueueClient(q) 155 execs := new(mockExecs) 156 execs.SetQueueClient(q) 157 158 mock.q = queue 159 mock.chain = chain 160 mock.mem = mem 161 mock.wallet = wallet 162 mock.p2p = p2p 163 mock.consensus = consensus 164 mock.store = store 165 mock.execs = execs 166 if mock.grpcMock != nil { 167 mock.grpcMock.OnStartup(mock) 168 } 169 if mock.jrpcMock != nil { 170 mock.jrpcMock.OnStartup(mock) 171 } 172 return mock.getAPI() 173 } 174 175 func (mock *mockSystem) stop() { 176 if mock.jrpcMock != nil { 177 mock.jrpcMock.OnStop() 178 } 179 if mock.grpcMock != nil { 180 mock.grpcMock.OnStop() 181 } 182 mock.chain.Close() 183 mock.mem.Close() 184 mock.wallet.Close() 185 mock.p2p.Close() 186 mock.consensus.Close() 187 mock.store.Close() 188 mock.execs.Close() 189 mock.q.Close() 190 } 191 192 func (mock *mockSystem) getAPI() client.QueueProtocolAPI { 193 api, _ := client.New(mock.q.Client(), nil) 194 return api 195 } 196 197 type mockJRPCSystem struct { 198 japi *rpc.JSONRPCServer 199 ctx *JSONRPCCtx 200 } 201 202 func (mock *mockJRPCSystem) OnStartup(m *mockSystem) { 203 println("=============jrpc====") 204 mock.japi = rpc.NewJSONRPCServer(m.q.Client(), nil) 205 ch := make(chan struct{}, 1) 206 go func() { 207 ch <- struct{}{} 208 _, err := mock.japi.Listen() 209 if err != nil { 210 panic(err) 211 } 212 }() 213 <-ch 214 time.Sleep(time.Millisecond) 215 } 216 217 func (mock *mockJRPCSystem) OnStop() { 218 mock.japi.Close() 219 } 220 221 func (mock *mockJRPCSystem) newRPCCtx(methed string, params, res interface{}) error { 222 if mock.ctx == nil { 223 mock.ctx = NewJSONRPCCtx(methed, params, res) 224 } else { 225 mock.ctx.Method = methed 226 mock.ctx.Params = params 227 mock.ctx.Res = res 228 } 229 return mock.ctx.Run() 230 } 231 232 type mockGRPCSystem struct { 233 gapi *rpc.Grpcserver 234 ctx *GrpcCtx 235 } 236 237 func (mock *mockGRPCSystem) OnStartup(m *mockSystem) { 238 println("=============grpc====") 239 mock.gapi = rpc.NewGRpcServer(m.q.Client(), nil) 240 ch := make(chan struct{}, 1) 241 go func() { 242 ch <- struct{}{} 243 _, err := mock.gapi.Listen() 244 if err != nil { 245 panic(err) 246 } 247 }() 248 <-ch 249 time.Sleep(time.Millisecond) 250 } 251 252 func (mock *mockGRPCSystem) OnStop() { 253 mock.gapi.Close() 254 } 255 256 func (mock *mockGRPCSystem) newRPCCtx(method string, param, res interface{}) error { 257 if mock.ctx == nil { 258 mock.ctx = NewGRpcCtx(method, param, res) 259 } else { 260 mock.ctx.Method = method 261 mock.ctx.Params = param 262 mock.ctx.Res = res 263 } 264 return mock.ctx.Run() 265 }