From 342acb0fe7ccfd3c263e72b73ea13de8acefe390 Mon Sep 17 00:00:00 2001 From: Ivan Kozlovic Date: Wed, 1 Jul 2020 15:53:08 -0600 Subject: [PATCH] Fixed async client tests In some tests, async clients are used with a running server (which was not original intent). This resulted in the client writeLoop to be started twice. Signed-off-by: Ivan Kozlovic --- server/client_test.go | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/server/client_test.go b/server/client_test.go index c1b20f6b..ee4eabcb 100644 --- a/server/client_test.go +++ b/server/client_test.go @@ -23,6 +23,7 @@ import ( "net" "reflect" "regexp" + "runtime" "strings" "sync" "sync/atomic" @@ -73,12 +74,21 @@ func (c *testAsyncClient) parseAndClose(proto []byte) { } func createClientAsync(ch chan *client, s *Server, cli net.Conn) { - s.grWG.Add(1) + // Normally, those type of clients are used against non running servers. + // However, some don't, which would then cause the writeLoop to be + // started twice for the same client (since createClient() start both + // read and write loop if it is detected as running). + startWriteLoop := !s.isRunning() + if startWriteLoop { + s.grWG.Add(1) + } go func() { c := s.createClient(cli, nil) // Must be here to suppress +OK c.opts.Verbose = false - go c.writeLoop() + if startWriteLoop { + go c.writeLoop() + } ch <- c }() } @@ -166,6 +176,23 @@ func checkAccClientsCount(t *testing.T, acc *Account, expected int) { }) } +func TestAsyncClientWithRunningServer(t *testing.T) { + o := DefaultOptions() + s := RunServer(o) + defer s.Shutdown() + + c, _, _ := newClientForServer(s) + defer c.close() + + buf := make([]byte, 1000000) + n := runtime.Stack(buf, true) + + writeLoopTxt := fmt.Sprintf("writeLoop(%p)", c.client) + if count := strings.Count(string(buf[:n]), writeLoopTxt); count != 1 { + t.Fatalf("writeLoop for client started more than once: %v", count) + } +} + func TestClientCreateAndInfo(t *testing.T) { c, l := setUpClientWithResponse() defer c.close()