Skip to content

Commit

Permalink
Remove logs duplicating & add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
yaronius committed Apr 28, 2021
1 parent 098accf commit e987bc3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
11 changes: 5 additions & 6 deletions svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,7 @@ func (s *SVC) Run() {
go func(name string, w Worker) {
defer s.recoverWait(name, &wg, errs)
if err := w.Run(); err != nil {
if errors.Is(err, context.Canceled) {
s.logger.Warn("Worker context canceled", zap.String("worker", name))
return
}
s.logger.Error("Run exited with error", zap.Error(err), zap.String("worker", name))
err = fmt.Errorf("worker %s exited: %w", name, err)
errs <- err
}
}(name, w)
Expand All @@ -153,7 +149,10 @@ func (s *SVC) Run() {

select {
case err := <-errs:
s.logger.Fatal("Worker Init/Run failure", zap.Error(err))
if !errors.Is(err, context.Canceled) {
s.logger.Fatal("Worker Init/Run failure", zap.Error(err))
}
s.logger.Warn("Worker context canceled", zap.Error(err))
case sig := <-s.signals:
s.logger.Warn("Caught signal", zap.String("signal", sig.String()))
case <-waitGroupToChan(&wg):
Expand Down
19 changes: 19 additions & 0 deletions svc_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package svc

import (
"context"
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -87,6 +89,23 @@ func TestShutdown(t *testing.T) {
}
}

func TestContextCanceled(t *testing.T) {
dummyWorker := &WorkerMock{
InitFunc: func(*zap.Logger) error { return nil },
RunFunc: func() error {
return fmt.Errorf("stopped: %w", context.Canceled)
},
TerminateFunc: func() error { return nil },
}

s, err := New("dummy-service", "v0.0.1")
require.NoError(t, err)
s.AddWorker("dummy-worker", dummyWorker)

// Run should not log fatal
s.Run()
}

var _ Worker = (*WorkerMock)(nil)

type WorkerMock struct {
Expand Down

0 comments on commit e987bc3

Please sign in to comment.