Merge pull request #791 from dgsb/master

properly fix the hooks race test
This commit is contained in:
David Bariod 2018-07-20 13:34:42 +02:00 committed by GitHub
commit 07e1216af7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 6 deletions

View File

@ -1,8 +1,10 @@
package test package test
import ( import (
"math/rand"
"sync" "sync"
"testing" "testing"
"time"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -38,24 +40,34 @@ func TestAllHooks(t *testing.T) {
} }
func TestLoggingWithHooksRace(t *testing.T) { func TestLoggingWithHooksRace(t *testing.T) {
rand.Seed(time.Now().Unix())
unlocker := rand.Int() % 100
assert := assert.New(t) assert := assert.New(t)
logger, hook := NewNullLogger() logger, hook := NewNullLogger()
var wg sync.WaitGroup var wgOne, wgAll sync.WaitGroup
wg.Add(100) wgOne.Add(1)
wgAll.Add(100)
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
go func() { go func(i int) {
logger.Info("info") logger.Info("info")
wg.Done() wgAll.Done()
}() if i == unlocker {
wgOne.Done()
}
}(i)
} }
wg.Wait() wgOne.Wait()
assert.Equal(logrus.InfoLevel, hook.LastEntry().Level) assert.Equal(logrus.InfoLevel, hook.LastEntry().Level)
assert.Equal("info", hook.LastEntry().Message) assert.Equal("info", hook.LastEntry().Message)
wgAll.Wait()
entries := hook.AllEntries() entries := hook.AllEntries()
assert.Equal(100, len(entries)) assert.Equal(100, len(entries))
} }