refactor: only use shaping dialer for ndt7 and dash (#754)
See https://github.com/ooni/probe/issues/2112 for context. While there, run `go fix -fix buildtag ./...`
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
package netxlite
|
||||
|
||||
import "github.com/ooni/probe-cli/v3/internal/model"
|
||||
|
||||
// NewMaybeShapingDialer takes in input a model.Dialer and returns in output another
|
||||
// model.Dialer that MAY dial connections with I/O shaping, depending on whether
|
||||
// the user builds with or without the `-tags shaping` CLI flag.
|
||||
//
|
||||
// We typically use `-tags shaping` when running integration tests for dash and ndt7 to
|
||||
// avoiod hammering m-lab servers from the very-fast GitHub CI servers.
|
||||
//
|
||||
// See https://github.com/ooni/probe/issues/2112 for extra context.
|
||||
func NewMaybeShapingDialer(dialer model.Dialer) model.Dialer {
|
||||
return newMaybeShapingDialer(dialer)
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
//go:build !shaping
|
||||
|
||||
package netxlite
|
||||
|
||||
import (
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
)
|
||||
|
||||
func newMaybeShapingDialer(dialer model.Dialer) model.Dialer {
|
||||
return dialer
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
//go:build !shaping
|
||||
|
||||
package netxlite
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/model/mocks"
|
||||
)
|
||||
|
||||
func TestNewShapingDialer(t *testing.T) {
|
||||
in := &mocks.Dialer{}
|
||||
out := NewMaybeShapingDialer(in)
|
||||
if in != out {
|
||||
t.Fatal("expected to see the same pointer")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
//go:build shaping
|
||||
|
||||
package netxlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
)
|
||||
|
||||
func newMaybeShapingDialer(dialer model.Dialer) model.Dialer {
|
||||
return &shapingDialer{dialer}
|
||||
}
|
||||
|
||||
type shapingDialer struct {
|
||||
model.Dialer
|
||||
}
|
||||
|
||||
// DialContext implements Dialer.DialContext
|
||||
func (d *shapingDialer) DialContext(
|
||||
ctx context.Context, network, address string) (net.Conn, error) {
|
||||
conn, err := d.Dialer.DialContext(ctx, network, address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &shapingConn{Conn: conn}, nil
|
||||
}
|
||||
|
||||
type shapingConn struct {
|
||||
net.Conn
|
||||
}
|
||||
|
||||
func (c *shapingConn) Read(p []byte) (int, error) {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
return c.Conn.Read(p)
|
||||
}
|
||||
|
||||
func (c *shapingConn) Write(p []byte) (int, error) {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
return c.Conn.Write(p)
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
//go:build shaping
|
||||
|
||||
package netxlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/model/mocks"
|
||||
)
|
||||
|
||||
func TestNewShapingDialerx(t *testing.T) {
|
||||
t.Run("failure", func(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
d := &mocks.Dialer{
|
||||
MockDialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
return nil, expected
|
||||
},
|
||||
}
|
||||
shd := NewMaybeShapingDialer(d)
|
||||
conn, err := shd.DialContext(context.Background(), "tcp", "8.8.8.8:443")
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
if conn != nil {
|
||||
t.Fatal("expected nil conn")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("success", func(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
uc := &mocks.Conn{
|
||||
MockRead: func(b []byte) (int, error) {
|
||||
return 0, expected
|
||||
},
|
||||
MockWrite: func(b []byte) (int, error) {
|
||||
return 0, expected
|
||||
},
|
||||
}
|
||||
d := &mocks.Dialer{
|
||||
MockDialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
return uc, nil
|
||||
},
|
||||
}
|
||||
shd := NewMaybeShapingDialer(d)
|
||||
conn, err := shd.DialContext(context.Background(), "tcp", "8.8.8.8:443")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := conn.(*shapingConn); !ok {
|
||||
t.Fatal("not shapingConn")
|
||||
}
|
||||
validateCountAndErr := func(count int, err error) {
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
if count != 0 {
|
||||
t.Fatal("expected zero")
|
||||
}
|
||||
}
|
||||
validateCountAndErr(conn.Read(make([]byte, 16)))
|
||||
validateCountAndErr(conn.Write(make([]byte, 16)))
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user