From 95a748ee3cf524ecb49cb0d52a904a536c7908e1 Mon Sep 17 00:00:00 2001 From: Patrick Dawkins Date: Wed, 8 Feb 2023 23:50:13 +0000 Subject: [PATCH] Call OnConnect after a connection regardless of events --- client.go | 10 +++++----- client_test.go | 6 ++---- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/client.go b/client.go index 61772b6..510e22e 100644 --- a/client.go +++ b/client.go @@ -209,6 +209,11 @@ func (c *Client) startReadLoop(reader *EventStreamReader) (chan *Event, chan err } func (c *Client) readLoop(reader *EventStreamReader, outCh chan *Event, erChan chan error) { + if !c.Connected && c.connectedcb != nil { + c.Connected = true + c.connectedcb(c) + } + for { // Read each new line and process the type of event event, err := reader.ReadEvent() @@ -226,11 +231,6 @@ func (c *Client) readLoop(reader *EventStreamReader, outCh chan *Event, erChan c return } - if !c.Connected && c.connectedcb != nil { - c.Connected = true - c.connectedcb(c) - } - // If we get an error, ignore it. var msg *Event if msg, err = c.processEvent(event); err == nil { diff --git a/client_test.go b/client_test.go index 3468df0..36e61ce 100644 --- a/client_test.go +++ b/client_test.go @@ -214,7 +214,7 @@ func TestClientOnDisconnect(t *testing.T) { } func TestClientOnConnect(t *testing.T) { - setup(false) + newServer() defer cleanup() c := NewClient(urlPath) @@ -226,10 +226,8 @@ func TestClientOnConnect(t *testing.T) { go c.Subscribe("test", func(msg *Event) {}) - time.Sleep(time.Second) + time.Sleep(time.Millisecond * 50) assert.Equal(t, struct{}{}, <-called) - - server.CloseClientConnections() } func TestClientChanReconnect(t *testing.T) {