Make fireHooks() method receive a copy of Entry structure to avoid race conditions

Signed-off-by: Jay Ching Lim <imjching@users.noreply.github.com>
This commit is contained in:
Jay Ching Lim
2018-02-12 17:26:48 -05:00
parent 9f91ab2ef9
commit be569094e9
2 changed files with 28 additions and 4 deletions
+24 -2
View File
@@ -1,6 +1,7 @@
package test
import (
"sync"
"testing"
"github.com/sirupsen/logrus"
@@ -8,7 +9,6 @@ import (
)
func TestAllHooks(t *testing.T) {
assert := assert.New(t)
logger, hook := NewNullLogger()
@@ -35,5 +35,27 @@ func TestAllHooks(t *testing.T) {
assert.Equal(logrus.ErrorLevel, hook.LastEntry().Level)
assert.Equal("Hello error", hook.LastEntry().Message)
assert.Equal(1, len(hook.Entries))
}
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()
entries := hook.AllEntries()
assert.Equal(100, len(entries))
}