From 95204a9f5b25bf812167f6d42b37f985db10c39f Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Sat, 24 Aug 2024 22:30:21 +0200 Subject: [PATCH] modification for testability caused a panic when client tries to connect to unknown agent. --- pkg/server/matchmaker/matchmaker.go | 4 +++- pkg/server/matchmaker/matchmaker_test.go | 13 ++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/pkg/server/matchmaker/matchmaker.go b/pkg/server/matchmaker/matchmaker.go index 5a2e04a..e933c1d 100644 --- a/pkg/server/matchmaker/matchmaker.go +++ b/pkg/server/matchmaker/matchmaker.go @@ -119,7 +119,9 @@ func (converge *MatchMaker) Connect(wsProxyMode bool, client, err := converge.admin.AddClient(publicId, conn) cleanUpFunc := func() { - converge.admin.RemoveClient(client) + if client != nil { + converge.admin.RemoveClient(client) + } converge.logStatus() } defer func() { diff --git a/pkg/server/matchmaker/matchmaker_test.go b/pkg/server/matchmaker/matchmaker_test.go index c220042..da3261e 100644 --- a/pkg/server/matchmaker/matchmaker_test.go +++ b/pkg/server/matchmaker/matchmaker_test.go @@ -171,6 +171,14 @@ func (s *MatchMakerTestSuite) Test_singleAgentAndClient() { s.checkState(1, 0) } +func (s *MatchMakerTestSuite) Test_ConnectCLientToUnknownAgent() { + publicId := models.RendezVousId("abc") + + client, err := s.connectClient(publicId) + s.NotNil(err) + s.Nil(client) +} + func (s *MatchMakerTestSuite) Test_multipleAgentsAndClients() { agents := []string{"abc", "def", "ghi"} clients := map[string]int{"abc": 3, "def": 2, "ghi": 5} @@ -232,13 +240,12 @@ func (s *MatchMakerTestSuite) connectClient(publicId models.RendezVousId) (*Test return nil }) - var err error = nil if res[0] != nil { - err = res[0].(error) + return nil, res[0].(error) } client.publicId = publicId client.clientId = clientId - return client, err + return client, nil } func (s *MatchMakerTestSuite) checkState(nAgents int, nClients int) {