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 2 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
42 changes: 42 additions & 0 deletions transport/websocketpeer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package transport_test

import (
"context"
"fmt"
"testing"

"github.com/stretchr/testify/require"

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

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

routerConfig := &router.Config{
RealmConfigs: []*router.RealmConfig{
{
URI: wamp.URI("nexus.test.realm"),
},
},
}
r, err := router.NewRouter(routerConfig, nil)
require.NoError(t, err)
defer r.Close()

const wsAddr = "127.0.0.1:8000"
closer, err := router.NewWebsocketServer(r).ListenAndServe(wsAddr)
require.NoError(t, err)
defer closer.Close()

client, err := transport.ConnectWebsocketPeer(
context.Background(), fmt.Sprintf("ws://%s/", wsAddr), serialize.JSON, nil, r.Logger(), nil)
require.NoError(t, err)

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

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