Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client: reconnect after an EOF #148

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"encoding/base64"
"errors"
"fmt"
"io"
"net/http"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -213,10 +212,6 @@ func (c *Client) readLoop(reader *EventStreamReader, outCh chan *Event, erChan c
// Read each new line and process the type of event
event, err := reader.ReadEvent()
if err != nil {
if err == io.EOF {
erChan <- nil
return
}
// run user specified disconnect function
if c.disconnectcb != nil {
c.Connected = false
Expand Down
37 changes: 37 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"crypto/rand"
"encoding/hex"
"io"
"net/http"
"net/http/httptest"
"runtime"
Expand Down Expand Up @@ -258,6 +259,42 @@ func TestClientChanReconnect(t *testing.T) {
c.Unsubscribe(events)
}

func TestClientChanReconnectOnEOF(t *testing.T) {
setup(false)
defer cleanup()
streamID := "test"

c := NewClient(urlPath)

var (
reconnectErr error
reconnectDuration time.Duration
)
c.ReconnectStrategy = backoff.NewConstantBackOff(time.Millisecond * 10)
c.ReconnectNotify = func(err error, duration time.Duration) {
reconnectErr = err
reconnectDuration = duration
}

events := make(chan *Event)
err := c.SubscribeChan(streamID, events)
require.Nil(t, err)

msg, err := wait(events, time.Millisecond*100)
assert.NoError(t, err)
assert.Equal(t, []byte(`ping`), msg)

srv.RemoveStream(streamID)
srv.CreateStream(streamID)
msg, err = wait(events, time.Millisecond*100)
assert.NoError(t, err)
assert.Equal(t, io.EOF, reconnectErr)
assert.Equal(t, time.Millisecond*10, reconnectDuration)
assert.Equal(t, []byte(`ping`), msg)

c.Unsubscribe(events)
}

func TestClientUnsubscribe(t *testing.T) {
setup(false)
defer cleanup()
Expand Down