Skip to content

Commit 01721c9

Browse files
committed
Improve reliability of wstest in tests
Ensures each invocation gets a unique address to listen on.
1 parent 63f27e2 commit 01721c9

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

websocket_test.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88
"io/ioutil"
9+
"net"
910
"net/http"
1011
"net/http/cookiejar"
1112
"net/http/httptest"
@@ -781,12 +782,28 @@ func discardLoop(ctx context.Context, c *websocket.Conn) {
781782
}
782783
}
783784

785+
func unusedListenAddr() (string, error) {
786+
l, err := net.Listen("tcp", "localhost:0")
787+
if err != nil {
788+
return "", err
789+
}
790+
l.Close()
791+
return l.Addr().String(), nil
792+
}
793+
784794
// https://github.com/crossbario/autobahn-python/blob/master/wstest/testee_client_aio.py
785795
func TestAutobahnClient(t *testing.T) {
786796
t.Parallel()
787797

798+
serverAddr, err := unusedListenAddr()
799+
if err != nil {
800+
t.Fatalf("failed to get unused listen addr for wstest: %v", err)
801+
}
802+
803+
wsServerURL := "ws://" + serverAddr
804+
788805
spec := map[string]interface{}{
789-
"url": "ws://localhost:9001",
806+
"url": wsServerURL,
790807
"outdir": "ci/out/wstestClientReports",
791808
"cases": []string{"*"},
792809
// See TestAutobahnServer for the reasons why we exclude these.
@@ -814,9 +831,10 @@ func TestAutobahnClient(t *testing.T) {
814831
ctx, cancel := context.WithTimeout(ctx, time.Minute*10)
815832
defer cancel()
816833

817-
args := []string{"--mode", "fuzzingserver", "--spec", specFile.Name()}
818-
if os.Getenv("CI") == "" {
819-
args = append([]string{"--debug"}, args...)
834+
args := []string{"--mode", "fuzzingserver", "--spec", specFile.Name(),
835+
// Disables some server that runs as part of fuzzingserver mode.
836+
// See https://github.com/crossbario/autobahn-testsuite/blob/058db3a36b7c3a1edf68c282307c6b899ca4857f/autobahntestsuite/autobahntestsuite/wstest.py#L124
837+
"--webport=0",
820838
}
821839
wstest := exec.CommandContext(ctx, "wstest", args...)
822840
err = wstest.Start()
@@ -835,9 +853,9 @@ func TestAutobahnClient(t *testing.T) {
835853

836854
var cases int
837855
func() {
838-
c, _, err := websocket.Dial(ctx, "ws://localhost:9001/getCaseCount", websocket.DialOptions{})
856+
c, _, err := websocket.Dial(ctx, wsServerURL+"/getCaseCount", websocket.DialOptions{})
839857
if err != nil {
840-
t.Fatalf("failed to dial: %v", err)
858+
t.Fatal(err)
841859
}
842860
defer c.Close(websocket.StatusInternalError, "")
843861

@@ -862,17 +880,17 @@ func TestAutobahnClient(t *testing.T) {
862880
ctx, cancel := context.WithTimeout(ctx, time.Second*45)
863881
defer cancel()
864882

865-
c, _, err := websocket.Dial(ctx, fmt.Sprintf("ws://localhost:9001/runCase?case=%v&agent=main", i), websocket.DialOptions{})
883+
c, _, err := websocket.Dial(ctx, fmt.Sprintf(wsServerURL+"/runCase?case=%v&agent=main", i), websocket.DialOptions{})
866884
if err != nil {
867-
t.Fatalf("failed to dial: %v", err)
885+
t.Fatal(err)
868886
}
869887
echoLoop(ctx, c)
870888
}()
871889
}
872890

873-
c, _, err := websocket.Dial(ctx, fmt.Sprintf("ws://localhost:9001/updateReports?agent=main"), websocket.DialOptions{})
891+
c, _, err := websocket.Dial(ctx, fmt.Sprintf(wsServerURL+"/updateReports?agent=main"), websocket.DialOptions{})
874892
if err != nil {
875-
t.Fatalf("failed to dial: %v", err)
893+
t.Fatal(err)
876894
}
877895
c.Close(websocket.StatusNormalClosure, "")
878896

@@ -944,7 +962,7 @@ func benchConn(b *testing.B, echo, stream bool, size int) {
944962

945963
c, _, err := websocket.Dial(ctx, wsURL, websocket.DialOptions{})
946964
if err != nil {
947-
b.Fatalf("failed to dial: %v", err)
965+
b.Fatal(err)
948966
}
949967
defer c.Close(websocket.StatusInternalError, "")
950968

0 commit comments

Comments
 (0)