diff options
| author | Paul Buetow <paul@buetow.org> | 2026-02-25 08:24:28 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-02-25 08:24:28 +0200 |
| commit | 615f305f308c849c1a7b6cadb93e5e520455e422 (patch) | |
| tree | 43fa082c8f7433f2ea3f1987e29a62226776459b | |
| parent | 78e9de77d8c443fe8dbe560b5ccb168a6142eb55 (diff) | |
Add TUI StreamEvent constructor and tests
| -rw-r--r-- | go.mod | 1 | ||||
| -rw-r--r-- | go.sum | 2 | ||||
| -rw-r--r-- | integrationtests/readwrite_test.go | 1 | ||||
| -rw-r--r-- | internal/tui/eventstream/streamevent.go | 43 | ||||
| -rw-r--r-- | internal/tui/eventstream/streamevent_test.go | 87 |
5 files changed, 134 insertions, 0 deletions
@@ -29,6 +29,7 @@ require ( github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect github.com/muesli/cancelreader v0.2.2 // indirect github.com/muesli/termenv v0.16.0 // indirect + github.com/pelletier/go-toml/v2 v2.2.4 // indirect github.com/rivo/uniseg v0.4.7 // indirect github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect golang.org/x/sys v0.38.0 // indirect @@ -46,6 +46,8 @@ github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELU github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo= github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc= github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk= +github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= +github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= diff --git a/integrationtests/readwrite_test.go b/integrationtests/readwrite_test.go index e7492ec..7251d51 100644 --- a/integrationtests/readwrite_test.go +++ b/integrationtests/readwrite_test.go @@ -2,6 +2,7 @@ package integrationtests import "testing" +// TODO: Also test how much bytes were read and writen func TestReadwriteBasic(t *testing.T) { runScenario(t, "readwrite-basic", []ExpectedEvent{ { diff --git a/internal/tui/eventstream/streamevent.go b/internal/tui/eventstream/streamevent.go new file mode 100644 index 0000000..85ea217 --- /dev/null +++ b/internal/tui/eventstream/streamevent.go @@ -0,0 +1,43 @@ +package eventstream + +import ( + "ior/internal/event" + "ior/internal/types" +) + +type StreamEvent struct { + Seq uint64 + TimeNs uint64 + Syscall string + Comm string + PID uint32 + TID uint32 + FileName string + DurationNs uint64 + GapNs uint64 + Bytes uint64 + RetVal int64 + IsError bool +} + +func NewStreamEvent(seq uint64, pair *event.Pair) StreamEvent { + e := StreamEvent{ + Seq: seq, + TimeNs: pair.EnterEv.GetTime(), + Syscall: pair.EnterEv.GetTraceId().Name(), + Comm: pair.Comm, + PID: pair.EnterEv.GetPid(), + TID: pair.EnterEv.GetTid(), + FileName: pair.FileName(), + DurationNs: pair.Duration, + GapNs: pair.DurationToPrev, + Bytes: pair.Bytes, + } + + if retEv, ok := pair.ExitEv.(*types.RetEvent); ok { + e.RetVal = retEv.Ret + e.IsError = retEv.Ret < 0 + } + + return e +} diff --git a/internal/tui/eventstream/streamevent_test.go b/internal/tui/eventstream/streamevent_test.go new file mode 100644 index 0000000..d053072 --- /dev/null +++ b/internal/tui/eventstream/streamevent_test.go @@ -0,0 +1,87 @@ +package eventstream + +import ( + "ior/internal/event" + "ior/internal/file" + "ior/internal/types" + "testing" +) + +func TestNewStreamEventPopulatesFields(t *testing.T) { + enter := &types.OpenEvent{TraceId: types.SYS_ENTER_OPENAT, Time: 1234, Pid: 42, Tid: 84} + exit := &types.RetEvent{TraceId: types.SYS_EXIT_OPENAT, Time: 1300, Ret: -2, Pid: 42, Tid: 84} + pair := event.NewPair(enter) + pair.ExitEv = exit + pair.File = file.NewFd(7, "/tmp/test.txt", 0) + pair.Comm = "cat" + pair.Duration = 66 + pair.DurationToPrev = 19 + pair.Bytes = 512 + + got := NewStreamEvent(9, pair) + + if got.Seq != 9 { + t.Fatalf("Seq = %d, want 9", got.Seq) + } + if got.TimeNs != 1234 { + t.Fatalf("TimeNs = %d, want 1234", got.TimeNs) + } + if got.Syscall != "openat" { + t.Fatalf("Syscall = %q, want openat", got.Syscall) + } + if got.Comm != "cat" { + t.Fatalf("Comm = %q, want cat", got.Comm) + } + if got.PID != 42 || got.TID != 84 { + t.Fatalf("PID/TID = %d/%d, want 42/84", got.PID, got.TID) + } + if got.FileName != "/tmp/test.txt" { + t.Fatalf("FileName = %q, want /tmp/test.txt", got.FileName) + } + if got.DurationNs != 66 || got.GapNs != 19 || got.Bytes != 512 { + t.Fatalf("DurationNs/GapNs/Bytes = %d/%d/%d, want 66/19/512", got.DurationNs, got.GapNs, got.Bytes) + } + if got.RetVal != -2 { + t.Fatalf("RetVal = %d, want -2", got.RetVal) + } + if !got.IsError { + t.Fatalf("IsError = false, want true") + } +} + +func TestNewStreamEventCopiesBeforeRecycle(t *testing.T) { + enter := &types.OpenEvent{TraceId: types.SYS_ENTER_READ, Time: 2000, Pid: 1, Tid: 2} + exit := &types.RetEvent{TraceId: types.SYS_EXIT_READ, Time: 2010, Ret: 8, Pid: 1, Tid: 2} + pair := event.NewPair(enter) + pair.ExitEv = exit + pair.File = file.NewFd(3, "/tmp/in.txt", 0) + pair.Comm = "reader" + pair.Duration = 10 + pair.DurationToPrev = 5 + pair.Bytes = 8 + + got := NewStreamEvent(1, pair) + pair.Recycle() + + if got.Syscall != "read" || got.Comm != "reader" || got.FileName != "/tmp/in.txt" { + t.Fatalf("event changed after recycle: %+v", got) + } + if got.RetVal != 8 || got.IsError { + t.Fatalf("RetVal/IsError = %d/%v, want 8/false", got.RetVal, got.IsError) + } +} + +func TestNewStreamEventWithoutRetEvent(t *testing.T) { + enter := &types.NullEvent{TraceId: types.SYS_ENTER_SYNC, Time: 10, Pid: 1, Tid: 1} + exit := &types.NullEvent{TraceId: types.SYS_EXIT_SYNC, Time: 11, Pid: 1, Tid: 1} + pair := event.NewPair(enter) + pair.ExitEv = exit + + got := NewStreamEvent(3, pair) + if got.RetVal != 0 { + t.Fatalf("RetVal = %d, want 0", got.RetVal) + } + if got.IsError { + t.Fatalf("IsError = true, want false") + } +} |
