TestLazyCoreRace tests concurrent access to lazyWithCore methods This is a regression test for issue #1426
(t *testing.T)
| 190 | // TestLazyCoreRace tests concurrent access to lazyWithCore methods |
| 191 | // This is a regression test for issue #1426 |
| 192 | func TestLazyCoreRace(t *testing.T) { |
| 193 | core, _ := observer.New(zapcore.InfoLevel) |
| 194 | lazyCore := zapcore.NewLazyWith(core, []zapcore.Field{ |
| 195 | makeInt64Field("lazy", 42), |
| 196 | }) |
| 197 | |
| 198 | var wg sync.WaitGroup |
| 199 | const numGoroutines = 50 |
| 200 | |
| 201 | // Test concurrent access to Enabled() method which was the source of the race |
| 202 | for i := 0; i < numGoroutines; i++ { |
| 203 | wg.Add(1) |
| 204 | go func() { |
| 205 | defer wg.Done() |
| 206 | |
| 207 | // These operations should not race |
| 208 | _ = lazyCore.Enabled(zapcore.InfoLevel) |
| 209 | _ = lazyCore.Enabled(zapcore.DebugLevel) |
| 210 | |
| 211 | // Also test other methods for good measure |
| 212 | if ce := lazyCore.Check(zapcore.Entry{Level: zapcore.InfoLevel, Message: "test"}, nil); ce != nil { |
| 213 | _ = lazyCore.Write(zapcore.Entry{Level: zapcore.InfoLevel, Message: "test"}, nil) |
| 214 | } |
| 215 | }() |
| 216 | } |
| 217 | |
| 218 | wg.Wait() |
| 219 | } |
nothing calls this directly
no test coverage detected