github.com/turingchain2020/turingchain@v1.1.21/client/mock_mempool_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 "bytes" 9 10 "github.com/turingchain2020/turingchain/queue" 11 "github.com/turingchain2020/turingchain/types" 12 ) 13 14 type mockMempool struct { 15 } 16 17 func (m *mockMempool) SetQueueClient(q queue.Queue) { 18 go func() { 19 mempoolKey := "mempool" 20 client := q.Client() 21 client.Sub(mempoolKey) 22 for msg := range client.Recv() { 23 switch msg.Ty { 24 case types.EventTx: 25 if req, ok := msg.GetData().(*types.Transaction); ok { 26 if bytes.Equal([]byte("case1"), req.Execer) { 27 msg.Reply(client.NewMessage(mempoolKey, types.EventReply, &types.Reply{IsOk: false, Msg: []byte("do not support abc")})) 28 } else if bytes.Equal([]byte("case2"), req.Execer) { 29 msg.Reply(client.NewMessage(mempoolKey, types.EventReply, &types.Transaction{})) 30 } else { 31 msg.Reply(client.NewMessage(mempoolKey, types.EventReply, &types.Reply{IsOk: true, Msg: []byte("word")})) 32 } 33 } else { 34 msg.ReplyErr("Do not support", types.ErrInvalidParam) 35 } 36 case types.EventTxList: 37 if req, ok := msg.GetData().(*types.TxHashList); ok { 38 if req.Count == 1 { 39 msg.Reply(client.NewMessage(mempoolKey, types.EventReplyTxList, &types.Transaction{})) 40 } else { 41 msg.Reply(client.NewMessage(mempoolKey, types.EventReplyTxList, &types.ReplyTxList{})) 42 } 43 } else { 44 msg.ReplyErr("Do not support", types.ErrInvalidParam) 45 } 46 case types.EventGetMempool: 47 msg.Reply(client.NewMessage(mempoolKey, types.EventReplyTxList, &types.ReplyTxList{})) 48 case types.EventGetLastMempool: 49 msg.Reply(client.NewMessage(mempoolKey, types.EventReplyTxList, &types.ReplyTxList{})) 50 case types.EventGetProperFee: 51 msg.Reply(client.NewMessage(mempoolKey, types.EventReplyProperFee, &types.ReplyProperFee{})) 52 default: 53 msg.ReplyErr("Do not support", types.ErrNotSupport) 54 } 55 } 56 }() 57 } 58 59 func (m *mockMempool) Close() { 60 }