Debian Codegraph @

vnet/delay_filter_test.go

raw symbols SCIP
1 package vnet
2
3 import (
4 "context"
5 "testing"
6 "time"
7
8 "github.com/stretchr/testify/assert"
9 )
10
11 func TestDelayFilter(t *testing.T) {
12 t.Run("schedulesOnePacketAtATime", func(t *testing.T) {
13 nic := newMockNIC(t)
14 df, err := NewDelayFilter(nic, 10*time.Millisecond)
15 if !assert.NoError(t, err, "should succeed") {
16 return
17 }
18
19 ctx, cancel := context.WithCancel(context.Background())
20 defer cancel()
21 go df.Run(ctx)
22
23 type TimestampedChunk struct {
24 ts time.Time
25 c Chunk
26 }
27 receiveCh := make(chan TimestampedChunk)
28 nic.mockOnInboundChunk = func(c Chunk) {
29 receivedAt := time.Now()
30 receiveCh <- TimestampedChunk{
31 ts: receivedAt,
32 c: c,
33 }
34 }
35
36 lastNr := -1
37 for i := 0; i < 100; i++ {
38 sent := time.Now()
39 df.onInboundChunk(&chunkUDP{
40 chunkIP: chunkIP{timestamp: sent},
41 userData: []byte{byte(i)},
42 })
43
44 select {
45 case c := <-receiveCh:
46 nr := int(c.c.UserData()[0])
47
48 assert.Greater(t, nr, lastNr)
49 lastNr = nr
50
51 assert.Greater(t, c.ts.Sub(sent), 10*time.Millisecond)
52 case <-time.After(time.Second):
53 assert.Fail(t, "expected to receive next chunk")
54 }
55 }
56 })
57
58 t.Run("schedulesSubsequentManyPackets", func(t *testing.T) {
59 nic := newMockNIC(t)
60 df, err := NewDelayFilter(nic, 10*time.Millisecond)
61 if !assert.NoError(t, err, "should succeed") {
62 return
63 }
64
65 ctx, cancel := context.WithCancel(context.Background())
66 defer cancel()
67 go df.Run(ctx)
68
69 type TimestampedChunk struct {
70 ts time.Time
71 c Chunk
72 }
73 receiveCh := make(chan TimestampedChunk)
74 nic.mockOnInboundChunk = func(c Chunk) {
75 receivedAt := time.Now()
76 receiveCh <- TimestampedChunk{
77 ts: receivedAt,
78 c: c,
79 }
80 }
81
82 // schedule 100 chunks
83 sent := time.Now()
84 for i := 0; i < 100; i++ {
85 df.onInboundChunk(&chunkUDP{
86 chunkIP: chunkIP{timestamp: sent},
87 userData: []byte{byte(i)},
88 })
89 }
90
91 // receive 100 chunks with delay>10ms
92 for i := 0; i < 100; i++ {
93 select {
94 case c := <-receiveCh:
95 nr := int(c.c.UserData()[0])
96 assert.Equal(t, i, nr)
97 assert.Greater(t, c.ts.Sub(sent), 10*time.Millisecond)
98 case <-time.After(time.Second):
99 assert.Fail(t, "expected to receive next chunk")
100 }
101 }
102 })
103 }