use a mock net.PacketConn in tests

This commit is contained in:
Marten Seemann 2020-09-27 14:42:11 +07:00
parent ebe051b2cc
commit a65274942c
9 changed files with 577 additions and 442 deletions

View file

@ -3,6 +3,7 @@ package quic
import (
"net"
"github.com/golang/mock/gomock"
mocklogging "github.com/lucas-clemente/quic-go/internal/mocks/logging"
. "github.com/onsi/ginkgo"
@ -14,16 +15,19 @@ type testConn struct {
net.PacketConn
}
var _ = Describe("Client Multiplexer", func() {
var _ = Describe("Multiplexer", func() {
It("adds a new packet conn ", func() {
conn := newMockPacketConn()
conn := NewMockPacketConn(mockCtrl)
conn.EXPECT().ReadFrom(gomock.Any()).Do(func([]byte) { <-(make(chan struct{})) }).MaxTimes(1)
conn.EXPECT().LocalAddr().Return(&net.UDPAddr{IP: net.IPv4(1, 2, 3, 4), Port: 1234})
_, err := getMultiplexer().AddConn(conn, 8, nil, nil)
Expect(err).ToNot(HaveOccurred())
})
It("recognizes when the same connection is added twice", func() {
pconn := newMockPacketConn()
pconn.addr = &net.UDPAddr{IP: net.IPv4(1, 2, 3, 4), Port: 4321}
pconn := NewMockPacketConn(mockCtrl)
pconn.EXPECT().LocalAddr().Return(&net.UDPAddr{IP: net.IPv4(1, 2, 3, 4), Port: 4321}).Times(2)
pconn.EXPECT().ReadFrom(gomock.Any()).Do(func([]byte) { <-(make(chan struct{})) }).MaxTimes(1)
conn := testConn{PacketConn: pconn}
tracer := mocklogging.NewMockTracer(mockCtrl)
_, err := getMultiplexer().AddConn(conn, 8, []byte("foobar"), tracer)
@ -35,7 +39,9 @@ var _ = Describe("Client Multiplexer", func() {
})
It("errors when adding an existing conn with a different connection ID length", func() {
conn := newMockPacketConn()
conn := NewMockPacketConn(mockCtrl)
conn.EXPECT().ReadFrom(gomock.Any()).Do(func([]byte) { <-(make(chan struct{})) }).MaxTimes(1)
conn.EXPECT().LocalAddr().Return(&net.UDPAddr{IP: net.IPv4(1, 2, 3, 4), Port: 1234}).Times(2)
_, err := getMultiplexer().AddConn(conn, 5, nil, nil)
Expect(err).ToNot(HaveOccurred())
_, err = getMultiplexer().AddConn(conn, 6, nil, nil)
@ -43,7 +49,9 @@ var _ = Describe("Client Multiplexer", func() {
})
It("errors when adding an existing conn with a different stateless rest key", func() {
conn := newMockPacketConn()
conn := NewMockPacketConn(mockCtrl)
conn.EXPECT().ReadFrom(gomock.Any()).Do(func([]byte) { <-(make(chan struct{})) }).MaxTimes(1)
conn.EXPECT().LocalAddr().Return(&net.UDPAddr{IP: net.IPv4(1, 2, 3, 4), Port: 1234}).Times(2)
_, err := getMultiplexer().AddConn(conn, 7, []byte("foobar"), nil)
Expect(err).ToNot(HaveOccurred())
_, err = getMultiplexer().AddConn(conn, 7, []byte("raboof"), nil)
@ -51,7 +59,9 @@ var _ = Describe("Client Multiplexer", func() {
})
It("errors when adding an existing conn with different tracers", func() {
conn := newMockPacketConn()
conn := NewMockPacketConn(mockCtrl)
conn.EXPECT().ReadFrom(gomock.Any()).Do(func([]byte) { <-(make(chan struct{})) }).MaxTimes(1)
conn.EXPECT().LocalAddr().Return(&net.UDPAddr{IP: net.IPv4(1, 2, 3, 4), Port: 1234}).Times(2)
_, err := getMultiplexer().AddConn(conn, 7, nil, mocklogging.NewMockTracer(mockCtrl))
Expect(err).ToNot(HaveOccurred())
_, err = getMultiplexer().AddConn(conn, 7, nil, mocklogging.NewMockTracer(mockCtrl))