/
loss_filter_test.go
109 lines (88 loc) · 2.23 KB
/
loss_filter_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
package vnet
import (
"net"
"testing"
"github.com/pion/transport"
"github.com/stretchr/testify/assert"
)
type mockNIC struct {
mockGetInterface func(ifName string) (*transport.Interface, error)
mockOnInboundChunk func(c Chunk)
mockGetStaticIPs func() []net.IP
mockSetRouter func(r *Router) error
}
func (n *mockNIC) getInterface(ifName string) (*transport.Interface, error) {
return n.mockGetInterface(ifName)
}
func (n *mockNIC) onInboundChunk(c Chunk) {
n.mockOnInboundChunk(c)
}
func (n *mockNIC) getStaticIPs() []net.IP {
return n.mockGetStaticIPs()
}
func (n *mockNIC) setRouter(r *Router) error {
return n.mockSetRouter(r)
}
func newMockNIC(t *testing.T) *mockNIC {
return &mockNIC{
mockGetInterface: func(string) (*transport.Interface, error) {
assert.Fail(t, "unexpected call to mockGetInterface")
return nil, nil
},
mockOnInboundChunk: func(Chunk) {
assert.Fail(t, "unexpected call to mockOnInboundChunk")
},
mockGetStaticIPs: func() []net.IP {
assert.Fail(t, "unexpected call to mockGetStaticIPs")
return nil
},
mockSetRouter: func(*Router) error {
assert.Fail(t, "unexpected call to mockSetRouter")
return nil
},
}
}
func TestLossFilter(t *testing.T) {
t.Run("FullLoss", func(t *testing.T) {
mnic := newMockNIC(t)
f, err := NewLossFilter(mnic, 100)
if !assert.NoError(t, err, "should succeed") {
return
}
f.onInboundChunk(&chunkUDP{})
})
t.Run("NoLoss", func(t *testing.T) {
mnic := newMockNIC(t)
f, err := NewLossFilter(mnic, 0)
if !assert.NoError(t, err, "should succeed") {
return
}
packets := 100
received := 0
mnic.mockOnInboundChunk = func(Chunk) {
received++
}
for i := 0; i < packets; i++ {
f.onInboundChunk(&chunkUDP{})
}
assert.Equal(t, packets, received)
})
t.Run("SomeLoss", func(t *testing.T) {
mnic := newMockNIC(t)
f, err := NewLossFilter(mnic, 50)
if !assert.NoError(t, err, "should succeed") {
return
}
packets := 1000
received := 0
mnic.mockOnInboundChunk = func(Chunk) {
received++
}
for i := 0; i < packets; i++ {
f.onInboundChunk(&chunkUDP{})
}
// One of the following could technically fail, but very unlikely
assert.Less(t, 0, received)
assert.Greater(t, packets, received)
})
}