From 1fb5673d2778df1abc5b0443d67f56adcf970a23 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Fri, 25 Oct 2024 17:20:35 +0300 Subject: [PATCH] fixup graceful stop Signed-off-by: Vasiliy Tolstov --- service.go | 7 +++++-- service_test.go | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/service.go b/service.go index a15897d4..1276c9a3 100644 --- a/service.go +++ b/service.go @@ -86,13 +86,14 @@ func RegisterSubscriber(topic string, s server.Server, h interface{}, opts ...se } type service struct { + done chan struct{} opts Options sync.RWMutex } // NewService creates and returns a new Service based on the packages within. func NewService(opts ...Option) Service { - return &service{opts: NewOptions(opts...)} + return &service{opts: NewOptions(opts...), done: make(chan struct{})} } func (s *service) Name() string { @@ -362,6 +363,8 @@ func (s *service) Stop() error { } } + close(s.done) + return nil } @@ -385,7 +388,7 @@ func (s *service) Run() error { } // wait on context cancel - <-s.opts.Context.Done() + <-s.done return s.Stop() } diff --git a/service_test.go b/service_test.go index b6bca118..7a39c1b5 100644 --- a/service_test.go +++ b/service_test.go @@ -134,7 +134,7 @@ func TestNewService(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := NewService(tt.args.opts...); !reflect.DeepEqual(got, tt.want) { + if got := NewService(tt.args.opts...); got.Name() != tt.want.Name() { t.Errorf("NewService() = %v, want %v", got.Options().Name, tt.want.Options().Name) } })