From 9496807a27519cc40325f92ea369b6a652b652e4 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sun, 1 Aug 2021 12:51:38 +0300 Subject: [PATCH] lint Signed-off-by: Vasiliy Tolstov --- api/router/router_test.go | 41 ++++++++++++++----- broker/segmentio/broker_test.go | 23 +++++++---- flow/flow_test.go | 9 +++- server/http/http_test.go | 31 +++++++++----- server/tcp/tcp_test.go | 14 +++---- wrapper/metrics/prometheus/prometheus_test.go | 15 ++++--- wrapper/trace/opentracing/opentracing_test.go | 4 +- 7 files changed, 93 insertions(+), 44 deletions(-) diff --git a/api/router/router_test.go b/api/router/router_test.go index 2b51f8c..9d52abb 100644 --- a/api/router/router_test.go +++ b/api/router/router_test.go @@ -89,7 +89,9 @@ func initial(t *testing.T) (server.Server, client.Client) { ) h := &testServer{} - gpb.RegisterTestServer(s, h) + if err := gpb.RegisterTestServer(s, h); err != nil { + t.Fatal(err) + } if err := s.Init(); err != nil { t.Fatalf("failed to init: %v", err) @@ -118,7 +120,9 @@ func check(t *testing.T, addr string, path string, expected string, timeout bool if err != nil { t.Fatalf("Failed to created http.Request: %v", err) } - defer rsp.Body.Close() + defer func() { + _ = rsp.Body.Close() + }() buf, err := ioutil.ReadAll(rsp.Body) if err != nil { @@ -140,7 +144,9 @@ func check(t *testing.T, addr string, path string, expected string, timeout bool func TestApiTimeout(t *testing.T) { s, c := initial(t) - defer s.Stop() + defer func() { + _ = s.Stop() + }() router := rregister.NewRouter( router.WithHandler(rpc.Handler), @@ -167,14 +173,19 @@ func TestApiTimeout(t *testing.T) { log.Println(hsrv.ListenAndServe()) }() - defer hsrv.Close() + defer func() { + _ = hsrv.Close() + }() + time.Sleep(1 * time.Second) check(t, hsrv.Addr, "http://%s/api/v0/test/call/TEST", `{"Id":"go.micro.client","Code":408,"Detail":"context deadline exceeded","Status":"Request Timeout"}`, true) } func TestRouterRegisterPcre(t *testing.T) { s, c := initial(t) - defer s.Stop() + defer func() { + _ = s.Stop() + }() router := rregister.NewRouter( router.WithHandler(rpc.Handler), @@ -208,7 +219,9 @@ func TestRouterRegisterPcre(t *testing.T) { func TestRouterStaticPcre(t *testing.T) { s, c := initial(t) - defer s.Stop() + defer func() { + _ = s.Stop() + }() router := rstatic.NewRouter( router.WithHandler(rpc.Handler), @@ -244,7 +257,9 @@ func TestRouterStaticPcre(t *testing.T) { go func() { log.Println(hsrv.ListenAndServe()) }() - defer hsrv.Close() + defer func() { + hsrv.Close() + }() time.Sleep(1 * time.Second) check(t, hsrv.Addr, "http://%s/api/v0/test/call", `{"msg":"Hello "}`, false) @@ -252,7 +267,9 @@ func TestRouterStaticPcre(t *testing.T) { func TestRouterStaticGpath(t *testing.T) { s, c := initial(t) - defer s.Stop() + defer func() { + _ = s.Stop() + }() router := rstatic.NewRouter( router.WithHandler(rpc.Handler), @@ -285,7 +302,9 @@ func TestRouterStaticGpath(t *testing.T) { go func() { log.Println(hsrv.ListenAndServe()) }() - defer hsrv.Close() + defer func() { + _ = hsrv.Close() + }() time.Sleep(1 * time.Second) check(t, hsrv.Addr, "http://%s/api/v0/test/call/TEST", `{"msg":"Hello TEST"}`, false) @@ -296,7 +315,9 @@ func TestRouterStaticPcreInvalid(t *testing.T) { var err error s, c := initial(t) - defer s.Stop() + defer func() { + _ = s.Stop() + }() router := rstatic.NewRouter( router.WithHandler(rpc.Handler), diff --git a/broker/segmentio/broker_test.go b/broker/segmentio/broker_test.go index 0092c88..ed479d7 100644 --- a/broker/segmentio/broker_test.go +++ b/broker/segmentio/broker_test.go @@ -38,12 +38,14 @@ var ( ) func TestConsumerGroup(t *testing.T) { - topic := fmt.Sprintf("test_topic") + topic := "test_topic" if tr := os.Getenv("INTEGRATION_TESTS"); len(tr) > 0 { t.Skip() } - logger.DefaultLogger.Init(logger.WithLevel(logger.TraceLevel)) + if err := logger.DefaultLogger.Init(logger.WithLevel(logger.TraceLevel)); err != nil { + t.Fatal(err) + } ctx := context.Background() var addrs []string @@ -189,12 +191,14 @@ func TestConsumerGroup(t *testing.T) { } func TestSub(t *testing.T) { - topic := fmt.Sprintf("test_topic") + topic := "test_topic" if tr := os.Getenv("INTEGRATION_TESTS"); len(tr) > 0 { t.Skip() } - logger.DefaultLogger.Init(logger.WithLevel(logger.ErrorLevel)) + if err := logger.DefaultLogger.Init(logger.WithLevel(logger.ErrorLevel)); err != nil { + t.Fatal(err) + } ctx := context.Background() var addrs []string @@ -256,6 +260,7 @@ func TestSub(t *testing.T) { }() fmt.Printf("prefill topic\n") + go func() { for i := 0; i < 900000; i++ { if err := brk.Publish(ctx, topic, bm); err != nil { @@ -326,7 +331,9 @@ func BenchmarkPub(b *testing.B) { b.Skip() } - logger.DefaultLogger.Init(logger.WithLevel(logger.TraceLevel)) + if err := logger.DefaultLogger.Init(logger.WithLevel(logger.TraceLevel)); err != nil { + b.Fatal(err) + } ctx := context.Background() var addrs []string @@ -415,7 +422,7 @@ func BenchmarkPub(b *testing.B) { func BenchmarkPubSub(b *testing.B) { b.Skip() ctx := context.Background() - topic := fmt.Sprintf("test_topic") + topic := "test_topic" var addrs []string if addr := os.Getenv("BROKER_ADDRS"); len(addr) == 0 { addrs = []string{"127.0.0.1:9092"} @@ -500,7 +507,9 @@ func TestPubSub(t *testing.T) { t.Skip() } - logger.DefaultLogger.Init(logger.WithLevel(logger.ErrorLevel)) + if err := logger.DefaultLogger.Init(logger.WithLevel(logger.ErrorLevel)); err != nil { + t.Fatal(err) + } ctx := context.Background() var addrs []string diff --git a/flow/flow_test.go b/flow/flow_test.go index dd7f32f..deac080 100644 --- a/flow/flow_test.go +++ b/flow/flow_test.go @@ -118,14 +118,21 @@ func TestFlow(t *testing.T) { } id, err := w.Execute(ctx, &flow.Message{Body: req}, flow.ExecuteTimeout(2*time.Second)) - keys, err := s.List(ctx) if err != nil { t.Fatal(err) } + + var keys []string + keys, err = s.List(ctx) + if err != nil { + t.Fatal(err) + } + _ = keys keys, err = store.NewNamespaceStore(s, filepath.Join("workflows", id)).List(ctx) if err != nil { t.Fatal(err) } + _ = keys keys, err = store.NewNamespaceStore(s, filepath.Join("steps", id)).List(ctx) if err != nil { t.Fatal(err) diff --git a/server/http/http_test.go b/server/http/http_test.go index 7001b2d..0fdbfbc 100644 --- a/server/http/http_test.go +++ b/server/http/http_test.go @@ -101,7 +101,9 @@ func TestMultipart(t *testing.T) { } h := &Handler{t: t} - pb.RegisterTestServer(srv, h) + if err := pb.RegisterTestServer(srv, h); err != nil { + t.Fatal(err) + } // start server if err := srv.Start(); err != nil { @@ -140,9 +142,6 @@ func NewServerHandlerWrapper(t *testing.T) server.HandlerWrapper { t.Fatal("metadata empty") } if v, ok := md.Get("Authorization"); ok && v == "test" { - type err struct { - detail string - } nmd := metadata.New(1) nmd.Set("my-key", "my-val") nmd.Set("Content-Type", "text/xml") @@ -234,7 +233,9 @@ func TestNativeFormUrlencoded(t *testing.T) { t.Fatal(err) } h := &Handler{t: t} - pb.RegisterTestServer(srv, h) + if err := pb.RegisterTestServer(srv, h); err != nil { + t.Fatal(err) + } // start server if err := srv.Start(); err != nil { @@ -266,6 +267,9 @@ func TestNativeFormUrlencoded(t *testing.T) { req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("http://%s/v1/test/call/my_name", service[0].Nodes[0].Address), strings.NewReader(data.Encode())) // URL-encoded payload req.Header.Add("Content-Type", "application/x-www-form-urlencoded") //req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode()))) + if err != nil { + t.Fatal(err) + } rsp, err := http.DefaultClient.Do(req) if err != nil { @@ -432,8 +436,11 @@ func TestNativeClientServer(t *testing.T) { if err != nil { t.Fatal(err) } - defer hrsp.Body.Close() - buf, err := io.ReadAll(hrsp.Body) + defer func() { + _ = hrsp.Body.Close() + }() + + _, err = io.ReadAll(hrsp.Body) if err != nil { t.Fatal(err) } @@ -460,7 +467,7 @@ func TestNativeClientServer(t *testing.T) { t.Fatal(err) } - buf, err = io.ReadAll(hrsp.Body) + buf, err := io.ReadAll(hrsp.Body) if err != nil { t.Fatal(err) } @@ -491,7 +498,9 @@ func TestNativeServer(t *testing.T) { ) h := &Handler{t: t} - pb.RegisterTestServer(srv, h) + if err := pb.RegisterTestServer(srv, h); err != nil { + t.Fatal(err) + } // start server if err := srv.Start(); err != nil { @@ -633,7 +642,7 @@ func TestHTTPHandler(t *testing.T) { // create server mux mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte(`hello world`)) + _, _ = w.Write([]byte(`hello world`)) }) // create handler @@ -692,7 +701,7 @@ func TestHTTPServer(t *testing.T) { // create server mux mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte(`hello world`)) + _, _ = w.Write([]byte(`hello world`)) }) // create server diff --git a/server/tcp/tcp_test.go b/server/tcp/tcp_test.go index e661498..cc9f423 100644 --- a/server/tcp/tcp_test.go +++ b/server/tcp/tcp_test.go @@ -65,14 +65,6 @@ func TestTCPServer(t *testing.T) { t.Fatalf("Expected 1 node got %d: %+v", len(service[0].Nodes), service[0].Nodes) } - go func() { - <-h.done - // stop server - if err := srv.Stop(); err != nil { - t.Fatal(err) - } - }() - c, err := net.DialTimeout("tcp", srv.Options().Address, 5*time.Second) if err != nil { t.Fatal(err) @@ -82,6 +74,12 @@ func TestTCPServer(t *testing.T) { if _, err = c.Write([]byte("test")); err != nil { t.Fatal(err) } + + <-h.done + // stop server + if err := srv.Stop(); err != nil { + t.Fatal(err) + } } func (h *testHandler) Serve(c net.Conn) { diff --git a/wrapper/metrics/prometheus/prometheus_test.go b/wrapper/metrics/prometheus/prometheus_test.go index 77670e4..f39d59d 100644 --- a/wrapper/metrics/prometheus/prometheus_test.go +++ b/wrapper/metrics/prometheus/prometheus_test.go @@ -1,3 +1,5 @@ +// +build ignore + package prometheus_test import ( @@ -14,7 +16,7 @@ import ( srv "github.com/unistack-org/micro-server-grpc/v3" "github.com/unistack-org/micro/v3/broker" "github.com/unistack-org/micro/v3/client" - errors "github.com/unistack-org/micro/v3/errors" + "github.com/unistack-org/micro/v3/errors" "github.com/unistack-org/micro/v3/register" "github.com/unistack-org/micro/v3/router" "github.com/unistack-org/micro/v3/server" @@ -39,7 +41,6 @@ func (t *testHandler) Method(ctx context.Context, req *TestRequest, rsp *TestRes } func TestPrometheusMetrics(t *testing.T) { - t.Skip("not implemented now") // setup reg := register.NewRegister() brk := broker.NewBroker(broker.Register(reg)) @@ -78,14 +79,16 @@ func TestPrometheusMetrics(t *testing.T) { *testHandler } - s.Handle( - s.NewHandler(&Test{new(testHandler)}), - ) + if err := s.Handle(s.NewHandler(&Test{new(testHandler)})); err != nil { + t.Fatal(err) + } if err := s.Start(); err != nil { t.Fatalf("Unexpected error starting server: %v", err) } - defer s.Stop() + defer func() { + _ = s.Stop() + }() req := c.NewRequest(name, "Test.Method", &TestRequest{IsError: false}, client.RequestContentType("application/json")) rsp := TestResponse{} diff --git a/wrapper/trace/opentracing/opentracing_test.go b/wrapper/trace/opentracing/opentracing_test.go index 7fd988e..feda47b 100644 --- a/wrapper/trace/opentracing/opentracing_test.go +++ b/wrapper/trace/opentracing/opentracing_test.go @@ -99,7 +99,9 @@ func TestClient(t *testing.T) { if err := s.Init(); err != nil { t.Fatal(err) } - defer s.Stop() + defer func() { + _ = s.Stop() + }() type Test struct { *testHandler