|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package statsd_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "errors" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + csink "github.com/stablekernel/crucible/sink" |
| 12 | + statsdsink "github.com/stablekernel/crucible/sink/statsd" |
| 13 | +) |
| 14 | + |
| 15 | +// TestWithName_AppearsInFlushError verifies WithName overrides the outlet name |
| 16 | +// carried on the *sink.Error returned from a failing flush. |
| 17 | +func TestWithName_AppearsInFlushError(t *testing.T) { |
| 18 | + t.Parallel() |
| 19 | + |
| 20 | + boom := errors.New("emit failed") |
| 21 | + fc := &fakeClient{err: boom} |
| 22 | + agg := statsdsink.NewAggregator(fc, |
| 23 | + statsdsink.WithName("custom-statsd"), |
| 24 | + statsdsink.WithInterval(0), // no background loop; Flush is the only emit |
| 25 | + ) |
| 26 | + |
| 27 | + if err := agg.Sink(context.Background(), statsdsink.Metric{Type: statsdsink.TypeCount, Name: "n", Int: 1, Rate: 1}); err != nil { |
| 28 | + t.Fatalf("Sink() error = %v", err) |
| 29 | + } |
| 30 | + f, ok := agg.(csink.Flusher) |
| 31 | + if !ok { |
| 32 | + t.Fatal("Aggregator does not implement sink.Flusher") |
| 33 | + } |
| 34 | + err := f.Flush(context.Background()) |
| 35 | + if !errors.Is(err, boom) { |
| 36 | + t.Fatalf("Flush() = %v, want to wrap %v", err, boom) |
| 37 | + } |
| 38 | + var se *csink.Error |
| 39 | + if !errors.As(err, &se) || se.Outlet != "custom-statsd" { |
| 40 | + t.Fatalf("error = %+v, want Outlet=custom-statsd", se) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// TestWithName_EmptyIgnored verifies an empty name leaves the default in place. |
| 45 | +func TestWithName_EmptyIgnored(t *testing.T) { |
| 46 | + t.Parallel() |
| 47 | + |
| 48 | + boom := errors.New("emit failed") |
| 49 | + fc := &fakeClient{err: boom} |
| 50 | + agg := statsdsink.NewAggregator(fc, statsdsink.WithName(""), statsdsink.WithInterval(0)) |
| 51 | + _ = agg.Sink(context.Background(), statsdsink.Metric{Type: statsdsink.TypeCount, Name: "n", Int: 1, Rate: 1}) |
| 52 | + |
| 53 | + err := agg.(csink.Flusher).Flush(context.Background()) |
| 54 | + var se *csink.Error |
| 55 | + if !errors.As(err, &se) || se.Outlet != "statsd" { |
| 56 | + t.Fatalf("error = %+v, want default Outlet=statsd", se) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// TestWithClock_NonNilAcceptedNilIgnored verifies WithClock installs a non-nil |
| 61 | +// clock and ignores a nil one. The aggregator stays usable in both cases. |
| 62 | +func TestWithClock_NonNilAcceptedNilIgnored(t *testing.T) { |
| 63 | + t.Parallel() |
| 64 | + |
| 65 | + fixed := time.Unix(42, 0) |
| 66 | + clock := func() time.Time { return fixed } |
| 67 | + |
| 68 | + for _, tc := range []struct { |
| 69 | + name string |
| 70 | + now func() time.Time |
| 71 | + }{ |
| 72 | + {"non-nil clock", clock}, |
| 73 | + {"nil clock falls back to default", nil}, |
| 74 | + } { |
| 75 | + tc := tc |
| 76 | + t.Run(tc.name, func(t *testing.T) { |
| 77 | + t.Parallel() |
| 78 | + fc := &fakeClient{} |
| 79 | + agg := statsdsink.NewAggregator(fc, statsdsink.WithClock(tc.now), statsdsink.WithInterval(0)) |
| 80 | + if err := agg.Sink(context.Background(), statsdsink.Metric{Type: statsdsink.TypeGauge, Name: "g", Value: 1, Rate: 1}); err != nil { |
| 81 | + t.Fatalf("Sink() error = %v", err) |
| 82 | + } |
| 83 | + if err := agg.(csink.Flusher).Flush(context.Background()); err != nil { |
| 84 | + t.Fatalf("Flush() error = %v", err) |
| 85 | + } |
| 86 | + if got := len(fc.snapshot()); got != 1 { |
| 87 | + t.Fatalf("emitted %d metrics, want 1", got) |
| 88 | + } |
| 89 | + }) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// TestDial_ReturnsUsableClient verifies Dial constructs a Client over a UDP |
| 94 | +// address without requiring a live StatsD server. The datadog SDK resolves the |
| 95 | +// address lazily, so a valid host:port yields a ready client. |
| 96 | +func TestDial_ReturnsUsableClient(t *testing.T) { |
| 97 | + t.Parallel() |
| 98 | + |
| 99 | + c, err := statsdsink.Dial("127.0.0.1:8125") |
| 100 | + if err != nil { |
| 101 | + t.Fatalf("Dial() error = %v", err) |
| 102 | + } |
| 103 | + if c == nil { |
| 104 | + t.Fatal("Dial() returned a nil Client") |
| 105 | + } |
| 106 | + if err := c.Count("dial.test", 1, nil, 1); err != nil { |
| 107 | + t.Fatalf("Count() on dialed client error = %v", err) |
| 108 | + } |
| 109 | +} |
0 commit comments