From b6530770a1b3752c30913ce1a3bd8993cd3b9a1e Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Mon, 29 Mar 2021 00:27:32 +0300 Subject: [PATCH] add health handler tests Signed-off-by: Vasiliy Tolstov --- go.mod | 4 ++-- go.sum | 10 ++++---- server/grpc/grpc_test.go | 52 +++++++++++++++++++++++++++++++++++++--- server/http/http_test.go | 2 +- 4 files changed, 57 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index 55db710..4c47797 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/unistack-org/micro-tests -go 1.15 +go 1.16 require ( github.com/google/uuid v1.2.0 @@ -29,7 +29,7 @@ require ( github.com/unistack-org/micro-server-http/v3 v3.3.1 github.com/unistack-org/micro-server-tcp/v3 v3.3.0 github.com/unistack-org/micro-wrapper-trace-opentracing/v3 v3.2.0 - github.com/unistack-org/micro/v3 v3.3.4 + github.com/unistack-org/micro/v3 v3.3.8 google.golang.org/genproto v0.0.0-20210325224202-eed09b1b5210 google.golang.org/grpc v1.36.1 google.golang.org/protobuf v1.26.0 diff --git a/go.sum b/go.sum index 1457f3d..18ab9c1 100644 --- a/go.sum +++ b/go.sum @@ -497,7 +497,8 @@ github.com/unistack-org/micro/v3 v3.2.22/go.mod h1:oI8H/uGq1h4i5cvUycEoFKJQC7G8y github.com/unistack-org/micro/v3 v3.2.24/go.mod h1:iJwCWq2PECMxigfqe6TPC5GLWvj6P94Kk+PTVZGL3w8= github.com/unistack-org/micro/v3 v3.3.0/go.mod h1:iJwCWq2PECMxigfqe6TPC5GLWvj6P94Kk+PTVZGL3w8= github.com/unistack-org/micro/v3 v3.3.3/go.mod h1:tX95c0Qx4w6oqU7qKThs9lya9P507BdZ29MsTVDmU6w= -github.com/unistack-org/micro/v3 v3.3.4/go.mod h1:tX95c0Qx4w6oqU7qKThs9lya9P507BdZ29MsTVDmU6w= +github.com/unistack-org/micro/v3 v3.3.8 h1:4e4gWuZI/cRUGZ6ZZpdrIDOF2MLO6cjHxeFfZzy610Y= +github.com/unistack-org/micro/v3 v3.3.8/go.mod h1:M3RGgKZX1OlTn0QB+3R1tlSoGwmEg6rb/Isr0lLYmjQ= github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/valyala/fastrand v1.0.0 h1:LUKT9aKer2dVQNUi3waewTbKV+7H17kvWFNKs2ObdkI= @@ -594,8 +595,9 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc= golang.org/x/net v0.0.0-20210323141857-08027d57d8cf/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc= golang.org/x/net v0.0.0-20210324051636-2c4c8ecb7826/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc= -golang.org/x/net v0.0.0-20210324205630-d1beb07c2056 h1:sANdAef76Ioam9aQUUdcAqricwY/WUaMc4+7LY4eGg8= golang.org/x/net v0.0.0-20210324205630-d1beb07c2056/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k= +golang.org/x/net v0.0.0-20210326220855-61e056675ecf h1:WUcCxqQqDT0aXO4VnQbfMvp4zh7m1Gb2clVuHUAGGRE= +golang.org/x/net v0.0.0-20210326220855-61e056675ecf/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -653,8 +655,8 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20181227161524-e6919f6577db/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= -golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ= golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -717,7 +719,6 @@ google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98 google.golang.org/genproto v0.0.0-20190911173649-1774047e7e51/go.mod h1:IbNlFCBrqXvoKpeg0TB2l7cyZUmoaFKYIwrEpbDKLA8= google.golang.org/genproto v0.0.0-20191216164720-4f79533eabd1/go.mod h1:n3cpQtvxv34hfy77yVDNjmbRyujviMdxYliBSkLhpCc= google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= -google.golang.org/genproto v0.0.0-20200904004341-0bd0a958aa1d h1:92D1fum1bJLKSdr11OJ+54YeCMCGYIygTA7R/YZxH5M= google.golang.org/genproto v0.0.0-20200904004341-0bd0a958aa1d/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/genproto v0.0.0-20210325224202-eed09b1b5210 h1:fFxjezD+ZiiYJ6zyfH738tgcWOqfzWl9I1GoepZzrI4= google.golang.org/genproto v0.0.0-20210325224202-eed09b1b5210/go.mod h1:f2Bd7+2PlaVKmvKQ52aspJZXIDaRQBVdOOBfJ5i8OEs= @@ -737,7 +738,6 @@ google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQ google.golang.org/grpc v1.26.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/grpc v1.35.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= -google.golang.org/grpc v1.36.0 h1:o1bcQ6imQMIOpdrO3SWf2z5RV72WbDwdXuK0MDlc8As= google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= google.golang.org/grpc v1.36.1 h1:cmUfbeGKnz9+2DD/UYsMQXeqbHZqZDs4eQwW0sFOpBY= google.golang.org/grpc v1.36.1/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= diff --git a/server/grpc/grpc_test.go b/server/grpc/grpc_test.go index 8e2fae1..b9a3f4a 100644 --- a/server/grpc/grpc_test.go +++ b/server/grpc/grpc_test.go @@ -3,20 +3,25 @@ package grpc_test import ( "context" "fmt" + "io" + "net/http" "testing" gclient "github.com/unistack-org/micro-client-grpc/v3" protocodec "github.com/unistack-org/micro-codec-proto/v3" regRouter "github.com/unistack-org/micro-router-register/v3" gserver "github.com/unistack-org/micro-server-grpc/v3" + httpsrv "github.com/unistack-org/micro-server-http/v3" gpb "github.com/unistack-org/micro-tests/server/grpc/gproto" pb "github.com/unistack-org/micro-tests/server/grpc/proto" "github.com/unistack-org/micro/v3/broker" "github.com/unistack-org/micro/v3/client" + "github.com/unistack-org/micro/v3/codec" "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" + health "github.com/unistack-org/micro/v3/server/health" jsonpb "google.golang.org/protobuf/encoding/protojson" ) @@ -46,20 +51,40 @@ func (g *testServer) Call(ctx context.Context, req *pb.Request, rsp *pb.Response func TestGRPCServer(t *testing.T) { var err error + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + r := register.NewRegister() b := broker.NewBroker(broker.Register(r)) - s := gserver.NewServer(server.Codec("application/grpc+proto", protocodec.NewCodec()), server.Address(":0"), server.Register(r), server.Name("helloworld"), gserver.Reflection(true), + s := gserver.NewServer( + server.Codec("application/grpc+proto", protocodec.NewCodec()), + server.Address(":0"), server.Register(r), server.Name("helloworld"), gserver.Reflection(true), server.WrapHandler(NewServerHandlerWrapper()), ) // create router rtr := regRouter.NewRouter(router.Register(r)) h := &testServer{} - err = gpb.RegisterTestServer(s, h) - if err != nil { + if err = gpb.RegisterTestServer(s, h); err != nil { t.Fatalf("can't register handler: %v", err) } + srv := httpsrv.NewServer( + server.Address(":0"), + server.Codec("text/plain", codec.NewCodec()), + ) + if err = health.RegisterHealthServer(srv, health.NewHandler(health.Version("0.0.1"))); err != nil { + t.Fatalf("cant register health handler: %v", err) + } + + if err = srv.Init(); err != nil { + t.Fatal(err) + } + + if err = srv.Start(); err != nil { + t.Fatal(err) + } + if err = s.Init(); err != nil { t.Fatal(err) } @@ -69,11 +94,32 @@ func TestGRPCServer(t *testing.T) { } defer func() { + if err = srv.Stop(); err != nil { + t.Fatal(err) + } + if err = s.Stop(); err != nil { t.Fatal(err) } }() + hr, err := http.NewRequestWithContext(ctx, "GET", "http://"+srv.Options().Address+"/version", nil) + if err != nil { + t.Fatal(err) + } + hr.Header.Set("Content-Type", "text/plain") + rsp, err := http.DefaultClient.Do(hr) + if err != nil { + t.Fatal(err) + } + defer rsp.Body.Close() + buf, err := io.ReadAll(rsp.Body) + if err != nil { + t.Fatal(err) + } else if string(buf) != "0.0.1" { + t.Fatalf("unknown version returned from health handler: %s", buf) + } + // create client c := gclient.NewClient(client.Codec("application/grpc+proto", protocodec.NewCodec()), client.Router(rtr), client.Register(r), client.Broker(b)) diff --git a/server/http/http_test.go b/server/http/http_test.go index e4565f3..7ff423c 100644 --- a/server/http/http_test.go +++ b/server/http/http_test.go @@ -97,7 +97,7 @@ func TestNativeClientServer(t *testing.T) { server.WrapHandler(mwrapper.NewHandlerWrapper(mwrapper.Meter(m))), server.WrapHandler(lwrapper.NewServerHandlerWrapper(lwrapper.WithEnabled(true), lwrapper.WithLevel(logger.InfoLevel))), httpsrv.Middleware(mwf), - //server.WrapHandler(NewServerHandlerWrapper()), + server.WrapHandler(NewServerHandlerWrapper()), ) h := &Handler{t: t}