Skip to content
This repository has been archived by the owner on Aug 27, 2022. It is now read-only.

fix testutils test #459

Merged
merged 1 commit into from Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 33 additions & 2 deletions testutils/utils.go
Expand Up @@ -8,7 +8,9 @@ import (
"github.com/Qitmeer/qitmeer/common/hash"
"github.com/Qitmeer/qitmeer/core/types"
"github.com/Qitmeer/qitmeer/engine/txscript"
"sync"
"testing"
"time"
)

// GenerateBlock will generate a number of blocks by the input number for
Expand Down Expand Up @@ -126,15 +128,21 @@ func AssertScan(t *testing.T, h *Harness, maxOrder, scanCount uint64) {
}
}

func AssertMempoolTxNotify(t *testing.T, h *Harness, txid, addr string) {
func AssertMempoolTxNotify(t *testing.T, h *Harness, txid, addr string, timeout int) {
TimeoutFunc(t, func() bool {
if h.Wallet.mempoolTx != nil {
return true
}
return false
}, timeout)
if h.Wallet.mempoolTx == nil {
t.Fatalf("not match mempool tx")
}
if _, ok := h.Wallet.mempoolTx[txid]; !ok {
t.Fatalf("not has mempool tx %s", txid)
}
if h.Wallet.mempoolTx[txid] != addr {
t.Fatalf("mempool tx vout address %s not match%s", h.Wallet.mempoolTx[txid], addr)
t.Fatalf("mempool tx vout address %s not match %s", h.Wallet.mempoolTx[txid], addr)
}
}

Expand All @@ -149,3 +157,26 @@ func AssertTxConfirm(t *testing.T, h *Harness, txid string, confirms uint64) {
t.Fatalf("tx %s confirms %d not right,should more than %d", txid, h.Wallet.confirmTxs[txid], confirms)
}
}

func TimeoutFunc(t *testing.T, f func() bool, timeout int) {
wg := sync.WaitGroup{}
wg.Add(1)
start := time.Now().UnixNano()
go func() {
defer wg.Done()
t1 := time.NewTicker(time.Duration(timeout) * time.Second)
defer t1.Stop()
for {
select {
case <-t1.C:
return
default:
if f() {
return
}
}
}
}()
wg.Wait()
t.Logf("time use:%.4fs", float64(time.Now().UnixNano())/float64(start))
}
4 changes: 2 additions & 2 deletions testutils/ws_test.go
Expand Up @@ -46,8 +46,8 @@ func TestWsNotify(t *testing.T) {
spendAmt := types.Amount{Value: 50 * types.AtomsPerCoin, Id: types.MEERID}
txid := Spend(t, h, spendAmt)
t.Logf("[%v]: tx %v which spend %v has been sent", h.Node.Id(), txid, spendAmt.String())
time.Sleep(1 * time.Second)
AssertMempoolTxNotify(t, h, txid.String(), h.Wallet.Addresses()[1])
t.Log(h.Wallet.Addresses())
AssertMempoolTxNotify(t, h, txid.String(), h.Wallet.Addresses()[1], 5)
blocks := GenerateBlock(t, h, 4)
AssertTxMinedUseNotifierAPI(t, h, txid, blocks[0])
AssertBlockOrderAndHeight(t, h, 6, 6, 5)
Expand Down