diff --git a/nsqadmin/nsqadmin_test.go b/nsqadmin/nsqadmin_test.go index c9a7ab891..6220e2826 100644 --- a/nsqadmin/nsqadmin_test.go +++ b/nsqadmin/nsqadmin_test.go @@ -85,7 +85,7 @@ func TestTLSHTTPClient(t *testing.T) { } resp, err := http.Get(u.String()) - test.Equal(t, nil, err) + test.Nil(t, err) defer resp.Body.Close() test.Equal(t, resp.StatusCode < 500, true) diff --git a/nsqd/nsqd.go b/nsqd/nsqd.go index de9286605..285ae8713 100644 --- a/nsqd/nsqd.go +++ b/nsqd/nsqd.go @@ -77,6 +77,8 @@ type NSQD struct { } func New(opts *Options) (*NSQD, error) { + var err error + dataPath := opts.DataPath if opts.DataPath == "" { cwd, _ := os.Getwd() @@ -152,6 +154,21 @@ func New(opts *Options) (*NSQD, error) { n.logf(LOG_INFO, version.String("nsqd")) n.logf(LOG_INFO, "ID: %d", opts.ID) + n.tcpListener, err = net.Listen("tcp", opts.TCPAddress) + if err != nil { + return nil, fmt.Errorf("listen (%s) failed - %s", opts.TCPAddress, err) + } + n.httpListener, err = net.Listen("tcp", opts.HTTPAddress) + if err != nil { + return nil, fmt.Errorf("listen (%s) failed - %s", opts.HTTPAddress, err) + } + if n.tlsConfig != nil && opts.HTTPSAddress != "" { + n.httpsListener, err = tls.Listen("tcp", opts.HTTPSAddress, n.tlsConfig) + if err != nil { + return nil, fmt.Errorf("listen (%s) failed - %s", opts.HTTPSAddress, err) + } + } + return n, nil } @@ -225,24 +242,8 @@ func (n *NSQD) RemoveClient(clientID int64) { } func (n *NSQD) Main() error { - var err error ctx := &context{n} - n.tcpListener, err = net.Listen("tcp", n.getOpts().TCPAddress) - if err != nil { - return fmt.Errorf("listen (%s) failed - %s", n.getOpts().TCPAddress, err) - } - n.httpListener, err = net.Listen("tcp", n.getOpts().HTTPAddress) - if err != nil { - return fmt.Errorf("listen (%s) failed - %s", n.getOpts().HTTPAddress, err) - } - if n.tlsConfig != nil && n.getOpts().HTTPSAddress != "" { - n.httpsListener, err = tls.Listen("tcp", n.getOpts().HTTPSAddress, n.tlsConfig) - if err != nil { - return fmt.Errorf("listen (%s) failed - %s", n.getOpts().HTTPSAddress, err) - } - } - exitCh := make(chan error) var once sync.Once exitFunc := func(err error) { @@ -275,7 +276,7 @@ func (n *NSQD) Main() error { n.waitGroup.Wrap(n.statsdLoop) } - err = <-exitCh + err := <-exitCh return err } diff --git a/nsqd/nsqd_test.go b/nsqd/nsqd_test.go index 9bacd0e6e..25c6d8286 100644 --- a/nsqd/nsqd_test.go +++ b/nsqd/nsqd_test.go @@ -244,8 +244,16 @@ func TestPauseMetadata(t *testing.T) { func mustStartNSQLookupd(opts *nsqlookupd.Options) (*net.TCPAddr, *net.TCPAddr, *nsqlookupd.NSQLookupd) { opts.TCPAddress = "127.0.0.1:0" opts.HTTPAddress = "127.0.0.1:0" - lookupd := nsqlookupd.New(opts) - lookupd.Main() + lookupd, err := nsqlookupd.New(opts) + if err != nil { + panic(err) + } + go func() { + err := lookupd.Main() + if err != nil { + panic(err) + } + }() return lookupd.RealTCPAddr(), lookupd.RealHTTPAddr(), lookupd } @@ -411,14 +419,15 @@ func TestCluster(t *testing.T) { func TestSetHealth(t *testing.T) { opts := NewOptions() opts.Logger = test.NewTestLogger(t) - nsqd := New(opts) + nsqd, err := New(opts) + test.Nil(t, err) defer nsqd.Exit() - test.Equal(t, nil, nsqd.GetError()) + test.Nil(t, nsqd.GetError()) test.Equal(t, true, nsqd.IsHealthy()) nsqd.SetHealth(nil) - test.Equal(t, nil, nsqd.GetError()) + test.Nil(t, nsqd.GetError()) test.Equal(t, true, nsqd.IsHealthy()) nsqd.SetHealth(errors.New("health error")) @@ -437,8 +446,8 @@ func TestCrashingLogger(t *testing.T) { // Test invalid log level causes error opts := NewOptions() opts.LogLevel = "bad" - _ = New(opts) - return + _, err := New(opts) + test.Nil(t, err) } cmd := exec.Command(os.Args[0], "-test.run=TestCrashingLogger") cmd.Env = append(os.Environ(), "BE_CRASHER=1") diff --git a/nsqd/protocol_v2_test.go b/nsqd/protocol_v2_test.go index 20e31ed71..cbd068664 100644 --- a/nsqd/protocol_v2_test.go +++ b/nsqd/protocol_v2_test.go @@ -41,8 +41,16 @@ func mustStartNSQD(opts *Options) (*net.TCPAddr, *net.TCPAddr, *NSQD) { } opts.DataPath = tmpDir } - nsqd := New(opts) - nsqd.Main() + nsqd, err := New(opts) + if err != nil { + panic(err) + } + go func() { + err := nsqd.Main() + if err != nil { + panic(err) + } + }() return nsqd.RealTCPAddr(), nsqd.RealHTTPAddr(), nsqd } @@ -1594,12 +1602,12 @@ func testIOLoopReturnsClientErr(t *testing.T, fakeConn test.FakeNetConn) { opts := NewOptions() opts.Logger = test.NewTestLogger(t) opts.LogLevel = "debug" - - prot := &protocolV2{ctx: &context{nsqd: New(opts)}} + nsqd, err := New(opts) + test.Nil(t, err) + prot := &protocolV2{ctx: &context{nsqd: nsqd}} defer prot.ctx.nsqd.Exit() - err := prot.IOLoop(fakeConn) - + err = prot.IOLoop(fakeConn) test.NotNil(t, err) test.Equal(t, "E_INVALID invalid command INVALID_COMMAND", err.Error()) test.NotNil(t, err.(*protocol.FatalClientErr)) @@ -1609,7 +1617,7 @@ func BenchmarkProtocolV2Exec(b *testing.B) { b.StopTimer() opts := NewOptions() opts.Logger = test.NewTestLogger(b) - nsqd := New(opts) + nsqd, _ := New(opts) ctx := &context{nsqd} p := &protocolV2{ctx} c := newClientV2(0, nil, ctx) diff --git a/nsqlookupd/nsqlookupd.go b/nsqlookupd/nsqlookupd.go index 2b5723c1f..11dc3b547 100644 --- a/nsqlookupd/nsqlookupd.go +++ b/nsqlookupd/nsqlookupd.go @@ -23,6 +23,8 @@ type NSQLookupd struct { } func New(opts *Options) (*NSQLookupd, error) { + var err error + if opts.Logger == nil { opts.Logger = log.New(os.Stderr, opts.LogPrefix, log.Ldate|log.Ltime|log.Lmicroseconds) } @@ -33,24 +35,23 @@ func New(opts *Options) (*NSQLookupd, error) { l.logf(LOG_INFO, version.String("nsqlookupd")) + l.tcpListener, err = net.Listen("tcp", opts.TCPAddress) + if err != nil { + return nil, fmt.Errorf("listen (%s) failed - %s", opts.TCPAddress, err) + } + l.httpListener, err = net.Listen("tcp", opts.HTTPAddress) + if err != nil { + return nil, fmt.Errorf("listen (%s) failed - %s", opts.TCPAddress, err) + } + return l, nil } // Main starts an instance of nsqlookupd and returns an // error if there was a problem starting up. func (l *NSQLookupd) Main() error { - var err error ctx := &Context{l} - l.tcpListener, err = net.Listen("tcp", l.opts.TCPAddress) - if err != nil { - return fmt.Errorf("listen (%s) failed - %s", l.opts.TCPAddress, err) - } - l.httpListener, err = net.Listen("tcp", l.opts.HTTPAddress) - if err != nil { - return fmt.Errorf("listen (%s) failed - %s", l.opts.TCPAddress, err) - } - exitCh := make(chan error) var once sync.Once exitFunc := func(err error) { @@ -71,7 +72,7 @@ func (l *NSQLookupd) Main() error { exitFunc(http_api.Serve(l.httpListener, httpServer, "HTTP", l.logf)) }) - err = <-exitCh + err := <-exitCh return err }