ooni-probe-cli/internal/netxlite/mocks/dialer_test.go
Simone Basso 7a9499fee3
refactor(dialer): it should close idle connections (#457)
Like we did before for the resolver, a dialer should propagate the
request to close idle connections to underlying types.

See https://github.com/ooni/probe/issues/1591
2021-09-05 19:55:28 +02:00

39 lines
719 B
Go

package mocks
import (
"context"
"errors"
"net"
"testing"
)
func TestDialerDialContext(t *testing.T) {
expected := errors.New("mocked error")
d := Dialer{
MockDialContext: func(ctx context.Context, network string, address string) (net.Conn, error) {
return nil, expected
},
}
ctx := context.Background()
conn, err := d.DialContext(ctx, "tcp", "8.8.8.8:53")
if !errors.Is(err, expected) {
t.Fatal("not the error we expected")
}
if conn != nil {
t.Fatal("expected nil conn")
}
}
func TestDialerCloseIdleConnections(t *testing.T) {
var called bool
d := &Dialer{
MockCloseIdleConnections: func() {
called = true
},
}
d.CloseIdleConnections()
if !called {
t.Fatal("not called")
}
}