Skip to content

Commit

Permalink
(fixup) coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
tnasu committed Jul 4, 2023
1 parent 69e97ac commit c7e68c2
Showing 1 changed file with 112 additions and 26 deletions.
138 changes: 112 additions & 26 deletions rpc/core/mempool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package core

import (
"context"
"errors"
"fmt"
ocabcicli "github.com/Finschia/ostracon/abci/client"
ocabci "github.com/Finschia/ostracon/abci/types"
"github.com/Finschia/ostracon/config"
"github.com/Finschia/ostracon/libs/log"
"github.com/Finschia/ostracon/mempool"
"github.com/Finschia/ostracon/proxy/mocks"
ctypes "github.com/Finschia/ostracon/rpc/core/types"
Expand Down Expand Up @@ -42,7 +44,7 @@ func TestBroadcastTxAsync(t *testing.T) {
tx: tx,
},
want: &ctypes.ResultBroadcastTx{
Code: 0x0,
Code: abci.CodeTypeOK,
Data: nil,
Log: "",
Codespace: "",
Expand Down Expand Up @@ -70,7 +72,7 @@ func TestBroadcastTxAsync(t *testing.T) {
env.Mempool = mempool.NewCListMempool(config.TestConfig().Mempool, mockAppConnMempool, 0)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockAppConnMempool.On("Error").Return(tt.err)
mockAppConnMempool.On("Error").Return(tt.err).Once()
got, err := BroadcastTxAsync(tt.args.ctx, tt.args.tx)
if !tt.wantErr(t, err, fmt.Sprintf("BroadcastTxAsync(%v, %v)", tt.args.ctx, tt.args.tx)) {
return
Expand Down Expand Up @@ -101,7 +103,7 @@ func TestBroadcastTxSync(t *testing.T) {
tx: tx,
},
want: &ctypes.ResultBroadcastTx{
Code: 0x0,
Code: ocabci.CodeTypeOK,
Data: nil,
Log: "",
Codespace: "",
Expand Down Expand Up @@ -129,7 +131,7 @@ func TestBroadcastTxSync(t *testing.T) {
env.Mempool = mempool.NewCListMempool(config.TestConfig().Mempool, mockAppConnMempool, 0)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockAppConnMempool.On("Error").Return(tt.err)
mockAppConnMempool.On("Error").Return(tt.err).Once()
got, err := BroadcastTxSync(tt.args.ctx, tt.args.tx)
if !tt.wantErr(t, err, fmt.Sprintf("BroadcastTxSync(%v, %v)", tt.args.ctx, tt.args.tx)) {
return
Expand All @@ -140,8 +142,8 @@ func TestBroadcastTxSync(t *testing.T) {
}
}

// TestBroadcastTxSyncWithCancelContext test in isolation from TestBroadcastTxSync since avoiding coexistence
func TestBroadcastTxSyncWithCancelContext(t *testing.T) {
// TestBroadcastTxSyncWithCancelContextForCheckTxSync test in isolation from TestBroadcastTxSync since avoiding coexistence
func TestBroadcastTxSyncWithCancelContextForCheckTxSync(t *testing.T) {
type args struct {
ctx *rpctypes.Context
tx types.Tx
Expand All @@ -160,7 +162,7 @@ func TestBroadcastTxSyncWithCancelContext(t *testing.T) {
err error
}{
{
name: "failure: interrupted by context",
name: "failure(non-deterministic test, retry please): interrupted by context",
args: args{
ctx: &errRpcContext,
tx: tx,
Expand All @@ -173,14 +175,15 @@ func TestBroadcastTxSyncWithCancelContext(t *testing.T) {
env = &Environment{}
mockAppConnMempool := &mocks.AppConnMempool{}
mockAppConnMempool.On("SetGlobalCallback", mock.Anything)
mockAppConnMempool.On("CheckTxSync", mock.Anything).Return(&ocabci.ResponseCheckTx{}, nil).WaitUntil(
time.After(1 * time.Second)) // Wait calling the context cancel
mockAppConnMempool.On("CheckTxSync", mock.Anything).Return(
&ocabci.ResponseCheckTx{Code: abci.CodeTypeOK}, nil).WaitUntil(
time.After(1000 * time.Millisecond)) // Wait calling the context cancel
mockAppConnMempool.On("Error").Return(nil) // Not to use tt.err
env.Mempool = mempool.NewCListMempool(config.TestConfig().Mempool, mockAppConnMempool, 0)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// cancel context
// cancel context for while doing `env.Mempool.CheckTxSync`
cancel()
mockAppConnMempool.On("Error").Return(nil) // Not to use tt.err
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
Expand All @@ -204,10 +207,7 @@ func TestBroadcastTxCommit(t *testing.T) {
}
height := int64(1)
tx := types.Tx{}
resDeliverTx := abci.ResponseDeliverTx{
Code: abci.CodeTypeOK,
Data: tx,
}
tx1 := types.Tx{1}
tests := []struct {
name string
args args
Expand All @@ -224,14 +224,34 @@ func TestBroadcastTxCommit(t *testing.T) {
CheckTx: ocabci.ResponseCheckTx{
Code: abci.CodeTypeOK,
},
DeliverTx: resDeliverTx,
Hash: tx.Hash(),
Height: height,
DeliverTx: abci.ResponseDeliverTx{
Code: abci.CodeTypeOK,
Data: tx,
},
Hash: tx.Hash(),
Height: height,
},
wantErr: assert.NoError,
},
{
name: "success but CheckTxResponse is not OK",
args: args{
ctx: &rpctypes.Context{},
tx: tx1,
},
want: &ctypes.ResultBroadcastTxCommit{
CheckTx: ocabci.ResponseCheckTx{
Code: abci.CodeTypeOK + 1, // Not OK
},
DeliverTx: abci.ResponseDeliverTx{}, // return empty response
Hash: tx1.Hash(),
Height: 0, // return empty height
},
wantErr: assert.NoError,
},
}
env = &Environment{}
env.Logger = log.TestingLogger()
env.Config = *config.TestConfig().RPC
env.EventBus = types.NewEventBus()
err := env.EventBus.OnStart()
Expand All @@ -240,10 +260,10 @@ func TestBroadcastTxCommit(t *testing.T) {
mockAppConnMempool := &mocks.AppConnMempool{}
mockAppConnMempool.On("SetGlobalCallback", mock.Anything)
mockAppConnMempool.On("Error").Return(nil)
mockAppConnMempool.On("CheckTxSync", mock.Anything).Return(&ocabci.ResponseCheckTx{}, nil)
env.Mempool = mempool.NewCListMempool(config.TestConfig().Mempool, mockAppConnMempool, 0)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockAppConnMempool.On("CheckTxSync", mock.Anything).Return(&tt.want.CheckTx, nil).Once()
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
Expand All @@ -261,8 +281,8 @@ func TestBroadcastTxCommit(t *testing.T) {
TxResult: abci.TxResult{
Height: height,
Index: 0,
Tx: tx,
Result: resDeliverTx,
Tx: tt.args.tx,
Result: tt.want.DeliverTx,
},
})
assert.NoError(t, err)
Expand All @@ -271,18 +291,21 @@ func TestBroadcastTxCommit(t *testing.T) {
}
}

func TestBroadcastTxCommitWithCancelContext(t *testing.T) {
func TestBroadcastTxCommitWithCancelContextForCheckTxSync(t *testing.T) {
type args struct {
ctx *rpctypes.Context
tx types.Tx
}
errContext, cancel := context.WithCancel(context.TODO())
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}
height := int64(1)
tx := types.Tx{}
resCheckTx := ocabci.ResponseCheckTx{
Code: abci.CodeTypeOK,
}
resDeliverTx := abci.ResponseDeliverTx{
Code: abci.CodeTypeOK,
Data: tx,
Expand All @@ -295,7 +318,7 @@ func TestBroadcastTxCommitWithCancelContext(t *testing.T) {
err error
}{
{
name: "failure: interrupted by context",
name: "failure(non-deterministic test, retry please): interrupted by context",
args: args{
ctx: &errRpcContext,
tx: tx,
Expand All @@ -306,6 +329,7 @@ func TestBroadcastTxCommitWithCancelContext(t *testing.T) {
},
}
env = &Environment{}
env.Logger = log.TestingLogger()
env.Config = *config.TestConfig().RPC
env.EventBus = types.NewEventBus()
err := env.EventBus.OnStart()
Expand All @@ -314,7 +338,7 @@ func TestBroadcastTxCommitWithCancelContext(t *testing.T) {
mockAppConnMempool := &mocks.AppConnMempool{}
mockAppConnMempool.On("SetGlobalCallback", mock.Anything)
mockAppConnMempool.On("Error").Return(nil)
mockAppConnMempool.On("CheckTxSync", mock.Anything).Return(&ocabci.ResponseCheckTx{}, nil).WaitUntil(
mockAppConnMempool.On("CheckTxSync", mock.Anything).Return(&resCheckTx, 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 {
Expand All @@ -333,7 +357,7 @@ func TestBroadcastTxCommitWithCancelContext(t *testing.T) {
},
})
assert.NoError(t, err)
// cancel context
// cancel context for while doing `env.Mempool.CheckTxSync`
cancel()
wg.Done()
}()
Expand All @@ -350,3 +374,65 @@ func TestBroadcastTxCommitWithCancelContext(t *testing.T) {
})
}
}

func TestBroadcastTxCommitTimeout(t *testing.T) {
type args struct {
ctx *rpctypes.Context
tx types.Tx
}
tx := types.Tx{}
tests := []struct {
name string
args args
want *ctypes.ResultBroadcastTxCommit
wantErr ErrorAssertionFunc
err error
}{
{
name: "failure: timeout",
args: args{
ctx: &rpctypes.Context{},
tx: tx,
},
want: &ctypes.ResultBroadcastTxCommit{
CheckTx: ocabci.ResponseCheckTx{
Code: abci.CodeTypeOK,
},
DeliverTx: abci.ResponseDeliverTx{}, // return empty response
Hash: tx.Hash(),
Height: 0, // return empty height
},
wantErr: assert.ErrorContains,
err: errors.New("timed out waiting for tx to be included in a block"),
},
}
env = &Environment{}
env.Logger = log.TestingLogger()
env.Config = *config.TestConfig().RPC
env.Config.TimeoutBroadcastTxCommit = 1 // For test
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)
env.Mempool = mempool.NewCListMempool(config.TestConfig().Mempool, mockAppConnMempool, 0)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockAppConnMempool.On("CheckTxSync", mock.Anything).Return(&tt.want.CheckTx, nil).Once()
wg := &sync.WaitGroup{}
wg.Add(1)
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 c7e68c2

Please sign in to comment.