Skip to content

Commit

Permalink
Changes to Mocking pattern
Browse files Browse the repository at this point in the history
Documentation
  • Loading branch information
bw19 committed May 23, 2024
1 parent 082ebdf commit 2861f79
Show file tree
Hide file tree
Showing 50 changed files with 652 additions and 387 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Review each of the major project packages to get oriented in the code structure:
* [errors](docs/structure/errors.md) - An enhancement of the standard `errors` package
* [examples](docs/structure/examples.md) - Demo microservices
* [frame](docs/structure/frame.md) - A utility for type-safe manipulation of the HTTP control headers used by the framework
* [httpx](docs/structure/httpx.md) - Various HTTP utilities
* [httpx](docs/structure/httpx.md) - Various HTTP utilities
* [log](docs/structure/log.md) - Fields for attaching data to log messages
* [lru](docs/structure/lru.md) - An LRU with with limits on age and weight
* [mtr](docs/structure/mtr.md) - Metrics collectors
Expand Down Expand Up @@ -79,7 +79,7 @@ Go deep into the philosophy and implementation of `Microbus`:
* [Environment variables](docs/tech/envars.md) - Environment variables used to initialize microservices
* [Code generation](docs/tech/codegen.md) - Discover the power of `Microbus`'s most powerful RAD tool
* [Events](docs/tech/events.md) - Using event-driven architecture to decouple microservices
* [Integration testing](docs/tech/integrationtesting.md) - Testing multiple microservices together
* [Integration testing](docs/tech/integrationtesting.md) - Testing multiple microservices together
* [Distributed tracing](docs/tech/distribtracing.md) - Distributed tracing using OpenTelemetry and Jaeger
* [OpenAPI](docs/tech/openapi.md) - OpenAPI document generation for microservices

Expand Down
5 changes: 3 additions & 2 deletions codegen/bundle/integration_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ var (

// Initialize starts up the testing app.
func Initialize() (err error) {
// Include all downstream microservices in the testing app
App.Init(func(svc service.Service) {
// Initialize all microservices
})

// Include all downstream microservices in the testing app
App.Include(
Svc.Init(func(svc *Service) {
// Initialize the individual microservice
// Initialize the microservice under test
{{- range .Configs }}
// svc.Set{{ .Name }}({{ with index .Signature.OutputArgs 0 }}{{ .Name }}{{ end }})
{{- end}}
Expand Down
41 changes: 29 additions & 12 deletions codegen/bundle/intermediate/mock-gen.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,25 @@ var (
_ {{ .PackageSuffix }}api.Client
)

// Mock is a mockable version of the {{ .General.Host }} microservice,
// allowing functions, sinks and web handlers to be mocked.
// Mock is a mockable version of the {{ .General.Host }} microservice, allowing functions, event sinks and web handlers to be mocked.
type Mock struct {
*connector.Connector

{{- range (JoinHandlers .Functions .Sinks)}}
Mock{{ .Name }} func({{ .In }}) ({{ .Out }})
mock{{ .Name }} func({{ .In }}) ({{ .Out }})
{{- end}}

{{- range .Webs}}
Mock{{ .Name }} func(w http.ResponseWriter, r *http.Request) (err error)
mock{{ .Name }} func(w http.ResponseWriter, r *http.Request) (err error)
{{- end}}
}

// NewMock creates a new mockable version of the microservice.
func NewMock(version int) *Mock {
func NewMock() *Mock {
svc := &Mock{
Connector: connector.New("{{ .General.Host }}"),
}
svc.SetVersion(version)
svc.SetVersion(7357) // Stands for TEST
svc.SetDescription(`{{ .General.Description }}`)
svc.SetOnStartup(svc.doOnStartup)

Expand Down Expand Up @@ -96,7 +95,7 @@ func (svc *Mock) doOnStartup(ctx context.Context) (err error) {
{{- range .Functions }}
// do{{ .Name }} handles marshaling for the {{ .Name }} function.
func (svc *Mock) do{{ .Name }}(w http.ResponseWriter, r *http.Request) error {
if svc.Mock{{ .Name }} == nil {
if svc.mock{{ .Name }} == nil {
return errors.New("mocked endpoint '{{ .Name }}' not implemented")
}
var i {{ $shortPackage }}api.{{ .Name }}In
Expand All @@ -105,7 +104,7 @@ func (svc *Mock) do{{ .Name }}(w http.ResponseWriter, r *http.Request) error {
if err!=nil {
return errors.Trace(err)
}
{{ range .Signature.OutputArgs }}o.{{ CapitalizeIdentifier .Name }}, {{ end }}err = svc.Mock{{ .Name }}(
{{ range .Signature.OutputArgs }}o.{{ CapitalizeIdentifier .Name }}, {{ end }}err = svc.mock{{ .Name }}(
r.Context(),
{{- range .Signature.InputArgs }}
i.{{ CapitalizeIdentifier .Name }},
Expand All @@ -124,28 +123,46 @@ func (svc *Mock) do{{ .Name }}(w http.ResponseWriter, r *http.Request) error {
}
return nil
}

// Mock{{ .Name }} sets up a mock handler for the {{ .Name }} function.
func (svc *Mock) Mock{{ .Name }}(handler func({{ .In }}) ({{ .Out }})) *Mock {
svc.mock{{ .Name }} = handler
return svc
}
{{ end}}

{{- range .Webs}}
// do{{ .Name }} handles the {{ .Name }} web handler.
func (svc *Mock) do{{ .Name }}(w http.ResponseWriter, r *http.Request) (err error) {
if svc.Mock{{ .Name }} == nil {
if svc.mock{{ .Name }} == nil {
return errors.New("mocked endpoint '{{ .Name }}' not implemented")
}
err = svc.Mock{{ .Name }}(w, r)
err = svc.mock{{ .Name }}(w, r)
return errors.Trace(err)
}

// Mock{{ .Name }} sets up a mock handler for the {{ .Name }} web handler.
func (svc *Mock) Mock{{ .Name }}(handler func(w http.ResponseWriter, r *http.Request) (err error)) *Mock {
svc.mock{{ .Name }} = handler
return svc
}
{{ end }}

{{- range .Sinks }}
// do{{ .Name }} handles marshaling for the {{ .Name }} event sink.
func (svc *Mock) do{{ .Name }}({{ .In }}) ({{ .Out }}) {
if svc.Mock{{ .Name }} == nil {
if svc.mock{{ .Name }} == nil {
err = errors.New("mocked endpoint '{{ .Name }}' not implemented")
return
}
{{ range .Signature.OutputArgs }}{{ .Name }}, {{ end }}err = svc.Mock{{ .Name }}(ctx{{ range .Signature.InputArgs }}, {{ .Name }}{{ end }})
{{ range .Signature.OutputArgs }}{{ .Name }}, {{ end }}err = svc.mock{{ .Name }}(ctx{{ range .Signature.InputArgs }}, {{ .Name }}{{ end }})
err = errors.Trace(err)
return
}

// Mock{{ .Name }} sets up a mock handler for the {{ .Name }} event sink.
func (svc *Mock) Mock{{ .Name }}(handler func({{ .In }}) ({{ .Out }})) *Mock {
svc.mock{{ .Name }} = handler
return svc
}
{{ end }}
5 changes: 2 additions & 3 deletions codegen/bundle/service-gen.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,12 @@ func NewService() *Service {
return s
}

// Mock is a mockable version of the {{ .General.Host }} microservice,
// allowing functions, sinks and web handlers to be mocked.
// Mock is a mockable version of the {{ .General.Host }} microservice, allowing functions, event sinks and web handlers to be mocked.
type Mock = intermediate.Mock

// New creates a new mockable version of the microservice.
func NewMock() *Mock {
return intermediate.NewMock(Version)
return intermediate.NewMock()
}

/*
Expand Down
3 changes: 3 additions & 0 deletions codegen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ func (gen *Generator) Run() error {
verNum := 1
if v != nil {
verNum = v.Version + 1
if verNum == 7357 { // Reserved to indicate TEST
verNum++
}
}
err := gen.makeVersion(verNum)
if err != nil {
Expand Down
43 changes: 30 additions & 13 deletions coreservices/configurator/intermediate/mock-gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions coreservices/configurator/service-gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions coreservices/configurator/version-gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2861f79

Please sign in to comment.