fix(netxlite): make default resolver converge faster (#456)
* fix(netxlite): make default resolver converge faster Closes https://github.com/ooni/probe/issues/1726 * Update internal/netxlite/resolver.go * fix(ndt7): adapt tests after previous change Because now we're running the DNS resolution inside a goroutine with a child context, the returned error string is different. The previous error said we canceled the whole dialing operation, while now we see directly that the context was canceled.
This commit is contained in:
parent
a3654f60b7
commit
a3a27b1ebf
4 changed files with 125 additions and 16 deletions
|
|
@ -4,7 +4,9 @@ import (
|
|||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
|
|
@ -12,7 +14,7 @@ import (
|
|||
)
|
||||
|
||||
func TestResolverSystemNetworkAddress(t *testing.T) {
|
||||
r := resolverSystem{}
|
||||
r := &resolverSystem{}
|
||||
if r.Network() != "system" {
|
||||
t.Fatal("invalid Network")
|
||||
}
|
||||
|
|
@ -22,7 +24,8 @@ func TestResolverSystemNetworkAddress(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestResolverSystemWorksAsIntended(t *testing.T) {
|
||||
r := resolverSystem{}
|
||||
r := &resolverSystem{}
|
||||
defer r.CloseIdleConnections()
|
||||
addrs, err := r.LookupHost(context.Background(), "dns.google.com")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
|
@ -32,6 +35,73 @@ func TestResolverSystemWorksAsIntended(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestResolverSystemDefaultTimeout(t *testing.T) {
|
||||
r := &resolverSystem{}
|
||||
if r.timeout() != 15*time.Second {
|
||||
t.Fatal("unexpected default timeout")
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolverSystemWithTimeoutAndSuccess(t *testing.T) {
|
||||
wg := &sync.WaitGroup{}
|
||||
wg.Add(1)
|
||||
r := &resolverSystem{
|
||||
testableTimeout: 1 * time.Microsecond,
|
||||
testableLookupHost: func(ctx context.Context, domain string) ([]string, error) {
|
||||
defer wg.Done()
|
||||
time.Sleep(1 * time.Millisecond)
|
||||
return []string{"8.8.8.8"}, nil
|
||||
},
|
||||
}
|
||||
ctx := context.Background()
|
||||
addrs, err := r.LookupHost(ctx, "example.antani")
|
||||
if !errors.Is(err, context.DeadlineExceeded) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if addrs != nil {
|
||||
t.Fatal("invalid addrs")
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func TestResolverSystemWithTimeoutAndFailure(t *testing.T) {
|
||||
wg := &sync.WaitGroup{}
|
||||
wg.Add(1)
|
||||
r := &resolverSystem{
|
||||
testableTimeout: 1 * time.Microsecond,
|
||||
testableLookupHost: func(ctx context.Context, domain string) ([]string, error) {
|
||||
defer wg.Done()
|
||||
time.Sleep(1 * time.Millisecond)
|
||||
return nil, errors.New("no such host")
|
||||
},
|
||||
}
|
||||
ctx := context.Background()
|
||||
addrs, err := r.LookupHost(ctx, "example.antani")
|
||||
if !errors.Is(err, context.DeadlineExceeded) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if addrs != nil {
|
||||
t.Fatal("invalid addrs")
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func TestResolverSystemWithNXDOMAIN(t *testing.T) {
|
||||
r := &resolverSystem{
|
||||
testableLookupHost: func(ctx context.Context, domain string) ([]string, error) {
|
||||
return nil, errors.New("no such host")
|
||||
},
|
||||
}
|
||||
ctx := context.Background()
|
||||
addrs, err := r.LookupHost(ctx, "example.antani")
|
||||
if err == nil || !strings.HasSuffix(err.Error(), "no such host") {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if addrs != nil {
|
||||
t.Fatal("invalid addrs")
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolverLoggerWithSuccess(t *testing.T) {
|
||||
expected := []string{"1.1.1.1"}
|
||||
r := resolverLogger{
|
||||
|
|
|
|||
Loading…
Reference in a new issue