-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(coretesting): add test events service. (#20579)
Co-authored-by: unknown unknown <unknown@unknown>
- Loading branch information
1 parent
467cc6d
commit b03a2c6
Showing
8 changed files
with
127 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package coretesting | ||
|
||
import ( | ||
"context" | ||
|
||
"google.golang.org/protobuf/runtime/protoiface" | ||
|
||
"cosmossdk.io/core/event" | ||
) | ||
|
||
var _ event.Service = (*MemEventsService)(nil) | ||
|
||
// EventsService attaches an event service to the context. | ||
// Adding an existing module will reset the events. | ||
func EventsService(ctx context.Context, moduleName string) MemEventsService { | ||
unwrap(ctx).events[moduleName] = nil | ||
unwrap(ctx).protoEvents[moduleName] = nil | ||
return MemEventsService{moduleName: moduleName} | ||
} | ||
|
||
type MemEventsService struct { | ||
moduleName string | ||
} | ||
|
||
func (e MemEventsService) EventManager(ctx context.Context) event.Manager { | ||
return eventManager{moduleName: e.moduleName, ctx: unwrap(ctx)} | ||
} | ||
|
||
func (e MemEventsService) GetEvents(ctx context.Context) []event.Event { | ||
return unwrap(ctx).events[e.moduleName] | ||
} | ||
|
||
func (e MemEventsService) GetProtoEvents(ctx context.Context) []protoiface.MessageV1 { | ||
return unwrap(ctx).protoEvents[e.moduleName] | ||
} | ||
|
||
type eventManager struct { | ||
moduleName string | ||
ctx *dummyCtx | ||
} | ||
|
||
func (e eventManager) Emit(event protoiface.MessageV1) error { | ||
e.ctx.protoEvents[e.moduleName] = append(e.ctx.protoEvents[e.moduleName], event) | ||
return nil | ||
} | ||
|
||
func (e eventManager) EmitKV(eventType string, attrs ...event.Attribute) error { | ||
e.ctx.events[e.moduleName] = append(e.ctx.events[e.moduleName], event.NewEvent(eventType, attrs...)) | ||
return nil | ||
} |
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,38 @@ | ||
package coretesting | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"google.golang.org/protobuf/runtime/protoiface" | ||
"google.golang.org/protobuf/types/known/wrapperspb" | ||
|
||
"cosmossdk.io/core/event" | ||
) | ||
|
||
func TestEventsService(t *testing.T) { | ||
ctx := Context() | ||
es := EventsService(ctx, "auth") | ||
|
||
wantProtoEvent := &wrapperspb.BoolValue{Value: true} | ||
err := es.EventManager(ctx).Emit(wantProtoEvent) | ||
require.NoError(t, err) | ||
|
||
wantEvent := event.NewEvent("new-account", event.Attribute{ | ||
Key: "number", | ||
Value: "1", | ||
}) | ||
err = es.EventManager(ctx).EmitKV(wantEvent.Type, wantEvent.Attributes...) | ||
require.NoError(t, err) | ||
|
||
gotProtoEvents := es.GetProtoEvents(ctx) | ||
require.Equal(t, []protoiface.MessageV1{wantProtoEvent}, gotProtoEvents) | ||
|
||
gotEvents := es.GetEvents(ctx) | ||
require.Equal(t, []event.Event{wantEvent}, gotEvents) | ||
|
||
// test reset | ||
es = EventsService(ctx, "auth") | ||
require.Nil(t, es.GetEvents(ctx)) | ||
require.Nil(t, es.GetProtoEvents(ctx)) | ||
} |
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