Skip to content

Commit

Permalink
test(hooks): fix logs after main routine (#5284)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsamin authored Jun 29, 2020
1 parent bf1afd3 commit 8211112
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
6 changes: 3 additions & 3 deletions engine/hooks/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ func (s *Service) deleteTaskExecutionsRoutine(ctx context.Context) error {
func (s *Service) dequeueTaskExecutions(ctx context.Context) error {
for {
if ctx.Err() != nil {
log.Error(ctx, "dequeueTaskExecutions> exiting go routine: %v", ctx.Err())
return ctx.Err()
}
size, err := s.Dao.QueueLen()
Expand All @@ -261,14 +260,15 @@ func (s *Service) dequeueTaskExecutions(ctx context.Context) error {
// Dequeuing context
var taskKey string
if ctx.Err() != nil {
log.Error(ctx, "dequeueTaskExecutions> exiting go routine: %v", err)
return ctx.Err()
}
if err := s.Cache.DequeueWithContext(ctx, schedulerQueueKey, &taskKey); err != nil {
log.Error(ctx, "dequeueTaskExecutions> store.DequeueWithContext err: %v", err)
continue
}
s.Dao.dequeuedIncr()
if taskKey == "" {
continue
}
log.Info(ctx, "dequeueTaskExecutions> work on taskKey: %s", taskKey)

// Load the task execution
Expand Down
4 changes: 1 addition & 3 deletions engine/hooks/tasks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ func Test_dequeueTaskExecutions_ScheduledTask(t *testing.T) {

// Start the goroutine
go func() {
if err := s.dequeueTaskExecutions(ctx); err != nil {
t.Logf("dequeueTaskExecutions error: %v", err)
}
s.dequeueTaskExecutions(ctx) // nolint
}()

h := &sdk.NodeHook{
Expand Down
32 changes: 32 additions & 0 deletions sdk/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"os"
"strings"
"testing"

log "github.com/sirupsen/logrus"

Expand Down Expand Up @@ -48,8 +49,39 @@ type Logger interface {
Fatalf(fmt string, values ...interface{})
}

type TestingLogger struct {
t *testing.T
}

var _ Logger = new(TestingLogger)

func (t *TestingLogger) isDone() bool {
return t.t.Failed() || t.t.Skipped()
}

func (t *TestingLogger) Logf(fmt string, values ...interface{}) {
if !t.isDone() {
t.t.Logf(fmt, values...)
}
}
func (t *TestingLogger) Errorf(fmt string, values ...interface{}) {
if !t.isDone() {
t.t.Errorf(fmt, values...)
}
}
func (t *TestingLogger) Fatalf(fmt string, values ...interface{}) {
if !t.isDone() {
t.t.Fatalf(fmt, values...)
}
}

// SetLogger replace logrus logger with custom one.
func SetLogger(l Logger) {
t, isTesting := l.(*testing.T)
if isTesting {
logger = &TestingLogger{t: t}
return
}
logger = l
}

Expand Down

0 comments on commit 8211112

Please sign in to comment.