forked from quic-go/quic-go
-
Notifications
You must be signed in to change notification settings - Fork 3
/
send_queue_test.go
173 lines (145 loc) · 4.18 KB
/
send_queue_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
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
package quic
import (
"errors"
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("Send Queue", func() {
var q sender
var c *MockSendConn
BeforeEach(func() {
c = NewMockSendConn(mockCtrl)
q = newSendQueue(c)
})
getPacket := func(b []byte) *packetBuffer {
buf := getPacketBuffer()
buf.Data = buf.Data[:len(b)]
copy(buf.Data, b)
return buf
}
It("sends a packet", func() {
p := getPacket([]byte("foobar"))
q.Send(p)
written := make(chan struct{})
c.EXPECT().Write([]byte("foobar")).Do(func([]byte) { close(written) })
done := make(chan struct{})
go func() {
defer GinkgoRecover()
q.Run()
close(done)
}()
Eventually(written).Should(BeClosed())
q.Close()
Eventually(done).Should(BeClosed())
})
It("panics when Send() is called although there's no space in the queue", func() {
for i := 0; i < sendQueueCapacity; i++ {
Expect(q.WouldBlock()).To(BeFalse())
q.Send(getPacket([]byte("foobar")))
}
Expect(q.WouldBlock()).To(BeTrue())
Expect(func() { q.Send(getPacket([]byte("raboof"))) }).To(Panic())
})
It("signals when sending is possible again", func() {
Expect(q.WouldBlock()).To(BeFalse())
q.Send(getPacket([]byte("foobar1")))
Consistently(q.Available()).ShouldNot(Receive())
// now start sending out packets. This should free up queue space.
c.EXPECT().Write(gomock.Any()).MinTimes(1).MaxTimes(2)
done := make(chan struct{})
go func() {
defer GinkgoRecover()
q.Run()
close(done)
}()
Eventually(q.Available()).Should(Receive())
Expect(q.WouldBlock()).To(BeFalse())
Expect(func() { q.Send(getPacket([]byte("foobar2"))) }).ToNot(Panic())
q.Close()
Eventually(done).Should(BeClosed())
})
It("signals when sending is possible again, when the first write succeeded", func() {
write := make(chan struct{}, 1)
written := make(chan struct{}, 100)
// now start sending out packets. This should free up queue space.
c.EXPECT().Write(gomock.Any()).DoAndReturn(func(b []byte) error {
written <- struct{}{}
<-write
return nil
}).AnyTimes()
// allow the first packet to be sent immediately
write <- struct{}{}
done := make(chan struct{})
go func() {
defer GinkgoRecover()
q.Run()
close(done)
}()
q.Send(getPacket([]byte("foobar")))
<-written
// now fill up the send queue
for i := 0; i < sendQueueCapacity; i++ {
Expect(q.WouldBlock()).To(BeFalse())
q.Send(getPacket([]byte("foobar")))
}
// One more packet is queued when it's picked up by Run and written to the connection.
// In this test, it's blocked on write channel in the mocked Write call.
<-written
Eventually(q.WouldBlock()).Should(BeFalse())
q.Send(getPacket([]byte("foobar")))
Expect(q.WouldBlock()).To(BeTrue())
Consistently(q.Available()).ShouldNot(Receive())
write <- struct{}{}
Eventually(q.Available()).Should(Receive())
// test shutdown
for i := 0; i < sendQueueCapacity; i++ {
write <- struct{}{}
}
q.Close()
Eventually(done).Should(BeClosed())
})
It("does not block pending send after the queue has stopped running", func() {
done := make(chan struct{})
go func() {
defer GinkgoRecover()
q.Run()
close(done)
}()
// the run loop exits if there is a write error
testErr := errors.New("test error")
c.EXPECT().Write(gomock.Any()).Return(testErr)
q.Send(getPacket([]byte("foobar")))
Eventually(done).Should(BeClosed())
sent := make(chan struct{})
go func() {
defer GinkgoRecover()
q.Send(getPacket([]byte("raboof")))
q.Send(getPacket([]byte("quux")))
close(sent)
}()
Eventually(sent).Should(BeClosed())
})
It("blocks Close() until the packet has been sent out", func() {
written := make(chan []byte)
c.EXPECT().Write(gomock.Any()).Do(func(p []byte) { written <- p })
done := make(chan struct{})
go func() {
defer GinkgoRecover()
q.Run()
close(done)
}()
q.Send(getPacket([]byte("foobar")))
closed := make(chan struct{})
go func() {
defer GinkgoRecover()
q.Close()
close(closed)
}()
Consistently(closed).ShouldNot(BeClosed())
// now write the packet
Expect(written).To(Receive())
Eventually(done).Should(BeClosed())
Eventually(closed).Should(BeClosed())
})
})