summaryrefslogtreecommitdiff
path: root/internal/streamrow
diff options
context:
space:
mode:
Diffstat (limited to 'internal/streamrow')
-rw-r--r--internal/streamrow/ringbuffer_test.go104
-rw-r--r--internal/streamrow/row_test.go61
2 files changed, 165 insertions, 0 deletions
diff --git a/internal/streamrow/ringbuffer_test.go b/internal/streamrow/ringbuffer_test.go
new file mode 100644
index 0000000..91f3f7d
--- /dev/null
+++ b/internal/streamrow/ringbuffer_test.go
@@ -0,0 +1,104 @@
+package streamrow
+
+import (
+ "testing"
+)
+
+// TestRingBufferPushAndSnapshot verifies that pushed rows are retrievable in
+// insertion order.
+func TestRingBufferPushAndSnapshot(t *testing.T) {
+ rb := NewRingBuffer()
+
+ if got := rb.Len(); got != 0 {
+ t.Fatalf("expected empty buffer, got len=%d", got)
+ }
+
+ rows := []Row{
+ {Seq: 1, Syscall: "read"},
+ {Seq: 2, Syscall: "write"},
+ {Seq: 3, Syscall: "openat"},
+ }
+ for _, r := range rows {
+ rb.Push(r)
+ }
+
+ if got := rb.Len(); got != 3 {
+ t.Fatalf("expected len=3, got %d", got)
+ }
+ if got := rb.TotalPushed(); got != 3 {
+ t.Fatalf("expected totalPushed=3, got %d", got)
+ }
+
+ snap := rb.Snapshot()
+ if len(snap) != 3 {
+ t.Fatalf("expected snapshot len 3, got %d", len(snap))
+ }
+ for i, want := range rows {
+ if snap[i].Seq != want.Seq || snap[i].Syscall != want.Syscall {
+ t.Fatalf("row[%d] = %+v, want %+v", i, snap[i], want)
+ }
+ }
+}
+
+// TestRingBufferWrapsAroundCapacity verifies that the ring buffer overwrites the
+// oldest entry when full and preserves insertion order in the snapshot.
+func TestRingBufferWrapsAroundCapacity(t *testing.T) {
+ rb := NewRingBuffer()
+
+ // Fill beyond capacity to force wrap-around.
+ const extra = 5
+ for i := range RingBufferCapacity + extra {
+ rb.Push(Row{Seq: uint64(i + 1)})
+ }
+
+ if got := rb.Len(); got != RingBufferCapacity {
+ t.Fatalf("expected len=%d after overflow, got %d", RingBufferCapacity, got)
+ }
+ if got := rb.TotalPushed(); got != uint64(RingBufferCapacity+extra) {
+ t.Fatalf("expected totalPushed=%d, got %d", RingBufferCapacity+extra, got)
+ }
+
+ snap := rb.Snapshot()
+ if len(snap) != RingBufferCapacity {
+ t.Fatalf("snapshot len = %d, want %d", len(snap), RingBufferCapacity)
+ }
+ // After filling cap+extra rows the oldest surviving seq should be extra+1.
+ wantFirstSeq := uint64(extra + 1)
+ if snap[0].Seq != wantFirstSeq {
+ t.Fatalf("oldest surviving seq = %d, want %d", snap[0].Seq, wantFirstSeq)
+ }
+}
+
+// TestRingBufferSnapshotOnEmpty verifies that Snapshot on an empty buffer
+// returns an empty (non-nil) slice.
+func TestRingBufferSnapshotOnEmpty(t *testing.T) {
+ rb := NewRingBuffer()
+ snap := rb.Snapshot()
+ if snap == nil {
+ t.Fatal("expected non-nil snapshot on empty buffer")
+ }
+ if len(snap) != 0 {
+ t.Fatalf("expected empty snapshot, got len=%d", len(snap))
+ }
+}
+
+// TestRingBufferReset verifies that Reset clears all rows and counters.
+func TestRingBufferReset(t *testing.T) {
+ rb := NewRingBuffer()
+ for i := range 10 {
+ rb.Push(Row{Seq: uint64(i + 1)})
+ }
+
+ rb.Reset()
+
+ if got := rb.Len(); got != 0 {
+ t.Fatalf("expected len=0 after reset, got %d", got)
+ }
+ if got := rb.TotalPushed(); got != 0 {
+ t.Fatalf("expected totalPushed=0 after reset, got %d", got)
+ }
+ snap := rb.Snapshot()
+ if len(snap) != 0 {
+ t.Fatalf("expected empty snapshot after reset, got len=%d", len(snap))
+ }
+}
diff --git a/internal/streamrow/row_test.go b/internal/streamrow/row_test.go
index 17d6c40..ea63bcc 100644
--- a/internal/streamrow/row_test.go
+++ b/internal/streamrow/row_test.go
@@ -98,3 +98,64 @@ func TestNewWarningPopulatesSyntheticWarningFields(t *testing.T) {
t.Fatalf("RetVal/IsError = %d/%v, want -1/true", got.RetVal, got.IsError)
}
}
+
+// TestRowValueAccessors verifies that all typed accessor methods return the
+// underlying field values set on a Row.
+func TestRowValueAccessors(t *testing.T) {
+ r := Row{
+ Syscall: "read",
+ Comm: "cat",
+ FileName: "/etc/hosts",
+ PID: 10,
+ TID: 11,
+ FD: 3,
+ DurationNs: 500,
+ GapNs: 200,
+ Bytes: 1024,
+ RetVal: -1,
+ IsError: true,
+ }
+
+ if r.SyscallValue() != "read" {
+ t.Fatalf("SyscallValue = %q, want read", r.SyscallValue())
+ }
+ if r.CommValue() != "cat" {
+ t.Fatalf("CommValue = %q, want cat", r.CommValue())
+ }
+ if r.FileValue() != "/etc/hosts" {
+ t.Fatalf("FileValue = %q, want /etc/hosts", r.FileValue())
+ }
+ if r.PIDValue() != 10 {
+ t.Fatalf("PIDValue = %d, want 10", r.PIDValue())
+ }
+ if r.TIDValue() != 11 {
+ t.Fatalf("TIDValue = %d, want 11", r.TIDValue())
+ }
+ if r.FDValue() != 3 {
+ t.Fatalf("FDValue = %d, want 3", r.FDValue())
+ }
+ if r.LatencyValue() != 500 {
+ t.Fatalf("LatencyValue = %d, want 500", r.LatencyValue())
+ }
+ if r.GapValue() != 200 {
+ t.Fatalf("GapValue = %d, want 200", r.GapValue())
+ }
+ if r.BytesValue() != 1024 {
+ t.Fatalf("BytesValue = %d, want 1024", r.BytesValue())
+ }
+ if r.ReturnValue() != -1 {
+ t.Fatalf("ReturnValue = %d, want -1", r.ReturnValue())
+ }
+ if !r.ErrorValue() {
+ t.Fatal("ErrorValue = false, want true")
+ }
+}
+
+// TestSequencerNilSafeNext verifies that calling Next on a nil Sequencer returns
+// 0 without panicking.
+func TestSequencerNilSafeNext(t *testing.T) {
+ var s *Sequencer
+ if got := s.Next(); got != 0 {
+ t.Fatalf("nil Sequencer.Next() = %d, want 0", got)
+ }
+}