forked from quic-go/quic-go
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsend_queue_test.go
More file actions
207 lines (176 loc) · 4.29 KB
/
send_queue_test.go
File metadata and controls
207 lines (176 loc) · 4.29 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package quic
import (
"net"
"net/netip"
"testing"
"testing/synctest"
"time"
"github.com/quic-go/quic-go/internal/protocol"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)
func getPacketWithContents(b []byte) *packetBuffer {
buf := getPacketBuffer()
buf.Data = buf.Data[:len(b)]
copy(buf.Data, b)
return buf
}
func TestSendQueueSendOnePacket(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
mockCtrl := gomock.NewController(t)
c := NewMockSendConn(mockCtrl)
q := newSendQueue(c)
written := make(chan struct{})
c.EXPECT().Write([]byte("foobar"), uint16(10), protocol.ECT1).Do(
func([]byte, uint16, protocol.ECN) error { close(written); return nil },
)
done := make(chan struct{})
go func() {
q.Run()
close(done)
}()
q.Send(getPacketWithContents([]byte("foobar")), 10, protocol.ECT1)
synctest.Wait()
select {
case <-written:
default:
t.Fatal("write should have returned")
}
q.Close()
synctest.Wait()
select {
case <-done:
default:
t.Fatal("Run should have returned")
}
})
}
func TestSendQueueBlocking(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
mockCtrl := gomock.NewController(t)
c := NewMockSendConn(mockCtrl)
q := newSendQueue(c)
blockWrite := make(chan struct{})
written := make(chan struct{}, 1)
c.EXPECT().Write(gomock.Any(), gomock.Any(), gomock.Any()).Do(
func([]byte, uint16, protocol.ECN) error {
select {
case written <- struct{}{}:
default:
}
<-blockWrite
return nil
},
).AnyTimes()
done := make(chan struct{})
go func() {
q.Run()
close(done)
}()
// +1, since one packet will be queued in the Write call
for i := range sendQueueCapacity + 1 {
require.False(t, q.WouldBlock())
q.Send(getPacketWithContents([]byte("foobar")), 10, protocol.ECT1)
// make sure that the first packet is actually enqueued in the Write call
if i == 0 {
select {
case <-written:
case <-time.After(time.Second):
t.Fatal("timeout")
}
}
}
require.True(t, q.WouldBlock())
select {
case <-q.Available():
t.Fatal("should not be available")
default:
}
require.Panics(t, func() { q.Send(getPacketWithContents([]byte("foobar")), 10, protocol.ECT1) })
// allow one packet to be sent
blockWrite <- struct{}{}
select {
case <-written:
case <-time.After(time.Second):
t.Fatal("timeout")
}
select {
case <-q.Available():
require.False(t, q.WouldBlock())
case <-time.After(time.Second):
t.Fatal("timeout")
}
// when calling Close, all packets are first sent out
closed := make(chan struct{})
go func() {
q.Close()
close(closed)
}()
synctest.Wait()
select {
case <-closed:
t.Fatal("Close should have blocked")
default:
}
for range sendQueueCapacity {
blockWrite <- struct{}{}
}
synctest.Wait()
select {
case <-closed:
default:
t.Fatal("Close should have returned")
}
select {
case <-done:
default:
t.Fatal("Run should have returned")
}
})
}
func TestSendQueueWriteError(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
mockCtrl := gomock.NewController(t)
c := NewMockSendConn(mockCtrl)
q := newSendQueue(c)
c.EXPECT().Write(gomock.Any(), gomock.Any(), gomock.Any()).Return(assert.AnError)
q.Send(getPacketWithContents([]byte("foobar")), 6, protocol.ECNNon)
errChan := make(chan error, 1)
go func() { errChan <- q.Run() }()
synctest.Wait()
select {
case err := <-errChan:
require.ErrorIs(t, err, assert.AnError)
default:
t.Fatal("Run should have returned")
}
// further calls to Send should not block
sent := make(chan struct{})
go func() {
defer close(sent)
for range 2 * sendQueueCapacity {
q.Send(getPacketWithContents([]byte("raboof")), 6, protocol.ECNNon)
}
}()
synctest.Wait()
select {
case <-sent:
default:
t.Fatal("Send should have returned")
}
})
}
func TestSendQueueSendProbe(t *testing.T) {
mockCtrl := gomock.NewController(t)
c := NewMockSendConn(mockCtrl)
q := newSendQueue(c)
addr := &net.UDPAddr{IP: net.IPv4(42, 42, 42, 42), Port: 42}
localAddr := netip.MustParseAddr("43.43.43.43")
c.EXPECT().WriteTo([]byte("foobar"), addr, packetInfo{
addr: localAddr,
})
q.SendProbe(getPacketWithContents([]byte("foobar")), addr, packetInfo{
addr: localAddr,
})
}