summaryrefslogtreecommitdiff
path: root/internal/streamrow/ringbuffer_test.go
blob: 91f3f7dc6ff11581e6a53b5045ad0bee8b942581 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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))
	}
}