Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for Commiter change #5

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions baseapp/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,27 @@ func TestGetBlockRentionHeight(t *testing.T) {
}
}

// Verifies that the Commiter is called with the checkState.
func TestCommiterCalledWithCheckState(t *testing.T) {
t.Parallel()

logger := defaultLogger()
db := dbm.NewMemDB()
name := t.Name()
app := NewBaseApp(name, logger, db, nil)

wasCommiterCalled := false
app.commiter = func(ctx sdk.Context) {
require.Equal(t, app.checkState.ctx, ctx)
wasCommiterCalled = true
}

app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: 1}})
Copy link

@jayy04 jayy04 Feb 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this needed to reset/initialize deliverState?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wondered the same thing, but the test fails with a nil pointer deference here when Commit tries to reference deliverState.ctx, which is nil (it's set by BeginBlock using the block header here).

app.Commit()

require.Equal(t, true, wasCommiterCalled)
}

// Test and ensure that invalid block heights always cause errors.
// See issues:
// - https://github.com/cosmos/cosmos-sdk/issues/11220
Expand Down
30 changes: 30 additions & 0 deletions baseapp/deliver_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,9 @@ func TestBaseAppOptionSeal(t *testing.T) {
require.Panics(t, func() {
app.SetEndBlocker(nil)
})
require.Panics(t, func() {
app.SetCommiter(nil)
})
require.Panics(t, func() {
app.SetAnteHandler(nil)
})
Expand Down Expand Up @@ -1002,6 +1005,33 @@ func TestBaseApp_EndBlock(t *testing.T) {
require.Equal(t, cp.Block.MaxGas, res.ConsensusParamUpdates.Block.MaxGas)
}

func TestBaseApp_Commit(t *testing.T) {
db := dbm.NewMemDB()
name := t.Name()
logger := defaultLogger()

cp := &tmproto.ConsensusParams{
Block: &tmproto.BlockParams{
MaxGas: 5000000,
},
}

app := baseapp.NewBaseApp(name, logger, db, nil)
app.SetParamStore(&paramStore{db: dbm.NewMemDB()})
app.InitChain(abci.RequestInitChain{
ConsensusParams: cp,
})

wasCommiterCalled := false
app.SetCommiter(func(ctx sdk.Context) {
wasCommiterCalled = true
})
app.Seal()

app.Commit()
require.Equal(t, true, wasCommiterCalled)
}

// Test that txs can be unmarshalled and read and that
// correct error codes are returned when not
func TestTxDecoder(t *testing.T) {
Expand Down
141 changes: 141 additions & 0 deletions testutil/mock/types_module_module.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions types/module/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ func TestManagerOrderSetters(t *testing.T) {
require.Equal(t, []string{"module1", "module2"}, mm.OrderEndBlockers)
mm.SetOrderEndBlockers("module2", "module1")
require.Equal(t, []string{"module2", "module1"}, mm.OrderEndBlockers)

require.Equal(t, []string{"module1", "module2"}, mm.OrderCommiters)
mm.SetOrderCommiters("module2", "module1")
require.Equal(t, []string{"module2", "module1"}, mm.OrderCommiters)
}

func TestManager_RegisterInvariants(t *testing.T) {
Expand Down Expand Up @@ -249,3 +253,20 @@ func TestManager_EndBlock(t *testing.T) {
mockAppModule2.EXPECT().EndBlock(gomock.Any(), gomock.Eq(req)).Times(1).Return([]abci.ValidatorUpdate{{}})
require.Panics(t, func() { mm.EndBlock(sdk.Context{}, req) })
}

func TestManager_Commit(t *testing.T) {
mockCtrl := gomock.NewController(t)
t.Cleanup(mockCtrl.Finish)

mockAppModule1 := mock.NewMockCommitAppModule(mockCtrl)
mockAppModule2 := mock.NewMockCommitAppModule(mockCtrl)
mockAppModule1.EXPECT().Name().Times(2).Return("module1")
mockAppModule2.EXPECT().Name().Times(2).Return("module2")
mm := module.NewManager(mockAppModule1, mockAppModule2)
require.NotNil(t, mm)
require.Equal(t, 2, len(mm.Modules))

mockAppModule1.EXPECT().Commit(gomock.Any()).Times(1)
mockAppModule2.EXPECT().Commit(gomock.Any()).Times(1)
mm.Commit(sdk.Context{})
}