Skip to content

Commit 308a8e2

Browse files
committed
autobahn_test.go: Fix TODOs
1 parent 5fe95bb commit 308a8e2

File tree

1 file changed

+36
-17
lines changed

1 file changed

+36
-17
lines changed

autobahn_test.go

+36-17
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package websocket_test
66
import (
77
"context"
88
"encoding/json"
9+
"errors"
910
"fmt"
1011
"io"
1112
"net"
@@ -20,6 +21,7 @@ import (
2021
"nhooyr.io/websocket/internal/errd"
2122
"nhooyr.io/websocket/internal/test/assert"
2223
"nhooyr.io/websocket/internal/test/wstest"
24+
"nhooyr.io/websocket/internal/util"
2325
)
2426

2527
var excludedAutobahnCases = []string{
@@ -37,8 +39,7 @@ var autobahnCases = []string{"*"}
3739
// Used to run individual test cases. autobahnCases runs only those cases matched
3840
// and not excluded by excludedAutobahnCases. Adding cases here means excludedAutobahnCases
3941
// is niled.
40-
// TODO:
41-
// var forceAutobahnCases = []string{}
42+
var onlyAutobahnCases = []string{}
4243

4344
func TestAutobahn(t *testing.T) {
4445
t.Parallel()
@@ -54,10 +55,15 @@ func TestAutobahn(t *testing.T) {
5455
)
5556
}
5657

58+
if len(onlyAutobahnCases) > 0 {
59+
excludedAutobahnCases = []string{}
60+
autobahnCases = onlyAutobahnCases
61+
}
62+
5763
ctx, cancel := context.WithTimeout(context.Background(), time.Hour)
5864
defer cancel()
5965

60-
wstestURL, closeFn, err := wstestServer(ctx)
66+
wstestURL, closeFn, err := wstestServer(t, ctx)
6167
assert.Success(t, err)
6268
defer func() {
6369
assert.Success(t, closeFn())
@@ -90,7 +96,7 @@ func TestAutobahn(t *testing.T) {
9096
assert.Success(t, err)
9197
c.Close(websocket.StatusNormalClosure, "")
9298

93-
checkWSTestIndex(t, "./ci/out/wstestClientReports/index.json")
99+
checkWSTestIndex(t, "./ci/out/autobahn-report/index.json")
94100
}
95101

96102
func waitWS(ctx context.Context, url string) error {
@@ -109,9 +115,7 @@ func waitWS(ctx context.Context, url string) error {
109115
return ctx.Err()
110116
}
111117

112-
// TODO: Let docker pick the port and use docker port to find it.
113-
// Does mean we can't use -i but that's fine.
114-
func wstestServer(ctx context.Context) (url string, closeFn func() error, err error) {
118+
func wstestServer(tb testing.TB, ctx context.Context) (url string, closeFn func() error, err error) {
115119
defer errd.Wrap(&err, "failed to start autobahn wstest server")
116120

117121
serverAddr, err := unusedListenAddr()
@@ -124,7 +128,7 @@ func wstestServer(ctx context.Context) (url string, closeFn func() error, err er
124128
}
125129

126130
url = "ws://" + serverAddr
127-
const outDir = "ci/out/wstestClientReports"
131+
const outDir = "ci/out/autobahn-report"
128132

129133
specFile, err := tempJSONFile(map[string]interface{}{
130134
"url": url,
@@ -144,9 +148,15 @@ func wstestServer(ctx context.Context) (url string, closeFn func() error, err er
144148
}()
145149

146150
dockerPull := exec.CommandContext(ctx, "docker", "pull", "crossbario/autobahn-testsuite")
147-
// TODO: log to *testing.T
148-
dockerPull.Stdout = os.Stdout
149-
dockerPull.Stderr = os.Stderr
151+
dockerPull.Stdout = util.WriterFunc(func(p []byte) (int, error) {
152+
tb.Log(string(p))
153+
return len(p), nil
154+
})
155+
dockerPull.Stderr = util.WriterFunc(func(p []byte) (int, error) {
156+
tb.Log(string(p))
157+
return len(p), nil
158+
})
159+
tb.Log(dockerPull)
150160
err = dockerPull.Run()
151161
if err != nil {
152162
return "", nil, fmt.Errorf("failed to pull docker image: %w", err)
@@ -169,23 +179,32 @@ func wstestServer(ctx context.Context) (url string, closeFn func() error, err er
169179
// See https://github.com/crossbario/autobahn-testsuite/blob/058db3a36b7c3a1edf68c282307c6b899ca4857f/autobahntestsuite/autobahntestsuite/wstest.py#L124
170180
"--webport=0",
171181
)
172-
fmt.Println(strings.Join(args, " "))
173182
wstest := exec.CommandContext(ctx, "docker", args...)
174-
// TODO: log to *testing.T
175-
wstest.Stdout = os.Stdout
176-
wstest.Stderr = os.Stderr
183+
wstest.Stdout = util.WriterFunc(func(p []byte) (int, error) {
184+
tb.Log(string(p))
185+
return len(p), nil
186+
})
187+
wstest.Stderr = util.WriterFunc(func(p []byte) (int, error) {
188+
tb.Log(string(p))
189+
return len(p), nil
190+
})
191+
tb.Log(wstest)
177192
err = wstest.Start()
178193
if err != nil {
179194
return "", nil, fmt.Errorf("failed to start wstest: %w", err)
180195
}
181196

182-
// TODO: kill
183197
return url, func() error {
184198
err = wstest.Process.Kill()
185199
if err != nil {
186200
return fmt.Errorf("failed to kill wstest: %w", err)
187201
}
188-
return nil
202+
err = wstest.Wait()
203+
var ee *exec.ExitError
204+
if errors.As(err, &ee) && ee.ExitCode() == -1 {
205+
return nil
206+
}
207+
return err
189208
}, nil
190209
}
191210

0 commit comments

Comments
 (0)