Browse Source

rpc/test: wait for mempool CheckTx callback (#4908)

Fixes race conditions causing the following test failures:

```
=== RUN   TestUnconfirmedTxs
    TestUnconfirmedTxs: rpc_test.go:342:
                Error Trace:    rpc_test.go:342
                Error:          Not equal:
                                expected: 1
                                actual  : 0
                Test:           TestUnconfirmedTxs
    TestUnconfirmedTxs: rpc_test.go:343:
                Error Trace:    rpc_test.go:343
                Error:          Not equal:
                                expected: 1
                                actual  : 0
                Test:           TestUnconfirmedTxs
    TestUnconfirmedTxs: rpc_test.go:345:
                Error Trace:    rpc_test.go:345
                Error:          Not equal:
                                expected: types.Txs{types.Tx{0x39, 0x44, 0x4d, 0x6c, 0x4b, 0x66, 0x46, 0x78, 0x3d, 0x45, 0x33, 0x33, 0x68, 0x47, 0x6e, 0x79, 0x58}}
                                actual  : types.Txs(nil)

                                Diff:
                                --- Expected
                                +++ Actual
                                @@ -1,4 +1,2 @@
                                -(types.Txs) (len=1) {
                                - (types.Tx) (len=17) Tx{39444D6C4B6646783D45333368476E7958}
                                -}
                                +(types.Txs) <nil>

                Test:           TestUnconfirmedTxs
    TestUnconfirmedTxs: rpc_test.go:342:
                Error Trace:    rpc_test.go:342
                Error:          Not equal:
                                expected: 1
                                actual  : 0
                Test:           TestUnconfirmedTxs
    TestUnconfirmedTxs: rpc_test.go:343:
                Error Trace:    rpc_test.go:343
                Error:          Not equal:
                                expected: 1
                                actual  : 0
                Test:           TestUnconfirmedTxs
    TestUnconfirmedTxs: rpc_test.go:345:
                Error Trace:    rpc_test.go:345
                Error:          Not equal:
                                expected: types.Txs{types.Tx{0x39, 0x44, 0x4d, 0x6c, 0x4b, 0x66, 0x46, 0x78, 0x3d, 0x45, 0x33, 0x33, 0x68, 0x47, 0x6e, 0x79, 0x58}}
                                actual  : types.Txs{}

                                Diff:
                                --- Expected
                                +++ Actual
                                @@ -1,3 +1,2 @@
                                -(types.Txs) (len=1) {
                                - (types.Tx) (len=17) Tx{39444D6C4B6646783D45333368476E7958}
                                +(types.Txs) {
                                 }
                Test:           TestUnconfirmedTxs
--- FAIL: TestUnconfirmedTxs (0.20s)
=== RUN   TestNumUnconfirmedTxs
    TestNumUnconfirmedTxs: rpc_test.go:364:
                Error Trace:    rpc_test.go:364
                Error:          Not equal:
                                expected: 1
                                actual  : 0
                Test:           TestNumUnconfirmedTxs
    TestNumUnconfirmedTxs: rpc_test.go:365:
                Error Trace:    rpc_test.go:365
                Error:          Not equal:
                                expected: 1
                                actual  : 0
                Test:           TestNumUnconfirmedTxs
    TestNumUnconfirmedTxs: rpc_test.go:364:
                Error Trace:    rpc_test.go:364
                Error:          Not equal:
                                expected: 1
                                actual  : 0
                Test:           TestNumUnconfirmedTxs
    TestNumUnconfirmedTxs: rpc_test.go:365:
                Error Trace:    rpc_test.go:365
                Error:          Not equal:
                                expected: 1
                                actual  : 0
                Test:           TestNumUnconfirmedTxs
--- FAIL: TestNumUnconfirmedTxs (0.09s)
```
pull/4911/head
Erik Grinaker 5 years ago
committed by GitHub
parent
commit
804e2d1f97
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 7 deletions
  1. +25
    -7
      rpc/client/rpc_test.go

+ 25
- 7
rpc/client/rpc_test.go View File

@ -7,6 +7,7 @@ import (
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@ -331,14 +332,22 @@ func TestBroadcastTxCommit(t *testing.T) {
func TestUnconfirmedTxs(t *testing.T) {
_, _, tx := MakeTxKV()
ch := make(chan *abci.Response, 1)
mempool := node.Mempool()
_ = mempool.CheckTx(tx, nil, mempl.TxInfo{})
err := mempool.CheckTx(tx, func(resp *abci.Response) { ch <- resp }, mempl.TxInfo{})
require.NoError(t, err)
for i, c := range GetClients() {
mc, ok := c.(client.MempoolClient)
require.True(t, ok, "%d", i)
// wait for tx to arrive in mempoool.
select {
case <-ch:
case <-time.After(5 * time.Second):
t.Error("Timed out waiting for CheckTx callback")
}
for _, c := range GetClients() {
mc := c.(client.MempoolClient)
res, err := mc.UnconfirmedTxs(1)
require.Nil(t, err, "%d: %+v", i, err)
require.NoError(t, err)
assert.Equal(t, 1, res.Count)
assert.Equal(t, 1, res.Total)
@ -352,10 +361,19 @@ func TestUnconfirmedTxs(t *testing.T) {
func TestNumUnconfirmedTxs(t *testing.T) {
_, _, tx := MakeTxKV()
ch := make(chan *abci.Response, 1)
mempool := node.Mempool()
_ = mempool.CheckTx(tx, nil, mempl.TxInfo{})
mempoolSize := mempool.Size()
err := mempool.CheckTx(tx, func(resp *abci.Response) { ch <- resp }, mempl.TxInfo{})
require.NoError(t, err)
// wait for tx to arrive in mempoool.
select {
case <-ch:
case <-time.After(5 * time.Second):
t.Error("Timed out waiting for CheckTx callback")
}
mempoolSize := mempool.Size()
for i, c := range GetClients() {
mc, ok := c.(client.MempoolClient)
require.True(t, ok, "%d", i)


Loading…
Cancel
Save