Skip to content

Commit

Permalink
properly fix the hooks race test
Browse files Browse the repository at this point in the history
  • Loading branch information
dgsb committed Jul 20, 2018
1 parent 9205268 commit 6999e59
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions hooks/test/test_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package test

import (
"math/rand"
"sync"
"testing"
"time"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -38,24 +40,34 @@ func TestAllHooks(t *testing.T) {
}

func TestLoggingWithHooksRace(t *testing.T) {

rand.Seed(time.Now().Unix())
unlocker := rand.Int() % 100

assert := assert.New(t)
logger, hook := NewNullLogger()

var wg sync.WaitGroup
wg.Add(100)
var wgOne, wgAll sync.WaitGroup
wgOne.Add(1)
wgAll.Add(100)

for i := 0; i < 100; i++ {
go func() {
go func(i int) {
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("info", hook.LastEntry().Message)

wgAll.Wait()

entries := hook.AllEntries()
assert.Equal(100, len(entries))
}

0 comments on commit 6999e59

Please sign in to comment.