Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mreiferson committed Mar 1, 2019
1 parent d6b87a5 commit 76f3702
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 43 deletions.
2 changes: 1 addition & 1 deletion nsqadmin/nsqadmin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 18 additions & 17 deletions nsqd/nsqd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -275,7 +276,7 @@ func (n *NSQD) Main() error {
n.waitGroup.Wrap(n.statsdLoop)
}

err = <-exitCh
err := <-exitCh
return err
}

Expand Down
23 changes: 16 additions & 7 deletions nsqd/nsqd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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"))
Expand All @@ -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")
Expand Down
22 changes: 15 additions & 7 deletions nsqd/protocol_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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))
Expand All @@ -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)
Expand Down
23 changes: 12 additions & 11 deletions nsqlookupd/nsqlookupd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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) {
Expand All @@ -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
}

Expand Down

0 comments on commit 76f3702

Please sign in to comment.