Skip to content

Commit

Permalink
Add concurrency test
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed Mar 12, 2024
1 parent cebb87f commit 2e13353
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions bridges/sloghandler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"log/slog"
"reflect"
"runtime"
"sync"
"testing"
"testing/slogtest"
"time"
Expand Down Expand Up @@ -387,6 +388,31 @@ func value2Result(v log.Value) any {
return nil
}

func TestConcurrentSafety(t *testing.T) {
lp := noop.NewLoggerProvider()
h := sloghandler.New(lp)

const goroutineN = 10

var wg sync.WaitGroup
wg.Add(goroutineN)

for i := 0; i < goroutineN; i++ {
go func() {
defer wg.Done()

_ = h.Enabled(context.Background(), slog.LevelDebug)

r := slog.NewRecord(time.Now(), slog.LevelDebug, "msg", 0)
_ = h.Handle(context.Background(), r)

_ = h.WithAttrs([]slog.Attr{slog.Any("key", nil)})
_ = h.WithGroup("name")
}()
}
wg.Wait()
}

func BenchmarkHandler(b *testing.B) {
var (
h slog.Handler
Expand Down

0 comments on commit 2e13353

Please sign in to comment.