-
Notifications
You must be signed in to change notification settings - Fork 1
/
linkfwdfast_test.go
99 lines (84 loc) · 2.08 KB
/
linkfwdfast_test.go
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
package netem
import (
"sync"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
func TestLinkFwdFast(t *testing.T) {
// testcase describes a test case for [LinkFwdFast]
type testcase struct {
// name is the name of this test case
name string
// contains the list of frames that we should emit
emit []*Frame
// expect contains the list of frames we expect
expect []*Frame
}
var testcases = []testcase{{
name: "when we send no frame",
emit: []*Frame{},
expect: []*Frame{},
}, {
name: "when we send some frames",
emit: []*Frame{{
Deadline: time.Time{},
Flags: 0,
Payload: []byte("abcdef"),
}, {
Deadline: time.Time{},
Flags: 0,
Payload: []byte("ghi"),
}},
expect: []*Frame{{
Deadline: time.Time{},
Flags: 0,
Payload: []byte("abcdef"),
}, {
Deadline: time.Time{},
Flags: 0,
Payload: []byte("ghi"),
}},
}}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
// create the NIC from which to read
reader := NewStaticReadableNIC("eth0", tc.emit...)
// create a NIC that will collect frames
writer := NewStaticWriteableNIC("eth1")
// create the link configuration
cfg := &LinkFwdConfig{
DPIEngine: nil,
Logger: &NullLogger{},
OneWayDelay: 0,
PLR: 0,
Reader: reader,
Writer: writer,
Wg: &sync.WaitGroup{},
}
// run the link forwarding algorithm in the background
cfg.Wg.Add(1)
go LinkFwdFast(cfg)
// read the expected number of frames or timeout after a minute.
got := []*Frame{}
timer := time.NewTimer(time.Minute)
defer timer.Stop()
for len(got) < len(tc.expect) {
select {
case frame := <-writer.Frames():
got = append(got, frame)
case <-timer.C:
t.Fatal("we have been reading frames for too much time")
}
}
// tell the network stack it can shut down now.
reader.CloseNetworkStack()
// wait for the algorithm to terminate.
cfg.Wg.Wait()
// compare the frames we obtained.
if diff := cmp.Diff(tc.expect, got); diff != "" {
t.Fatal(diff)
}
})
}
}