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
|
package quic
import (
"net"
"testing"
"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) {
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)
select {
case <-written:
case <-time.After(time.Second):
t.Fatal("timeout")
}
q.Close()
select {
case <-done:
case <-time.After(time.Second):
t.Fatal("timeout")
}
}
func TestSendQueueBlocking(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 := 0; i < sendQueueCapacity+1; i++ {
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)
}()
select {
case <-closed:
t.Fatal("Close should have blocked")
case <-time.After(scaleDuration(10 * time.Millisecond)):
}
for i := 0; i < sendQueueCapacity; i++ {
blockWrite <- struct{}{}
}
select {
case <-closed:
case <-time.After(time.Second):
t.Fatal("timeout")
}
select {
case <-done:
case <-time.After(time.Second):
t.Fatal("timeout")
}
}
func TestSendQueueWriteError(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() }()
select {
case err := <-errChan:
require.ErrorIs(t, err, assert.AnError)
case <-time.After(time.Second):
t.Fatal("timeout")
}
// further calls to Send should not block
sent := make(chan struct{})
go func() {
defer close(sent)
for i := 0; i < 2*sendQueueCapacity; i++ {
q.Send(getPacketWithContents([]byte("raboof")), 6, protocol.ECNNon)
}
}()
select {
case <-sent:
case <-time.After(time.Second):
t.Fatal("timeout")
}
}
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}
c.EXPECT().WriteTo([]byte("foobar"), addr)
q.SendProbe(getPacketWithContents([]byte("foobar")), addr)
}
|