add service test and shutdown based on context

This commit is contained in:
Asim Aslam
2017-01-07 14:52:23 +00:00
parent e84e5ae303
commit 942985ce51
3 changed files with 111 additions and 5 deletions

View File

@@ -28,6 +28,8 @@ type Options struct {
// Before and After funcs
BeforeStart []func() error
BeforeStop []func() error
AfterStart []func() error
AfterStop []func() error
// Other options for implementations of the interface
@@ -43,6 +45,7 @@ func newOptions(opts ...Option) Options {
Server: server.DefaultServer,
Registry: registry.DefaultRegistry,
Transport: transport.DefaultTransport,
Context: context.Background(),
}
for _, o := range opts {
@@ -73,6 +76,15 @@ func Client(c client.Client) Option {
}
}
// Context specifies a context for the service
// Can be used to signal shutdown of the service
// Can be used for extra option values
func Context(ctx context.Context) Option {
return func(o *Options) {
o.Context = ctx
}
}
func Server(s server.Server) Option {
return func(o *Options) {
o.Server = s
@@ -204,6 +216,18 @@ func BeforeStart(fn func() error) Option {
}
}
func BeforeStop(fn func() error) Option {
return func(o *Options) {
o.BeforeStop = append(o.BeforeStop, fn)
}
}
func AfterStart(fn func() error) Option {
return func(o *Options) {
o.AfterStart = append(o.AfterStart, fn)
}
}
func AfterStop(fn func() error) Option {
return func(o *Options) {
o.AfterStop = append(o.AfterStop, fn)