From 81bb3f15de8f90ef11197471d7f6aee26052220b Mon Sep 17 00:00:00 2001 From: Sergey Cheperis Date: Sat, 6 Jan 2024 21:25:39 +0300 Subject: [PATCH] Apply https://github.com/r3labs/sse/pull/151 --- client.go | 10 +++++----- client_test.go | 6 ++---- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/client.go b/client.go index 78c936e..4bfb280 100644 --- a/client.go +++ b/client.go @@ -208,6 +208,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() @@ -221,11 +226,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 0e7cffa..ecb8c45 100644 --- a/client_test.go +++ b/client_test.go @@ -215,7 +215,7 @@ func TestClientOnDisconnect(t *testing.T) { } func TestClientOnConnect(t *testing.T) { - setup(false) + newServer() defer cleanup() c := NewClient(urlPath) @@ -227,10 +227,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) {