good morning!!!!

Skip to content
Snippets Groups Projects
Unverified Commit 3f2589ff authored by Anmol Sethi's avatar Anmol Sethi
Browse files

Remove quite a bit of slog

parent faadcc96
No related branches found
No related tags found
No related merge requests found
......@@ -14,4 +14,4 @@ coveralls: gotest
gotest:
go test -covermode=count -coverprofile=ci/out/coverage.prof -coverpkg=./... $${GOTESTFLAGS-} ./...
sed -i '/stringer\.go/d' ci/out/coverage.prof
sed -i '/assert/d' ci/out/coverage.prof
sed -i '/nhooyr.io\/websocket\/internal\/test/d' ci/out/coverage.prof
......@@ -8,7 +8,7 @@ import (
"strings"
"testing"
"cdr.dev/slog/sloggers/slogtest/assert"
"nhooyr.io/websocket/internal/test/cmp"
)
func TestCloseError(t *testing.T) {
......@@ -51,13 +51,23 @@ func TestCloseError(t *testing.T) {
t.Parallel()
_, err := tc.ce.bytesErr()
if tc.success {
assert.Success(t, "CloseError.bytesErr", err)
} else {
assert.Error(t, "CloseError.bytesErr", err)
if tc.success != (err == nil) {
t.Fatalf("unexpected error value (wanted err == nil == %v): %v", tc.success, err)
}
})
}
t.Run("Error", func(t *testing.T) {
exp := `status = StatusInternalError and reason = "meow"`
act := CloseError{
Code: StatusInternalError,
Reason: "meow",
}.Error()
if (act) != exp {
t.Fatal(cmp.Diff(exp, act))
}
})
}
func Test_parseClosePayload(t *testing.T) {
......@@ -104,10 +114,14 @@ func Test_parseClosePayload(t *testing.T) {
ce, err := parseClosePayload(tc.p)
if tc.success {
assert.Success(t, "parse err", err)
assert.Equal(t, "ce", tc.ce, ce)
} else {
assert.Error(t, "parse err", err)
if err != nil {
t.Fatal(err)
}
if !cmp.Equal(tc.ce, ce) {
t.Fatalf("expected %v but got %v", tc.ce, ce)
}
} else if err == nil {
t.Errorf("expected error: %v %v", ce, err)
}
})
}
......@@ -153,7 +167,10 @@ func Test_validWireCloseCode(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, "valid", tc.valid, validWireCloseCode(tc.code))
act := validWireCloseCode(tc.code)
if !cmp.Equal(tc.valid, act) {
t.Fatalf("unexpected valid: %v", cmp.Diff(tc.valid, act))
}
})
}
}
......@@ -190,7 +207,10 @@ func TestCloseStatus(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, "closeStatus", tc.exp, CloseStatus(tc.in))
act := CloseStatus(tc.in)
if !cmp.Equal(tc.exp, act) {
t.Fatalf("unexpected closeStatus: %v", cmp.Diff(tc.exp, act))
}
})
}
}
......@@ -14,13 +14,17 @@ import (
"nhooyr.io/websocket/internal/test/cmp"
"nhooyr.io/websocket/internal/test/wstest"
"nhooyr.io/websocket/internal/test/xrand"
"nhooyr.io/websocket/wsjson"
)
func goFn(fn func() error) chan error {
errs := make(chan error)
go func() {
defer close(errs)
defer func() {
r := recover()
if r != nil {
errs <- xerrors.Errorf("panic in gofn: %v", r)
}
}()
errs <- fn()
}()
......@@ -33,7 +37,7 @@ func TestConn(t *testing.T) {
t.Run("data", func(t *testing.T) {
t.Parallel()
for i := 0; i < 10; i++ {
for i := 0; i < 5; i++ {
t.Run("", func(t *testing.T) {
t.Parallel()
......@@ -41,7 +45,7 @@ func TestConn(t *testing.T) {
defer cancel()
copts := websocket.CompressionOptions{
Mode: websocket.CompressionMode(xrand.Int(int(websocket.CompressionDisabled))),
Mode: websocket.CompressionMode(xrand.Int(int(websocket.CompressionDisabled) + 1)),
Threshold: xrand.Int(9999),
}
......@@ -70,17 +74,21 @@ func TestConn(t *testing.T) {
c2.SetReadLimit(1 << 30)
for i := 0; i < 10; i++ {
for i := 0; i < 5; i++ {
n := xrand.Int(131_072)
msg := xrand.String(n)
msg := xrand.Bytes(n)
expType := websocket.MessageBinary
if xrand.Bool() {
expType = websocket.MessageText
}
writeErr := goFn(func() error {
return wsjson.Write(ctx, c2, msg)
return c2.Write(ctx, expType, msg)
})
var act interface{}
err := wsjson.Read(ctx, c2, &act)
actType, act, err := c2.Read(ctx)
if err != nil {
t.Fatal(err)
}
......@@ -90,6 +98,10 @@ func TestConn(t *testing.T) {
t.Fatal(err)
}
if expType != actType {
t.Fatalf("unexpected message typ (%v): %v", expType, actType)
}
if !cmp.Equal(msg, act) {
t.Fatalf("unexpected msg read: %v", cmp.Diff(msg, act))
}
......
......@@ -13,9 +13,10 @@ import (
"time"
_ "unsafe"
"cdr.dev/slog/sloggers/slogtest/assert"
"github.com/gobwas/ws"
_ "github.com/gorilla/websocket"
"nhooyr.io/websocket/internal/test/cmp"
)
func TestHeader(t *testing.T) {
......@@ -80,14 +81,22 @@ func testHeader(t *testing.T, h header) {
r := bufio.NewReader(b)
err := writeFrameHeader(h, w)
assert.Success(t, "writeFrameHeader", err)
if err != nil {
t.Fatal(err)
}
err = w.Flush()
assert.Success(t, "flush", err)
if err != nil {
t.Fatal(err)
}
h2, err := readFrameHeader(r)
assert.Success(t, "readFrameHeader", err)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "header", h, h2)
if !cmp.Equal(h, h2) {
t.Fatal(cmp.Diff(h, h2))
}
}
func Test_mask(t *testing.T) {
......@@ -98,8 +107,15 @@ func Test_mask(t *testing.T) {
p := []byte{0xa, 0xb, 0xc, 0xf2, 0xc}
gotKey32 := mask(key32, p)
assert.Equal(t, "mask", []byte{0, 0, 0, 0x0d, 0x6}, p)
assert.Equal(t, "maskKey", bits.RotateLeft32(key32, -8), gotKey32)
expP := []byte{0, 0, 0, 0x0d, 0x6}
if !cmp.Equal(expP, p) {
t.Fatal(cmp.Diff(expP, p))
}
expKey32 := bits.RotateLeft32(key32, -8)
if !cmp.Equal(expKey32, gotKey32) {
t.Fatal(cmp.Diff(expKey32, gotKey32))
}
}
func basicMask(maskKey [4]byte, pos int, b []byte) int {
......
......@@ -43,7 +43,7 @@ func Pipe(dialOpts *websocket.DialOptions, acceptOpts *websocket.AcceptOptions)
return nil, nil, xerrors.Errorf("failed to get server conn from fake transport: %w", acceptErr)
}
if xrand.True() {
if xrand.Bool() {
return serverConn, clientConn, nil
}
return clientConn, serverConn, nil
......
......@@ -32,8 +32,8 @@ func String(n int) string {
return s
}
// True returns a randomly generated boolean.
func True() bool {
// Bool returns a randomly generated boolean.
func Bool() bool {
return Int(2) == 1
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment