Commit 5eef0b20 authored by kingwang's avatar kingwang

add test

parent 5c62f0d9
package price package price
import ( import (
"log"
"testing" "testing"
"github.com/33cn/chain33/common" "github.com/33cn/chain33/common"
...@@ -9,8 +10,12 @@ import ( ...@@ -9,8 +10,12 @@ import (
cty "github.com/33cn/chain33/system/dapp/coins/types" cty "github.com/33cn/chain33/system/dapp/coins/types"
drivers "github.com/33cn/chain33/system/mempool" drivers "github.com/33cn/chain33/system/mempool"
"github.com/33cn/chain33/types" "github.com/33cn/chain33/types"
"github.com/33cn/chain33/util"
"github.com/33cn/chain33/util/testnode"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
_ "github.com/33cn/chain33/system"
) )
var ( var (
...@@ -162,3 +167,52 @@ func TestGetProperFee(t *testing.T) { ...@@ -162,3 +167,52 @@ func TestGetProperFee(t *testing.T) {
txSize2 := proto.Size(item4.Value) txSize2 := proto.Size(item4.Value)
assert.Equal(t, (item1.Value.Fee/int64(txSize1/1000+1)+item4.Value.Fee/int64(txSize2/1000+1))/2, cache.GetProperFee()) assert.Equal(t, (item1.Value.Fee/int64(txSize1/1000+1)+item4.Value.Fee/int64(txSize2/1000+1))/2, cache.GetProperFee())
} }
func TestRealNodeMempool(t *testing.T) {
mock33 := testnode.New("chain33.test.toml", nil)
defer mock33.Close()
mock33.Listen()
mock33.WaitHeight(0)
mock33.SendHot()
mock33.WaitHeight(1)
n := 300
done := make(chan struct{}, n)
keys := make([]crypto.PrivKey, n)
for i := 0; i < n; i++ {
addr, priv := util.Genaddress()
tx := util.CreateCoinsTx(mock33.GetHotKey(), addr, 10*types.Coin)
mock33.SendTx(tx)
keys[i] = priv
}
mock33.Wait()
for i := 0; i < n; i++ {
go func(priv crypto.PrivKey) {
for i := 0; i < 100; i++ {
tx := util.CreateCoinsTx(priv, mock33.GetGenesisAddress(), types.Coin/1000)
reply, err := mock33.GetAPI().SendTx(tx)
if err != nil {
log.Println(err)
continue
}
mock33.SetLastSend(reply.GetMsg())
}
done <- struct{}{}
}(keys[i])
}
for i := 0; i < n; i++ {
<-done
}
for {
txs, err := mock33.GetAPI().GetMempool()
assert.Nil(t, err)
if len(txs.GetTxs()) > 0 {
mock33.Wait()
continue
}
break
}
peer, err := mock33.GetAPI().PeerInfo()
assert.Nil(t, err)
assert.Equal(t, len(peer.Peers), 1)
assert.Equal(t, peer.Peers[0].MempoolSize, int32(0))
}
...@@ -12,6 +12,7 @@ import ( ...@@ -12,6 +12,7 @@ import (
"math/rand" "math/rand"
"os" "os"
"strings" "strings"
"sync"
"time" "time"
"github.com/33cn/chain33/account" "github.com/33cn/chain33/account"
...@@ -67,6 +68,7 @@ type Chain33Mock struct { ...@@ -67,6 +68,7 @@ type Chain33Mock struct {
sub *types.ConfigSubModule sub *types.ConfigSubModule
datadir string datadir string
lastsend []byte lastsend []byte
mu sync.Mutex
} }
//GetDefaultConfig : //GetDefaultConfig :
...@@ -369,10 +371,17 @@ func (mock *Chain33Mock) SendTx(tx *types.Transaction) []byte { ...@@ -369,10 +371,17 @@ func (mock *Chain33Mock) SendTx(tx *types.Transaction) []byte {
if err != nil { if err != nil {
panic(err) panic(err)
} }
mock.lastsend = reply.GetMsg() mock.SetLastSend(reply.GetMsg())
return reply.GetMsg() return reply.GetMsg()
} }
//SetLastSend :
func (mock *Chain33Mock) SetLastSend(hash []byte) {
mock.mu.Lock()
mock.lastsend = hash
mock.mu.Unlock()
}
//SendTxRPC : //SendTxRPC :
func (mock *Chain33Mock) SendTxRPC(tx *types.Transaction) []byte { func (mock *Chain33Mock) SendTxRPC(tx *types.Transaction) []byte {
var txhash string var txhash string
......
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