Skip to content

Commit 5445abf

Browse files
committed
Add wspb test
1 parent 2f11da9 commit 5445abf

File tree

2 files changed

+104
-3
lines changed

2 files changed

+104
-3
lines changed

Diff for: conn_test.go

+103-2
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,24 @@ import (
1515
"testing"
1616
"time"
1717

18+
"github.com/golang/protobuf/proto"
19+
"github.com/golang/protobuf/ptypes"
20+
"github.com/golang/protobuf/ptypes/duration"
1821
"golang.org/x/xerrors"
1922

2023
"nhooyr.io/websocket"
2124
"nhooyr.io/websocket/internal/test/cmp"
2225
"nhooyr.io/websocket/internal/test/wstest"
2326
"nhooyr.io/websocket/internal/test/xrand"
2427
"nhooyr.io/websocket/internal/xsync"
28+
"nhooyr.io/websocket/wsjson"
29+
"nhooyr.io/websocket/wspb"
2530
)
2631

2732
func TestConn(t *testing.T) {
2833
t.Parallel()
2934

30-
t.Run("data", func(t *testing.T) {
35+
t.Run("fuzzData", func(t *testing.T) {
3136
t.Parallel()
3237

3338
for i := 0; i < 5; i++ {
@@ -317,6 +322,102 @@ func TestConn(t *testing.T) {
317322
t.Fatal(err)
318323
}
319324
})
325+
326+
t.Run("wsjson", func(t *testing.T) {
327+
t.Parallel()
328+
329+
c1, c2, err := wstest.Pipe(nil, nil)
330+
if err != nil {
331+
t.Fatal(err)
332+
}
333+
defer c2.Close(websocket.StatusInternalError, "")
334+
defer c1.Close(websocket.StatusInternalError, "")
335+
336+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
337+
defer cancel()
338+
339+
echoLoopErr := xsync.Go(func() error {
340+
err := wstest.EchoLoop(ctx, c2)
341+
return assertCloseStatus(websocket.StatusNormalClosure, err)
342+
})
343+
defer func() {
344+
err := <-echoLoopErr
345+
if err != nil {
346+
t.Errorf("echo loop error: %v", err)
347+
}
348+
}()
349+
defer cancel()
350+
351+
c1.SetReadLimit(131072)
352+
353+
exp := xrand.String(xrand.Int(131072))
354+
err = wsjson.Write(ctx, c1, exp)
355+
if err != nil {
356+
t.Fatal(err)
357+
}
358+
359+
var act interface{}
360+
err = wsjson.Read(ctx, c1, &act)
361+
if err != nil {
362+
t.Fatal(err)
363+
}
364+
if exp != act {
365+
t.Fatal(cmp.Diff(exp, act))
366+
}
367+
368+
err = c1.Close(websocket.StatusNormalClosure, "")
369+
if err != nil {
370+
t.Fatalf("unexpected error: %v", err)
371+
}
372+
})
373+
374+
t.Run("wspb", func(t *testing.T) {
375+
t.Parallel()
376+
377+
c1, c2, err := wstest.Pipe(nil, nil)
378+
if err != nil {
379+
t.Fatal(err)
380+
}
381+
defer c2.Close(websocket.StatusInternalError, "")
382+
defer c1.Close(websocket.StatusInternalError, "")
383+
384+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
385+
defer cancel()
386+
387+
echoLoopErr := xsync.Go(func() error {
388+
err := wstest.EchoLoop(ctx, c2)
389+
return assertCloseStatus(websocket.StatusNormalClosure, err)
390+
})
391+
defer func() {
392+
err := <-echoLoopErr
393+
if err != nil {
394+
t.Errorf("echo loop error: %v", err)
395+
}
396+
}()
397+
defer cancel()
398+
399+
c1.SetReadLimit(131072)
400+
401+
exp := ptypes.DurationProto(100)
402+
err = wspb.Write(ctx, c1, exp)
403+
if err != nil {
404+
t.Fatal(err)
405+
}
406+
407+
act := &duration.Duration{}
408+
err = wspb.Read(ctx, c1, act)
409+
if err != nil {
410+
t.Fatal(err)
411+
}
412+
if !proto.Equal(exp, act) {
413+
t.Fatal(cmp.Diff(exp, act))
414+
}
415+
416+
err = c1.Close(websocket.StatusNormalClosure, "")
417+
if err != nil {
418+
t.Fatalf("unexpected error: %v", err)
419+
}
420+
})
320421
}
321422

322423
func TestWasm(t *testing.T) {
@@ -345,7 +446,7 @@ func TestWasm(t *testing.T) {
345446
defer wg.Wait()
346447
defer s.Close()
347448

348-
ctx, cancel := context.WithTimeout(context.Background(), time.Second*20)
449+
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
349450
defer cancel()
350451

351452
cmd := exec.CommandContext(ctx, "go", "test", "-exec=wasmbrowsertest", "./...")

Diff for: ws_js_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
func TestWasm(t *testing.T) {
1616
t.Parallel()
1717

18-
ctx, cancel := context.WithTimeout(context.Background(), time.Second*20)
18+
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
1919
defer cancel()
2020

2121
c, resp, err := websocket.Dial(ctx, os.Getenv("WS_ECHO_SERVER_URL"), &websocket.DialOptions{

0 commit comments

Comments
 (0)