Test_ContextTemplate_ConcurrentRegistration exercises the lock split between the atomic-pointer read path and the mutex-guarded rebuild path. It must be run with -race to be meaningful. The test is order-independent: it registers its own "tenant" tag rather than relying on a sibling test having alr
(t *testing.T)
| 345 | // than relying on a sibling test having already done so, which matters when |
| 346 | // the test runner is invoked with -shuffle=on. |
| 347 | func Test_ContextTemplate_ConcurrentRegistration(t *testing.T) { |
| 348 | t.Cleanup(func() { MustSetContextTemplate(ContextConfig{}) }) |
| 349 | |
| 350 | require.NoError(t, RegisterContextTag("tenant", func(output Buffer, _ any, _ *ContextData, _ string) (int, error) { |
| 351 | return output.WriteString("acme") |
| 352 | })) |
| 353 | require.NoError(t, SetContextTemplate(ContextConfig{Format: "[${tenant}]"})) |
| 354 | |
| 355 | const goroutines = 8 |
| 356 | const iterations = 200 |
| 357 | |
| 358 | var wg sync.WaitGroup |
| 359 | wg.Add(goroutines * 3) |
| 360 | |
| 361 | register := func(id int) { |
| 362 | defer wg.Done() |
| 363 | for range iterations { |
| 364 | //nolint:errcheck // race-stress test intentionally ignores transient errors |
| 365 | _ = RegisterContextTag("tenant", func(output Buffer, _ any, _ *ContextData, _ string) (int, error) { |
| 366 | return output.WriteString("acme") |
| 367 | }) |
| 368 | _ = id |
| 369 | } |
| 370 | } |
| 371 | reformat := func() { |
| 372 | defer wg.Done() |
| 373 | for range iterations { |
| 374 | //nolint:errcheck // race-stress test intentionally ignores transient errors |
| 375 | _ = SetContextTemplate(ContextConfig{Format: "[${tenant}]"}) |
| 376 | } |
| 377 | } |
| 378 | emit := func() { |
| 379 | defer wg.Done() |
| 380 | for range iterations { |
| 381 | tmpl := contextTemplate.Load() |
| 382 | if tmpl == nil { |
| 383 | continue |
| 384 | } |
| 385 | buf := bytebufferpool.Get() |
| 386 | //nolint:errcheck // race-stress test intentionally ignores transient errors |
| 387 | _ = tmpl.Execute(buf, context.Background(), &ContextData{}) |
| 388 | bytebufferpool.Put(buf) |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | for i := range goroutines { |
| 393 | go register(i) |
| 394 | go reformat() |
| 395 | go emit() |
| 396 | } |
| 397 | |
| 398 | wg.Wait() |
| 399 | } |
| 400 | |
| 401 | func Benchmark_ContextTemplate_Execute(b *testing.B) { |
| 402 | tmpl, err := logtemplate.Build[any, ContextData]("[${requestid}] ", map[string]ContextTagFunc{ |
nothing calls this directly
no test coverage detected