Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

websocketpeer: don't crash if Close() called multiple times #313

Merged
merged 3 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions transport/websocketpeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,12 @@ func (w *websocketPeer) IsLocal() bool { return false }
//
// *** Do not call Send after calling Close. ***
func (w *websocketPeer) Close() {
select {
case <-w.closed:
return
default:
}

// Tell sendHandler to exit and discard any queued messages. Do not close
// wr channel in case there are incoming messages during close.
w.cancelSender()
Expand Down
50 changes: 50 additions & 0 deletions transport/websocketpeer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package transport_test

import (
"fmt"
"log"
"testing"
"time"

"github.com/gorilla/websocket"

"github.com/gammazero/nexus/v3/transport"
"github.com/gammazero/nexus/v3/transport/serialize"
)

type mockWSConnection struct{}

func (m mockWSConnection) Close() error { return nil }

func (m mockWSConnection) WriteControl(messageType int, data []byte, deadline time.Time) error {
return nil
}

func (m mockWSConnection) WriteMessage(messageType int, data []byte) error {
return nil
}

func (m mockWSConnection) ReadMessage() (messageType int, p []byte, err error) {
err = fmt.Errorf("implement me")
return
}

func (m mockWSConnection) SetPongHandler(h func(appData string) error) {}

func (m mockWSConnection) SetPingHandler(h func(appData string) error) {}

func (m mockWSConnection) Subprotocol() string { return "" }

func newMockSession() transport.WebsocketConnection {
return &mockWSConnection{}
}

func TestCloseWebsocketPeer(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of starting a router and creating a websocket connection you should just mock websocket connection. You test will look something like

func TestCloseWebsocketPeer(t *testing.T) {
	logger := log.Default()
	peer := transport.NewWebsocketPeer(newMockSession(), &serialize.JSONSerializer{}, 1, logger, 0, 0)

	// Close the client connection.
	peer.Close()

	// Try closing the client connection again. It should not cause an error.
	peer.Close()
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

peer := transport.NewWebsocketPeer(newMockSession(), &serialize.JSONSerializer{}, websocket.TextMessage, log.Default(), 0, 0)

// Close the client connection.
peer.Close()

// Try closing the client connection again. It should not cause an error.
peer.Close()
}
Loading