-
Notifications
You must be signed in to change notification settings - Fork 1
/
linkfwdfull_test.go
126 lines (106 loc) · 2.92 KB
/
linkfwdfull_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package netem
import (
"bytes"
"sort"
"sync"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
func TestLinkFwdFull(t *testing.T) {
// testcase describes a test case for [LinkFwdFull]
type testcase struct {
// name is the name of this test case
name string
// delay is the one-way delay to use for forwarding frames.
delay time.Duration
// contains the list of frames that we should emit
emit []*Frame
// expect contains the list of frames we expect
expect []*Frame
// expectRuntimeAtLeast is the minimum runtime we expect
// to see when running this test case
expectRuntimeAtLeast time.Duration
}
var testcases = []testcase{{
name: "when we send no frame",
delay: 0,
emit: []*Frame{},
expect: []*Frame{},
expectRuntimeAtLeast: 0,
}, {
name: "when we send some frames",
delay: time.Second,
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"),
}},
expectRuntimeAtLeast: time.Second,
}}
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: tc.delay,
PLR: 0,
Reader: reader,
Writer: writer,
Wg: &sync.WaitGroup{},
}
// save the time before starting the link
t0 := time.Now()
// run the link forwarding algorithm in the background
cfg.Wg.Add(1)
go LinkFwdFull(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()
elapsed := time.Since(t0)
if elapsed < tc.expectRuntimeAtLeast {
t.Fatal("expected runtime to be at least", tc.expectRuntimeAtLeast, "got", elapsed)
}
// sort the frames we obtained by payload because this
// forwarder may deliver them out of order
sort.SliceStable(got, func(i, j int) bool {
return bytes.Compare(got[i].Payload, got[j].Payload) < 0
})
// compare the frames we obtained.
if diff := cmp.Diff(tc.expect, got); diff != "" {
t.Fatal(diff)
}
})
}
}