Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Commit

Permalink
add hook test
Browse files Browse the repository at this point in the history
  • Loading branch information
taryune committed Sep 20, 2023
1 parent 509fca7 commit 8e7c5d0
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 8 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ require (
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/viper v1.16.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
github.com/tendermint/go-amino v0.16.0 // indirect
Expand Down
96 changes: 96 additions & 0 deletions x/epochs/keeper/hooks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package keeper_test

import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/mycel-domain/mycel/x/epochs/types"
"strconv"
"time"
)

type MockHooks struct{}

const (
EpochIdentifier = types.DayEpochId
BeforeEpochStartEventType = "BeforeEpochStart"
AfterEpochEndEventType = "AfterEpochEnd"
)

func (h *MockHooks) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, epochNumber int64) {
if epochIdentifier == EpochIdentifier {
ctx.EventManager().EmitEvent(
sdk.NewEvent(
AfterEpochEndEventType,
sdk.NewAttribute("epochIdentifier", epochIdentifier),
sdk.NewAttribute("epochNumber", strconv.FormatInt(epochNumber, 10)),
),
)
}
}

func (h *MockHooks) BeforeEpochStart(ctx sdk.Context, epochIdentifier string, epochNumber int64) {
if epochIdentifier == EpochIdentifier {
ctx.EventManager().EmitEvent(
sdk.NewEvent(
BeforeEpochStartEventType,
sdk.NewAttribute("epochIdentifier", epochIdentifier),
sdk.NewAttribute("epochNumber", strconv.FormatInt(epochNumber, 10)),
),
)
}
}

func (suite *KeeperTestSuite) TestAfterEpochHooks() {
var (
now = time.Now()
// oneDayDuration = time.Hour * 24
)
testCases := []struct {
expEpochNumber string
expEventIndex int
expEventType string

fn func()
}{
{
expEpochNumber: "1",
expEventIndex: 1,
expEventType: BeforeEpochStartEventType,
fn: func() {
// Begin first block
suite.ctx = suite.ctx.WithBlockHeight(2).WithBlockTime(now.Add(time.Second))
suite.app.EpochsKeeper.BeginBlocker(suite.ctx)

// Check if curent epoch is expected
epochInfo, found := suite.app.EpochsKeeper.GetEpochInfo(suite.ctx, types.DayEpochId)
suite.Require().True(found)
suite.Require().Equal(int64(1), epochInfo.CurrentEpoch)
},
},
}

for i, tc := range testCases {
suite.Run(fmt.Sprintf("Case %d", i), func() {
suite.SetupTest()

// Remove hooks
suite.app.EpochsKeeper.RemoveHooks()

_ = tc

// Register hooks
hook := new(MockHooks)
suite.app.EpochsKeeper.SetHooks(hook)

// Run test Case
tc.fn()

// Check events
event := suite.ctx.EventManager().Events()[tc.expEventIndex]
suite.Require().Equal(tc.expEventType, event.Type)
suite.Require().Equal(EpochIdentifier, event.Attributes[0].Value)
suite.Require().Equal(tc.expEpochNumber, event.Attributes[1].Value)
})
}

}
6 changes: 6 additions & 0 deletions x/epochs/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ func (k *Keeper) SetHooks(eh types.EpochHooks) *Keeper {
return k
}

// Remove all hooks. Only used for testing.
func (k *Keeper) RemoveHooks() *Keeper {
k.hooks = nil
return k
}

func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
7 changes: 0 additions & 7 deletions x/epochs/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,3 @@ type BankKeeper interface {
SpendableCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins
// Methods imported from bank should be defined here
}

type EpochHooks interface {
// the first block whose timestamp is after the duration is counted as the end of the epoch
AfterEpochEnd(ctx sdk.Context, epochIdentifier string, epochNumber int64)
// new epoch is next block of epoch end block
BeforeEpochStart(ctx sdk.Context, epochIdentifier string, epochNumber int64)
}
9 changes: 8 additions & 1 deletion x/epochs/types/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

type EpochHooks interface {
// the first block whose timestamp is after the duration is counted as the end of the epoch
AfterEpochEnd(ctx sdk.Context, epochIdentifier string, epochNumber int64)
// new epoch is next block of epoch end block
BeforeEpochStart(ctx sdk.Context, epochIdentifier string, epochNumber int64)
}

var _ EpochHooks = MultiEpochHooks{}

// combine multiple epoch hooks, all hook functions are run in array sequence
// combine multiple gamm hooks, all hook functions are run in array sequence.
type MultiEpochHooks []EpochHooks

func NewMultiEpochHooks(hooks ...EpochHooks) MultiEpochHooks {
Expand All @@ -28,4 +34,5 @@ func (h MultiEpochHooks) BeforeEpochStart(ctx sdk.Context, epochIdentifier strin
for i := range h {
h[i].BeforeEpochStart(ctx, epochIdentifier, epochNumber)
}

}

0 comments on commit 8e7c5d0

Please sign in to comment.