Skip to content

Commit

Permalink
add tests for events from hooks.
Browse files Browse the repository at this point in the history
  • Loading branch information
puneet2019 committed Aug 26, 2022
1 parent 8174571 commit d49cb6a
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion x/epochs/types/hooks_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types_test

import (
"strconv"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -26,6 +27,22 @@ func (suite *KeeperTestSuite) SetupTest() {
suite.queryClient = types.NewQueryClient(suite.QueryHelper)
}

func dummyAfterEpochEndEvent(epochIdentifier string, epochNumber int64) sdk.Event {
return sdk.NewEvent(
"afterEpochEnd",
sdk.NewAttribute("epochIdentifier", epochIdentifier),
sdk.NewAttribute("epochNumber", strconv.FormatInt(epochNumber, 10)),
)
}

func dummyBeforeEpochStartEvent(epochIdentifier string, epochNumber int64) sdk.Event {
return sdk.NewEvent(
"beforeEpochStart",
sdk.NewAttribute("epochIdentifier", epochIdentifier),
sdk.NewAttribute("epochNumber", strconv.FormatInt(epochNumber, 10)),
)
}

// dummyEpochHook is a struct satisfying the epoch hook interface,
// that maintains a counter for how many times its been succesfully called,
// and a boolean for whether it should panic during its execution.
Expand All @@ -39,13 +56,16 @@ func (hook *dummyEpochHook) AfterEpochEnd(ctx sdk.Context, epochIdentifier strin
panic("dummyEpochHook is panicking")
}
hook.successCounter += 1
ctx.EventManager().EmitEvent(dummyAfterEpochEndEvent(epochIdentifier, epochNumber))
}

func (hook *dummyEpochHook) BeforeEpochStart(ctx sdk.Context, epochIdentifier string, epochNumber int64) {
if hook.shouldPanic {
panic("dummyEpochHook is panicking")
}
hook.successCounter += 1
ctx.EventManager().EmitEvent(dummyBeforeEpochStartEvent(epochIdentifier, epochNumber))

}

func (hook *dummyEpochHook) Clone() *dummyEpochHook {
Expand All @@ -55,7 +75,7 @@ func (hook *dummyEpochHook) Clone() *dummyEpochHook {

var _ types.EpochHooks = &dummyEpochHook{}

func (suite *KeeperTestSuite) TestHooksPanicRecovery() {
func (suite *KeeperTestSuite) TestHooksPanicRecoveryAndEvents() {
panicHook := dummyEpochHook{shouldPanic: true}
noPanicHook := dummyEpochHook{shouldPanic: false}
simpleHooks := []dummyEpochHook{panicHook, noPanicHook}
Expand All @@ -81,8 +101,13 @@ func (suite *KeeperTestSuite) TestHooksPanicRecovery() {
suite.NotPanics(func() {
if epochActionSelector == 0 {
hooks.BeforeEpochStart(suite.Ctx, "id", 0)
suite.Require().Equal(suite.Ctx.EventManager().Events(), sdk.Events{dummyBeforeEpochStartEvent("id", 0)},
"test case index %d, before epoch event check", tcIndex)
} else if epochActionSelector == 1 {
hooks.AfterEpochEnd(suite.Ctx, "id", 0)
suite.Require().Equal(suite.Ctx.EventManager().Events(), sdk.Events{dummyAfterEpochEndEvent("id", 0)},
"test case index %d, after epoch event check", tcIndex)

}
})

Expand Down

0 comments on commit d49cb6a

Please sign in to comment.