MCPcopy
hub / github.com/gofiber/fiber / Test_ContextTemplate_ConcurrentRegistration

Function Test_ContextTemplate_ConcurrentRegistration

log/context_test.go:347–399  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

345// than relying on a sibling test having already done so, which matters when
346// the test runner is invoked with -shuffle=on.
347func 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
401func Benchmark_ContextTemplate_Execute(b *testing.B) {
402 tmpl, err := logtemplate.Build[any, ContextData]("[${requestid}] ", map[string]ContextTagFunc{

Callers

nothing calls this directly

Calls 10

MustSetContextTemplateFunction · 0.85
SetContextTemplateFunction · 0.85
RegisterContextTagFunction · 0.70
WriteStringMethod · 0.65
AddMethod · 0.65
DoneMethod · 0.65
LoadMethod · 0.65
GetMethod · 0.65
ExecuteMethod · 0.65
PutMethod · 0.65

Tested by

no test coverage detected