Debian Codegraph @

vnet/loss_filter_test.go

raw symbols SCIP
1 package vnet
2
3 import (
4 "net"
5 "testing"
6
7 "github.com/pion/transport/v2"
8 "github.com/stretchr/testify/assert"
9 )
10
11 type mockNIC struct {
12 mockGetInterface func(ifName string) (*transport.Interface, error)
13 mockOnInboundChunk func(c Chunk)
14 mockGetStaticIPs func() []net.IP
15 mockSetRouter func(r *Router) error
16 }
17
18 func (n *mockNIC) getInterface(ifName string) (*transport.Interface, error) {
19 return n.mockGetInterface(ifName)
20 }
21
22 func (n *mockNIC) onInboundChunk(c Chunk) {
23 n.mockOnInboundChunk(c)
24 }
25
26 func (n *mockNIC) getStaticIPs() []net.IP {
27 return n.mockGetStaticIPs()
28 }
29
30 func (n *mockNIC) setRouter(r *Router) error {
31 return n.mockSetRouter(r)
32 }
33
34 func newMockNIC(t *testing.T) *mockNIC {
35 return &mockNIC{
36 mockGetInterface: func(string) (*transport.Interface, error) {
37 assert.Fail(t, "unexpected call to mockGetInterface")
38 return nil, nil
39 },
40 mockOnInboundChunk: func(Chunk) {
41 assert.Fail(t, "unexpected call to mockOnInboundChunk")
42 },
43 mockGetStaticIPs: func() []net.IP {
44 assert.Fail(t, "unexpected call to mockGetStaticIPs")
45 return nil
46 },
47 mockSetRouter: func(*Router) error {
48 assert.Fail(t, "unexpected call to mockSetRouter")
49 return nil
50 },
51 }
52 }
53
54 func TestLossFilter(t *testing.T) {
55 t.Run("FullLoss", func(t *testing.T) {
56 mnic := newMockNIC(t)
57
58 f, err := NewLossFilter(mnic, 100)
59 if !assert.NoError(t, err, "should succeed") {
60 return
61 }
62
63 f.onInboundChunk(&chunkUDP{})
64 })
65
66 t.Run("NoLoss", func(t *testing.T) {
67 mnic := newMockNIC(t)
68
69 f, err := NewLossFilter(mnic, 0)
70 if !assert.NoError(t, err, "should succeed") {
71 return
72 }
73
74 packets := 100
75 received := 0
76 mnic.mockOnInboundChunk = func(Chunk) {
77 received++
78 }
79
80 for i := 0; i < packets; i++ {
81 f.onInboundChunk(&chunkUDP{})
82 }
83
84 assert.Equal(t, packets, received)
85 })
86
87 t.Run("SomeLoss", func(t *testing.T) {
88 mnic := newMockNIC(t)
89
90 f, err := NewLossFilter(mnic, 50)
91 if !assert.NoError(t, err, "should succeed") {
92 return
93 }
94
95 packets := 1000
96 received := 0
97 mnic.mockOnInboundChunk = func(Chunk) {
98 received++
99 }
100
101 for i := 0; i < packets; i++ {
102 f.onInboundChunk(&chunkUDP{})
103 }
104
105 // One of the following could technically fail, but very unlikely
106 assert.Less(t, 0, received)
107 assert.Greater(t, packets, received)
108 })
109 }