feat(netxlite): add CloseIdleConnections to quic dialer (#469)
Like before, do not touch the rest of the tree. Rather create compatibility types declared as legacy. We will soon be able to close idle connections for an HTTP3 transport using any kind of resolvers more easily. See https://github.com/ooni/probe/issues/1591
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
|
||||
"github.com/lucas-clemente/quic-go"
|
||||
)
|
||||
|
||||
// QUICContextDialer is a mockable netxlite.QUICContextDialer.
|
||||
//
|
||||
// DEPRECATED: please use QUICDialer.
|
||||
type QUICContextDialer struct {
|
||||
MockDialContext func(ctx context.Context, network, address string,
|
||||
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error)
|
||||
}
|
||||
|
||||
// DialContext calls MockDialContext.
|
||||
func (qcd *QUICContextDialer) DialContext(ctx context.Context, network, address string,
|
||||
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error) {
|
||||
return qcd.MockDialContext(ctx, network, address, tlsConfig, quicConfig)
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/lucas-clemente/quic-go"
|
||||
)
|
||||
|
||||
func TestQUICContextDialerDialContext(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
qcd := &QUICContextDialer{
|
||||
MockDialContext: func(ctx context.Context, network string, address string, tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error) {
|
||||
return nil, expected
|
||||
},
|
||||
}
|
||||
ctx := context.Background()
|
||||
tlsConfig := &tls.Config{}
|
||||
quicConfig := &quic.Config{}
|
||||
sess, err := qcd.DialContext(ctx, "udp", "dns.google:443", tlsConfig, quicConfig)
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("not the error we expected")
|
||||
}
|
||||
if sess != nil {
|
||||
t.Fatal("expected nil session")
|
||||
}
|
||||
}
|
||||
@@ -21,18 +21,27 @@ func (ql *QUICListener) Listen(addr *net.UDPAddr) (quicx.UDPLikeConn, error) {
|
||||
return ql.MockListen(addr)
|
||||
}
|
||||
|
||||
// QUICContextDialer is a mockable netxlite.QUICContextDialer.
|
||||
type QUICContextDialer struct {
|
||||
// QUICDialer is a mockable netxlite.QUICDialer.
|
||||
type QUICDialer struct {
|
||||
// MockDialContext allows mocking DialContext.
|
||||
MockDialContext func(ctx context.Context, network, address string,
|
||||
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error)
|
||||
|
||||
// MockCloseIdleConnections allows mocking CloseIdleConnections.
|
||||
MockCloseIdleConnections func()
|
||||
}
|
||||
|
||||
// DialContext calls MockDialContext.
|
||||
func (qcd *QUICContextDialer) DialContext(ctx context.Context, network, address string,
|
||||
func (qcd *QUICDialer) DialContext(ctx context.Context, network, address string,
|
||||
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error) {
|
||||
return qcd.MockDialContext(ctx, network, address, tlsConfig, quicConfig)
|
||||
}
|
||||
|
||||
// CloseIdleConnections calls MockCloseIdleConnections.
|
||||
func (qcd *QUICDialer) CloseIdleConnections() {
|
||||
qcd.MockCloseIdleConnections()
|
||||
}
|
||||
|
||||
// QUICEarlySession is a mockable quic.EarlySession.
|
||||
type QUICEarlySession struct {
|
||||
MockAcceptStream func(context.Context) (quic.Stream, error)
|
||||
|
||||
@@ -31,9 +31,9 @@ func TestQUICListenerListen(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestQUICContextDialerDialContext(t *testing.T) {
|
||||
func TestQUICDialerDialContext(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
qcd := &QUICContextDialer{
|
||||
qcd := &QUICDialer{
|
||||
MockDialContext: func(ctx context.Context, network string, address string, tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error) {
|
||||
return nil, expected
|
||||
},
|
||||
@@ -50,6 +50,19 @@ func TestQUICContextDialerDialContext(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestQUICDialerCloseIdleConnections(t *testing.T) {
|
||||
var called bool
|
||||
qcd := &QUICDialer{
|
||||
MockCloseIdleConnections: func() {
|
||||
called = true
|
||||
},
|
||||
}
|
||||
qcd.CloseIdleConnections()
|
||||
if !called {
|
||||
t.Fatal("not called")
|
||||
}
|
||||
}
|
||||
|
||||
func TestQUICEarlySessionAcceptStream(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
sess := &QUICEarlySession{
|
||||
|
||||
Reference in New Issue
Block a user