2015-05-13 15:14:34 +03:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
2018-02-13 01:26:48 +03:00
|
|
|
"sync"
|
2015-05-13 15:14:34 +03:00
|
|
|
"testing"
|
|
|
|
|
2017-05-19 15:00:16 +03:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-05-13 15:14:34 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAllHooks(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
logger, hook := NewNullLogger()
|
2015-10-09 17:07:14 +03:00
|
|
|
assert.Nil(hook.LastEntry())
|
|
|
|
assert.Equal(0, len(hook.Entries))
|
2015-05-13 15:14:34 +03:00
|
|
|
|
|
|
|
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()
|
2015-10-09 17:07:14 +03:00
|
|
|
assert.Nil(hook.LastEntry())
|
2015-05-13 15:14:34 +03:00
|
|
|
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))
|
2018-02-13 01:26:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoggingWithHooksRace(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
logger, hook := NewNullLogger()
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(100)
|
|
|
|
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
go func() {
|
|
|
|
logger.Info("info")
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(logrus.InfoLevel, hook.LastEntry().Level)
|
|
|
|
assert.Equal("info", hook.LastEntry().Message)
|
|
|
|
|
|
|
|
wg.Wait()
|
2015-05-13 15:14:34 +03:00
|
|
|
|
2018-02-13 01:26:48 +03:00
|
|
|
entries := hook.AllEntries()
|
|
|
|
assert.Equal(100, len(entries))
|
2015-05-13 15:14:34 +03:00
|
|
|
}
|