Skip to content

Commit

Permalink
Check for closed connection on timeout in Request
Browse files Browse the repository at this point in the history
  • Loading branch information
derekcollison committed May 26, 2017
1 parent 14704f1 commit c0af51d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -2068,6 +2068,9 @@ func (nc *Conn) Request(subj string, data []byte, timeout time.Duration) (*Msg,
return nil, ErrConnectionClosed
}
case <-t.C:
if nc.isClosed() {
return nil, ErrConnectionClosed
}
return nil, ErrTimeout
}

Expand Down
21 changes: 21 additions & 0 deletions test/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,12 +589,33 @@ func TestSimultaneousRequests(t *testing.T) {
wg.Wait()
}

func TestRequestClose(t *testing.T) {
s := RunDefaultServer()
defer s.Shutdown()

nc := NewDefaultConnection(t)
defer nc.Close()

wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
time.Sleep(100 * time.Millisecond)
nc.Close()
}()
if _, err := nc.Request("foo", []byte("help"), 500*time.Millisecond); err != nats.ErrInvalidConnection && err != nats.ErrConnectionClosed {
t.Fatalf("Expected connection error: got %v", err)
}
wg.Wait()
}

func TestRequestCloseTimeout(t *testing.T) {
// Make sure we return a timeout when we close
// the connection even if response is queued.

s := RunDefaultServer()
defer s.Shutdown()

nc := NewDefaultConnection(t)
defer nc.Close()

Expand Down

0 comments on commit c0af51d

Please sign in to comment.