Skip to content

Commit

Permalink
Fix use of default connect timeout in opts.Connect()
Browse files Browse the repository at this point in the history
If one creates a nats.Options struct and does not specify a timeout,
the library would use 0 for the connect timeout which would prevent
the connect to succeed. If timeout is not specify, the default
value (2 seconds) will now be used.
  • Loading branch information
kozlovic committed May 26, 2016
1 parent fc85f44 commit f847f95
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,10 @@ func (o Options) Connect() (*Conn, error) {
if nc.Opts.ReconnectBufSize == 0 {
nc.Opts.ReconnectBufSize = DefaultReconnectBufSize
}
// Ensure that Timeout is not 0
if nc.Opts.Timeout == 0 {
nc.Opts.Timeout = DefaultTimeout
}

if err := nc.setupServerPool(); err != nil {
return nil, err
Expand Down
17 changes: 17 additions & 0 deletions test/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1199,3 +1199,20 @@ func TestServerErrorClosesConnection(t *testing.T) {

close(done)
}

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

opts := &nats.Options{
Servers: []string{nats.DefaultURL},
}
nc, err := opts.Connect()
if err != nil {
t.Fatalf("Unexpected error on connect: %v", err)
}
defer nc.Close()
if nc.Opts.Timeout != nats.DefaultTimeout {
t.Fatalf("Expected Timeout to be set to %v, got %v", nats.DefaultTimeout, nc.Opts.Timeout)
}
}

0 comments on commit f847f95

Please sign in to comment.