-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(coretesting): add test events service. #20579
Changes from 2 commits
3eb5c99
cc5a539
d0a0b7c
0db2bc7
363efd1
83b007d
4f1680d
118d718
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,22 +3,32 @@ package coretesting | |
import ( | ||
"context" | ||
|
||
"google.golang.org/protobuf/runtime/protoiface" | ||
|
||
"cosmossdk.io/core/event" | ||
"cosmossdk.io/core/store" | ||
) | ||
|
||
type dummyKey struct{} | ||
|
||
func Context() context.Context { | ||
dummy := &dummyCtx{ | ||
stores: map[string]store.KVStore{}, | ||
stores: map[string]store.KVStore{}, | ||
events: map[string][]event.Event{}, | ||
protoEvents: map[string][]protoiface.MessageV1{}, | ||
} | ||
|
||
ctx := context.WithValue(context.Background(), dummyKey{}, dummy) | ||
return ctx | ||
} | ||
|
||
type dummyCtx struct { | ||
// maps store by the actor. | ||
stores map[string]store.KVStore | ||
// maps event emitted by the actor. | ||
events map[string][]event.Event | ||
// maps proto events emitted by the actor. | ||
protoEvents map[string][]protoiface.MessageV1 | ||
} | ||
|
||
func unwrap(ctx context.Context) *dummyCtx { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove or refactor the unused function The function Toolsgolangci-lint
|
||
|
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 | ||
Comment on lines
+16
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Resolve the undefined function The function Also applies to: 26-26, 30-30, 34-34 Toolsgolangci-lint
|
||
return MemEventsService{moduleName: moduleName} | ||
testinginprod marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
type MemEventsService struct { | ||
moduleName string | ||
} | ||
|
||
func (e MemEventsService) EventManager(ctx context.Context) event.Manager { | ||
return eventManager{moduleName: e.moduleName, ctx: unwrap(ctx)} | ||
} | ||
testinginprod marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Define or import The type Toolsgolangci-lint
|
||
} | ||
|
||
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 | ||
} | ||
testinginprod marked this conversation as resolved.
Show resolved
Hide resolved
|
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure The functions Also applies to: 15-15, 35-35 Toolsgolangci-lint
|
||
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)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we retain ordering a bit better? i.e. rather than a map how about an array of events + the module they came from
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aaronc currently it is possible to only extract events from the module that emitted them. We could change that ofc if needed.