diff --git a/context.go b/context.go index 20f1782ac..926bde899 100644 --- a/context.go +++ b/context.go @@ -41,9 +41,6 @@ func (nc *Conn) requestWithContext(ctx context.Context, subj string, hdr, data [ if ctx == nil { return nil, ErrInvalidContext } - if nc == nil { - return nil, ErrInvalidConnection - } // Check whether the context is done already before making // the request. if ctx.Err() != nil { @@ -172,9 +169,6 @@ func (s *Subscription) NextMsgWithContext(ctx context.Context) (*Msg, error) { // of a Flush() call. This context should be non-nil and should // have a deadline set. We will return an error if none is present. func (nc *Conn) FlushWithContext(ctx context.Context) error { - if nc == nil { - return ErrInvalidConnection - } if ctx == nil { return ErrInvalidContext } diff --git a/nats.go b/nats.go index c8ab3bd49..7ee736a0b 100644 --- a/nats.go +++ b/nats.go @@ -3533,9 +3533,6 @@ func (nc *Conn) processAsyncInfo(info []byte) { // It can be used reliably within ClosedCB in order to find out reason // why connection was closed for example. func (nc *Conn) LastError() error { - if nc == nil { - return ErrInvalidConnection - } nc.mu.RLock() err := nc.err nc.mu.RUnlock() @@ -3768,9 +3765,6 @@ const digits = "0123456789" // Sends a protocol data message by queuing into the bufio writer // and kicking the flush go routine. These writes should be protected. func (nc *Conn) publish(subj, reply string, hdr, data []byte) error { - if nc == nil { - return ErrInvalidConnection - } if subj == "" { return ErrBadSubject } @@ -3976,10 +3970,6 @@ func (nc *Conn) useOldRequestStyle() bool { } func (nc *Conn) request(subj string, hdr, data []byte, timeout time.Duration) (*Msg, error) { - if nc == nil { - return nil, ErrInvalidConnection - } - var m *Msg var err error @@ -4159,9 +4149,6 @@ func (nc *Conn) ChanQueueSubscribe(subj, group string, ch chan *Msg) (*Subscript // SubscribeSync will express interest on the given subject. Messages will // be received synchronously using Subscription.NextMsg(). func (nc *Conn) SubscribeSync(subj string) (*Subscription, error) { - if nc == nil { - return nil, ErrInvalidConnection - } mch := make(chan *Msg, nc.Opts.SubChanLen) return nc.subscribe(subj, _EMPTY_, nil, mch, true, nil) } @@ -4215,18 +4202,12 @@ func badQueue(qname string) bool { // subscribe is the internal subscribe function that indicates interest in a subject. func (nc *Conn) subscribe(subj, queue string, cb MsgHandler, ch chan *Msg, isSync bool, js *jsSub) (*Subscription, error) { - if nc == nil { - return nil, ErrInvalidConnection - } nc.mu.Lock() defer nc.mu.Unlock() return nc.subscribeLocked(subj, queue, cb, ch, isSync, js) } func (nc *Conn) subscribeLocked(subj, queue string, cb MsgHandler, ch chan *Msg, isSync bool, js *jsSub) (*Subscription, error) { - if nc == nil { - return nil, ErrInvalidConnection - } if badSubject(subj) { return nil, ErrBadSubject } @@ -4939,9 +4920,6 @@ func (nc *Conn) processPingTimer() { // FlushTimeout allows a Flush operation to have an associated timeout. func (nc *Conn) FlushTimeout(timeout time.Duration) (err error) { - if nc == nil { - return ErrInvalidConnection - } if timeout <= 0 { return ErrBadTimeout } diff --git a/test/basic_test.go b/test/basic_test.go index 75a187d05..d98f8f40d 100644 --- a/test/basic_test.go +++ b/test/basic_test.go @@ -772,7 +772,7 @@ func TestRequestClose(t *testing.T) { nc.Close() }() nc.SubscribeSync("foo") - if _, err := nc.Request("foo", []byte("help"), 2*time.Second); err != nats.ErrInvalidConnection && err != nats.ErrConnectionClosed { + if _, err := nc.Request("foo", []byte("help"), 2*time.Second); err != nats.ErrConnectionClosed { t.Fatalf("Expected connection error: got %v", err) } wg.Wait() @@ -968,53 +968,7 @@ func TestOptions(t *testing.T) { } } -func TestNilConnection(t *testing.T) { - var nc *nats.Conn - data := []byte("ok") - - // Publish - if err := nc.Publish("foo", data); err == nil || err != nats.ErrInvalidConnection { - t.Fatalf("Expected ErrInvalidConnection error, got %v\n", err) - } - if err := nc.PublishMsg(nil); err == nil || err != nats.ErrInvalidMsg { - t.Fatalf("Expected ErrInvalidMsg error, got %v\n", err) - } - if err := nc.PublishMsg(&nats.Msg{}); err == nil || err != nats.ErrInvalidConnection { - t.Fatalf("Expected ErrInvalidConnection error, got %v\n", err) - } - if err := nc.PublishRequest("foo", "reply", data); err == nil || err != nats.ErrInvalidConnection { - t.Fatalf("Expected ErrInvalidConnection error, got %v\n", err) - } - - // Subscribe - if _, err := nc.Subscribe("foo", nil); err == nil || err != nats.ErrInvalidConnection { - t.Fatalf("Expected ErrInvalidConnection error, got %v\n", err) - } - if _, err := nc.SubscribeSync("foo"); err == nil || err != nats.ErrInvalidConnection { - t.Fatalf("Expected ErrInvalidConnection error, got %v\n", err) - } - if _, err := nc.QueueSubscribe("foo", "bar", nil); err == nil || err != nats.ErrInvalidConnection { - t.Fatalf("Expected ErrInvalidConnection error, got %v\n", err) - } - ch := make(chan *nats.Msg) - if _, err := nc.ChanSubscribe("foo", ch); err == nil || err != nats.ErrInvalidConnection { - t.Fatalf("Expected ErrInvalidConnection error, got %v\n", err) - } - if _, err := nc.ChanQueueSubscribe("foo", "bar", ch); err == nil || err != nats.ErrInvalidConnection { - t.Fatalf("Expected ErrInvalidConnection error, got %v\n", err) - } - if _, err := nc.QueueSubscribeSyncWithChan("foo", "bar", ch); err == nil || err != nats.ErrInvalidConnection { - t.Fatalf("Expected ErrInvalidConnection error, got %v\n", err) - } - - // Flush - if err := nc.Flush(); err == nil || err != nats.ErrInvalidConnection { - t.Fatalf("Expected ErrInvalidConnection error, got %v\n", err) - } - if err := nc.FlushTimeout(time.Millisecond); err == nil || err != nats.ErrInvalidConnection { - t.Fatalf("Expected ErrInvalidConnection error, got %v\n", err) - } - +func TestNilSubscriber(t *testing.T) { // Nil Subscribers var sub *nats.Subscription if sub.Type() != nats.NilSubscription { diff --git a/test/context_test.go b/test/context_test.go index b9c2f24f6..e05f7fe18 100644 --- a/test/context_test.go +++ b/test/context_test.go @@ -26,21 +26,6 @@ import ( "github.com/nats-io/nats.go" ) -func TestContextRequestWithNilConnection(t *testing.T) { - var nc *nats.Conn - - ctx, cancelCB := context.WithTimeout(context.Background(), 100*time.Millisecond) - defer cancelCB() // should always be called, not discarded, to prevent context leak - - _, err := nc.RequestWithContext(ctx, "fast", []byte("")) - if err == nil { - t.Fatal("Expected request with context and nil connection to fail") - } - if err != nats.ErrInvalidConnection { - t.Fatalf("Expected nats.ErrInvalidConnection, got %v\n", err) - } -} - func testContextRequestWithTimeout(t *testing.T, nc *nats.Conn) { nc.Subscribe("slow", func(m *nats.Msg) { // Simulates latency into the client so that timeout is hit.