Skip to content

Commit

Permalink
codegen: Export event stream constructor for easier mocking (aws#3473)
Browse files Browse the repository at this point in the history
Fixes aws#3412 by exporting the operation's EventStream type's constructor function so it can be used to fully initialize fully when mocking out behavior for API operations with event streams.
  • Loading branch information
jasdel authored Aug 11, 2020
1 parent 88938cf commit f8f9d46
Show file tree
Hide file tree
Showing 9 changed files with 256 additions and 31 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
* Fixes [#3441](https://github.com/aws/aws-sdk-go/issues/3441) by adding a new XXX_Values function for each API enum type that returns a slice of enum values, e.g `DomainStatus_Values`.

### SDK Bugs
* `codegen`: Export event stream constructor for easier mocking ([#3473](https://github.com/aws/aws-sdk-go/pull/3473))
* Fixes [#3412](https://github.com/aws/aws-sdk-go/issues/3412) by exporting the operation's EventStream type's constructor function so it can be used to fully initialize fully when mocking out behavior for API operations with event streams.
50 changes: 44 additions & 6 deletions private/model/api/codegentest/service/restjsonservice/api.go

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

50 changes: 44 additions & 6 deletions private/model/api/codegentest/service/restxmlservice/api.go

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

50 changes: 44 additions & 6 deletions private/model/api/codegentest/service/rpcservice/api.go

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

45 changes: 42 additions & 3 deletions private/model/api/eventstream_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ const eventStreamAPITmplDef = `
{{- $inputStream := $esapi.InputStream }}
// {{ $esapi.Name }} provides the event stream handling for the {{ $.ExportedName }}.
//
// For testing and mocking the event stream this type should be initialized via
// the New{{ $esapi.Name }} constructor function. Using the functional options
// to pass in nested mock behavior.
type {{ $esapi.Name }} struct {
{{- if $inputStream }}
Expand Down Expand Up @@ -95,11 +99,46 @@ type {{ $esapi.Name }} struct {
err *eventstreamapi.OnceError
}
func new{{ $esapi.Name }}() *{{ $esapi.Name }} {
return &{{ $esapi.Name }} {
// New{{ $esapi.Name }} initializes an {{ $esapi.Name }}.
// This function should only be used for testing and mocking the {{ $esapi.Name }}
// stream within your application.
{{- if $inputStream }}
//
// The Writer member must be set before writing events to the stream.
{{- end }}
{{- if $outputStream }}
//
// The Reader member must be set before reading events from the stream.
{{- end }}
{{- if $esapi.Legacy }}
//
// The StreamCloser member should be set to the underlying io.Closer,
// (e.g. http.Response.Body), that will be closed when the stream Close method
// is called.
{{- end }}
//
// es := New{{ $esapi.Name }}(func(o *{{ $esapi.Name}}{
{{- if $inputStream }}
// es.Writer = myMockStreamWriter
{{- end }}
{{- if $outputStream }}
// es.Reader = myMockStreamReader
{{- end }}
{{- if $esapi.Legacy }}
// es.StreamCloser = myMockStreamCloser
{{- end }}
// })
func New{{ $esapi.Name }}(opts ...func(*{{ $esapi.Name}})) *{{ $esapi.Name }} {
es := &{{ $esapi.Name }} {
done: make(chan struct{}),
err: eventstreamapi.NewOnceError(),
}
for _, fn := range opts {
fn(es)
}
return es
}
{{- if $esapi.Legacy }}
Expand Down Expand Up @@ -328,7 +367,7 @@ func (es *{{ $esapi.Name }}) waitStreamPartClose() {
{{- if $inputStream }}
//
// Will close the underlying EventStream writer, and no more events can be
// sent.
// sent.
{{- end }}
{{- if $outputStream }}
//
Expand Down
2 changes: 1 addition & 1 deletion private/model/api/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (c *{{ .API.StructName }}) {{ .ExportedName }}Request(` +
)
{{- end }}
es := new{{ $esapi.Name }}()
es := New{{ $esapi.Name }}()
{{- if $esapi.Legacy }}
req.Handlers.Unmarshal.PushBack(es.setStreamCloser)
{{- end }}
Expand Down
Loading

0 comments on commit f8f9d46

Please sign in to comment.