golang.org/x/exp@v0.0.0-20240506185415-9bf2ced13842/slog/benchmarks/handlers_test.go (about) 1 package benchmarks 2 3 import ( 4 "bytes" 5 "context" 6 "testing" 7 8 "golang.org/x/exp/slices" 9 "golang.org/x/exp/slog" 10 ) 11 12 func TestHandlers(t *testing.T) { 13 ctx := context.Background() 14 r := slog.NewRecord(TestTime, slog.LevelInfo, TestMessage, 0) 15 r.AddAttrs(TestAttrs...) 16 t.Run("text", func(t *testing.T) { 17 var b bytes.Buffer 18 h := newFastTextHandler(&b) 19 if err := h.Handle(ctx, r); err != nil { 20 t.Fatal(err) 21 } 22 got := b.String() 23 if got != WantText { 24 t.Errorf("\ngot %q\nwant %q", got, WantText) 25 } 26 }) 27 t.Run("async", func(t *testing.T) { 28 h := newAsyncHandler() 29 if err := h.Handle(ctx, r); err != nil { 30 t.Fatal(err) 31 } 32 got := h.ringBuffer[0] 33 if !got.Time.Equal(r.Time) || !slices.EqualFunc(attrSlice(got), attrSlice(r), slog.Attr.Equal) { 34 t.Errorf("got %+v, want %+v", got, r) 35 } 36 }) 37 } 38 39 func attrSlice(r slog.Record) []slog.Attr { 40 var as []slog.Attr 41 r.Attrs(func(a slog.Attr) bool { as = append(as, a); return true }) 42 return as 43 }