Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasmatt committed Jul 31, 2023
1 parent d8c8064 commit 53d5c59
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
33 changes: 33 additions & 0 deletions x/common/testutil/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"reflect"
"strings"
"testing"

"github.com/cosmos/gogoproto/proto"

Expand All @@ -12,6 +13,38 @@ import (
"github.com/stretchr/testify/require"
)

// FilterNewEvents returns only the new events from afterEvents that were not present in beforeEvents
func FilterNewEvents(beforeEvents, afterEvents sdk.Events) sdk.Events {
newEvents := make(sdk.Events, 0)

for _, afterEvent := range afterEvents {
found := false
for _, beforeEvent := range beforeEvents {
if reflect.DeepEqual(afterEvent, beforeEvent) {
found = true
break
}
}
if !found {
newEvents = append(newEvents, afterEvent)
}
}

return newEvents
}

// AssertEventsPresent fails the test if the given eventsType are not present in the events
func AssertEventsPresent(t *testing.T, events sdk.Events, eventsType []string) {
for _, eventType := range eventsType {
for _, event := range events {
if event.Type == eventType {
return
}
}
t.Errorf("event %s not found", eventType)
}
}

func RequireNotHasTypedEvent(t require.TestingT, ctx sdk.Context, event proto.Message) {
name := proto.MessageName(event)
for _, ev := range ctx.EventManager().Events() {
Expand Down
44 changes: 44 additions & 0 deletions x/perp/v2/module/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

testutilevents "github.com/NibiruChain/nibiru/x/common/testutil"

"github.com/NibiruChain/nibiru/x/common/asset"
"github.com/NibiruChain/nibiru/x/common/denoms"
"github.com/NibiruChain/nibiru/x/common/testutil/mock"
Expand Down Expand Up @@ -100,3 +102,45 @@ func TestSnapshotUpdates(t *testing.T) {
require.NoError(t, err)
assert.EqualValues(t, expectedSnapshot, snapshot)
}

func TestEndBlocker(t *testing.T) {
app, ctx := testapp.NewNibiruTestAppAndContext(true)

initialMarket := *mock.TestMarket()
initialAmm := *mock.TestAMMDefault()

runBlock := func(duration time.Duration) {
perp.EndBlocker(ctx, app.PerpKeeperV2)
ctx = ctx.
WithBlockHeight(ctx.BlockHeight() + 1).
WithBlockTime(ctx.BlockTime().Add(duration))
}

ctx = ctx.WithBlockTime(time.Date(2015, 10, 21, 0, 0, 0, 0, time.UTC)).WithBlockHeight(1)

runBlock(5 * time.Second)

require.NoError(t, app.PerpKeeperV2.Admin().CreateMarket(
/* ctx */ ctx, keeper.ArgsCreateMarket{
Pair: asset.Registry.Pair(denoms.BTC, denoms.NUSD),
PriceMultiplier: initialAmm.PriceMultiplier,
SqrtDepth: initialAmm.SqrtDepth,
Market: &initialMarket,
},
))

t.Log("run one block of 5 seconds")
app.OracleKeeper.SetPrice(ctx, asset.Registry.Pair(denoms.BTC, denoms.NUSD), sdk.NewDec(100e6))

beforeEvents := ctx.EventManager().Events()
runBlock(5 * time.Second)
afterEvents := ctx.EventManager().Events()

testutilevents.AssertEventsPresent(
t,
testutilevents.FilterNewEvents(beforeEvents, afterEvents),
[]string{"nibiru.perp.v2.AmmUpdatedEvent", "nibiru.perp.v2.MarketUpdatedEvent"},
)

// add index price
}

0 comments on commit 53d5c59

Please sign in to comment.