diff --git a/apps/nsqd/main_test.go b/apps/nsqd/main_test.go index 9c1f69829..bb29e7793 100644 --- a/apps/nsqd/main_test.go +++ b/apps/nsqd/main_test.go @@ -7,11 +7,13 @@ import ( "github.com/BurntSushi/toml" "github.com/mreiferson/go-options" + "github.com/nsqio/nsq/internal/test" "github.com/nsqio/nsq/nsqd" ) func TestConfigFlagParsing(t *testing.T) { opts := nsqd.NewOptions() + opts.Logger = test.NewTestLogger(t) flagSet := nsqdFlagSet(opts) flagSet.Parse([]string{}) diff --git a/nsqadmin/http_test.go b/nsqadmin/http_test.go index 6ffbf6d97..9dc20960c 100644 --- a/nsqadmin/http_test.go +++ b/nsqadmin/http_test.go @@ -49,8 +49,16 @@ type ChannelStatsDoc struct { 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 } @@ -66,8 +74,16 @@ func bootstrapNSQClusterWithAuth(t *testing.T, withAuth bool) (string, []*nsqd.N nsqlookupdOpts.HTTPAddress = "127.0.0.1:0" nsqlookupdOpts.BroadcastAddress = "127.0.0.1" nsqlookupdOpts.Logger = lgr - nsqlookupd1 := nsqlookupd.New(nsqlookupdOpts) - nsqlookupd1.Main() + nsqlookupd1, err := nsqlookupd.New(nsqlookupdOpts) + if err != nil { + panic(err) + } + go func() { + err := nsqlookupd1.Main() + if err != nil { + panic(err) + } + }() time.Sleep(100 * time.Millisecond) @@ -82,8 +98,16 @@ func bootstrapNSQClusterWithAuth(t *testing.T, withAuth bool) (string, []*nsqd.N panic(err) } nsqdOpts.DataPath = tmpDir - nsqd1 := nsqd.New(nsqdOpts) - nsqd1.Main() + nsqd1, err := nsqd.New(nsqdOpts) + if err != nil { + panic(err) + } + go func() { + err := nsqd1.Main() + if err != nil { + panic(err) + } + }() nsqadminOpts := NewOptions() nsqadminOpts.HTTPAddress = "127.0.0.1:0" @@ -92,8 +116,16 @@ func bootstrapNSQClusterWithAuth(t *testing.T, withAuth bool) (string, []*nsqd.N if withAuth { nsqadminOpts.AdminUsers = []string{"matt"} } - nsqadmin1 := New(nsqadminOpts) - nsqadmin1.Main() + nsqadmin1, err := New(nsqadminOpts) + if err != nil { + panic(err) + } + go func() { + err := nsqadmin1.Main() + if err != nil { + panic(err) + } + }() time.Sleep(100 * time.Millisecond) @@ -573,7 +605,7 @@ func TestHTTPconfig(t *testing.T) { defer resp.Body.Close() body, _ = ioutil.ReadAll(resp.Body) test.Equal(t, 200, resp.StatusCode) - test.Equal(t, LOG_FATAL, nsqadmin1.getOpts().logLevel) + test.Equal(t, LOG_FATAL, nsqadmin1.getOpts().LogLevel) url = fmt.Sprintf("http://%s/config/log_level", nsqadmin1.RealHTTPAddr()) req, err = http.NewRequest("PUT", url, bytes.NewBuffer([]byte(`bad`))) @@ -591,8 +623,14 @@ func TestHTTPconfigCIDR(t *testing.T) { opts.NSQLookupdHTTPAddresses = []string{"127.0.0.1:4161"} opts.Logger = test.NewTestLogger(t) opts.AllowConfigFromCIDR = "10.0.0.0/8" - nsqadmin := New(opts) - nsqadmin.Main() + nsqadmin, err := New(opts) + test.Nil(t, err) + go func() { + err := nsqadmin.Main() + if err != nil { + panic(err) + } + }() defer nsqadmin.Exit() time.Sleep(100 * time.Millisecond) diff --git a/nsqadmin/nsqadmin_test.go b/nsqadmin/nsqadmin_test.go index c9a7ab891..fe19e2b1a 100644 --- a/nsqadmin/nsqadmin_test.go +++ b/nsqadmin/nsqadmin_test.go @@ -7,7 +7,6 @@ import ( "net/http" "net/url" "os" - "os/exec" "testing" "github.com/nsqio/nsq/internal/lg" @@ -16,41 +15,23 @@ import ( ) func TestNeitherNSQDAndNSQLookup(t *testing.T) { - if os.Getenv("BE_CRASHER") == "1" { - opts := NewOptions() - opts.Logger = lg.NilLogger{} - opts.HTTPAddress = "127.0.0.1:0" - New(opts) - return - } - cmd := exec.Command(os.Args[0], "-test.run=TestNeitherNSQDAndNSQLookup") - cmd.Env = append(os.Environ(), "BE_CRASHER=1") - err := cmd.Run() - test.Equal(t, "exit status 1", fmt.Sprintf("%v", err)) - if e, ok := err.(*exec.ExitError); ok && !e.Success() { - return - } - t.Fatalf("process ran with err %v, want exit status 1", err) + opts := NewOptions() + opts.Logger = lg.NilLogger{} + opts.HTTPAddress = "127.0.0.1:0" + _, err := New(opts) + test.NotNil(t, err) + test.Equal(t, "--nsqd-http-address or --lookupd-http-address required", fmt.Sprintf("%s", err)) } func TestBothNSQDAndNSQLookup(t *testing.T) { - if os.Getenv("BE_CRASHER") == "1" { - opts := NewOptions() - opts.Logger = lg.NilLogger{} - opts.HTTPAddress = "127.0.0.1:0" - opts.NSQLookupdHTTPAddresses = []string{"127.0.0.1:4161"} - opts.NSQDHTTPAddresses = []string{"127.0.0.1:4151"} - New(opts) - return - } - cmd := exec.Command(os.Args[0], "-test.run=TestBothNSQDAndNSQLookup") - cmd.Env = append(os.Environ(), "BE_CRASHER=1") - err := cmd.Run() - test.Equal(t, "exit status 1", fmt.Sprintf("%v", err)) - if e, ok := err.(*exec.ExitError); ok && !e.Success() { - return - } - t.Fatalf("process ran with err %v, want exit status 1", err) + opts := NewOptions() + opts.Logger = lg.NilLogger{} + opts.HTTPAddress = "127.0.0.1:0" + opts.NSQLookupdHTTPAddresses = []string{"127.0.0.1:4161"} + opts.NSQDHTTPAddresses = []string{"127.0.0.1:4151"} + _, err := New(opts) + test.NotNil(t, err) + test.Equal(t, "use --nsqd-http-address or --lookupd-http-address not both", fmt.Sprintf("%s", err)) } func TestTLSHTTPClient(t *testing.T) { @@ -73,8 +54,14 @@ func TestTLSHTTPClient(t *testing.T) { opts.HTTPClientTLSCert = "./test/client.pem" opts.HTTPClientTLSKey = "./test/client.key" opts.Logger = lgr - nsqadmin := New(opts) - nsqadmin.Main() + nsqadmin, err := New(opts) + test.Nil(t, err) + go func() { + err := nsqadmin.Main() + if err != nil { + panic(err) + } + }() defer nsqadmin.Exit() httpAddr := nsqadmin.RealHTTPAddr() @@ -85,7 +72,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) @@ -102,25 +89,15 @@ func mustStartNSQD(opts *nsqd.Options) (*net.TCPAddr, *net.TCPAddr, *nsqd.NSQD) } opts.DataPath = tmpDir } - nsqd := nsqd.New(opts) - nsqd.Main() - return nsqd.RealTCPAddr(), nsqd.RealHTTPAddr(), nsqd -} - -func TestCrashingLogger(t *testing.T) { - if os.Getenv("BE_CRASHER") == "1" { - // Test invalid log level causes error - opts := NewOptions() - opts.LogLevel = "bad" - opts.NSQLookupdHTTPAddresses = []string{"127.0.0.1:4161"} - _ = New(opts) - return - } - cmd := exec.Command(os.Args[0], "-test.run=TestCrashingLogger") - cmd.Env = append(os.Environ(), "BE_CRASHER=1") - err := cmd.Run() - if e, ok := err.(*exec.ExitError); ok && !e.Success() { - return + nsqd, err := nsqd.New(opts) + if err != nil { + panic(err) } - t.Fatalf("process ran with err %v, want exit status 1", err) + go func() { + err := nsqd.Main() + if err != nil { + panic(err) + } + }() + return nsqd.RealTCPAddr(), nsqd.RealHTTPAddr(), nsqd } diff --git a/nsqd/http_test.go b/nsqd/http_test.go index 09e89eb68..47e42f0bb 100644 --- a/nsqd/http_test.go +++ b/nsqd/http_test.go @@ -232,7 +232,7 @@ func TestHTTPpubDefer(t *testing.T) { func TestHTTPSRequire(t *testing.T) { opts := NewOptions() opts.Logger = test.NewTestLogger(t) - opts.LogLevel = "debug" + opts.LogLevel = LOG_DEBUG opts.TLSCert = "./test/certs/server.pem" opts.TLSKey = "./test/certs/server.key" opts.TLSClientAuthPolicy = "require" @@ -277,7 +277,7 @@ func TestHTTPSRequire(t *testing.T) { func TestHTTPSRequireVerify(t *testing.T) { opts := NewOptions() opts.Logger = test.NewTestLogger(t) - opts.LogLevel = "debug" + opts.LogLevel = LOG_DEBUG opts.TLSCert = "./test/certs/server.pem" opts.TLSKey = "./test/certs/server.key" opts.TLSRootCAFile = "./test/certs/ca.pem" @@ -341,7 +341,7 @@ func TestHTTPSRequireVerify(t *testing.T) { func TestTLSRequireVerifyExceptHTTP(t *testing.T) { opts := NewOptions() opts.Logger = test.NewTestLogger(t) - opts.LogLevel = "debug" + opts.LogLevel = LOG_DEBUG opts.TLSCert = "./test/certs/server.pem" opts.TLSKey = "./test/certs/server.key" opts.TLSRootCAFile = "./test/certs/ca.pem" @@ -633,7 +633,7 @@ func TestHTTPconfig(t *testing.T) { defer resp.Body.Close() body, _ = ioutil.ReadAll(resp.Body) test.Equal(t, 200, resp.StatusCode) - test.Equal(t, LOG_FATAL, nsqd.getOpts().logLevel) + test.Equal(t, LOG_FATAL, nsqd.getOpts().LogLevel) url = fmt.Sprintf("http://%s/config/log_level", httpAddr) req, err = http.NewRequest("PUT", url, bytes.NewBuffer([]byte(`bad`))) @@ -643,25 +643,6 @@ func TestHTTPconfig(t *testing.T) { defer resp.Body.Close() body, _ = ioutil.ReadAll(resp.Body) test.Equal(t, 400, resp.StatusCode) - - url = fmt.Sprintf("http://%s/config/verbose", httpAddr) - req, err = http.NewRequest("PUT", url, bytes.NewBuffer([]byte(`true`))) - test.Nil(t, err) - resp, err = client.Do(req) - test.Nil(t, err) - defer resp.Body.Close() - body, _ = ioutil.ReadAll(resp.Body) - test.Equal(t, 200, resp.StatusCode) - test.Equal(t, true, nsqd.getOpts().Verbose) - - url = fmt.Sprintf("http://%s/config/verbose", httpAddr) - req, err = http.NewRequest("PUT", url, bytes.NewBuffer([]byte(`bad`))) - test.Nil(t, err) - resp, err = client.Do(req) - test.Nil(t, err) - defer resp.Body.Close() - body, _ = ioutil.ReadAll(resp.Body) - test.Equal(t, 400, resp.StatusCode) } func TestHTTPerrors(t *testing.T) { diff --git a/nsqd/nsqd_test.go b/nsqd/nsqd_test.go index 9bacd0e6e..147280fa7 100644 --- a/nsqd/nsqd_test.go +++ b/nsqd/nsqd_test.go @@ -7,7 +7,6 @@ import ( "io/ioutil" "net" "os" - "os/exec" "strconv" "sync/atomic" "testing" @@ -244,8 +243,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 +418,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")) @@ -431,20 +439,3 @@ func TestSetHealth(t *testing.T) { test.Equal(t, "OK", nsqd.GetHealth()) test.Equal(t, true, nsqd.IsHealthy()) } - -func TestCrashingLogger(t *testing.T) { - if os.Getenv("BE_CRASHER") == "1" { - // Test invalid log level causes error - opts := NewOptions() - opts.LogLevel = "bad" - _ = New(opts) - return - } - cmd := exec.Command(os.Args[0], "-test.run=TestCrashingLogger") - cmd.Env = append(os.Environ(), "BE_CRASHER=1") - err := cmd.Run() - if e, ok := err.(*exec.ExitError); ok && !e.Success() { - return - } - t.Fatalf("process ran with err %v, want exit status 1", err) -} diff --git a/nsqd/protocol_v2_test.go b/nsqd/protocol_v2_test.go index 20e31ed71..e8f7d1ff6 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 } @@ -208,7 +216,7 @@ func TestClientTimeout(t *testing.T) { opts := NewOptions() opts.Logger = test.NewTestLogger(t) opts.ClientTimeout = 150 * time.Millisecond - opts.LogLevel = "debug" + opts.LogLevel = LOG_DEBUG tcpAddr, _, nsqd := mustStartNSQD(opts) defer os.RemoveAll(opts.DataPath) defer nsqd.Exit() @@ -281,7 +289,7 @@ func TestClientHeartbeatDisableSUB(t *testing.T) { opts := NewOptions() opts.Logger = test.NewTestLogger(t) opts.ClientTimeout = 200 * time.Millisecond - opts.LogLevel = "debug" + opts.LogLevel = LOG_DEBUG tcpAddr, _, nsqd := mustStartNSQD(opts) defer os.RemoveAll(opts.DataPath) defer nsqd.Exit() @@ -441,7 +449,7 @@ func TestEmptyCommand(t *testing.T) { func TestSizeLimits(t *testing.T) { opts := NewOptions() opts.Logger = test.NewTestLogger(t) - opts.LogLevel = "debug" + opts.LogLevel = LOG_DEBUG opts.MaxMsgSize = 100 opts.MaxBodySize = 1000 tcpAddr, _, nsqd := mustStartNSQD(opts) @@ -558,7 +566,7 @@ func TestSizeLimits(t *testing.T) { func TestDPUB(t *testing.T) { opts := NewOptions() opts.Logger = test.NewTestLogger(t) - opts.LogLevel = "debug" + opts.LogLevel = LOG_DEBUG tcpAddr, _, nsqd := mustStartNSQD(opts) defer os.RemoveAll(opts.DataPath) defer nsqd.Exit() @@ -601,7 +609,7 @@ func TestDPUB(t *testing.T) { func TestTouch(t *testing.T) { opts := NewOptions() opts.Logger = test.NewTestLogger(t) - opts.LogLevel = "debug" + opts.LogLevel = LOG_DEBUG opts.MsgTimeout = 150 * time.Millisecond tcpAddr, _, nsqd := mustStartNSQD(opts) defer os.RemoveAll(opts.DataPath) @@ -647,7 +655,7 @@ func TestTouch(t *testing.T) { func TestMaxRdyCount(t *testing.T) { opts := NewOptions() opts.Logger = test.NewTestLogger(t) - opts.LogLevel = "debug" + opts.LogLevel = LOG_DEBUG opts.MaxRdyCount = 50 tcpAddr, _, nsqd := mustStartNSQD(opts) defer os.RemoveAll(opts.DataPath) @@ -719,7 +727,7 @@ func TestFatalError(t *testing.T) { func TestOutputBuffering(t *testing.T) { opts := NewOptions() opts.Logger = test.NewTestLogger(t) - opts.LogLevel = "debug" + opts.LogLevel = LOG_DEBUG opts.MaxOutputBufferSize = 512 * 1024 opts.MaxOutputBufferTimeout = time.Second tcpAddr, _, nsqd := mustStartNSQD(opts) @@ -771,7 +779,7 @@ func TestOutputBuffering(t *testing.T) { func TestOutputBufferingValidity(t *testing.T) { opts := NewOptions() opts.Logger = test.NewTestLogger(t) - opts.LogLevel = "debug" + opts.LogLevel = LOG_DEBUG opts.MaxOutputBufferSize = 512 * 1024 opts.MaxOutputBufferTimeout = time.Second tcpAddr, _, nsqd := mustStartNSQD(opts) @@ -814,7 +822,7 @@ func TestOutputBufferingValidity(t *testing.T) { func TestTLS(t *testing.T) { opts := NewOptions() opts.Logger = test.NewTestLogger(t) - opts.LogLevel = "debug" + opts.LogLevel = LOG_DEBUG opts.TLSCert = "./test/certs/server.pem" opts.TLSKey = "./test/certs/server.key" tcpAddr, _, nsqd := mustStartNSQD(opts) @@ -853,7 +861,7 @@ func TestTLS(t *testing.T) { func TestTLSRequired(t *testing.T) { opts := NewOptions() opts.Logger = test.NewTestLogger(t) - opts.LogLevel = "debug" + opts.LogLevel = LOG_DEBUG opts.TLSCert = "./test/certs/server.pem" opts.TLSKey = "./test/certs/server.key" opts.TLSRequired = TLSRequiredExceptHTTP @@ -902,7 +910,7 @@ func TestTLSRequired(t *testing.T) { func TestTLSAuthRequire(t *testing.T) { opts := NewOptions() opts.Logger = test.NewTestLogger(t) - opts.LogLevel = "debug" + opts.LogLevel = LOG_DEBUG opts.TLSCert = "./test/certs/server.pem" opts.TLSKey = "./test/certs/server.key" opts.TLSClientAuthPolicy = "require" @@ -968,7 +976,7 @@ func TestTLSAuthRequire(t *testing.T) { func TestTLSAuthRequireVerify(t *testing.T) { opts := NewOptions() opts.Logger = test.NewTestLogger(t) - opts.LogLevel = "debug" + opts.LogLevel = LOG_DEBUG opts.TLSCert = "./test/certs/server.pem" opts.TLSKey = "./test/certs/server.key" opts.TLSRootCAFile = "./test/certs/ca.pem" @@ -1057,7 +1065,7 @@ func TestTLSAuthRequireVerify(t *testing.T) { func TestDeflate(t *testing.T) { opts := NewOptions() opts.Logger = test.NewTestLogger(t) - opts.LogLevel = "debug" + opts.LogLevel = LOG_DEBUG opts.DeflateEnabled = true tcpAddr, _, nsqd := mustStartNSQD(opts) defer os.RemoveAll(opts.DataPath) @@ -1093,7 +1101,7 @@ type readWriter struct { func TestSnappy(t *testing.T) { opts := NewOptions() opts.Logger = test.NewTestLogger(t) - opts.LogLevel = "debug" + opts.LogLevel = LOG_DEBUG opts.SnappyEnabled = true tcpAddr, _, nsqd := mustStartNSQD(opts) defer os.RemoveAll(opts.DataPath) @@ -1146,7 +1154,7 @@ func TestSnappy(t *testing.T) { func TestTLSDeflate(t *testing.T) { opts := NewOptions() opts.Logger = test.NewTestLogger(t) - opts.LogLevel = "debug" + opts.LogLevel = LOG_DEBUG opts.DeflateEnabled = true opts.TLSCert = "./test/certs/cert.pem" opts.TLSKey = "./test/certs/key.pem" @@ -1203,7 +1211,7 @@ func TestSampling(t *testing.T) { opts := NewOptions() opts.Logger = test.NewTestLogger(t) - opts.LogLevel = "debug" + opts.LogLevel = LOG_DEBUG opts.MaxRdyCount = int64(num) tcpAddr, _, nsqd := mustStartNSQD(opts) defer os.RemoveAll(opts.DataPath) @@ -1270,7 +1278,7 @@ func TestSampling(t *testing.T) { func TestTLSSnappy(t *testing.T) { opts := NewOptions() opts.Logger = test.NewTestLogger(t) - opts.LogLevel = "debug" + opts.LogLevel = LOG_DEBUG opts.SnappyEnabled = true opts.TLSCert = "./test/certs/cert.pem" opts.TLSKey = "./test/certs/key.pem" @@ -1321,7 +1329,7 @@ func TestTLSSnappy(t *testing.T) { func TestClientMsgTimeout(t *testing.T) { opts := NewOptions() opts.Logger = test.NewTestLogger(t) - opts.LogLevel = "debug" + opts.LogLevel = LOG_DEBUG opts.QueueScanRefreshInterval = 100 * time.Millisecond tcpAddr, _, nsqd := mustStartNSQD(opts) defer os.RemoveAll(opts.DataPath) @@ -1380,7 +1388,7 @@ func TestClientMsgTimeout(t *testing.T) { func TestBadFin(t *testing.T) { opts := NewOptions() opts.Logger = test.NewTestLogger(t) - opts.LogLevel = "debug" + opts.LogLevel = LOG_DEBUG tcpAddr, _, nsqd := mustStartNSQD(opts) defer os.RemoveAll(opts.DataPath) defer nsqd.Exit() @@ -1406,7 +1414,7 @@ func TestBadFin(t *testing.T) { func TestReqTimeoutRange(t *testing.T) { opts := NewOptions() opts.Logger = test.NewTestLogger(t) - opts.LogLevel = "debug" + opts.LogLevel = LOG_DEBUG opts.MaxReqTimeout = 1 * time.Minute tcpAddr, _, nsqd := mustStartNSQD(opts) defer os.RemoveAll(opts.DataPath) @@ -1511,7 +1519,7 @@ func runAuthTest(t *testing.T, authResponse string, authSecret string, authError opts := NewOptions() opts.Logger = test.NewTestLogger(t) - opts.LogLevel = "debug" + opts.LogLevel = LOG_DEBUG opts.AuthHTTPAddresses = []string{addr.Host} if tlsEnabled { opts.TLSCert = "./test/certs/server.pem" @@ -1593,13 +1601,14 @@ func testIOLoopReturnsClientErr(t *testing.T, fakeConn test.FakeNetConn) { opts := NewOptions() opts.Logger = test.NewTestLogger(t) - opts.LogLevel = "debug" + opts.LogLevel = LOG_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 +1618,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/nsqd/stats_test.go b/nsqd/stats_test.go index 081ade097..7ec8a461f 100644 --- a/nsqd/stats_test.go +++ b/nsqd/stats_test.go @@ -70,7 +70,7 @@ func TestClientAttributes(t *testing.T) { opts := NewOptions() opts.Logger = test.NewTestLogger(t) - opts.LogLevel = "debug" + opts.LogLevel = LOG_DEBUG opts.SnappyEnabled = true tcpAddr, httpAddr, nsqd := mustStartNSQD(opts) defer os.RemoveAll(opts.DataPath) diff --git a/nsqlookupd/http_test.go b/nsqlookupd/http_test.go index d2250cb69..ef704a220 100644 --- a/nsqlookupd/http_test.go +++ b/nsqlookupd/http_test.go @@ -35,8 +35,16 @@ func bootstrapNSQCluster(t *testing.T) (string, []*nsqd.NSQD, *NSQLookupd) { nsqlookupdOpts.HTTPAddress = "127.0.0.1:0" nsqlookupdOpts.BroadcastAddress = "127.0.0.1" nsqlookupdOpts.Logger = lgr - nsqlookupd1 := New(nsqlookupdOpts) - nsqlookupd1.Main() + nsqlookupd1, err := New(nsqlookupdOpts) + if err != nil { + panic(err) + } + go func() { + err := nsqlookupd1.Main() + if err != nil { + panic(err) + } + }() time.Sleep(100 * time.Millisecond) @@ -51,8 +59,16 @@ func bootstrapNSQCluster(t *testing.T) (string, []*nsqd.NSQD, *NSQLookupd) { panic(err) } nsqdOpts.DataPath = tmpDir - nsqd1 := nsqd.New(nsqdOpts) - nsqd1.Main() + nsqd1, err := nsqd.New(nsqdOpts) + if err != nil { + panic(err) + } + go func() { + err := nsqd1.Main() + if err != nil { + panic(err) + } + }() time.Sleep(100 * time.Millisecond) diff --git a/nsqlookupd/lookup_protocol_v1_test.go b/nsqlookupd/lookup_protocol_v1_test.go index 966c6e540..5ff524bd9 100644 --- a/nsqlookupd/lookup_protocol_v1_test.go +++ b/nsqlookupd/lookup_protocol_v1_test.go @@ -34,9 +34,11 @@ func testIOLoopReturnsClientErr(t *testing.T, fakeConn test.FakeNetConn) { opts := NewOptions() opts.Logger = test.NewTestLogger(t) - opts.LogLevel = "debug" + opts.LogLevel = LOG_DEBUG - prot := &LookupProtocolV1{ctx: &Context{nsqlookupd: New(opts)}} + nsqlookupd, err := New(opts) + test.Nil(t, err) + prot := &LookupProtocolV1{ctx: &Context{nsqlookupd: nsqlookupd}} errChan := make(chan error) testIOLoop := func() { @@ -45,7 +47,6 @@ func testIOLoopReturnsClientErr(t *testing.T, fakeConn test.FakeNetConn) { } go testIOLoop() - var err error var timeout bool select { diff --git a/nsqlookupd/nsqlookupd_test.go b/nsqlookupd/nsqlookupd_test.go index 19f2f2b2c..eb66123e8 100644 --- a/nsqlookupd/nsqlookupd_test.go +++ b/nsqlookupd/nsqlookupd_test.go @@ -3,8 +3,6 @@ package nsqlookupd import ( "fmt" "net" - "os" - "os/exec" "testing" "time" @@ -39,8 +37,16 @@ type LookupDoc struct { func mustStartLookupd(opts *Options) (*net.TCPAddr, *net.TCPAddr, *NSQLookupd) { opts.TCPAddress = "127.0.0.1:0" opts.HTTPAddress = "127.0.0.1:0" - nsqlookupd := New(opts) - nsqlookupd.Main() + nsqlookupd, err := New(opts) + if err != nil { + panic(err) + } + go func() { + err := nsqlookupd.Main() + if err != nil { + panic(err) + } + }() return nsqlookupd.RealTCPAddr(), nsqlookupd.RealHTTPAddr(), nsqlookupd } @@ -351,20 +357,3 @@ func TestTombstonedNodes(t *testing.T) { test.Equal(t, topicName, producers[0].Topics[0].Topic) test.Equal(t, true, producers[0].Topics[0].Tombstoned) } - -func TestCrashingLogger(t *testing.T) { - if os.Getenv("BE_CRASHER") == "1" { - // Test invalid log level causes error - opts := NewOptions() - opts.LogLevel = "bad" - _ = New(opts) - return - } - cmd := exec.Command(os.Args[0], "-test.run=TestCrashingLogger") - cmd.Env = append(os.Environ(), "BE_CRASHER=1") - err := cmd.Run() - if e, ok := err.(*exec.ExitError); ok && !e.Success() { - return - } - t.Fatalf("process ran with err %v, want exit status 1", err) -}