Skip to content

Commit

Permalink
Add UnexpectedMessage.Implementation field.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalloc committed Nov 22, 2020
1 parent 1f58153 commit a20640f
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 18 deletions.
3 changes: 3 additions & 0 deletions engine/controller/aggregate/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func (c *Controller) Handle(
c.Config,
"AggregateMessageHandler",
"RouteCommandToInstance",
c.Config.Handler(),
env.Message,
func() {
id = c.Config.Handler().RouteCommandToInstance(env.Message)
Expand Down Expand Up @@ -79,6 +80,7 @@ func (c *Controller) Handle(
c.Config,
"AggregateRoot",
"ApplyEvent",
r,
env.Message,
func() {
r.ApplyEvent(env.Message)
Expand Down Expand Up @@ -115,6 +117,7 @@ func (c *Controller) Handle(
c.Config,
"AggregateMessageHandler",
"HandleCommand",
c.Config.Handler(),
env.Message,
func() {
c.Config.Handler().HandleCommand(r, s, env.Message)
Expand Down
1 change: 1 addition & 0 deletions engine/controller/aggregate/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func (s *scope) RecordEvent(m dogma.Message) {
s.config,
"AggregateRoot",
"ApplyEvent",
s.root,
m,
func() {
s.root.ApplyEvent(m)
Expand Down
2 changes: 2 additions & 0 deletions engine/controller/integration/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (c *Controller) Handle(
c.Config,
"IntegrationMessageHandler",
"TimeoutHint",
c.Config.Handler(),
env.Message,
func() {
t = c.Config.Handler().TimeoutHint(env.Message)
Expand All @@ -72,6 +73,7 @@ func (c *Controller) Handle(
c.Config,
"IntegrationMessageHandler",
"HandleCommand",
c.Config.Handler(),
env.Message,
func() {
err = c.Config.Handler().HandleCommand(ctx, s, env.Message)
Expand Down
3 changes: 3 additions & 0 deletions engine/controller/process/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func (c *Controller) Handle(
c.Config,
"ProcessMessageHandler",
"TimeoutHint",
c.Config.Handler(),
env.Message,
func() {
t = c.Config.Handler().TimeoutHint(env.Message)
Expand Down Expand Up @@ -173,6 +174,7 @@ func (c *Controller) routeEvent(
c.Config,
"ProcessMessageHandler",
"RouteEventToInstance",
handler,
env.Message,
func() {
id, ok, err = handler.RouteEventToInstance(ctx, env.Message)
Expand Down Expand Up @@ -234,6 +236,7 @@ func (c *Controller) handle(ctx context.Context, s *scope) error {
c.Config,
"ProcessMessageHandler",
method,
c.Config.Handler(),
s.env.Message,
func() {
if s.env.Role == message.EventRole {
Expand Down
2 changes: 2 additions & 0 deletions engine/controller/projection/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (c *Controller) Handle(
c.Config,
"ProjectionMessageHandler",
"TimeoutHint",
handler,
env.Message,
func() {
t = handler.TimeoutHint(env.Message)
Expand Down Expand Up @@ -149,6 +150,7 @@ func (c *Controller) Handle(
c.Config,
"ProjectionMessageHandler",
"HandleEvent",
handler,
env.Message,
func() {
ok, err = handler.HandleEvent(
Expand Down
20 changes: 13 additions & 7 deletions engine/panicx/unexpectedmessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ type UnexpectedMessage struct {
// controller called resulting in the panic.
Interface string

// Implementation is the value that implements the nominated interface.
Implementation interface{}

// Method is the name of the method that the controller called resulting in
// the panic.
Method string
Expand All @@ -30,9 +33,10 @@ type UnexpectedMessage struct {

func (x UnexpectedMessage) String() string {
return fmt.Sprintf(
"the '%s' %s message handler did not expect %s() to be called with a message of type %T",
"the '%s' %s message handler did not expect %T.%s() to be called with a message of type %T",
x.Handler.Identity().Name,
x.Handler.HandlerType(),
x.Implementation,
x.Method,
x.Message,
)
Expand All @@ -43,7 +47,8 @@ func (x UnexpectedMessage) String() string {
// the failure.
func EnrichUnexpectedMessage(
h configkit.RichHandler,
iface, method string,
iface string, method string,
impl interface{},
m dogma.Message,
fn func(),
) {
Expand All @@ -56,11 +61,12 @@ func EnrichUnexpectedMessage(

if v == dogma.UnexpectedMessage {
v = UnexpectedMessage{
Handler: h,
Interface: iface,
Method: method,
Message: m,
PanicLocation: LocationOfPanic(),
Handler: h,
Interface: iface,
Method: method,
Implementation: impl,
Message: m,
PanicLocation: LocationOfPanic(),
}
}

Expand Down
29 changes: 18 additions & 11 deletions engine/panicx/unexpectedmessage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ var _ = Describe("type UnexpectedMessage", func() {

x := r.(UnexpectedMessage)
Expect(x.String()).To(Equal(
"the '<name>' projection message handler did not expect <method>() to be called with a message of type fixtures.MessageA",
"the '<name>' projection message handler did not expect *fixtures.ProjectionMessageHandler.<method>() to be called with a message of type fixtures.MessageA",
))
}()

EnrichUnexpectedMessage(
config,
"<interface>",
"<method>",
config.Handler(),
MessageA1,
func() {
panic(dogma.UnexpectedMessage)
Expand All @@ -62,6 +63,7 @@ var _ = Describe("func EnrichUnexpectedMessage()", func() {
config,
"<interface>",
"<method>",
config.Handler(),
MessageA1,
func() {
called = true
Expand All @@ -77,6 +79,7 @@ var _ = Describe("func EnrichUnexpectedMessage()", func() {
config,
"<interface>",
"<method>",
config.Handler(),
MessageA1,
func() {
panic("<panic>")
Expand All @@ -91,27 +94,31 @@ var _ = Describe("func EnrichUnexpectedMessage()", func() {
config,
"<interface>",
"<method>",
config.Handler(),
MessageA1,
func() {
panic(dogma.UnexpectedMessage)
},
panicWithUnexpectedMessage,
)
}).To(PanicWith(
MatchAllFields(
Fields{
"Handler": Equal(config),
"Interface": Equal("<interface>"),
"Method": Equal("<method>"),
"Message": Equal(MessageA1),
"Handler": Equal(config),
"Interface": Equal("<interface>"),
"Method": Equal("<method>"),
"Implementation": Equal(config.Handler()),
"Message": Equal(MessageA1),
"PanicLocation": MatchAllFields(
Fields{
"Func": Not(BeEmpty()),
"File": HaveSuffix("/unexpectedmessage_test.go"),
"Line": Equal(96),
"Func": Equal("github.com/dogmatiq/testkit/engine/panicx_test.panicWithUnexpectedMessage"),
"File": HaveSuffix("/engine/panicx/unexpectedmessage_test.go"),
"Line": Equal(123),
},
),
},
),
))
})
})

func panicWithUnexpectedMessage() {
panic(dogma.UnexpectedMessage)
}

0 comments on commit a20640f

Please sign in to comment.