mirror of
https://github.com/refraction-networking/uquic.git
synced 2025-04-04 20:57:36 +03:00
also use the multiplexer for the server
This commit is contained in:
parent
c8d20e86d7
commit
ad5a3e2fa0
15 changed files with 631 additions and 512 deletions
|
@ -2,6 +2,7 @@ package quic
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
|
@ -18,66 +19,38 @@ var _ = Describe("Packet Handler Map", func() {
|
|||
conn *mockPacketConn
|
||||
)
|
||||
|
||||
getPacket := func(connID protocol.ConnectionID) []byte {
|
||||
buf := &bytes.Buffer{}
|
||||
err := (&wire.Header{
|
||||
DestConnectionID: connID,
|
||||
PacketNumberLen: protocol.PacketNumberLen1,
|
||||
}).Write(buf, protocol.PerspectiveServer, protocol.VersionWhatever)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
BeforeEach(func() {
|
||||
conn = newMockPacketConn()
|
||||
handler = newPacketHandlerMap(conn, 5, utils.DefaultLogger, true).(*packetHandlerMap)
|
||||
})
|
||||
|
||||
It("adds and gets", func() {
|
||||
connID := protocol.ConnectionID{1, 2, 3, 4, 5}
|
||||
sess := &mockSession{}
|
||||
handler.Add(connID, sess)
|
||||
session, ok := handler.Get(connID)
|
||||
Expect(ok).To(BeTrue())
|
||||
Expect(session).To(Equal(sess))
|
||||
})
|
||||
|
||||
It("deletes", func() {
|
||||
connID := protocol.ConnectionID{1, 2, 3, 4, 5}
|
||||
handler.Add(connID, &mockSession{})
|
||||
handler.Remove(connID)
|
||||
session, ok := handler.Get(connID)
|
||||
Expect(ok).To(BeTrue())
|
||||
Expect(session).To(BeNil())
|
||||
})
|
||||
|
||||
It("deletes nil session entries after a wait time", func() {
|
||||
handler.deleteClosedSessionsAfter = 25 * time.Millisecond
|
||||
connID := protocol.ConnectionID{1, 2, 3, 4, 5}
|
||||
handler.Add(connID, &mockSession{})
|
||||
handler.Remove(connID)
|
||||
Eventually(func() bool {
|
||||
_, ok := handler.Get(connID)
|
||||
return ok
|
||||
}).Should(BeFalse())
|
||||
handler = newPacketHandlerMap(conn, 5, utils.DefaultLogger).(*packetHandlerMap)
|
||||
})
|
||||
|
||||
It("closes", func() {
|
||||
sess1 := NewMockQuicSession(mockCtrl)
|
||||
sess1.EXPECT().Close()
|
||||
sess2 := NewMockQuicSession(mockCtrl)
|
||||
sess2.EXPECT().Close()
|
||||
testErr := errors.New("test error ")
|
||||
sess1 := NewMockPacketHandler(mockCtrl)
|
||||
sess1.EXPECT().destroy(testErr)
|
||||
sess2 := NewMockPacketHandler(mockCtrl)
|
||||
sess2.EXPECT().destroy(testErr)
|
||||
handler.Add(protocol.ConnectionID{1, 1, 1, 1}, sess1)
|
||||
handler.Add(protocol.ConnectionID{2, 2, 2, 2}, sess2)
|
||||
handler.Close()
|
||||
handler.close(testErr)
|
||||
})
|
||||
|
||||
Context("handling packets", func() {
|
||||
getPacket := func(connID protocol.ConnectionID) []byte {
|
||||
buf := &bytes.Buffer{}
|
||||
err := (&wire.Header{
|
||||
DestConnectionID: connID,
|
||||
PacketNumberLen: protocol.PacketNumberLen1,
|
||||
}).Write(buf, protocol.PerspectiveServer, protocol.VersionWhatever)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
It("handles packets for different packet handlers on the same packet conn", func() {
|
||||
connID1 := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
|
||||
connID2 := protocol.ConnectionID{8, 7, 6, 5, 4, 3, 2, 1}
|
||||
packetHandler1 := NewMockQuicSession(mockCtrl)
|
||||
packetHandler2 := NewMockQuicSession(mockCtrl)
|
||||
packetHandler1 := NewMockPacketHandler(mockCtrl)
|
||||
packetHandler2 := NewMockPacketHandler(mockCtrl)
|
||||
handledPacket1 := make(chan struct{})
|
||||
handledPacket2 := make(chan struct{})
|
||||
packetHandler1.EXPECT().handlePacket(gomock.Any()).Do(func(p *receivedPacket) {
|
||||
|
@ -85,11 +58,13 @@ var _ = Describe("Packet Handler Map", func() {
|
|||
close(handledPacket1)
|
||||
})
|
||||
packetHandler1.EXPECT().GetVersion()
|
||||
packetHandler1.EXPECT().GetPerspective().Return(protocol.PerspectiveClient)
|
||||
packetHandler2.EXPECT().handlePacket(gomock.Any()).Do(func(p *receivedPacket) {
|
||||
Expect(p.header.DestConnectionID).To(Equal(connID2))
|
||||
close(handledPacket2)
|
||||
})
|
||||
packetHandler2.EXPECT().GetVersion()
|
||||
packetHandler2.EXPECT().GetPerspective().Return(protocol.PerspectiveClient)
|
||||
handler.Add(connID1, packetHandler1)
|
||||
handler.Add(connID2, packetHandler2)
|
||||
|
||||
|
@ -99,8 +74,8 @@ var _ = Describe("Packet Handler Map", func() {
|
|||
Eventually(handledPacket2).Should(BeClosed())
|
||||
|
||||
// makes the listen go routine return
|
||||
packetHandler1.EXPECT().Close().AnyTimes()
|
||||
packetHandler2.EXPECT().Close().AnyTimes()
|
||||
packetHandler1.EXPECT().destroy(gomock.Any()).AnyTimes()
|
||||
packetHandler2.EXPECT().destroy(gomock.Any()).AnyTimes()
|
||||
close(conn.dataToRead)
|
||||
})
|
||||
|
||||
|
@ -110,10 +85,20 @@ var _ = Describe("Packet Handler Map", func() {
|
|||
Expect(err.Error()).To(ContainSubstring("error parsing invariant header:"))
|
||||
})
|
||||
|
||||
It("deletes nil session entries after a wait time", func() {
|
||||
handler.deleteClosedSessionsAfter = 10 * time.Millisecond
|
||||
connID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
|
||||
handler.Add(connID, NewMockPacketHandler(mockCtrl))
|
||||
handler.Remove(connID)
|
||||
Eventually(func() error {
|
||||
return handler.handlePacket(nil, getPacket(connID))
|
||||
}).Should(MatchError("received a packet with an unexpected connection ID 0x0102030405060708"))
|
||||
})
|
||||
|
||||
It("ignores packets arriving late for closed sessions", func() {
|
||||
handler.deleteClosedSessionsAfter = time.Hour
|
||||
connID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
|
||||
handler.Add(connID, NewMockQuicSession(mockCtrl))
|
||||
handler.Add(connID, NewMockPacketHandler(mockCtrl))
|
||||
handler.Remove(connID)
|
||||
err := handler.handlePacket(nil, getPacket(connID))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
@ -127,8 +112,9 @@ var _ = Describe("Packet Handler Map", func() {
|
|||
|
||||
It("errors on packets that are smaller than the Payload Length in the packet header", func() {
|
||||
connID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
|
||||
packetHandler := NewMockQuicSession(mockCtrl)
|
||||
packetHandler := NewMockPacketHandler(mockCtrl)
|
||||
packetHandler.EXPECT().GetVersion().Return(versionIETFFrames)
|
||||
packetHandler.EXPECT().GetPerspective().Return(protocol.PerspectiveClient)
|
||||
handler.Add(connID, packetHandler)
|
||||
hdr := &wire.Header{
|
||||
IsLongHeader: true,
|
||||
|
@ -148,8 +134,9 @@ var _ = Describe("Packet Handler Map", func() {
|
|||
|
||||
It("cuts packets at the Payload Length", func() {
|
||||
connID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
|
||||
packetHandler := NewMockQuicSession(mockCtrl)
|
||||
packetHandler := NewMockPacketHandler(mockCtrl)
|
||||
packetHandler.EXPECT().GetVersion().Return(versionIETFFrames)
|
||||
packetHandler.EXPECT().GetPerspective().Return(protocol.PerspectiveClient)
|
||||
handler.Add(connID, packetHandler)
|
||||
packetHandler.EXPECT().handlePacket(gomock.Any()).Do(func(p *receivedPacket) {
|
||||
Expect(p.data).To(HaveLen(456))
|
||||
|
@ -172,8 +159,9 @@ var _ = Describe("Packet Handler Map", func() {
|
|||
|
||||
It("closes the packet handlers when reading from the conn fails", func() {
|
||||
done := make(chan struct{})
|
||||
packetHandler := NewMockQuicSession(mockCtrl)
|
||||
packetHandler.EXPECT().Close().Do(func() {
|
||||
packetHandler := NewMockPacketHandler(mockCtrl)
|
||||
packetHandler.EXPECT().destroy(gomock.Any()).Do(func(e error) {
|
||||
Expect(e).To(HaveOccurred())
|
||||
close(done)
|
||||
})
|
||||
handler.Add(protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}, packetHandler)
|
||||
|
@ -181,4 +169,38 @@ var _ = Describe("Packet Handler Map", func() {
|
|||
Eventually(done).Should(BeClosed())
|
||||
})
|
||||
})
|
||||
|
||||
Context("running a server", func() {
|
||||
It("adds a server", func() {
|
||||
connID := protocol.ConnectionID{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}
|
||||
p := getPacket(connID)
|
||||
server := NewMockUnknownPacketHandler(mockCtrl)
|
||||
server.EXPECT().handlePacket(gomock.Any()).Do(func(p *receivedPacket) {
|
||||
Expect(p.header.DestConnectionID).To(Equal(connID))
|
||||
})
|
||||
handler.SetServer(server)
|
||||
Expect(handler.handlePacket(nil, p)).To(Succeed())
|
||||
})
|
||||
|
||||
It("closes all server sessions", func() {
|
||||
clientSess := NewMockPacketHandler(mockCtrl)
|
||||
clientSess.EXPECT().GetPerspective().Return(protocol.PerspectiveClient)
|
||||
serverSess := NewMockPacketHandler(mockCtrl)
|
||||
serverSess.EXPECT().GetPerspective().Return(protocol.PerspectiveServer)
|
||||
serverSess.EXPECT().Close()
|
||||
|
||||
handler.Add(protocol.ConnectionID{1, 1, 1, 1}, clientSess)
|
||||
handler.Add(protocol.ConnectionID{2, 2, 2, 2}, serverSess)
|
||||
handler.CloseServer()
|
||||
})
|
||||
|
||||
It("stops handling packets with unknown connection IDs after the server is closed", func() {
|
||||
connID := protocol.ConnectionID{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}
|
||||
p := getPacket(connID)
|
||||
server := NewMockUnknownPacketHandler(mockCtrl)
|
||||
handler.SetServer(server)
|
||||
handler.CloseServer()
|
||||
Expect(handler.handlePacket(nil, p)).To(MatchError("received a packet with an unexpected connection ID 0x1122334455667788"))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue