Skip to content

Commit 412f574

Browse files
committed
Simplify tests
Many new helpers. Closes #124
1 parent 5a463b7 commit 412f574

File tree

5 files changed

+315
-449
lines changed

5 files changed

+315
-449
lines changed

cmp_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package websocket_test
2+
3+
import (
4+
"reflect"
5+
6+
"github.com/google/go-cmp/cmp"
7+
)
8+
9+
// https://github.com/google/go-cmp/issues/40#issuecomment-328615283
10+
func cmpDiff(exp, act interface{}) string {
11+
return cmp.Diff(exp, act, deepAllowUnexported(exp, act))
12+
}
13+
14+
func deepAllowUnexported(vs ...interface{}) cmp.Option {
15+
m := make(map[reflect.Type]struct{})
16+
for _, v := range vs {
17+
structTypes(reflect.ValueOf(v), m)
18+
}
19+
var typs []interface{}
20+
for t := range m {
21+
typs = append(typs, reflect.New(t).Elem().Interface())
22+
}
23+
return cmp.AllowUnexported(typs...)
24+
}
25+
26+
func structTypes(v reflect.Value, m map[reflect.Type]struct{}) {
27+
if !v.IsValid() {
28+
return
29+
}
30+
switch v.Kind() {
31+
case reflect.Ptr:
32+
if !v.IsNil() {
33+
structTypes(v.Elem(), m)
34+
}
35+
case reflect.Interface:
36+
if !v.IsNil() {
37+
structTypes(v.Elem(), m)
38+
}
39+
case reflect.Slice, reflect.Array:
40+
for i := 0; i < v.Len(); i++ {
41+
structTypes(v.Index(i), m)
42+
}
43+
case reflect.Map:
44+
for _, k := range v.MapKeys() {
45+
structTypes(v.MapIndex(k), m)
46+
}
47+
case reflect.Struct:
48+
m[v.Type()] = struct{}{}
49+
for i := 0; i < v.NumField(); i++ {
50+
structTypes(v.Field(i), m)
51+
}
52+
}
53+
}

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ require (
77
github.com/google/go-cmp v0.2.0
88
github.com/kr/pretty v0.1.0 // indirect
99
go.coder.com/go-tools v0.0.0-20190317003359-0c6a35b74a16
10+
go.uber.org/atomic v1.4.0 // indirect
11+
go.uber.org/multierr v1.1.0
1012
golang.org/x/lint v0.0.0-20190409202823-959b441ac422
1113
golang.org/x/net v0.0.0-20190424112056-4829fb13d2c6
1214
golang.org/x/text v0.3.2 // indirect

go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0
4040
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
4141
go.coder.com/go-tools v0.0.0-20190317003359-0c6a35b74a16 h1:3gGa1bM0nG7Ruhu5b7wKnoOOwAD/fJ8iyyAcpOzDG3A=
4242
go.coder.com/go-tools v0.0.0-20190317003359-0c6a35b74a16/go.mod h1:iKV5yK9t+J5nG9O3uF6KYdPEz3dyfMyB15MN1rbQ8Qw=
43+
go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU=
44+
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
45+
go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI=
46+
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
4347
golang.org/x/crypto v0.0.0-20180426230345-b49d69b5da94/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
4448
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
4549
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

websocket.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ func (c *Conn) timeoutLoop() {
166166
case readCtx = <-c.setReadTimeout:
167167

168168
case <-readCtx.Done():
169-
c.close(xerrors.Errorf("data read timed out: %w", readCtx.Err()))
169+
c.close(xerrors.Errorf("read timed out: %w", readCtx.Err()))
170170
case <-writeCtx.Done():
171-
c.close(xerrors.Errorf("data write timed out: %w", writeCtx.Err()))
171+
c.close(xerrors.Errorf("write timed out: %w", writeCtx.Err()))
172172
}
173173
}
174174
}

0 commit comments

Comments
 (0)