Skip to content

Commit

Permalink
(fixup) Add TestBroadcastTxCommitWithCancelContext
Browse files Browse the repository at this point in the history
  • Loading branch information
tnasu committed Jul 3, 2023
1 parent 27912b1 commit 69e97ac
Showing 1 changed file with 97 additions and 17 deletions.
114 changes: 97 additions & 17 deletions rpc/core/mempool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"time"
)

type ErrorAssertionFunc func(t assert.TestingT, theError error, contains string, msgAndArgs ...interface{}) bool

func TestBroadcastTxAsync(t *testing.T) {
type args struct {
ctx *rpctypes.Context
Expand Down Expand Up @@ -138,35 +140,34 @@ func TestBroadcastTxSync(t *testing.T) {
}
}

// TestBroadcastTxSyncWithContext test in isolation from TestBroadcastTxSync since avoiding coexistence
func TestBroadcastTxSyncWithContext(t *testing.T) {
// TestBroadcastTxSyncWithCancelContext test in isolation from TestBroadcastTxSync since avoiding coexistence
func TestBroadcastTxSyncWithCancelContext(t *testing.T) {
type args struct {
ctx *rpctypes.Context
tx types.Tx
}
type ErrorAssertionFunc func(t assert.TestingT, theError error, contains string, msgAndArgs ...interface{}) bool
errContext, cancel := context.WithCancel(context.Background())
defer cancel() // for safety to avoid memory leaks
req := &http.Request{}
req = req.WithContext(errContext)
errRpcContext := rpctypes.Context{HTTPReq: req}
tx := types.Tx{}
tests := []struct {
name string
args args
want *ctypes.ResultBroadcastTx
wantErr ErrorAssertionFunc
err error
cancelFunc context.CancelFunc
name string
args args
want *ctypes.ResultBroadcastTx
wantErr ErrorAssertionFunc
err error
}{
{
name: "failure: interrupted by context",
args: args{
ctx: &errRpcContext,
tx: types.Tx{1},
tx: tx,
},
want: nil,
wantErr: assert.ErrorContains,
err: fmt.Errorf("broadcast confirmation not received: context canceled"),
cancelFunc: cancel,
want: nil,
wantErr: assert.ErrorContains,
err: fmt.Errorf("broadcast confirmation not received: context canceled"),
},
}
env = &Environment{}
Expand All @@ -177,9 +178,8 @@ func TestBroadcastTxSyncWithContext(t *testing.T) {
env.Mempool = mempool.NewCListMempool(config.TestConfig().Mempool, mockAppConnMempool, 0)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.cancelFunc != nil {
tt.cancelFunc()
}
// cancel context
cancel()
mockAppConnMempool.On("Error").Return(nil) // Not to use tt.err
wg := &sync.WaitGroup{}
wg.Add(1)
Expand Down Expand Up @@ -270,3 +270,83 @@ func TestBroadcastTxCommit(t *testing.T) {
})
}
}

func TestBroadcastTxCommitWithCancelContext(t *testing.T) {
type args struct {
ctx *rpctypes.Context
tx types.Tx
}
errContext, cancel := context.WithCancel(context.TODO())
defer cancel() // for safety to avoid memory leaks
req := &http.Request{}
req = req.WithContext(errContext)
errRpcContext := rpctypes.Context{HTTPReq: req}
height := int64(1)
tx := types.Tx{}
resDeliverTx := abci.ResponseDeliverTx{
Code: abci.CodeTypeOK,
Data: tx,
}
tests := []struct {
name string
args args
want *ctypes.ResultBroadcastTxCommit
wantErr ErrorAssertionFunc
err error
}{
{
name: "failure: interrupted by context",
args: args{
ctx: &errRpcContext,
tx: tx,
},
want: nil,
wantErr: assert.ErrorContains,
err: fmt.Errorf("broadcast confirmation not received: context canceled"),
},
}
env = &Environment{}
env.Config = *config.TestConfig().RPC
env.EventBus = types.NewEventBus()
err := env.EventBus.OnStart()
defer env.EventBus.OnStop()
assert.NoError(t, err)
mockAppConnMempool := &mocks.AppConnMempool{}
mockAppConnMempool.On("SetGlobalCallback", mock.Anything)
mockAppConnMempool.On("Error").Return(nil)
mockAppConnMempool.On("CheckTxSync", mock.Anything).Return(&ocabci.ResponseCheckTx{}, nil).WaitUntil(
time.After(1 * time.Second)) // Wait calling the context cancel
env.Mempool = mempool.NewCListMempool(config.TestConfig().Mempool, mockAppConnMempool, 0)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
wg := &sync.WaitGroup{}
wg.Add(2)
go func() {
// Wait the time for `env.EventBus.Subscribe` in BroadcastTxCommit
time.Sleep(10 * time.Millisecond)
err := env.EventBus.PublishEventTx(types.EventDataTx{
TxResult: abci.TxResult{
Height: height,
Index: 0,
Tx: tx,
Result: resDeliverTx,
},
})
assert.NoError(t, err)
// cancel context
cancel()
wg.Done()
}()
go func() {
got, err := BroadcastTxCommit(tt.args.ctx, tt.args.tx)
if !tt.wantErr(t, err, tt.err.Error(), fmt.Sprintf("BroadcastTxCommit(%v, %v)", tt.args.ctx, tt.args.tx)) {
wg.Done()
return
}
assert.Equalf(t, tt.want, got, "BroadcastTxCommit(%v, %v)", tt.args.ctx, tt.args.tx)
wg.Done()
}()
wg.Wait()
})
}
}

0 comments on commit 69e97ac

Please sign in to comment.