Merge pull request #1035 from alrs/fix-grpc-test-goroutines
Fix service/grpc Test Goroutines
This commit is contained in:
		@@ -5,6 +5,7 @@ import (
 | 
				
			|||||||
	"crypto/tls"
 | 
						"crypto/tls"
 | 
				
			||||||
	"sync"
 | 
						"sync"
 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/micro/go-micro"
 | 
						"github.com/micro/go-micro"
 | 
				
			||||||
	"github.com/micro/go-micro/registry/memory"
 | 
						"github.com/micro/go-micro/registry/memory"
 | 
				
			||||||
@@ -44,10 +45,10 @@ func TestGRPCService(t *testing.T) {
 | 
				
			|||||||
	hello.RegisterTestHandler(service.Server(), &testHandler{})
 | 
						hello.RegisterTestHandler(service.Server(), &testHandler{})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// run service
 | 
						// run service
 | 
				
			||||||
 | 
						errCh := make(chan error, 1)
 | 
				
			||||||
	go func() {
 | 
						go func() {
 | 
				
			||||||
		if err := service.Run(); err != nil {
 | 
							defer close(errCh)
 | 
				
			||||||
			t.Fatal(err)
 | 
							errCh <- service.Run()
 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}()
 | 
						}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// wait for start
 | 
						// wait for start
 | 
				
			||||||
@@ -57,13 +58,23 @@ func TestGRPCService(t *testing.T) {
 | 
				
			|||||||
	test := hello.NewTestService("test.service", service.Client())
 | 
						test := hello.NewTestService("test.service", service.Client())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// call service
 | 
						// 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",
 | 
							Name: "John",
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		t.Fatal(err)
 | 
							t.Fatal(err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// check server
 | 
				
			||||||
 | 
						select {
 | 
				
			||||||
 | 
						case err := <-errCh:
 | 
				
			||||||
 | 
							t.Fatal(err)
 | 
				
			||||||
 | 
						case <-time.After(time.Second):
 | 
				
			||||||
 | 
							break
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// check message
 | 
						// check message
 | 
				
			||||||
	if rsp.Msg != "Hello John" {
 | 
						if rsp.Msg != "Hello John" {
 | 
				
			||||||
		t.Fatalf("unexpected response %s", rsp.Msg)
 | 
							t.Fatalf("unexpected response %s", rsp.Msg)
 | 
				
			||||||
@@ -151,10 +162,10 @@ func TestGRPCTLSService(t *testing.T) {
 | 
				
			|||||||
	hello.RegisterTestHandler(service.Server(), &testHandler{})
 | 
						hello.RegisterTestHandler(service.Server(), &testHandler{})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// run service
 | 
						// run service
 | 
				
			||||||
 | 
						errCh := make(chan error, 1)
 | 
				
			||||||
	go func() {
 | 
						go func() {
 | 
				
			||||||
		if err := service.Run(); err != nil {
 | 
							defer close(errCh)
 | 
				
			||||||
			t.Fatal(err)
 | 
							errCh <- service.Run()
 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}()
 | 
						}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// wait for start
 | 
						// wait for start
 | 
				
			||||||
@@ -164,13 +175,23 @@ func TestGRPCTLSService(t *testing.T) {
 | 
				
			|||||||
	test := hello.NewTestService("test.service", service.Client())
 | 
						test := hello.NewTestService("test.service", service.Client())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// call service
 | 
						// 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",
 | 
							Name: "John",
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		t.Fatal(err)
 | 
							t.Fatal(err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// check server
 | 
				
			||||||
 | 
						select {
 | 
				
			||||||
 | 
						case err := <-errCh:
 | 
				
			||||||
 | 
							t.Fatal(err)
 | 
				
			||||||
 | 
						case <-time.After(time.Second):
 | 
				
			||||||
 | 
							break
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// check message
 | 
						// check message
 | 
				
			||||||
	if rsp.Msg != "Hello John" {
 | 
						if rsp.Msg != "Hello John" {
 | 
				
			||||||
		t.Fatalf("unexpected response %s", rsp.Msg)
 | 
							t.Fatalf("unexpected response %s", rsp.Msg)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user