Skip to content

Commit

Permalink
Use ioutil.Discard for log output on test
Browse files Browse the repository at this point in the history
  • Loading branch information
oklahomer committed Jun 15, 2018
1 parent ee4cde6 commit 2b81ab5
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 11 deletions.
17 changes: 17 additions & 0 deletions gitter/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,30 @@ package gitter
import (
"errors"
"github.com/oklahomer/go-sarah"
"github.com/oklahomer/go-sarah/log"
"github.com/oklahomer/go-sarah/retry"
"golang.org/x/net/context"
"io/ioutil"
stdLogger "log"
"os"
"reflect"
"testing"
"time"
)

func TestMain(m *testing.M) {
oldLogger := log.GetLogger()
defer log.SetLogger(oldLogger)

l := stdLogger.New(ioutil.Discard, "dummyLog", 0)
logger := log.NewWithStandardLogger(l)
log.SetLogger(logger)

code := m.Run()

os.Exit(code)
}

type DummyAPIClient struct {
RoomsFunc func(context.Context) (*Rooms, error)
PostMessageFunc func(context.Context, *Room, string) (*Message, error)
Expand Down
35 changes: 35 additions & 0 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,41 @@ func SetLogger(l Logger) {
logger = l
}

// GetLogger returns currently set Logger.
// Once a preferred Logger implementation is set via log.SetLogger,
// developer may use its method by calling this package's function: log.Debug, log.Debugf and others.
//
// However when developer wishes to retrieve the Logger instance, this function helps.
// This is particularly useful in such situation where Logger implementation must be temporarily switched but must be
// switched back when a task is done.
// Example follows.
//
// import (
// "github.com/oklahomer/go-sarah/log"
// "io/ioutil"
// stdLogger "log"
// "os"
// "testing"
// )
//
// func TestMain(m *testing.M) {
// oldLogger := log.GetLogger()
// defer log.SetLogger(oldLogger)
//
// l := stdLogger.New(ioutil.Discard, "dummyLog", 0)
// logger := log.NewWithStandardLogger(l)
// log.SetLogger(logger)
//
// code := m.Run()
//
// os.Exit(code)
// }
func GetLogger() Logger {
mutex.RLock()
defer mutex.RUnlock()
return logger
}

// SetOutputLevel sets what logging level to output.
// Application may call logging method any time, but Logger only outputs if the corresponding log level is equal to or higher than the level set here.
// e.g. When InfoLevel is set, output with Debug() and Debugf() are ignored.
Expand Down
19 changes: 11 additions & 8 deletions log/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func Test_newDefaultLogger(t *testing.T) {
}

func TestNewWithStandardLogger(t *testing.T) {
standardLogger := log.New(bytes.NewBuffer([]byte{}), "", 0)
standardLogger := log.New(ioutil.Discard, "", 0)
l := NewWithStandardLogger(standardLogger)

if l == nil {
Expand Down Expand Up @@ -308,28 +308,32 @@ func TestSetLogger(t *testing.T) {
}

func Test_concurrentAccess(t *testing.T) {
old := logger.(*defaultLogger)
impl := logger.(*defaultLogger)
old := impl.logger
impl.logger = log.New(ioutil.Discard, "", 0)
defer func() {
logger = old
impl.logger = old
}()

var wg sync.WaitGroup

wg.Add(1)
go func() {
defer wg.Done()

levels := []Level{
DebugLevel, InfoLevel, WarnLevel, ErrorLevel,
}
rand.Seed(time.Now().Unix())
for range make([]int, 1000) {
SetOutputLevel(levels[rand.Intn(len(levels))])
}

wg.Done()
}()

wg.Add(1)
go func() {
defer wg.Done()

for range make([]int, 1000) {
newLogger := &DummyLogger{
DebugFunc: func(args ...interface{}) {
Expand All @@ -339,16 +343,15 @@ func Test_concurrentAccess(t *testing.T) {
SetLogger(newLogger)
}

wg.Done()
}()

wg.Add(1)
go func() {
defer wg.Done()

for range make([]int, 1000) {
Debug("foo")
}

wg.Done()
}()

wg.Wait()
Expand Down
6 changes: 3 additions & 3 deletions retry/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package retry
import (
"errors"
"fmt"
"github.com/oklahomer/go-sarah/log"
"io/ioutil"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -101,7 +101,7 @@ func TestWithInterval(t *testing.T) {
interval := 100 * time.Millisecond
WithInterval(2, func() error {
i++
log.Error(i)
ioutil.Discard.Write([]byte("writing dummy output"))
if i == 1 {
startAt = time.Now()
} else {
Expand All @@ -126,7 +126,7 @@ func TestWithBackOff(t *testing.T) {
factor := 0.01
WithBackOff(2, func() error {
i++
log.Error(i)
ioutil.Discard.Write([]byte("writing dummy output"))
if i == 1 {
startAt = time.Now()
} else {
Expand Down
17 changes: 17 additions & 0 deletions runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,31 @@ package sarah

import (
"errors"
"github.com/oklahomer/go-sarah/log"
"golang.org/x/net/context"
"io/ioutil"
stdLogger "log"
"os"
"path/filepath"
"regexp"
"sync"
"testing"
"time"
)

func TestMain(m *testing.M) {
oldLogger := log.GetLogger()
defer log.SetLogger(oldLogger)

l := stdLogger.New(ioutil.Discard, "dummyLog", 0)
logger := log.NewWithStandardLogger(l)
log.SetLogger(logger)

code := m.Run()

os.Exit(code)
}

type DummyWorker struct {
EnqueueFunc func(func()) error
}
Expand Down
17 changes: 17 additions & 0 deletions watchers/dirwatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,30 @@ package watchers
import (
"errors"
"github.com/fsnotify/fsnotify"
"github.com/oklahomer/go-sarah/log"
"golang.org/x/net/context"
"io/ioutil"
stdLogger "log"
"os"
"path/filepath"
"reflect"
"testing"
"time"
)

func TestMain(m *testing.M) {
oldLogger := log.GetLogger()
defer log.SetLogger(oldLogger)

l := stdLogger.New(ioutil.Discard, "dummyLog", 0)
logger := log.NewWithStandardLogger(l)
log.SetLogger(logger)

code := m.Run()

os.Exit(code)
}

type dummyInternalWatcher struct {
AddFunc func(string) error
RemoveFunc func(string) error
Expand Down
17 changes: 17 additions & 0 deletions workers/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,29 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/oklahomer/go-sarah/log"
"golang.org/x/net/context"
"gopkg.in/yaml.v2"
"io/ioutil"
stdLogger "log"
"os"
"testing"
"time"
)

func TestMain(m *testing.M) {
oldLogger := log.GetLogger()
defer log.SetLogger(oldLogger)

l := stdLogger.New(ioutil.Discard, "dummyLog", 0)
logger := log.NewWithStandardLogger(l)
log.SetLogger(logger)

code := m.Run()

os.Exit(code)
}

type DummyReporter struct {
ReportFunc func(context.Context, *Stats)
}
Expand Down

0 comments on commit 2b81ab5

Please sign in to comment.