MCPcopy
hub / github.com/coder/websocket / TestConnClosePropagation

Function TestConnClosePropagation

conn_test.go:736–880  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

734}
735
736func TestConnClosePropagation(t *testing.T) {
737 t.Parallel()
738
739 want := []byte("hello")
740 keepWriting := func(c *websocket.Conn) <-chan error {
741 return xsync.Go(func() error {
742 for {
743 err := c.Write(context.Background(), websocket.MessageText, want)
744 if err != nil {
745 return err
746 }
747 }
748 })
749 }
750 keepReading := func(c *websocket.Conn) <-chan error {
751 return xsync.Go(func() error {
752 for {
753 _, got, err := c.Read(context.Background())
754 if err != nil {
755 return err
756 }
757 if !bytes.Equal(want, got) {
758 return fmt.Errorf("unexpected message: want %q, got %q", want, got)
759 }
760 }
761 })
762 }
763 checkReadErr := func(t *testing.T, err error) {
764 // Check read error (output depends on when read is called in relation to connection closure).
765 var ce websocket.CloseError
766 if errors.As(err, &ce) {
767 assert.Equal(t, "", websocket.StatusNormalClosure, ce.Code)
768 } else {
769 assert.ErrorIs(t, net.ErrClosed, err)
770 }
771 }
772 checkConnErrs := func(t *testing.T, conn ...*websocket.Conn) {
773 for _, c := range conn {
774 // Check write error.
775 err := c.Write(context.Background(), websocket.MessageText, want)
776 assert.ErrorIs(t, net.ErrClosed, err)
777
778 _, _, err = c.Read(context.Background())
779 checkReadErr(t, err)
780 }
781 }
782
783 t.Run("CloseOtherSideDuringWrite", func(t *testing.T) {
784 tt, this, other := newConnTest(t, nil, nil)
785
786 _ = this.CloseRead(tt.ctx)
787 thisWriteErr := keepWriting(this)
788
789 _, got, err := other.Read(tt.ctx)
790 assert.Success(t, err)
791 assert.Equal(t, "msg", want, got)
792
793 err = other.Close(websocket.StatusNormalClosure, "")

Callers

nothing calls this directly

Calls 9

GoFunction · 0.92
EqualFunction · 0.92
ErrorIsFunction · 0.92
SuccessFunction · 0.92
newConnTestFunction · 0.85
WriteMethod · 0.45
ReadMethod · 0.45
CloseReadMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…