Skip to content

Commit

Permalink
Refactor to work with Dogma v0.13.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalloc committed Mar 26, 2024
1 parent 4c269d0 commit 6e48260
Show file tree
Hide file tree
Showing 54 changed files with 326 additions and 898 deletions.
17 changes: 16 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ The format is based on [Keep a Changelog], and this project adheres to
[keep a changelog]: https://keepachangelog.com/en/1.0.0/
[semantic versioning]: https://semver.org/spec/v2.0.0.html

## [0.14.0] - 2024-03-26

This release updates the `testkit` implementation to adhere to Dogma v0.13.0
interfaces.

### Removed

- **[BC]** Removed `Test.EventRecorder()`.
- **[BC]** Removed `EventRecorder`.
- **[BC]** Removed `EventRecorderInterceptor` and `InterceptEventRecorder()`.
- **[BC]** Removed `engine.EventRecorder`.
- **[BC]** Removed `ActionScope.EventRecorder`.

## [0.13.12] - 2024-03-05

### Changed
Expand All @@ -30,7 +43,8 @@ interfaces.

### Added

- Added `MessageDescription` method to internal test messages in preparation for it becoming mandatory
- Added `MessageDescription` method to internal test messages in preparation for
it becoming mandatory.

## [0.13.9] - 2023-01-06

Expand Down Expand Up @@ -345,6 +359,7 @@ guide][0.11.0 migration guide] for detailed instructions.
[0.13.10]: https://github.com/dogmatiq/testkit/releases/tag/v0.13.10
[0.13.11]: https://github.com/dogmatiq/testkit/releases/tag/v0.13.11
[0.13.12]: https://github.com/dogmatiq/testkit/releases/tag/v0.13.12
[0.14.12]: https://github.com/dogmatiq/testkit/releases/tag/v0.14.0

<!-- version template
## [0.0.1] - YYYY-MM-DD
Expand Down
2 changes: 1 addition & 1 deletion action.advancetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (a advanceTimeAction) Location() location.Location {
return a.loc
}

func (a advanceTimeAction) ConfigurePredicate(o *PredicateOptions) {
func (a advanceTimeAction) ConfigurePredicate(*PredicateOptions) {
}

func (a advanceTimeAction) Do(ctx context.Context, s ActionScope) error {
Expand Down
12 changes: 1 addition & 11 deletions action.call.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ type callAction struct {
fn func()
loc location.Location
onExecute CommandExecutorInterceptor
onRecord EventRecorderInterceptor
}

func (a callAction) Caption() string {
Expand All @@ -67,7 +66,7 @@ func (a callAction) ConfigurePredicate(o *PredicateOptions) {
o.MatchDispatchCycleStartedFacts = true
}

func (a callAction) Do(ctx context.Context, s ActionScope) error {
func (a callAction) Do(_ context.Context, s ActionScope) error {
// Setup the command executor for use during this action.
s.Executor.Bind(s.Engine, s.OperationOptions)
defer s.Executor.Unbind()
Expand All @@ -77,15 +76,6 @@ func (a callAction) Do(ctx context.Context, s ActionScope) error {
defer s.Executor.Intercept(prev)
}

// Setup the event recorder for use during this action.
s.Recorder.Bind(s.Engine, s.OperationOptions)
defer s.Recorder.Unbind()

if a.onRecord != nil {
prev := s.Recorder.Intercept(a.onRecord)
defer s.Recorder.Intercept(prev)
}

// Execute the user-supplied function.
a.fn()

Expand Down
61 changes: 8 additions & 53 deletions action.call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ var _ = g.Describe("func Call()", func() {
c.RegisterAggregate(&AggregateMessageHandler{
ConfigureFunc: func(c dogma.AggregateConfigurer) {
c.Identity("<aggregate>", "832d78d7-a006-414f-b6d7-3153aa7c9ab8")
c.ConsumesCommandType(MessageC{})
c.ProducesEventType(MessageE{})
c.Routes(
dogma.HandlesCommand[MessageC](),
dogma.RecordsEvent[MessageE](),
)
},
RouteCommandToInstanceFunc: func(
dogma.Message,
Expand All @@ -47,8 +49,10 @@ var _ = g.Describe("func Call()", func() {
c.RegisterProcess(&ProcessMessageHandler{
ConfigureFunc: func(c dogma.ProcessConfigurer) {
c.Identity("<process>", "b64cdd19-782e-4e4e-9e5f-a95a943d6340")
c.ConsumesEventType(MessageE{})
c.ProducesCommandType(MessageC{})
c.Routes(
dogma.HandlesEvent[MessageE](),
dogma.ExecutesCommand[MessageC](),
)
},
RouteEventToInstanceFunc: func(
context.Context,
Expand Down Expand Up @@ -109,41 +113,6 @@ var _ = g.Describe("func Call()", func() {
))
})

g.It("allows use of the test's recorder", func() {
r := test.EventRecorder()

test.Prepare(
Call(func() {
r.RecordEvent(
context.Background(),
MessageE1,
)
}),
)

Expect(buf.Facts()).To(ContainElement(
fact.DispatchCycleBegun{
Envelope: &envelope.Envelope{
MessageID: "1",
CausationID: "1",
CorrelationID: "1",
Message: MessageE1,
Type: MessageEType,
Role: message.EventRole,
CreatedAt: startTime,
},
EngineTime: startTime,
EnabledHandlerTypes: map[configkit.HandlerType]bool{
configkit.AggregateHandlerType: true,
configkit.IntegrationHandlerType: false,
configkit.ProcessHandlerType: true,
configkit.ProjectionHandlerType: false,
},
EnabledHandlers: map[string]bool{},
},
))
})

g.It("allows expectations to match commands executed via the test's executor", func() {
e := test.CommandExecutor()

Expand All @@ -158,20 +127,6 @@ var _ = g.Describe("func Call()", func() {
)
})

g.It("allows expectations to match events recorded via the test's recorder", func() {
r := test.EventRecorder()

test.Expect(
Call(func() {
r.RecordEvent(
context.Background(),
MessageE1,
)
}),
ToRecordEvent(MessageE1),
)
})

g.It("produces the expected caption", func() {
test.Prepare(
Call(func() {}),
Expand Down
6 changes: 4 additions & 2 deletions action.dispatch.command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ var _ = g.Describe("func ExecuteCommand()", func() {
c.RegisterAggregate(&AggregateMessageHandler{
ConfigureFunc: func(c dogma.AggregateConfigurer) {
c.Identity("<aggregate>", "d1cf3af1-6c20-4125-8e68-192a6075d0b4")
c.ConsumesCommandType(MessageC{})
c.ProducesEventType(MessageE{})
c.Routes(
dogma.HandlesCommand[MessageC](),
dogma.RecordsEvent[MessageE](),
)
},
RouteCommandToInstanceFunc: func(
dogma.Message,
Expand Down
6 changes: 4 additions & 2 deletions action.dispatch.event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ var _ = g.Describe("func RecordEvent()", func() {
c.RegisterProcess(&ProcessMessageHandler{
ConfigureFunc: func(c dogma.ProcessConfigurer) {
c.Identity("<process>", "1c0dd111-fe12-4dee-a8bc-64abea1dce8f")
c.ConsumesEventType(MessageE{})
c.ProducesCommandType(MessageC{})
c.Routes(
dogma.HandlesEvent[MessageE](),
dogma.ExecutesCommand[MessageC](),
)
},
RouteEventToInstanceFunc: func(
context.Context,
Expand Down
6 changes: 3 additions & 3 deletions action.dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

// ExecuteCommand returns an Action that executes a command message.
func ExecuteCommand(m dogma.Message) Action {
if err := dogma.ValidateMessage(m); err != nil {
if err := validateMessage(m); err != nil {
panic(fmt.Sprintf("ExecuteCommand(%T): %s", m, err))
}

Expand All @@ -25,7 +25,7 @@ func ExecuteCommand(m dogma.Message) Action {

// RecordEvent returns an Action that records an event message.
func RecordEvent(m dogma.Message) Action {
if err := dogma.ValidateMessage(m); err != nil {
if err := validateMessage(m); err != nil {
panic(fmt.Sprintf("RecordEvent(%T): %s", m, err))
}

Expand Down Expand Up @@ -56,7 +56,7 @@ func (a dispatchAction) Location() location.Location {
return a.loc
}

func (a dispatchAction) ConfigurePredicate(o *PredicateOptions) {
func (a dispatchAction) ConfigurePredicate(*PredicateOptions) {
}

func (a dispatchAction) Do(ctx context.Context, s ActionScope) error {
Expand Down
4 changes: 0 additions & 4 deletions action.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ type ActionScope struct {
// method.
Executor *CommandExecutor

// Recorder is the event recorder returned by the Test's EventRecorder()
// method.
Recorder *EventRecorder

// OperationOptions is the set of options that should be used with calling
// Engine.Dispatch() or Engine.Tick().
OperationOptions []engine.OperationOption
Expand Down
6 changes: 4 additions & 2 deletions compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ var _ = g.Describe("func WithMessageComparator()", func() {
handler := &IntegrationMessageHandler{
ConfigureFunc: func(c dogma.IntegrationConfigurer) {
c.Identity("<handler-name>", "7cb41db6-0116-4d03-80d7-277cc391b47e")
c.ConsumesCommandType(MessageC{})
c.ProducesEventType(MessageE{})
c.Routes(
dogma.HandlesCommand[MessageC](),
dogma.RecordsEvent[MessageE](),
)
},
HandleCommandFunc: func(
_ context.Context,
Expand Down
2 changes: 1 addition & 1 deletion engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (e *Engine) Dispatch(
m dogma.Message,
options ...OperationOption,
) error {
if err := dogma.ValidateMessage(m); err != nil {
if err := m.Validate(); err != nil {
panic(fmt.Sprintf(
"cannot dispatch invalid %T message: %s",
m,
Expand Down
26 changes: 17 additions & 9 deletions engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ var _ = g.Describe("type Engine", func() {
aggregate = &AggregateMessageHandler{
ConfigureFunc: func(c dogma.AggregateConfigurer) {
c.Identity("<aggregate>", "c72c106b-771e-42f8-b3e6-05452d4002ed")
c.ConsumesCommandType(MessageA{})
c.ProducesEventType(MessageE{})
c.Routes(
dogma.HandlesCommand[MessageA](),
dogma.RecordsEvent[MessageE](),
)
},
RouteCommandToInstanceFunc: func(dogma.Message) string {
return "<instance>"
Expand All @@ -43,9 +45,11 @@ var _ = g.Describe("type Engine", func() {
process = &ProcessMessageHandler{
ConfigureFunc: func(c dogma.ProcessConfigurer) {
c.Identity("<process>", "4721492d-7fa3-4cfa-9f0f-a3cb1f95933e")
c.ConsumesEventType(MessageB{})
c.ConsumesEventType(MessageE{}) // shared with <projection>
c.ProducesCommandType(MessageC{})
c.Routes(
dogma.HandlesEvent[MessageB](),
dogma.HandlesEvent[MessageE](), // shared with <projection>
dogma.ExecutesCommand[MessageC](),
)
},
RouteEventToInstanceFunc: func(context.Context, dogma.Message) (string, bool, error) {
return "<instance>", true, nil
Expand All @@ -55,16 +59,20 @@ var _ = g.Describe("type Engine", func() {
integration = &IntegrationMessageHandler{
ConfigureFunc: func(c dogma.IntegrationConfigurer) {
c.Identity("<integration>", "8b840c55-0b04-4107-bd4c-c69052c9fca3")
c.ConsumesCommandType(MessageC{})
c.ProducesEventType(MessageF{})
c.Routes(
dogma.HandlesCommand[MessageC](),
dogma.RecordsEvent[MessageF](),
)
},
}

projection = &ProjectionMessageHandler{
ConfigureFunc: func(c dogma.ProjectionConfigurer) {
c.Identity("<projection>", "f2b324d6-74f1-409e-8b28-8e44454037a9")
c.ConsumesEventType(MessageD{})
c.ConsumesEventType(MessageE{}) // shared with <process>
c.Routes(
dogma.HandlesEvent[MessageD](),
dogma.HandlesEvent[MessageE](), // shared with <process>
)
},
}

Expand Down
2 changes: 1 addition & 1 deletion engine/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ type CommandExecutor struct {
// ExecuteCommand enqueues a command for execution.
//
// It panics if the command is not routed to any handlers.
func (e CommandExecutor) ExecuteCommand(ctx context.Context, m dogma.Message) error {
func (e CommandExecutor) ExecuteCommand(ctx context.Context, m dogma.Message, _ ...dogma.ExecuteCommandOption) error {
return e.Engine.mustDispatch(ctx, message.CommandRole, m, e.Options...)
}
6 changes: 4 additions & 2 deletions engine/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ var _ = g.Describe("type CommandExecutor", func() {
aggregate = &AggregateMessageHandler{
ConfigureFunc: func(c dogma.AggregateConfigurer) {
c.Identity("<aggregate>", "4acf3050-8d02-4052-a9af-abb9e67add78")
c.ConsumesCommandType(MessageC{})
c.ProducesEventType(MessageE{})
c.Routes(
dogma.HandlesCommand[MessageC](),
dogma.RecordsEvent[MessageE](),
)
},
RouteCommandToInstanceFunc: func(dogma.Message) string {
return "<instance>"
Expand Down
2 changes: 1 addition & 1 deletion engine/internal/aggregate/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (c *Controller) Tick(

// Handle handles a message.
func (c *Controller) Handle(
ctx context.Context,
_ context.Context,
obs fact.Observer,
now time.Time,
env *envelope.Envelope,
Expand Down
6 changes: 4 additions & 2 deletions engine/internal/aggregate/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ var _ = g.Describe("type Controller", func() {
handler = &AggregateMessageHandler{
ConfigureFunc: func(c dogma.AggregateConfigurer) {
c.Identity("<name>", "e8fd6bd4-c3a3-4eb4-bf0f-56862a123229")
c.ConsumesCommandType(MessageC{})
c.ProducesEventType(MessageE{})
c.Routes(
dogma.HandlesCommand[MessageC](),
dogma.RecordsEvent[MessageE](),
)
},
// setup routes for "C" (command) messages to an instance ID based on the
// message's content
Expand Down
2 changes: 1 addition & 1 deletion engine/internal/aggregate/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (s *scope) RecordEvent(m dogma.Message) {
})
}

if err := dogma.ValidateMessage(m); err != nil {
if err := m.Validate(); err != nil {
panic(panicx.UnexpectedBehavior{
Handler: s.config,
Interface: "AggregateMessageHandler",
Expand Down
6 changes: 4 additions & 2 deletions engine/internal/aggregate/scope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ var _ = g.Describe("type scope", func() {
handler = &AggregateMessageHandler{
ConfigureFunc: func(c dogma.AggregateConfigurer) {
c.Identity("<name>", "fd88e430-32fe-49a6-888f-f678dcf924ef")
c.ConsumesCommandType(MessageC{})
c.ProducesEventType(MessageE{})
c.Routes(
dogma.HandlesCommand[MessageC](),
dogma.RecordsEvent[MessageE](),
)
},
RouteCommandToInstanceFunc: func(m dogma.Message) string {
switch m.(type) {
Expand Down
6 changes: 4 additions & 2 deletions engine/internal/integration/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ var _ = g.Describe("type Controller", func() {
handler = &IntegrationMessageHandler{
ConfigureFunc: func(c dogma.IntegrationConfigurer) {
c.Identity("<name>", "8cbb8bca-b5eb-4c94-a877-dfc8dc9968ca")
c.ConsumesCommandType(MessageC{})
c.ProducesEventType(MessageE{})
c.Routes(
dogma.HandlesCommand[MessageC](),
dogma.RecordsEvent[MessageE](),
)
},
}

Expand Down
2 changes: 1 addition & 1 deletion engine/internal/integration/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (s *scope) RecordEvent(m dogma.Message) {
})
}

if err := dogma.ValidateMessage(m); err != nil {
if err := m.Validate(); err != nil {
panic(panicx.UnexpectedBehavior{
Handler: s.config,
Interface: "IntegrationMessageHandler",
Expand Down
Loading

0 comments on commit 6e48260

Please sign in to comment.