Fix #363; it can take longer than 1ms to resolve an ip, even when its just a map lookup.

GC takes longer, for instance. Also we should fail if we don't get the IP in time, otherwise
we just deadlock.  And fix some concurrent variable access.
This commit is contained in:
Tom Wilkie
2015-08-18 09:09:06 +00:00
parent e7ab9816cd
commit 501504bb40

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"net"
"runtime"
"sync"
"testing"
"time"
@@ -18,14 +19,22 @@ func TestResolver(t *testing.T) {
oldLookupIP := lookupIP
defer func() { lookupIP = oldLookupIP }()
ipsLock := sync.Mutex{}
ips := map[string][]net.IP{}
lookupIP = func(host string) ([]net.IP, error) {
ipsLock.Lock()
defer ipsLock.Unlock()
addrs, ok := ips[host]
if !ok {
return nil, fmt.Errorf("Not found")
}
return addrs, nil
}
updateIPs := func(key string, values []net.IP) {
ipsLock.Lock()
defer ipsLock.Unlock()
ips = map[string][]net.IP{key: values}
}
port := ":80"
ip1 := "192.168.0.1"
@@ -42,8 +51,8 @@ func TestResolver(t *testing.T) {
if want != have {
t.Errorf("line %d: want %q, have %q", line, want, have)
}
case <-time.After(time.Millisecond):
t.Errorf("line %d: didn't get add in time", line)
case <-time.After(100 * time.Millisecond):
t.Fatalf("line %d: didn't get add in time", line)
}
}
@@ -58,14 +67,14 @@ func TestResolver(t *testing.T) {
assertAdd(fmt.Sprintf("%s:%d", ip2, xfer.AppPort))
ip3 := "1.2.3.4"
ips = map[string][]net.IP{"symbolic.name": makeIPs(ip3)}
updateIPs("symbolic.name", makeIPs(ip3))
c <- time.Now() // trigger a resolve
assertAdd(ip3 + port) // we want 1 add
assertAdd(ip1 + port)
assertAdd(fmt.Sprintf("%s:%d", ip2, xfer.AppPort))
ip4 := "10.10.10.10"
ips = map[string][]net.IP{"symbolic.name": makeIPs(ip3, ip4)}
updateIPs("symbolic.name", makeIPs(ip3, ip4))
c <- time.Now() // trigger another resolve, this time with 2 adds
assertAdd(ip3 + port) // first add
assertAdd(ip4 + port) // second add