From e334554d4a229105d8e5f956f9aa831345eff647 Mon Sep 17 00:00:00 2001
From: Erik Brakkee <erik@brakkee.org>
Date: Sat, 24 Aug 2024 21:17:56 +0200
Subject: [PATCH] multiple clients and agents.

---
 pkg/server/matchmaker/matchmaker_test.go | 54 ++++++++++++++++++++++--
 1 file changed, 50 insertions(+), 4 deletions(-)

diff --git a/pkg/server/matchmaker/matchmaker_test.go b/pkg/server/matchmaker/matchmaker_test.go
index 41b9f64..c220042 100644
--- a/pkg/server/matchmaker/matchmaker_test.go
+++ b/pkg/server/matchmaker/matchmaker_test.go
@@ -11,6 +11,7 @@ import (
 	"io"
 	"log"
 	"net/http"
+	"sync"
 	"testing"
 	"time"
 )
@@ -108,6 +109,8 @@ func (agent *TestAgent) Register(s *MatchMakerTestSuite) error {
 type TestClient struct {
 	clientSideConn io.ReadWriteCloser
 	serverSIdeConn iowrappers.ReadWriteAddrCloser
+	publicId       models.RendezVousId
+	clientId       models.ClientId
 }
 
 func NewTestClient(ctx context.Context) *TestClient {
@@ -149,12 +152,12 @@ func (s *MatchMakerTestSuite) Test_singleAgentAndClient() {
 	waitForAgentFunc := s.registerAgent(publicId, agent)
 	go waitForAgentFunc()
 
-	client, clientId, err := s.connectClient(publicId)
+	client, err := s.connectClient(publicId)
 	s.Nil(err)
 
 	s.checkState(1, 1)
 
-	agentClientSideConn, err := agent.listener.GetConnection(string(clientId))
+	agentClientSideConn, err := agent.listener.GetConnection(string(client.clientId))
 	log.Printf("Agent side conn %v", agentClientSideConn)
 	s.Nil(err)
 	testsupport.BidirectionalConnectionCheck(
@@ -168,7 +171,48 @@ func (s *MatchMakerTestSuite) Test_singleAgentAndClient() {
 	s.checkState(1, 0)
 }
 
-func (s *MatchMakerTestSuite) connectClient(publicId models.RendezVousId) (*TestClient, models.ClientId, error) {
+func (s *MatchMakerTestSuite) Test_multipleAgentsAndClients() {
+	agents := []string{"abc", "def", "ghi"}
+	clients := map[string]int{"abc": 3, "def": 2, "ghi": 5}
+	log.Printf("agents %v, clients %v", agents, clients)
+	testAgents := make(map[string]*TestAgent)
+	for _, publicId := range agents {
+		agent := NewTestAgent(s.ctx)
+		testAgents[publicId] = agent
+		waitForAgentFunc := s.registerAgent(models.RendezVousId(publicId), agent)
+		s.Require().NotNil(waitForAgentFunc)
+		go waitForAgentFunc()
+	}
+	testClients := make([]*TestClient, 0)
+	for publicId, nclients := range clients {
+		for range nclients {
+			client, err := s.connectClient(models.RendezVousId(publicId))
+			s.Require().Nil(err)
+			testClients = append(testClients, client)
+		}
+	}
+
+	wg := sync.WaitGroup{}
+
+	// bidirectional connection test
+	for _, testClient := range testClients {
+		wg.Add(1)
+		go func() {
+			defer wg.Done()
+			agent := testAgents[string(testClient.publicId)]
+			agentClientSideConn, err := agent.listener.GetConnection(string(testClient.clientId))
+			s.Nil(err)
+			log.Printf("Testing bi-directional commication client %v agent %v", testClient.clientId, testClient.publicId)
+			testsupport.BidirectionalConnectionCheck(
+				&s.Suite, "testmsg"+string(testClient.clientId),
+				testClient.clientSideConn,
+				agentClientSideConn)
+		}()
+	}
+	wg.Wait()
+}
+
+func (s *MatchMakerTestSuite) connectClient(publicId models.RendezVousId) (*TestClient, error) {
 	client := NewTestClient(s.ctx)
 	var clientId models.ClientId
 	res := testsupport.RunAndWait(
@@ -192,7 +236,9 @@ func (s *MatchMakerTestSuite) connectClient(publicId models.RendezVousId) (*Test
 	if res[0] != nil {
 		err = res[0].(error)
 	}
-	return client, clientId, err
+	client.publicId = publicId
+	client.clientId = clientId
+	return client, err
 }
 
 func (s *MatchMakerTestSuite) checkState(nAgents int, nClients int) {