|
| 1 | +package slogmulti |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "log/slog" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestFanoutIncrementalBuildFlattensHandlers(t *testing.T) { |
| 10 | + t.Parallel() |
| 11 | + |
| 12 | + h1 := slog.NewJSONHandler(io.Discard, nil) |
| 13 | + h2 := slog.NewJSONHandler(io.Discard, nil) |
| 14 | + h3 := slog.NewJSONHandler(io.Discard, nil) |
| 15 | + |
| 16 | + var handler slog.Handler = h1 |
| 17 | + handler = Fanout(handler, h2) |
| 18 | + handler = Fanout(handler, h3) |
| 19 | + |
| 20 | + fanout, ok := handler.(*FanoutHandler) |
| 21 | + if !ok { |
| 22 | + t.Fatalf("expected FanoutHandler, got %T", handler) |
| 23 | + } |
| 24 | + if len(fanout.handlers) != 3 { |
| 25 | + t.Fatalf("expected 3 handlers, got %d", len(fanout.handlers)) |
| 26 | + } |
| 27 | + if fanout.handlers[0] != h1 || fanout.handlers[1] != h2 || fanout.handlers[2] != h3 { |
| 28 | + t.Fatalf("handlers were not flattened correctly") |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func TestFanoutFlattensMixedNestedHandlers(t *testing.T) { |
| 33 | + t.Parallel() |
| 34 | + |
| 35 | + h1 := slog.NewJSONHandler(io.Discard, nil) |
| 36 | + h2 := slog.NewJSONHandler(io.Discard, nil) |
| 37 | + h3 := slog.NewJSONHandler(io.Discard, nil) |
| 38 | + h4 := slog.NewJSONHandler(io.Discard, nil) |
| 39 | + |
| 40 | + handler := Fanout(Fanout(h1, h2), h3, Fanout(h4)) |
| 41 | + |
| 42 | + fanout, ok := handler.(*FanoutHandler) |
| 43 | + if !ok { |
| 44 | + t.Fatalf("expected FanoutHandler, got %T", handler) |
| 45 | + } |
| 46 | + if len(fanout.handlers) != 4 { |
| 47 | + t.Fatalf("expected 4 handlers, got %d", len(fanout.handlers)) |
| 48 | + } |
| 49 | + |
| 50 | + if fanout.handlers[0] != h1 || |
| 51 | + fanout.handlers[1] != h2 || |
| 52 | + fanout.handlers[2] != h3 || |
| 53 | + fanout.handlers[3] != h4 { |
| 54 | + t.Fatalf("handlers were not flattened correctly") |
| 55 | + } |
| 56 | +} |
0 commit comments