diff --git a/ringchan.go b/ringchan.go index bd9262a..69f7755 100644 --- a/ringchan.go +++ b/ringchan.go @@ -3,6 +3,9 @@ package ringchan type Ring[T any] struct { // C is the channel to receive values from. C <-chan T + + // Number of dropped items due to full buffer. + Dropped int } // New creates a ring-buffered channel with fixed capacity from incoming channel. @@ -29,6 +32,7 @@ func New[T any](in <-chan T, size int) *Ring[T] { default: out <- v } + rc.Dropped++ } } }() diff --git a/ringchan_test.go b/ringchan_test.go index ce6de36..1f3f8bc 100644 --- a/ringchan_test.go +++ b/ringchan_test.go @@ -33,6 +33,9 @@ func TestRingChanBasic(t *testing.T) { if len(got) != len(want) { t.Fatalf("expected %v values, got %v", len(want), len(got)) } + if rc.Dropped != 2 { + t.Fatalf("expected %d values to be dropped, got %d", 2, rc.Dropped) + } for i := range want { if got[i] != want[i] { t.Errorf("expected %v at index %d, got %v", want[i], i, got[i])