summaryrefslogtreecommitdiff
path: root/internal/tui/eventstream/streamevent_test.go
blob: a1df8a10b29f88544afdd8b735f2b3079b951eff (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package eventstream

import (
	"testing"

	"ior/internal/event"
	"ior/internal/file"
	"ior/internal/types"
)

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.FD != 7 {
		t.Fatalf("FD = %d, want 7", got.FD)
	}
	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)
	}
	if got.FD != 3 {
		t.Fatalf("FD = %d, want 3", got.FD)
	}
}

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")
	}
	if got.FD != UnknownFD {
		t.Fatalf("FD = %d, want %d", got.FD, UnknownFD)
	}
}

func TestNewWarningEventPopulatesFields(t *testing.T) {
	got := NewWarningEvent(7, "Dropped malformed event")

	if got.Syscall != "warning" {
		t.Fatalf("Syscall = %q, want warning", got.Syscall)
	}
	if got.Comm != "ior" {
		t.Fatalf("Comm = %q, want ior", got.Comm)
	}
	if got.FileName != "Dropped malformed event" {
		t.Fatalf("FileName = %q, want warning text", got.FileName)
	}
	if got.FD != UnknownFD {
		t.Fatalf("FD = %d, want %d", got.FD, UnknownFD)
	}
	if got.RetVal != -1 {
		t.Fatalf("RetVal = %d, want -1", got.RetVal)
	}
	if !got.IsError {
		t.Fatalf("IsError = false, want true")
	}
	if got.Seq != 7 || got.TimeNs == 0 {
		t.Fatalf("Seq/TimeNs = %d/%d, want 7/non-zero", got.Seq, got.TimeNs)
	}
}