Change:
https://travis-
Looking.
Another instance, or a different thing? https://travis-

Discussion:
import ( "fmt" <del> "io" </del> <ins> "io/ioutil" </ins> "net" <ins> "net/http" "net/http/httptest" "net/url" </ins> "strconv" "testing" "time"

return fmt.Errorf("port %s still open", proxyPort) } <del> // a simple echoServer that only accepts one connection. Returns port actually // being listened on, or an error. func echoServer(t *testing.T, addr string) (string, error) { l, err := net.Listen("tcp", addr) </del> <ins> var port string func init() { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) w.Write([]byte(r.URL.Path[1:])) })) u, err := url.Parse(ts.URL) </ins> if err != nil { <del> return "", fmt.Errorf("failed to start echo service: %v", err) </del> <ins> panic(fmt.Sprintf("failed to parse: %v", err)) } _, port, err = net.SplitHostPort(u.Host) if err != nil { panic(fmt.Sprintf("failed to parse: %v", err)) </ins> } <del> go func() { defer l.Close() conn, err := l.Accept() if err != nil { t.Errorf("failed to accept new conn to echo service: %v", err) } io.Copy(conn, conn) conn.Close() }() _, port, err := net.SplitHostPort(l.Addr().String()) return port, err </del> } func testEchoConnection(t *testing.T, address, port string) { <del> conn, err := net.Dial("tcp", net.JoinHostPort(address, port)) </del> <ins> path := "aaaaa" res, err := http.Get("http://" + address + ":" + port + "/" + path) </ins> if err != nil { <del> t.Fatalf("error connecting to proxy: %v", err) } magic := "aaaaa" if _, err := conn.Write([]byte(magic)); err != nil { t.Fatalf("error writing to proxy: %v", err) </del> <ins> t.Fatalf("error connecting to server: %v", err) </ins> } <del> buf := make([]byte, 5) if _, err := conn.Read(buf); err != nil { t.Fatalf("error reading from proxy: %v", err) </del> <ins> defer res.Body.Close() data, err := ioutil.ReadAll(res.Body) if err != nil { t.Errorf("error reading data: %v %v", err, string(data)) </ins> } <del> if string(buf) != magic { t.Fatalf("bad echo from proxy: got: %q, expected %q", string(buf), magic) </del> <ins> if string(data) != path { t.Errorf("expected: %s, got %s", path, string(data)) </ins> } } func TestProxy(t *testing.T) { <del> port, err := echoServer(t, "127.0.0.1:0") if err != nil { t.Fatalf("Unexpected error: %v", err) } </del> lb := NewLoadBalancerRR() lb.OnUpdate([]api.Endpoints{ {JSONBase: api.JSONBase{ID: "echo"}, Endpoints: []string{net.JoinHostPort("127.0.0.1", port)}}})

} func TestProxyStop(t *testing.T) { <del> port, err := echoServer(t, "127.0.0.1:0") if err != nil { t.Fatal(err) } </del> lb := NewLoadBalancerRR() lb.OnUpdate([]api.Endpoints{{JSONBase: api.JSONBase{ID: "echo"}, Endpoints: []string{net.JoinHostPort("127.0.0.1", port)}}})

} func TestProxyUpdateDelete(t *testing.T) { <del> port, err := echoServer(t, "127.0.0.1:0") if err != nil { t.Fatal(err) } </del> lb := NewLoadBalancerRR() lb.OnUpdate([]api.Endpoints{{JSONBase: api.JSONBase{ID: "echo"}, Endpoints: []string{net.JoinHostPort("127.0.0.1", port)}}})

} func TestProxyUpdatePort(t *testing.T) { <del> port, err := echoServer(t, "127.0.0.1:0") if err != nil { t.Fatal(err) } </del> lb := NewLoadBalancerRR() lb.OnUpdate([]api.Endpoints{{JSONBase: api.JSONBase{ID: "echo"}, Endpoints: []string{net.JoinHostPort("127.0.0.1", port)}}})

// add a new dummy listener in order to get a port that is free l, _ := net.Listen("tcp", ":0") <del> _, port, _ = net.SplitHostPort(l.Addr().String()) portNum, _ := strconv.Atoi(port) </del> <ins> _, newPort, _ := net.SplitHostPort(l.Addr().String()) portNum, _ := strconv.Atoi(newPort) </ins> l.Close() // Wait for the socket to actually get free. <del> if err := waitForClosedPort(p, port); err != nil { </del> <ins> if err := waitForClosedPort(p, newPort); err != nil { </ins> t.Fatalf(err.Error()) } <ins> if proxyPort == newPort { t.Errorf("expected difference, got %s %s", newPort, proxyPort) } </ins> p.OnUpdate([]api.Service{ {JSONBase: api.JSONBase{ID: "echo"}, Port: portNum}, }) if err := waitForClosedPort(p, proxyPort); err != nil { t.Fatalf(err.Error()) } <del> testEchoConnection(t, "127.0.0.1", port) </del> <ins> testEchoConnection(t, "127.0.0.1", newPort) </ins> }