Commit 9318c7b5 authored by mdj33's avatar mdj33 Committed by 33cn

fix para ut

parent 7362589a
...@@ -29,7 +29,8 @@ var ( ...@@ -29,7 +29,8 @@ var (
) )
func TestFilterTxsForPara(t *testing.T) { func TestFilterTxsForPara(t *testing.T) {
types.Init(Title, nil) cfg, _ := types.InitCfg("../../../plugin/dapp/paracross/cmd/build/chain33.para.test.toml")
types.Init(Title, cfg)
detail, filterTxs, _ := createTestTxs(t) detail, filterTxs, _ := createTestTxs(t)
rst := paraexec.FilterTxsForPara(Title, detail) rst := paraexec.FilterTxsForPara(Title, detail)
......
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package para
import (
"math/rand"
"testing"
"github.com/33cn/chain33/blockchain"
"github.com/33cn/chain33/common/log"
"github.com/33cn/chain33/executor"
"github.com/33cn/chain33/mempool"
"github.com/33cn/chain33/p2p"
"github.com/pkg/errors"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
//_ "github.com/33cn/plugin/plugin/dapp/paracross"
pp "github.com/33cn/plugin/plugin/dapp/paracross/executor"
//"github.com/33cn/plugin/plugin/dapp/paracross/rpc"
"time"
"github.com/33cn/chain33/queue"
"github.com/33cn/chain33/store"
_ "github.com/33cn/chain33/system"
"github.com/33cn/chain33/types"
typesmocks "github.com/33cn/chain33/types/mocks"
pt "github.com/33cn/plugin/plugin/dapp/paracross/types"
)
var random *rand.Rand
func init() {
types.Init("user.p.para.", nil)
pp.Init("paracross", nil)
random = rand.New(rand.NewSource(types.Now().UnixNano()))
consensusInterval = 1
log.SetLogLevel("error")
}
type suiteParaCommitMsg struct {
// Include our basic suite logic.
suite.Suite
para *client
grpcCli *typesmocks.Chain33Client
q queue.Queue
block *blockchain.BlockChain
exec *executor.Executor
store queue.Module
mem queue.Module
network *p2p.P2p
}
func initConfigFile() (*types.Config, *types.ConfigSubModule) {
cfg, sub := types.InitCfg("../../../plugin/dapp/paracross/cmd/build/chain33.para.test.toml")
return cfg, sub
}
func (s *suiteParaCommitMsg) initEnv(cfg *types.Config, sub *types.ConfigSubModule) {
q := queue.New("channel")
s.q = q
//api, _ = client.New(q.Client(), nil)
var subcfg subConfig
if sub != nil {
types.MustDecode(sub.Consensus["para"], &subcfg)
}
s.block = blockchain.New(cfg.BlockChain)
s.block.SetQueueClient(q.Client())
s.exec = executor.New(cfg.Exec, sub.Exec)
s.exec.SetQueueClient(q.Client())
s.store = store.New(cfg.Store, sub.Store)
s.store.SetQueueClient(q.Client())
s.para = New(cfg.Consensus, sub.Consensus["para"]).(*client)
s.grpcCli = &typesmocks.Chain33Client{}
// GetBlockBySeq return error to stop create's for cycle to request tx
s.grpcCli.On("GetBlockBySeq", mock.Anything, mock.Anything).Return(nil, errors.New("quit create"))
//data := &types.Int64{1}
s.grpcCli.On("GetLastBlockSequence", mock.Anything, mock.Anything).Return(nil, errors.New("nil")).Maybe()
reply := &types.Reply{IsOk: true}
s.grpcCli.On("IsSync", mock.Anything, mock.Anything).Return(reply, nil)
result := &pt.ParacrossStatus{Height: -1}
data := types.Encode(result)
ret := &types.Reply{IsOk: true, Msg: data}
s.grpcCli.On("QueryChain", mock.Anything, mock.Anything).Return(ret, nil).Maybe()
s.grpcCli.On("SendTransaction", mock.Anything, mock.Anything).Return(reply, nil).Maybe()
s.grpcCli.On("GetLastHeader", mock.Anything, mock.Anything).Return(&types.Header{Height: subcfg.StartHeight + minBlockNum}, nil).Maybe()
s.grpcCli.On("GetBlockHash", mock.Anything, mock.Anything).Return(&types.ReplyHash{Hash: []byte("1")}, nil).Maybe()
s.grpcCli.On("GetSequenceByHash", mock.Anything, mock.Anything).Return(&types.Int64{Data: subcfg.StartHeight}, nil).Maybe()
s.para.grpcClient = s.grpcCli
s.para.SetQueueClient(q.Client())
s.mem = mempool.New(cfg.Mempool, nil)
s.mem.SetQueueClient(q.Client())
s.mem.Wait()
s.network = p2p.New(cfg.P2P)
s.network.SetQueueClient(q.Client())
s.para.wg.Add(1)
go walletProcess(q, s.para)
}
func walletProcess(q queue.Queue, para *client) {
defer para.wg.Done()
client := q.Client()
client.Sub("wallet")
for {
select {
case <-para.commitMsgClient.quit:
return
case msg := <-client.Recv():
if msg.Ty == types.EventDumpPrivkey {
msg.Reply(client.NewMessage("", types.EventHeader, &types.ReplyString{Data: "6da92a632ab7deb67d38c0f6560bcfed28167998f6496db64c258d5e8393a81b"}))
}
}
}
}
func (s *suiteParaCommitMsg) SetupSuite() {
s.initEnv(initConfigFile())
}
func (s *suiteParaCommitMsg) createBlock() {
var i int64
for i = 0; i < 3; i++ {
lastBlock, err := s.para.RequestLastBlock()
if err != nil {
plog.Error("para test", "err", err.Error())
}
s.Equal(int64(i), lastBlock.Height)
s.para.createBlock(lastBlock, nil, i, getMainBlock(i+1, lastBlock.BlockTime+1))
}
}
func (s *suiteParaCommitMsg) TestRun_1() {
s.createBlock()
s.testRunRmvBlock()
lastBlock, _ := s.para.RequestLastBlock()
if lastBlock.Height > 0 {
s.para.DelBlock(lastBlock, 1)
}
}
func (s *suiteParaCommitMsg) testRunRmvBlock() {
lastBlock, err := s.para.RequestLastBlock()
s.Nil(err)
plog.Info("para test testRunRmvBlock------------pre", "last height", lastBlock.Height)
s.True(lastBlock.Height > 1)
s.para.removeBlocks(1)
lastBlock, err = s.para.RequestLastBlock()
s.Nil(err)
plog.Info("para test testRunRmvBlock----------after", "last height", lastBlock.Height)
s.Equal(int64(1), lastBlock.Height)
}
func testRunSuiteParaCommitMsg(t *testing.T) {
log := new(suiteParaCommitMsg)
suite.Run(t, log)
}
func (s *suiteParaCommitMsg) TearDownSuite() {
time.Sleep(time.Second * 2)
s.block.Close()
s.para.Close()
s.exec.Close()
s.store.Close()
s.mem.Close()
s.network.Close()
s.q.Close()
}
func getMainBlock(height int64, BlockTime int64) *types.BlockSeq {
return &types.BlockSeq{
Num: height,
Seq: &types.BlockSequence{Hash: []byte(string(height)), Type: addAct},
Detail: &types.BlockDetail{
Block: &types.Block{
ParentHash: []byte(string(height - 1)),
Height: height,
BlockTime: BlockTime,
},
},
}
}
...@@ -12,6 +12,7 @@ import ( ...@@ -12,6 +12,7 @@ import (
node "github.com/33cn/plugin/plugin/dapp/paracross/testnode" node "github.com/33cn/plugin/plugin/dapp/paracross/testnode"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
_ "github.com/33cn/plugin/plugin/dapp/init" //dapp init
) )
func TestParaNode(t *testing.T) { func TestParaNode(t *testing.T) {
......
...@@ -5,23 +5,14 @@ ...@@ -5,23 +5,14 @@
package para package para
import ( import (
"errors"
"testing" "testing"
"github.com/33cn/chain33/blockchain"
"github.com/33cn/chain33/common" "github.com/33cn/chain33/common"
"github.com/33cn/chain33/common/crypto" "github.com/33cn/chain33/common/crypto"
"github.com/33cn/chain33/common/log" "github.com/33cn/chain33/common/log"
"github.com/33cn/chain33/executor"
"github.com/33cn/chain33/mempool"
"github.com/33cn/chain33/p2p"
"github.com/stretchr/testify/mock" "github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"time" "time"
"github.com/33cn/chain33/queue"
"github.com/33cn/chain33/store"
_ "github.com/33cn/chain33/system" _ "github.com/33cn/chain33/system"
"github.com/33cn/chain33/types" "github.com/33cn/chain33/types"
typesmocks "github.com/33cn/chain33/types/mocks" typesmocks "github.com/33cn/chain33/types/mocks"
...@@ -34,177 +25,6 @@ func init() { ...@@ -34,177 +25,6 @@ func init() {
log.SetLogLevel("error") log.SetLogLevel("error")
} }
type suiteParaClient struct {
// Include our basic suite logic.
suite.Suite
para *client
grpcCli *typesmocks.Chain33Client
q queue.Queue
block *blockchain.BlockChain
exec *executor.Executor
store queue.Module
mem queue.Module
network *p2p.P2p
mainBlockList []*types.Block
}
func (s *suiteParaClient) initEnv(cfg *types.Config, sub *types.ConfigSubModule) {
s.createTempBlock()
q := queue.New("channel")
s.q = q
//api, _ = client.New(q.Client(), nil)
var subcfg subConfig
if sub != nil {
types.MustDecode(sub.Consensus["para"], &subcfg)
}
s.block = blockchain.New(cfg.BlockChain)
s.block.SetQueueClient(q.Client())
s.exec = executor.New(cfg.Exec, sub.Exec)
s.exec.SetQueueClient(q.Client())
s.store = store.New(cfg.Store, sub.Store)
s.store.SetQueueClient(q.Client())
//cfg.Consensus.StartHeight = 0
//add block by UT below
subcfg.EmptyBlockInterval = 100
s.para = New(cfg.Consensus, sub.Consensus["para"]).(*client)
s.grpcCli = &typesmocks.Chain33Client{}
s.createBlockMock()
reply := &types.Reply{IsOk: true}
s.grpcCli.On("IsSync", mock.Anything, mock.Anything).Return(reply, nil)
result := &pt.ParacrossStatus{Height: -1}
data := types.Encode(result)
ret := &types.Reply{IsOk: true, Msg: data}
s.grpcCli.On("QueryChain", mock.Anything, mock.Anything).Return(ret, nil).Maybe()
s.grpcCli.On("SendTransaction", mock.Anything, mock.Anything).Return(reply, nil).Maybe()
s.grpcCli.On("GetLastHeader", mock.Anything, mock.Anything).Return(&types.Header{Height: subcfg.StartHeight + minBlockNum}, nil).Maybe()
s.grpcCli.On("GetBlockHash", mock.Anything, mock.Anything).Return(&types.ReplyHash{Hash: []byte("1")}, nil).Maybe()
s.para.grpcClient = s.grpcCli
s.para.SetQueueClient(q.Client())
s.mem = mempool.New(cfg.Mempool, nil)
s.mem.SetQueueClient(q.Client())
s.mem.Wait()
s.network = p2p.New(cfg.P2P)
s.network.SetQueueClient(q.Client())
//create block self
s.createBlock()
}
func (s *suiteParaClient) createTempBlock() {
var parentHash []byte
for i := 0; i < 3; i++ {
block := &types.Block{
Height: int64(i),
ParentHash: parentHash,
}
hash := block.HashByForkHeight(1)
s.mainBlockList = append(s.mainBlockList, block)
parentHash = hash
}
}
func (s *suiteParaClient) createBlockMock() {
var i int64
for i = 0; i < 3; i++ {
blockSeq := &types.BlockSeq{
Seq: &types.BlockSequence{
Hash: s.mainBlockList[i].HashByForkHeight(1),
Type: 1,
},
Detail: &types.BlockDetail{Block: s.mainBlockList[i]},
}
s.grpcCli.On("GetBlockBySeq", mock.Anything, &types.Int64{Data: i}).Return(blockSeq, nil)
}
// set block 3's parentHasn not equal, enter switch
block3 := &types.Block{
Height: 3,
ParentHash: []byte(string(1)),
}
hash := block3.HashByForkHeight(1)
blockSeq3 := &types.BlockSeq{
Seq: &types.BlockSequence{
Hash: hash,
Type: 1,
},
Detail: &types.BlockDetail{Block: block3},
}
s.grpcCli.On("GetBlockBySeq", mock.Anything, &types.Int64{Data: 3}).Return(blockSeq3, nil)
// RequestTx GetLastSeqOnMainChain
seq := &types.Int64{Data: 1}
s.grpcCli.On("GetLastBlockSequence", mock.Anything, mock.Anything).Return(seq, nil).Once()
seq = &types.Int64{Data: 2}
s.grpcCli.On("GetLastBlockSequence", mock.Anything, mock.Anything).Return(seq, nil).Once()
seq = &types.Int64{Data: 3}
s.grpcCli.On("GetLastBlockSequence", mock.Anything, mock.Anything).Return(seq, nil)
// mock for switchHashMatchedBlock
s.grpcCli.On("GetSequenceByHash", mock.Anything, &types.ReqHash{Hash: []byte(string(3))}).Return(nil, errors.New("hash err")).Once()
s.grpcCli.On("GetSequenceByHash", mock.Anything, &types.ReqHash{Hash: []byte(string(2))}).Return(nil, errors.New("hash err")).Once()
// mock for removeBlocks
seq = &types.Int64{Data: 1}
s.grpcCli.On("GetSequenceByHash", mock.Anything, mock.Anything).Return(seq, nil)
}
func (s *suiteParaClient) createBlock() {
var i int64
for i = 0; i < 3; i++ {
lastBlock, err := s.para.RequestLastBlock()
if err != nil {
plog.Error("para test", "err", err.Error())
}
plog.Info("para test---------1", "last height", lastBlock.Height)
s.para.createBlock(lastBlock, nil, i, s.getParaMainBlock(i+1, lastBlock.BlockTime+1))
}
}
func (s *suiteParaClient) SetupSuite() {
s.initEnv(types.InitCfg("../../../plugin/dapp/paracross/cmd/build/chain33.para.test.toml"))
}
func testRunSuiteParaClient(t *testing.T) {
log := new(suiteParaClient)
suite.Run(t, log)
}
func (s *suiteParaClient) TearDownSuite() {
//time.Sleep(time.Second * 2)
s.block.Close()
s.para.Close()
s.network.Close()
s.exec.Close()
s.store.Close()
s.mem.Close()
s.q.Close()
}
func (s *suiteParaClient) getParaMainBlock(height int64, BlockTime int64) *types.BlockSeq {
return &types.BlockSeq{
Num: height,
Seq: &types.BlockSequence{Hash: s.mainBlockList[height-1].HashByForkHeight(1), Type: addAct},
Detail: &types.BlockDetail{
Block: &types.Block{
ParentHash: s.mainBlockList[height-1].ParentHash,
Height: height,
BlockTime: BlockTime,
},
},
}
}
func getPrivKey(t *testing.T) crypto.PrivKey { func getPrivKey(t *testing.T) crypto.PrivKey {
pk, err := common.FromHex("6da92a632ab7deb67d38c0f6560bcfed28167998f6496db64c258d5e8393a81b") pk, err := common.FromHex("6da92a632ab7deb67d38c0f6560bcfed28167998f6496db64c258d5e8393a81b")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment