func ConnectClients() { var wg sync.WaitGroup wg.Add(cliNum) for i := 0; i < cliNum; i++ { go func() { //create a client and connect to server cli := client.NewClient() cli.Connect(addr, port) //wait the server reply <-cli.IncomingId() //add to map climap.Set(cli.Id(), cli) if showStat { allStats.ClientsConnected.Increase(1) } wg.Done() }() } //wait for all clients to be ready wg.Wait() allCliId = climap.GetKeys() }
func TestPostDisconnect(t *testing.T) { assert := assert.New(t) cli := client.NewClient() err := cli.Connect(Addr, Port) assert.Nil(err, "new client should connect") //wait for id <-cli.IncomingId() cli.Send(message.NewRequest(message.List)) //wait for list <-cli.IncomingList() if len(cli.List()) > 0 { time.Sleep(time.Millisecond * 200) cli.Send(message.NewRequest(message.List)) //wait for list <-cli.IncomingList() } assert.Len(cli.List(), 0, "Server should return empty list") cli.Disconnect() }
func TestIdentity(t *testing.T) { assert := assert.New(t) var wg sync.WaitGroup wg.Add(ClientsNum) for i := 0; i < ClientsNum; i++ { go func() { c := client.NewClient() err := c.Connect(Addr, Port) assert.Nil(err, "Connection should be valid") ans := <-c.IncomingId() assert.NotNil(ans, "Should receive an answer from ID channel") climap.Set(c.Id(), c) wg.Done() }() } wg.Wait() allCliId = climap.GetKeys() }