From 27bab29e3c06636a0e7121f1adb3bfe750fc423d Mon Sep 17 00:00:00 2001 From: Lars Lehtonen Date: Wed, 11 Dec 2019 08:24:26 -0800 Subject: [PATCH 1/2] service/grpc: t.Fatal out of TestGRPCService() goroutine --- service/grpc/grpc_test.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/service/grpc/grpc_test.go b/service/grpc/grpc_test.go index bf7c0a95..2158d08a 100644 --- a/service/grpc/grpc_test.go +++ b/service/grpc/grpc_test.go @@ -5,6 +5,7 @@ import ( "crypto/tls" "sync" "testing" + "time" "github.com/micro/go-micro" "github.com/micro/go-micro/registry/memory" @@ -44,10 +45,10 @@ func TestGRPCService(t *testing.T) { hello.RegisterTestHandler(service.Server(), &testHandler{}) // run service + errCh := make(chan error, 1) go func() { - if err := service.Run(); err != nil { - t.Fatal(err) - } + defer close(errCh) + errCh <- service.Run() }() // wait for start @@ -57,13 +58,23 @@ func TestGRPCService(t *testing.T) { test := hello.NewTestService("test.service", service.Client()) // call service - rsp, err := test.Call(context.Background(), &hello.Request{ + ctx2, cancel2 := context.WithTimeout(context.Background(), time.Duration(time.Second)) + defer cancel2() + rsp, err := test.Call(ctx2, &hello.Request{ Name: "John", }) if err != nil { t.Fatal(err) } + // check server + select { + case err := <-errCh: + t.Fatal(err) + case <-time.After(time.Second): + break + } + // check message if rsp.Msg != "Hello John" { t.Fatalf("unexpected response %s", rsp.Msg) From fd531349d7edb98f02573908f5aeb4a8847aca51 Mon Sep 17 00:00:00 2001 From: Lars Lehtonen Date: Wed, 11 Dec 2019 08:28:49 -0800 Subject: [PATCH 2/2] service/grpc: t.Fatal out of TestGRPCTLSService() goroutine --- service/grpc/grpc_test.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/service/grpc/grpc_test.go b/service/grpc/grpc_test.go index 2158d08a..5978c4fb 100644 --- a/service/grpc/grpc_test.go +++ b/service/grpc/grpc_test.go @@ -162,10 +162,10 @@ func TestGRPCTLSService(t *testing.T) { hello.RegisterTestHandler(service.Server(), &testHandler{}) // run service + errCh := make(chan error, 1) go func() { - if err := service.Run(); err != nil { - t.Fatal(err) - } + defer close(errCh) + errCh <- service.Run() }() // wait for start @@ -175,13 +175,23 @@ func TestGRPCTLSService(t *testing.T) { test := hello.NewTestService("test.service", service.Client()) // call service - rsp, err := test.Call(context.Background(), &hello.Request{ + ctx2, cancel2 := context.WithTimeout(context.Background(), time.Duration(time.Second)) + defer cancel2() + rsp, err := test.Call(ctx2, &hello.Request{ Name: "John", }) if err != nil { t.Fatal(err) } + // check server + select { + case err := <-errCh: + t.Fatal(err) + case <-time.After(time.Second): + break + } + // check message if rsp.Msg != "Hello John" { t.Fatalf("unexpected response %s", rsp.Msg)