Skip to content

Commit 3f2589f

Browse files
committed
Remove quite a bit of slog
1 parent faadcc9 commit 3f2589f

File tree

6 files changed

+79
-31
lines changed

6 files changed

+79
-31
lines changed

ci/test.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ coveralls: gotest
1414
gotest:
1515
go test -covermode=count -coverprofile=ci/out/coverage.prof -coverpkg=./... $${GOTESTFLAGS-} ./...
1616
sed -i '/stringer\.go/d' ci/out/coverage.prof
17-
sed -i '/assert/d' ci/out/coverage.prof
17+
sed -i '/nhooyr.io\/websocket\/internal\/test/d' ci/out/coverage.prof

close_test.go

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"strings"
99
"testing"
1010

11-
"cdr.dev/slog/sloggers/slogtest/assert"
11+
"nhooyr.io/websocket/internal/test/cmp"
1212
)
1313

1414
func TestCloseError(t *testing.T) {
@@ -51,13 +51,23 @@ func TestCloseError(t *testing.T) {
5151
t.Parallel()
5252

5353
_, err := tc.ce.bytesErr()
54-
if tc.success {
55-
assert.Success(t, "CloseError.bytesErr", err)
56-
} else {
57-
assert.Error(t, "CloseError.bytesErr", err)
54+
if tc.success != (err == nil) {
55+
t.Fatalf("unexpected error value (wanted err == nil == %v): %v", tc.success, err)
5856
}
5957
})
6058
}
59+
60+
t.Run("Error", func(t *testing.T) {
61+
exp := `status = StatusInternalError and reason = "meow"`
62+
act := CloseError{
63+
Code: StatusInternalError,
64+
Reason: "meow",
65+
}.Error()
66+
67+
if (act) != exp {
68+
t.Fatal(cmp.Diff(exp, act))
69+
}
70+
})
6171
}
6272

6373
func Test_parseClosePayload(t *testing.T) {
@@ -104,10 +114,14 @@ func Test_parseClosePayload(t *testing.T) {
104114

105115
ce, err := parseClosePayload(tc.p)
106116
if tc.success {
107-
assert.Success(t, "parse err", err)
108-
assert.Equal(t, "ce", tc.ce, ce)
109-
} else {
110-
assert.Error(t, "parse err", err)
117+
if err != nil {
118+
t.Fatal(err)
119+
}
120+
if !cmp.Equal(tc.ce, ce) {
121+
t.Fatalf("expected %v but got %v", tc.ce, ce)
122+
}
123+
} else if err == nil {
124+
t.Errorf("expected error: %v %v", ce, err)
111125
}
112126
})
113127
}
@@ -153,7 +167,10 @@ func Test_validWireCloseCode(t *testing.T) {
153167
t.Run(tc.name, func(t *testing.T) {
154168
t.Parallel()
155169

156-
assert.Equal(t, "valid", tc.valid, validWireCloseCode(tc.code))
170+
act := validWireCloseCode(tc.code)
171+
if !cmp.Equal(tc.valid, act) {
172+
t.Fatalf("unexpected valid: %v", cmp.Diff(tc.valid, act))
173+
}
157174
})
158175
}
159176
}
@@ -190,7 +207,10 @@ func TestCloseStatus(t *testing.T) {
190207
t.Run(tc.name, func(t *testing.T) {
191208
t.Parallel()
192209

193-
assert.Equal(t, "closeStatus", tc.exp, CloseStatus(tc.in))
210+
act := CloseStatus(tc.in)
211+
if !cmp.Equal(tc.exp, act) {
212+
t.Fatalf("unexpected closeStatus: %v", cmp.Diff(tc.exp, act))
213+
}
194214
})
195215
}
196216
}

conn_test.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ import (
1414
"nhooyr.io/websocket/internal/test/cmp"
1515
"nhooyr.io/websocket/internal/test/wstest"
1616
"nhooyr.io/websocket/internal/test/xrand"
17-
"nhooyr.io/websocket/wsjson"
1817
)
1918

2019
func goFn(fn func() error) chan error {
2120
errs := make(chan error)
2221
go func() {
23-
defer close(errs)
22+
defer func() {
23+
r := recover()
24+
if r != nil {
25+
errs <- xerrors.Errorf("panic in gofn: %v", r)
26+
}
27+
}()
2428
errs <- fn()
2529
}()
2630

@@ -33,15 +37,15 @@ func TestConn(t *testing.T) {
3337
t.Run("data", func(t *testing.T) {
3438
t.Parallel()
3539

36-
for i := 0; i < 10; i++ {
40+
for i := 0; i < 5; i++ {
3741
t.Run("", func(t *testing.T) {
3842
t.Parallel()
3943

4044
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
4145
defer cancel()
4246

4347
copts := websocket.CompressionOptions{
44-
Mode: websocket.CompressionMode(xrand.Int(int(websocket.CompressionDisabled))),
48+
Mode: websocket.CompressionMode(xrand.Int(int(websocket.CompressionDisabled) + 1)),
4549
Threshold: xrand.Int(9999),
4650
}
4751

@@ -70,17 +74,21 @@ func TestConn(t *testing.T) {
7074

7175
c2.SetReadLimit(1 << 30)
7276

73-
for i := 0; i < 10; i++ {
77+
for i := 0; i < 5; i++ {
7478
n := xrand.Int(131_072)
7579

76-
msg := xrand.String(n)
80+
msg := xrand.Bytes(n)
81+
82+
expType := websocket.MessageBinary
83+
if xrand.Bool() {
84+
expType = websocket.MessageText
85+
}
7786

7887
writeErr := goFn(func() error {
79-
return wsjson.Write(ctx, c2, msg)
88+
return c2.Write(ctx, expType, msg)
8089
})
8190

82-
var act interface{}
83-
err := wsjson.Read(ctx, c2, &act)
91+
actType, act, err := c2.Read(ctx)
8492
if err != nil {
8593
t.Fatal(err)
8694
}
@@ -90,6 +98,10 @@ func TestConn(t *testing.T) {
9098
t.Fatal(err)
9199
}
92100

101+
if expType != actType {
102+
t.Fatalf("unexpected message typ (%v): %v", expType, actType)
103+
}
104+
93105
if !cmp.Equal(msg, act) {
94106
t.Fatalf("unexpected msg read: %v", cmp.Diff(msg, act))
95107
}

frame_test.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ import (
1313
"time"
1414
_ "unsafe"
1515

16-
"cdr.dev/slog/sloggers/slogtest/assert"
1716
"github.com/gobwas/ws"
1817
_ "github.com/gorilla/websocket"
18+
19+
"nhooyr.io/websocket/internal/test/cmp"
1920
)
2021

2122
func TestHeader(t *testing.T) {
@@ -80,14 +81,22 @@ func testHeader(t *testing.T, h header) {
8081
r := bufio.NewReader(b)
8182

8283
err := writeFrameHeader(h, w)
83-
assert.Success(t, "writeFrameHeader", err)
84+
if err != nil {
85+
t.Fatal(err)
86+
}
8487
err = w.Flush()
85-
assert.Success(t, "flush", err)
88+
if err != nil {
89+
t.Fatal(err)
90+
}
8691

8792
h2, err := readFrameHeader(r)
88-
assert.Success(t, "readFrameHeader", err)
93+
if err != nil {
94+
t.Fatal(err)
95+
}
8996

90-
assert.Equal(t, "header", h, h2)
97+
if !cmp.Equal(h, h2) {
98+
t.Fatal(cmp.Diff(h, h2))
99+
}
91100
}
92101

93102
func Test_mask(t *testing.T) {
@@ -98,8 +107,15 @@ func Test_mask(t *testing.T) {
98107
p := []byte{0xa, 0xb, 0xc, 0xf2, 0xc}
99108
gotKey32 := mask(key32, p)
100109

101-
assert.Equal(t, "mask", []byte{0, 0, 0, 0x0d, 0x6}, p)
102-
assert.Equal(t, "maskKey", bits.RotateLeft32(key32, -8), gotKey32)
110+
expP := []byte{0, 0, 0, 0x0d, 0x6}
111+
if !cmp.Equal(expP, p) {
112+
t.Fatal(cmp.Diff(expP, p))
113+
}
114+
115+
expKey32 := bits.RotateLeft32(key32, -8)
116+
if !cmp.Equal(expKey32, gotKey32) {
117+
t.Fatal(cmp.Diff(expKey32, gotKey32))
118+
}
103119
}
104120

105121
func basicMask(maskKey [4]byte, pos int, b []byte) int {

internal/test/wstest/pipe.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func Pipe(dialOpts *websocket.DialOptions, acceptOpts *websocket.AcceptOptions)
4343
return nil, nil, xerrors.Errorf("failed to get server conn from fake transport: %w", acceptErr)
4444
}
4545

46-
if xrand.True() {
46+
if xrand.Bool() {
4747
return serverConn, clientConn, nil
4848
}
4949
return clientConn, serverConn, nil

internal/test/xrand/xrand.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ func String(n int) string {
3232
return s
3333
}
3434

35-
// True returns a randomly generated boolean.
36-
func True() bool {
35+
// Bool returns a randomly generated boolean.
36+
func Bool() bool {
3737
return Int(2) == 1
3838
}
3939

0 commit comments

Comments
 (0)