multiple clients and agents.

This commit is contained in:
Erik Brakkee 2024-08-24 21:17:56 +02:00
parent 1ec7241c95
commit e334554d4a

View File

@ -11,6 +11,7 @@ import (
"io" "io"
"log" "log"
"net/http" "net/http"
"sync"
"testing" "testing"
"time" "time"
) )
@ -108,6 +109,8 @@ func (agent *TestAgent) Register(s *MatchMakerTestSuite) error {
type TestClient struct { type TestClient struct {
clientSideConn io.ReadWriteCloser clientSideConn io.ReadWriteCloser
serverSIdeConn iowrappers.ReadWriteAddrCloser serverSIdeConn iowrappers.ReadWriteAddrCloser
publicId models.RendezVousId
clientId models.ClientId
} }
func NewTestClient(ctx context.Context) *TestClient { func NewTestClient(ctx context.Context) *TestClient {
@ -149,12 +152,12 @@ func (s *MatchMakerTestSuite) Test_singleAgentAndClient() {
waitForAgentFunc := s.registerAgent(publicId, agent) waitForAgentFunc := s.registerAgent(publicId, agent)
go waitForAgentFunc() go waitForAgentFunc()
client, clientId, err := s.connectClient(publicId) client, err := s.connectClient(publicId)
s.Nil(err) s.Nil(err)
s.checkState(1, 1) 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) log.Printf("Agent side conn %v", agentClientSideConn)
s.Nil(err) s.Nil(err)
testsupport.BidirectionalConnectionCheck( testsupport.BidirectionalConnectionCheck(
@ -168,7 +171,48 @@ func (s *MatchMakerTestSuite) Test_singleAgentAndClient() {
s.checkState(1, 0) 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) client := NewTestClient(s.ctx)
var clientId models.ClientId var clientId models.ClientId
res := testsupport.RunAndWait( res := testsupport.RunAndWait(
@ -192,7 +236,9 @@ func (s *MatchMakerTestSuite) connectClient(publicId models.RendezVousId) (*Test
if res[0] != nil { if res[0] != nil {
err = res[0].(error) 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) { func (s *MatchMakerTestSuite) checkState(nAgents int, nClients int) {