2018-02-02 09:17:40 +05:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
2019-02-25 16:29:48 +05:00
|
|
|
"math/rand"
|
|
|
|
"sync"
|
2018-02-02 09:17:40 +05:00
|
|
|
"testing"
|
2019-02-25 16:29:48 +05:00
|
|
|
"time"
|
2018-02-02 09:17:40 +05:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAllHooks(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
logger, hook := NewNullLogger()
|
|
|
|
assert.Nil(hook.LastEntry())
|
|
|
|
assert.Equal(0, len(hook.Entries))
|
|
|
|
|
|
|
|
logger.Error("Hello error")
|
|
|
|
assert.Equal(logrus.ErrorLevel, hook.LastEntry().Level)
|
|
|
|
assert.Equal("Hello error", hook.LastEntry().Message)
|
|
|
|
assert.Equal(1, len(hook.Entries))
|
|
|
|
|
|
|
|
logger.Warn("Hello warning")
|
|
|
|
assert.Equal(logrus.WarnLevel, hook.LastEntry().Level)
|
|
|
|
assert.Equal("Hello warning", hook.LastEntry().Message)
|
|
|
|
assert.Equal(2, len(hook.Entries))
|
|
|
|
|
|
|
|
hook.Reset()
|
|
|
|
assert.Nil(hook.LastEntry())
|
|
|
|
assert.Equal(0, len(hook.Entries))
|
|
|
|
|
|
|
|
hook = NewGlobal()
|
|
|
|
|
|
|
|
logrus.Error("Hello error")
|
|
|
|
assert.Equal(logrus.ErrorLevel, hook.LastEntry().Level)
|
|
|
|
assert.Equal("Hello error", hook.LastEntry().Message)
|
|
|
|
assert.Equal(1, len(hook.Entries))
|
2019-02-25 16:29:48 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoggingWithHooksRace(t *testing.T) {
|
|
|
|
|
|
|
|
rand.Seed(time.Now().Unix())
|
|
|
|
unlocker := rand.Int() % 100
|
|
|
|
|
|
|
|
assert := assert.New(t)
|
|
|
|
logger, hook := NewNullLogger()
|
|
|
|
|
|
|
|
var wgOne, wgAll sync.WaitGroup
|
|
|
|
wgOne.Add(1)
|
|
|
|
wgAll.Add(100)
|
|
|
|
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
go func(i int) {
|
|
|
|
logger.Info("info")
|
|
|
|
wgAll.Done()
|
|
|
|
if i == unlocker {
|
|
|
|
wgOne.Done()
|
|
|
|
}
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
wgOne.Wait()
|
2018-02-02 09:17:40 +05:00
|
|
|
|
2019-02-25 16:29:48 +05:00
|
|
|
assert.Equal(logrus.InfoLevel, hook.LastEntry().Level)
|
|
|
|
assert.Equal("info", hook.LastEntry().Message)
|
|
|
|
|
|
|
|
wgAll.Wait()
|
|
|
|
|
|
|
|
entries := hook.AllEntries()
|
|
|
|
assert.Equal(100, len(entries))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFatalWithAlternateExit(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
logger, hook := NewNullLogger()
|
|
|
|
logger.ExitFunc = func(code int) {}
|
|
|
|
|
|
|
|
logger.Fatal("something went very wrong")
|
|
|
|
assert.Equal(logrus.FatalLevel, hook.LastEntry().Level)
|
|
|
|
assert.Equal("something went very wrong", hook.LastEntry().Message)
|
|
|
|
assert.Equal(1, len(hook.Entries))
|
2018-02-02 09:17:40 +05:00
|
|
|
}
|