refactor(ndt7): use netxlite rather than netx (#768)
This diff required us to move some code around, but no major change actually happened, except better tests. While there, I also slightly refactored ndt7's implementation and removed the ProxyURL setting, which was actually unused. See https://github.com/ooni/probe/issues/2121
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package bytecounter
|
||||
|
||||
//
|
||||
// model.Dialer wrappers
|
||||
//
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
)
|
||||
|
||||
// ContextAwareDialer is a model.Dialer that attempts to count bytes using
|
||||
// the MaybeWrapWithContextByteCounters function.
|
||||
//
|
||||
// Bug
|
||||
//
|
||||
// This implementation cannot properly account for the bytes that are sent by
|
||||
// persistent connections, because they stick to the counters set when the
|
||||
// connection was established. This typically means we miss the bytes sent and
|
||||
// received when submitting a measurement. Such bytes are specifically not
|
||||
// seen by the experiment specific byte counter.
|
||||
//
|
||||
// For this reason, this implementation may be heavily changed/removed
|
||||
// in the future (<- this message is now ~two years old, though).
|
||||
type ContextAwareDialer struct {
|
||||
Dialer model.Dialer
|
||||
}
|
||||
|
||||
// NewContextAwareDialer creates a new ContextAwareDialer.
|
||||
func NewContextAwareDialer(dialer model.Dialer) *ContextAwareDialer {
|
||||
return &ContextAwareDialer{Dialer: dialer}
|
||||
}
|
||||
|
||||
var _ model.Dialer = &ContextAwareDialer{}
|
||||
|
||||
// DialContext implements Dialer.DialContext
|
||||
func (d *ContextAwareDialer) DialContext(
|
||||
ctx context.Context, network, address string) (net.Conn, error) {
|
||||
conn, err := d.Dialer.DialContext(ctx, network, address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
conn = MaybeWrapWithContextByteCounters(ctx, conn)
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
// CloseIdleConnections implements Dialer.CloseIdleConnections.
|
||||
func (d *ContextAwareDialer) CloseIdleConnections() {
|
||||
d.Dialer.CloseIdleConnections()
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package bytecounter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/model/mocks"
|
||||
)
|
||||
|
||||
func TestContextAwareDialer(t *testing.T) {
|
||||
t.Run("DialContext", func(t *testing.T) {
|
||||
dialAndUseConn := func(ctx context.Context, bufsiz int) error {
|
||||
childConn := &mocks.Conn{
|
||||
MockRead: func(b []byte) (int, error) {
|
||||
return len(b), nil
|
||||
},
|
||||
MockWrite: func(b []byte) (int, error) {
|
||||
return len(b), nil
|
||||
},
|
||||
}
|
||||
child := &mocks.Dialer{
|
||||
MockDialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
return childConn, nil
|
||||
},
|
||||
}
|
||||
dialer := NewContextAwareDialer(child)
|
||||
conn, err := dialer.DialContext(ctx, "tcp", "10.0.0.1:443")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
buffer := make([]byte, bufsiz)
|
||||
conn.Read(buffer)
|
||||
conn.Write(buffer)
|
||||
return nil
|
||||
}
|
||||
|
||||
t.Run("normal usage", func(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skip test in short mode")
|
||||
}
|
||||
sess := New()
|
||||
ctx := context.Background()
|
||||
ctx = WithSessionByteCounter(ctx, sess)
|
||||
const count = 128
|
||||
if err := dialAndUseConn(ctx, count); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
exp := New()
|
||||
ctx = WithExperimentByteCounter(ctx, exp)
|
||||
if err := dialAndUseConn(ctx, count); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if exp.Received.Load() != count {
|
||||
t.Fatal("experiment should have received 128 bytes")
|
||||
}
|
||||
if sess.Received.Load() != 2*count {
|
||||
t.Fatal("session should have received 256 bytes")
|
||||
}
|
||||
if exp.Sent.Load() != count {
|
||||
t.Fatal("experiment should have sent 128 bytes")
|
||||
}
|
||||
if sess.Sent.Load() != 256 {
|
||||
t.Fatal("session should have sent 256 bytes")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("failure", func(t *testing.T) {
|
||||
dialer := &ContextAwareDialer{
|
||||
Dialer: &mocks.Dialer{
|
||||
MockDialContext: func(ctx context.Context, network string, address string) (net.Conn, error) {
|
||||
return nil, io.EOF
|
||||
},
|
||||
},
|
||||
}
|
||||
conn, err := dialer.DialContext(context.Background(), "tcp", "www.google.com:80")
|
||||
if !errors.Is(err, io.EOF) {
|
||||
t.Fatal("not the error we expected")
|
||||
}
|
||||
if conn != nil {
|
||||
t.Fatal("expected nil conn here")
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("CloseIdleConnections", func(t *testing.T) {
|
||||
var called bool
|
||||
child := &mocks.Dialer{
|
||||
MockCloseIdleConnections: func() {
|
||||
called = true
|
||||
},
|
||||
}
|
||||
dialer := NewContextAwareDialer(child)
|
||||
dialer.CloseIdleConnections()
|
||||
if !called {
|
||||
t.Fatal("not called")
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user