/
delay_filter_test.go
97 lines (85 loc) · 2.08 KB
/
delay_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
package vnet
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestDelayFilter(t *testing.T) {
t.Run("schedulesOnePacketAtATime", func(t *testing.T) {
nic := newMockNIC(t)
df, err := NewDelayFilter(nic, 10*time.Millisecond)
assert.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go df.Run(ctx)
type TimestampedChunk struct {
ts time.Time
c Chunk
}
receiveCh := make(chan TimestampedChunk)
nic.mockOnInboundChunk = func(c Chunk) {
receivedAt := time.Now()
receiveCh <- TimestampedChunk{
ts: receivedAt,
c: c,
}
}
lastNr := -1
for i := 0; i < 100; i++ {
sent := time.Now()
df.onInboundChunk(&chunkUDP{
chunkIP: chunkIP{timestamp: sent},
userData: []byte{byte(i)},
})
select {
case c := <-receiveCh:
nr := int(c.c.UserData()[0])
assert.Greater(t, nr, lastNr)
lastNr = nr
assert.Greater(t, c.ts.Sub(sent), 10*time.Millisecond)
case <-time.After(time.Second):
assert.Fail(t, "expected to receive next chunk")
}
}
})
t.Run("schedulesSubsequentManyPackets", func(t *testing.T) {
nic := newMockNIC(t)
df, err := NewDelayFilter(nic, 10*time.Millisecond)
assert.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go df.Run(ctx)
type TimestampedChunk struct {
ts time.Time
c Chunk
}
receiveCh := make(chan TimestampedChunk)
nic.mockOnInboundChunk = func(c Chunk) {
receivedAt := time.Now()
receiveCh <- TimestampedChunk{
ts: receivedAt,
c: c,
}
}
// schedule 100 chunks
sent := time.Now()
for i := 0; i < 100; i++ {
df.onInboundChunk(&chunkUDP{
chunkIP: chunkIP{timestamp: sent},
userData: []byte{byte(i)},
})
}
// receive 100 chunks with delay>10ms
for i := 0; i < 100; i++ {
select {
case c := <-receiveCh:
nr := int(c.c.UserData()[0])
assert.Equal(t, i, nr)
assert.Greater(t, c.ts.Sub(sent), 10*time.Millisecond)
case <-time.After(time.Second):
assert.Fail(t, "expected to receive next chunk")
}
}
})
}