fixup graceful stop

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2024-10-25 17:20:35 +03:00
parent 3bbb0cbc72
commit 1fb5673d27
2 changed files with 6 additions and 3 deletions

View File

@ -86,13 +86,14 @@ func RegisterSubscriber(topic string, s server.Server, h interface{}, opts ...se
} }
type service struct { type service struct {
done chan struct{}
opts Options opts Options
sync.RWMutex sync.RWMutex
} }
// NewService creates and returns a new Service based on the packages within. // NewService creates and returns a new Service based on the packages within.
func NewService(opts ...Option) Service { func NewService(opts ...Option) Service {
return &service{opts: NewOptions(opts...)} return &service{opts: NewOptions(opts...), done: make(chan struct{})}
} }
func (s *service) Name() string { func (s *service) Name() string {
@ -362,6 +363,8 @@ func (s *service) Stop() error {
} }
} }
close(s.done)
return nil return nil
} }
@ -385,7 +388,7 @@ func (s *service) Run() error {
} }
// wait on context cancel // wait on context cancel
<-s.opts.Context.Done() <-s.done
return s.Stop() return s.Stop()
} }

View File

@ -134,7 +134,7 @@ func TestNewService(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { 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) t.Errorf("NewService() = %v, want %v", got.Options().Name, tt.want.Options().Name)
} }
}) })