fix for entry data field race condition

This commit is contained in:
David Bariod
2021-02-16 10:29:37 +01:00
parent f513f99c15
commit ac6e35b4c2
5 changed files with 79 additions and 34 deletions
+36 -3
View File
@@ -588,15 +588,48 @@ func TestLoggingRaceWithHooksOnEntry(t *testing.T) {
logger.AddHook(hook)
entry := logger.WithField("context", "clue")
var wg sync.WaitGroup
var (
wg sync.WaitGroup
mtx sync.Mutex
start bool
)
cond := sync.NewCond(&mtx)
wg.Add(100)
for i := 0; i < 100; i++ {
for i := 0; i < 50; i++ {
go func() {
entry.Info("info")
cond.L.Lock()
for !start {
cond.Wait()
}
cond.L.Unlock()
for j := 0; j < 100; j++ {
entry.Info("info")
}
wg.Done()
}()
}
for i := 0; i < 50; i++ {
go func() {
cond.L.Lock()
for !start {
cond.Wait()
}
cond.L.Unlock()
for j := 0; j < 100; j++ {
entry.WithField("another field", "with some data").Info("info")
}
wg.Done()
}()
}
cond.L.Lock()
start = true
cond.L.Unlock()
cond.Broadcast()
wg.Wait()
}