-
Notifications
You must be signed in to change notification settings - Fork 591
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add panic recovery to hook logic, improve error logging for panics Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com> Co-authored-by: Roman <34196718+p0mvn@users.noreply.github.com>
- Loading branch information
1 parent
ed503e4
commit 84f0194
Showing
5 changed files
with
150 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package types_test | ||
|
||
import ( | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/suite" | ||
|
||
"github.com/osmosis-labs/osmosis/v7/app/apptesting" | ||
"github.com/osmosis-labs/osmosis/v7/x/epochs/types" | ||
) | ||
|
||
type KeeperTestSuite struct { | ||
apptesting.KeeperTestHelper | ||
|
||
queryClient types.QueryClient | ||
} | ||
|
||
func TestKeeperTestSuite(t *testing.T) { | ||
suite.Run(t, new(KeeperTestSuite)) | ||
} | ||
|
||
func (suite *KeeperTestSuite) SetupTest() { | ||
suite.Setup() | ||
|
||
suite.queryClient = types.NewQueryClient(suite.QueryHelper) | ||
} | ||
|
||
// 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. | ||
type dummyEpochHook struct { | ||
successCounter int | ||
shouldPanic bool | ||
} | ||
|
||
func (hook *dummyEpochHook) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, epochNumber int64) { | ||
if hook.shouldPanic { | ||
panic("dummyEpochHook is panicking") | ||
} | ||
hook.successCounter += 1 | ||
} | ||
|
||
func (hook *dummyEpochHook) BeforeEpochStart(ctx sdk.Context, epochIdentifier string, epochNumber int64) { | ||
if hook.shouldPanic { | ||
panic("dummyEpochHook is panicking") | ||
} | ||
hook.successCounter += 1 | ||
} | ||
|
||
func (hook *dummyEpochHook) Clone() *dummyEpochHook { | ||
newHook := dummyEpochHook{shouldPanic: hook.shouldPanic, successCounter: hook.successCounter} | ||
return &newHook | ||
} | ||
|
||
var _ types.EpochHooks = &dummyEpochHook{} | ||
|
||
func (suite *KeeperTestSuite) TestHooksPanicRecovery() { | ||
panicHook := dummyEpochHook{shouldPanic: true} | ||
noPanicHook := dummyEpochHook{shouldPanic: false} | ||
simpleHooks := []dummyEpochHook{panicHook, noPanicHook} | ||
|
||
tests := []struct { | ||
hooks []dummyEpochHook | ||
expectedCounterValues []int | ||
}{ | ||
{[]dummyEpochHook{noPanicHook}, []int{1}}, | ||
{simpleHooks, []int{0, 1}}, | ||
} | ||
|
||
for tcIndex, tc := range tests { | ||
for epochActionSelector := 0; epochActionSelector < 2; epochActionSelector++ { | ||
suite.SetupTest() | ||
hookRefs := []types.EpochHooks{} | ||
|
||
for _, hook := range tc.hooks { | ||
hookRefs = append(hookRefs, hook.Clone()) | ||
} | ||
|
||
hooks := types.NewMultiEpochHooks(hookRefs...) | ||
suite.NotPanics(func() { | ||
if epochActionSelector == 0 { | ||
hooks.BeforeEpochStart(suite.Ctx, "id", 0) | ||
} else if epochActionSelector == 1 { | ||
hooks.AfterEpochEnd(suite.Ctx, "id", 0) | ||
} | ||
}) | ||
|
||
for i := 0; i < len(hooks); i++ { | ||
epochHook := hookRefs[i].(*dummyEpochHook) | ||
suite.Require().Equal(tc.expectedCounterValues[i], epochHook.successCounter, "test case index %d", tcIndex) | ||
} | ||
} | ||
|
||
} | ||
} |