-
Notifications
You must be signed in to change notification settings - Fork 15
/
conn_test.go
170 lines (141 loc) · 3.99 KB
/
conn_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
package quicconn
import (
"context"
"crypto/tls"
"errors"
"net"
"time"
quic "github.com/lucas-clemente/quic-go"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type mockSession struct {
remoteAddr net.Addr
localAddr net.Addr
streamToAccept quic.Stream
acceptError error
streamToOpen quic.Stream
openError error
closed bool
closedWithError string
}
func (m *mockSession) AcceptStream(context.Context) (quic.Stream, error) {
if m.acceptError != nil {
return nil, m.acceptError
}
// AcceptStream blocks until a stream is available
if m.streamToAccept == nil {
time.Sleep(time.Hour)
}
return m.streamToAccept, nil
}
func (m *mockSession) OpenStream() (quic.Stream, error) {
if m.openError != nil {
return nil, m.openError
}
return m.streamToOpen, nil
}
func (m *mockSession) OpenStreamSync(context.Context) (quic.Stream, error) {
return m.streamToOpen, nil
}
func (m *mockSession) LocalAddr() net.Addr {
return m.localAddr
}
func (m *mockSession) RemoteAddr() net.Addr {
return m.remoteAddr
}
func (m *mockSession) CloseWithError(_ quic.ErrorCode, e string) error {
m.closedWithError = e
m.closed = true
return nil
}
func (m *mockSession) Close() error {
return m.CloseWithError(0, "")
}
func (m *mockSession) AcceptUniStream(context.Context) (quic.ReceiveStream, error) {
panic("not implemented")
}
func (m *mockSession) OpenUniStream() (quic.SendStream, error) { panic("not implemented") }
func (m *mockSession) OpenUniStreamSync(context.Context) (quic.SendStream, error) {
panic("not implemented")
}
func (m *mockSession) ConnectionState() tls.ConnectionState { panic("not implemented") }
func (m *mockSession) Context() context.Context { panic("not implemented") }
var _ quic.Session = &mockSession{}
var _ = Describe("Conn", func() {
var (
c *conn
sess *mockSession
sendStream *mockStream
receiveStream *mockStream
)
BeforeEach(func() {
var err error
receiveStream = &mockStream{}
sendStream = &mockStream{}
sess = &mockSession{
streamToOpen: sendStream,
}
c, err = newConn(sess)
Expect(err).ToNot(HaveOccurred())
})
It("errors when the send stream can't be opened", func() {
testErr := errors.New("test error")
sess.openError = testErr
_, err := newConn(sess)
Expect(err).To(MatchError(testErr))
})
It("returns the remote address", func() {
addr := &net.UDPAddr{IP: net.IPv4(127, 0, 0, 2), Port: 7331}
sess.remoteAddr = addr
Expect(c.RemoteAddr()).To(Equal(addr))
})
It("returns the local address", func() {
addr := &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 1337}
sess.localAddr = addr
Expect(c.LocalAddr()).To(Equal(addr))
})
It("writes data", func() {
n, err := c.Write([]byte("foobar"))
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(6))
Expect(sendStream.dataWritten.Bytes()).To(Equal([]byte("foobar")))
})
It("waits with reading until a stream can be accepted", func() {
var readReturned bool
go func() {
defer GinkgoRecover()
_, err := c.Read(make([]byte, 1))
Expect(err).ToNot(HaveOccurred())
readReturned = true
}()
Consistently(func() bool { return readReturned }).Should(BeFalse())
})
It("errors if accepting the stream fails", func() {
testErr := errors.New("test error")
sess.acceptError = testErr
_, err := c.Read(make([]byte, 1))
Expect(err).To(MatchError(testErr))
})
It("immediately closes the receive stream", func() {
receiveStream.dataToRead.Write([]byte("foobar"))
sess.streamToAccept = receiveStream
_, err := c.Read(make([]byte, 1))
Expect(err).ToNot(HaveOccurred())
Expect(receiveStream.closed).To(BeTrue())
})
It("reads data", func() {
receiveStream.dataToRead.Write([]byte("foobar"))
sess.streamToAccept = receiveStream
data := make([]byte, 10)
n, err := c.Read(data)
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(6))
Expect(data).To(ContainSubstring("foobar"))
})
It("closes", func() {
c.Close()
Expect(sess.closed).To(BeTrue())
Expect(sess.closedWithError).To(BeEmpty())
})
})