mirror of
https://git.mills.io/prologic/msgbus.git
synced 2024-06-16 03:49:03 +00:00
check for context cancelled errors using errors.Is (#39)
Co-authored-by: mlctrez <mlctrez@gmail.com> Reviewed-on: https://git.mills.io/prologic/msgbus/pulls/39 Reviewed-by: James Mills <james@mills.io> Co-authored-by: mlctrez <mlctrez@noreply@mills.io> Co-committed-by: mlctrez <mlctrez@noreply@mills.io>
This commit is contained in:
parent
a421534ee1
commit
902d51c951
@ -235,7 +235,7 @@ func (s *Subscriber) connect(ctx context.Context) (*websocket.Conn, error) {
|
|||||||
conn, _, err := websocket.Dial(ctx, url, nil)
|
conn, _, err := websocket.Dial(ctx, url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Debugf("dial error")
|
log.WithError(err).Debugf("dial error")
|
||||||
if err == context.Canceled {
|
if errors.Is(err, context.Canceled) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
log.Debugf("reconnecting in %s", b.Duration())
|
log.Debugf("reconnecting in %s", b.Duration())
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
@ -39,3 +41,50 @@ func TestClientPublish(t *testing.T) {
|
|||||||
assert.Equal(topic, msg.Topic)
|
assert.Equal(topic, msg.Topic)
|
||||||
assert.Equal([]byte("hello world"), msg.Payload)
|
assert.Equal([]byte("hello world"), msg.Payload)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCancelContextOnSubscribe(t *testing.T) {
|
||||||
|
|
||||||
|
assert := assert.New(t)
|
||||||
|
require := require.New(t)
|
||||||
|
|
||||||
|
logDir, err := ioutil.TempDir("", "msgbus-logs-*")
|
||||||
|
require.NoError(err)
|
||||||
|
defer os.RemoveAll(logDir)
|
||||||
|
|
||||||
|
mb, err := msgbus.NewMessageBus(msgbus.WithLogPath(logDir))
|
||||||
|
require.NoError(err)
|
||||||
|
defer os.RemoveAll(logDir)
|
||||||
|
|
||||||
|
server := httptest.NewServer(mb)
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
client := NewClient(server.URL, nil)
|
||||||
|
|
||||||
|
sub := client.Subscribe("hello", 1, func(msg *msgbus.Message) error { return nil })
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
cancel()
|
||||||
|
|
||||||
|
failAfter := time.NewTimer(5 * time.Second)
|
||||||
|
|
||||||
|
errChan := make(chan error)
|
||||||
|
go func() {
|
||||||
|
errChan <- sub.Run(ctx)
|
||||||
|
}()
|
||||||
|
|
||||||
|
loop := true
|
||||||
|
for loop {
|
||||||
|
select {
|
||||||
|
case <-failAfter.C:
|
||||||
|
loop = false
|
||||||
|
t.Fatal("Run() with cancelled context did not exit on time")
|
||||||
|
case runError := <-errChan:
|
||||||
|
loop = false
|
||||||
|
assert.ErrorIs(runError, context.Canceled)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cancel()
|
||||||
|
<-ctx.Done()
|
||||||
|
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user