Skip to content

Commit

Permalink
add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
brycekahle committed Oct 18, 2023
1 parent 8c7a0b5 commit 433565f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
21 changes: 16 additions & 5 deletions perf/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,15 @@ func TestPerfReader(t *testing.T) {
}
defer rd.Close()

outputSamples(t, events, 5)
qt.Assert(t, rd.Size(), qt.Equals, 4096)

checkRecord(t, rd)
outputSamples(t, events, 5, 5)

_, rem := checkRecord(t, rd)
qt.Assert(t, rem >= 5, qt.IsTrue, qt.Commentf("expected at least 5 Remaining"))

_, rem = checkRecord(t, rd)
qt.Assert(t, rem, qt.Equals, 0, qt.Commentf("expected zero Remaining"))

rd.SetDeadline(time.Now().Add(4 * time.Millisecond))
_, err = rd.Read()
Expand Down Expand Up @@ -155,7 +161,7 @@ func outputSamplesProg(tb testing.TB, events *ebpf.Map, sampleSizes ...byte) *eb
return prog
}

func checkRecord(tb testing.TB, rd *Reader) (id int) {
func checkRecord(tb testing.TB, rd *Reader) (id int, remaining int) {
tb.Helper()

rec, err := rd.Read()
Expand All @@ -172,7 +178,7 @@ func checkRecord(tb testing.TB, rd *Reader) (id int) {

// padding is ignored since it's value is undefined.

return int(rec.RawSample[1])
return int(rec.RawSample[1]), rec.Remaining
}

func TestPerfReaderLostSample(t *testing.T) {
Expand Down Expand Up @@ -305,8 +311,13 @@ func TestPerfReaderOverwritable(t *testing.T) {

nextID := maxEvents
for i := 0; i < maxEvents; i++ {
id := checkRecord(t, rd)
id, rem := checkRecord(t, rd)
qt.Assert(t, id, qt.Equals, nextID)
if i < maxEvents-1 {
qt.Assert(t, rem > 0, qt.IsTrue, qt.Commentf("expected non-zero Remaining"))
} else {

Check failure on line 318 in perf/reader_test.go

View workflow job for this annotation

GitHub Actions / Build and Lint

SA9003: empty branch (staticcheck)
// remaining can be zero or non-zero, depending on if the buffer has been overwritten
}
nextID--
}
}
Expand Down
13 changes: 13 additions & 0 deletions ringbuf/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func TestRingbufReader(t *testing.T) {
}
defer rd.Close()

if uint32(rd.Size()) != 2*events.MaxEntries() {
t.Errorf("expected %d Size, got %d", events.MaxEntries(), rd.Size())
}

ret, _, err := prog.Test(internal.EmptyBPFContext)
testutils.SkipIfNotSupported(t, err)
if err != nil {
Expand All @@ -77,6 +81,15 @@ func TestRingbufReader(t *testing.T) {
t.Fatal("Can't read samples:", err)
}
raw[len(record.RawSample)] = record.RawSample
if len(raw) == len(tt.want) {
if record.Remaining != 0 {
t.Errorf("expected 0 Remaining, got %d", record.Remaining)
}
} else {
if record.Remaining == 0 {
t.Error("expected non-zero Remaining, got 0")
}
}
}

if diff := cmp.Diff(tt.want, raw); diff != "" {
Expand Down

0 comments on commit 433565f

Please sign in to comment.