Skip to content

Commit faadcc9

Browse files
committed
Simplify tests
1 parent bbaf469 commit faadcc9

File tree

10 files changed

+234
-236
lines changed

10 files changed

+234
-236
lines changed

Diff for: assert_test.go

-112
This file was deleted.

Diff for: compress_test.go

+4-21
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package websocket
22

33
import (
4-
"crypto/rand"
5-
"encoding/base64"
6-
"math/big"
74
"strings"
85
"testing"
96

107
"cdr.dev/slog/sloggers/slogtest/assert"
8+
9+
"nhooyr.io/websocket/internal/test/xrand"
1110
)
1211

1312
func Test_slidingWindow(t *testing.T) {
@@ -16,8 +15,8 @@ func Test_slidingWindow(t *testing.T) {
1615
const testCount = 99
1716
const maxWindow = 99999
1817
for i := 0; i < testCount; i++ {
19-
input := randStr(t, maxWindow)
20-
windowLength := randInt(t, maxWindow)
18+
input := xrand.String(maxWindow)
19+
windowLength := xrand.Int(maxWindow)
2120
r := newSlidingWindow(windowLength)
2221
r.write([]byte(input))
2322

@@ -27,19 +26,3 @@ func Test_slidingWindow(t *testing.T) {
2726
assert.True(t, "hasSuffix", strings.HasSuffix(input, string(r.buf)))
2827
}
2928
}
30-
31-
func randStr(t *testing.T, max int) string {
32-
n := randInt(t, max)
33-
34-
b := make([]byte, n)
35-
_, err := rand.Read(b)
36-
assert.Success(t, "rand.Read", err)
37-
38-
return base64.StdEncoding.EncodeToString(b)
39-
}
40-
41-
func randInt(t *testing.T, max int) int {
42-
x, err := rand.Int(rand.Reader, big.NewInt(int64(max)))
43-
assert.Success(t, "rand.Int", err)
44-
return int(x.Int64())
45-
}

Diff for: conn_test.go

+70-95
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,96 @@
33
package websocket_test
44

55
import (
6-
"bufio"
76
"context"
8-
"crypto/rand"
97
"io"
10-
"math/big"
11-
"net"
12-
"net/http"
13-
"net/http/httptest"
148
"testing"
159
"time"
1610

17-
"cdr.dev/slog/sloggers/slogtest/assert"
11+
"golang.org/x/xerrors"
1812

1913
"nhooyr.io/websocket"
14+
"nhooyr.io/websocket/internal/test/cmp"
15+
"nhooyr.io/websocket/internal/test/wstest"
16+
"nhooyr.io/websocket/internal/test/xrand"
17+
"nhooyr.io/websocket/wsjson"
2018
)
2119

22-
func goFn(fn func()) func() {
23-
done := make(chan struct{})
20+
func goFn(fn func() error) chan error {
21+
errs := make(chan error)
2422
go func() {
25-
defer close(done)
26-
fn()
23+
defer close(errs)
24+
errs <- fn()
2725
}()
2826

29-
return func() {
30-
<-done
31-
}
27+
return errs
3228
}
3329

3430
func TestConn(t *testing.T) {
3531
t.Parallel()
3632

37-
t.Run("json", func(t *testing.T) {
33+
t.Run("data", func(t *testing.T) {
3834
t.Parallel()
3935

40-
for i := 0; i < 1; i++ {
36+
for i := 0; i < 10; i++ {
4137
t.Run("", func(t *testing.T) {
42-
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
38+
t.Parallel()
39+
40+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
4341
defer cancel()
4442

45-
c1, c2 := websocketPipe(t)
43+
copts := websocket.CompressionOptions{
44+
Mode: websocket.CompressionMode(xrand.Int(int(websocket.CompressionDisabled))),
45+
Threshold: xrand.Int(9999),
46+
}
47+
48+
c1, c2, err := wstest.Pipe(&websocket.DialOptions{
49+
CompressionOptions: copts,
50+
}, &websocket.AcceptOptions{
51+
CompressionOptions: copts,
52+
})
53+
if err != nil {
54+
t.Fatal(err)
55+
}
56+
defer c1.Close(websocket.StatusInternalError, "")
57+
defer c2.Close(websocket.StatusInternalError, "")
4658

47-
wait := goFn(func() {
59+
echoLoopErr := goFn(func() error {
4860
err := echoLoop(ctx, c1)
49-
assertCloseStatus(t, websocket.StatusNormalClosure, err)
61+
return assertCloseStatus(websocket.StatusNormalClosure, err)
5062
})
51-
defer wait()
63+
defer func() {
64+
err := <-echoLoopErr
65+
if err != nil {
66+
t.Errorf("echo loop error: %v", err)
67+
}
68+
}()
5269
defer cancel()
5370

5471
c2.SetReadLimit(1 << 30)
5572

5673
for i := 0; i < 10; i++ {
57-
n := randInt(t, 131_072)
58-
echoJSON(t, c2, n)
74+
n := xrand.Int(131_072)
75+
76+
msg := xrand.String(n)
77+
78+
writeErr := goFn(func() error {
79+
return wsjson.Write(ctx, c2, msg)
80+
})
81+
82+
var act interface{}
83+
err := wsjson.Read(ctx, c2, &act)
84+
if err != nil {
85+
t.Fatal(err)
86+
}
87+
88+
err = <-writeErr
89+
if err != nil {
90+
t.Fatal(err)
91+
}
92+
93+
if !cmp.Equal(msg, act) {
94+
t.Fatalf("unexpected msg read: %v", cmp.Diff(msg, act))
95+
}
5996
}
6097

6198
c2.Close(websocket.StatusNormalClosure, "")
@@ -64,6 +101,16 @@ func TestConn(t *testing.T) {
64101
})
65102
}
66103

104+
func assertCloseStatus(exp websocket.StatusCode, err error) error {
105+
if websocket.CloseStatus(err) == -1 {
106+
return xerrors.Errorf("expected websocket.CloseError: %T %v", err, err)
107+
}
108+
if websocket.CloseStatus(err) != exp {
109+
return xerrors.Errorf("unexpected close status (%v):%v", exp, err)
110+
}
111+
return nil
112+
}
113+
67114
// echoLoop echos every msg received from c until an error
68115
// occurs or the context expires.
69116
// The read limit is set to 1 << 30.
@@ -98,75 +145,3 @@ func echoLoop(ctx context.Context, c *websocket.Conn) error {
98145
}
99146
}
100147
}
101-
102-
func randBool(t testing.TB) bool {
103-
return randInt(t, 2) == 1
104-
}
105-
106-
func randInt(t testing.TB, max int) int {
107-
x, err := rand.Int(rand.Reader, big.NewInt(int64(max)))
108-
assert.Success(t, "rand.Int", err)
109-
return int(x.Int64())
110-
}
111-
112-
type testHijacker struct {
113-
*httptest.ResponseRecorder
114-
serverConn net.Conn
115-
hijacked chan struct{}
116-
}
117-
118-
var _ http.Hijacker = testHijacker{}
119-
120-
func (hj testHijacker) Hijack() (net.Conn, *bufio.ReadWriter, error) {
121-
close(hj.hijacked)
122-
return hj.serverConn, bufio.NewReadWriter(bufio.NewReader(hj.serverConn), bufio.NewWriter(hj.serverConn)), nil
123-
}
124-
125-
func websocketPipe(t *testing.T) (*websocket.Conn, *websocket.Conn) {
126-
var serverConn *websocket.Conn
127-
tt := testTransport{
128-
h: func(w http.ResponseWriter, r *http.Request) {
129-
serverConn = acceptWebSocket(t, r, w, nil)
130-
},
131-
}
132-
133-
dialOpts := &websocket.DialOptions{
134-
HTTPClient: &http.Client{
135-
Transport: tt,
136-
},
137-
}
138-
139-
clientConn, _, err := websocket.Dial(context.Background(), "ws://example.com", dialOpts)
140-
assert.Success(t, "websocket.Dial", err)
141-
142-
if randBool(t) {
143-
return serverConn, clientConn
144-
}
145-
return clientConn, serverConn
146-
}
147-
148-
type testTransport struct {
149-
h http.HandlerFunc
150-
}
151-
152-
func (t testTransport) RoundTrip(r *http.Request) (*http.Response, error) {
153-
clientConn, serverConn := net.Pipe()
154-
155-
hj := testHijacker{
156-
ResponseRecorder: httptest.NewRecorder(),
157-
serverConn: serverConn,
158-
hijacked: make(chan struct{}),
159-
}
160-
161-
done := make(chan struct{})
162-
t.h.ServeHTTP(hj, r)
163-
164-
select {
165-
case <-hj.hijacked:
166-
resp := hj.ResponseRecorder.Result()
167-
resp.Body = clientConn
168-
return resp, nil
169-
case <-done:
170-
return hj.ResponseRecorder.Result(), nil
171-
}
172-
}

Diff for: dial.go

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type DialOptions struct {
3535

3636
// CompressionOptions controls the compression options.
3737
// See docs on the CompressionOptions type.
38+
// TODO make *
3839
CompressionOptions CompressionOptions
3940
}
4041

Diff for: go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require (
1111
github.com/gobwas/ws v1.0.2
1212
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
1313
github.com/golang/protobuf v1.3.3
14-
github.com/google/go-cmp v0.4.0 // indirect
14+
github.com/google/go-cmp v0.4.0
1515
github.com/gorilla/websocket v1.4.1
1616
github.com/mattn/go-isatty v0.0.12 // indirect
1717
go.opencensus.io v0.22.3 // indirect

0 commit comments

Comments
 (0)