mirror of
https://github.com/gogrlx/nats-server.git
synced 2026-04-02 11:48:43 -07:00
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 <ivan@synadia.com>
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user