Skip to content

Commit

Permalink
Fixes issues where RLock was not unlocked properly (#34)
Browse files Browse the repository at this point in the history
This fixes an issue where RLock was unlocked with an Unlock instead of
and RUnlock causing a panic.
  • Loading branch information
jkongie authored Apr 20, 2023
1 parent 382a1ac commit b5d02d0
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
71 changes: 71 additions & 0 deletions intgtest/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/smartcontractkit/wsrpc"
"github.com/smartcontractkit/wsrpc/connectivity"
"github.com/smartcontractkit/wsrpc/credentials"
pb "github.com/smartcontractkit/wsrpc/intgtest/internal/rpcs"
)

Expand Down Expand Up @@ -159,3 +160,73 @@ func Test_InvalidCredentials(t *testing.T) {

waitForReadyConnection(t, conn)
}

func Test_GetConnectedPeerPublicKeys(t *testing.T) {
keypairs := generateKeys(t)
pubKeys := []ed25519.PublicKey{keypairs.Client1.PubKey}

// Start the server
lis, s := setupServer(t,
wsrpc.Creds(keypairs.Server.PrivKey, pubKeys),
)

// Register the ping server implementation with the wsrpc server
pb.RegisterEchoServer(s, &echoServer{})

// Start serving
go s.Serve(lis)
t.Cleanup(s.Stop)

require.Empty(t, s.GetConnectedPeerPublicKeys())

// Start client
conn, err := setupClientConn(t, 100*time.Millisecond,
wsrpc.WithTransportCreds(keypairs.Client1.PrivKey, keypairs.Server.PubKey),
)
require.NoError(t, err)
t.Cleanup(conn.Close)

waitForReadyConnection(t, conn)

connectedKeys := s.GetConnectedPeerPublicKeys()
require.Len(t, s.GetConnectedPeerPublicKeys(), 1)
actualKey, err := credentials.ToStaticallySizedPublicKey(keypairs.Client1.PubKey)
require.NoError(t, err)
require.Equal(t, actualKey, connectedKeys[0])
}

func Test_GetNotificationChan(t *testing.T) {
keypairs := generateKeys(t)
pubKeys := []ed25519.PublicKey{keypairs.Client1.PubKey}

// Start the server
lis, s := setupServer(t,
wsrpc.Creds(keypairs.Server.PrivKey, pubKeys),
)

// Register the ping server implementation with the wsrpc server
pb.RegisterEchoServer(s, &echoServer{})

// Start serving
go s.Serve(lis)
t.Cleanup(s.Stop)

notifyChan := s.GetConnectionNotifyChan()

// Start client
conn, err := setupClientConn(t, 100*time.Millisecond,
wsrpc.WithTransportCreds(keypairs.Client1.PrivKey, keypairs.Server.PubKey),
)
require.NoError(t, err)
t.Cleanup(conn.Close)

waitForReadyConnection(t, conn)

// Wait for connection notification
select {
case <-notifyChan:
t.Log("received notification")
case <-time.After(3 * time.Second):
assert.Fail(t, "did not notify")
}
}
4 changes: 2 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func (s *Server) UpdatePublicKeys(pubKeys []ed25519.PublicKey) {
// later release.
func (s *Server) GetConnectionNotifyChan() <-chan struct{} {
s.mu.RLock()
defer s.mu.Unlock()
defer s.mu.RUnlock()
return s.connMgr.getNotifyChan()
}

Expand All @@ -369,7 +369,7 @@ func (s *Server) GetConnectionNotifyChan() <-chan struct{} {
// later release.
func (s *Server) GetConnectedPeerPublicKeys() []credentials.StaticSizedPublicKey {
s.mu.RLock()
defer s.mu.Unlock()
defer s.mu.RUnlock()
return s.connMgr.getConnectionPublicKeys()
}

Expand Down

0 comments on commit b5d02d0

Please sign in to comment.