micro/service/grpc/grpc_test.go

156 lines
2.9 KiB
Go
Raw Normal View History

2019-06-03 18:44:43 +01:00
package grpc
import (
"context"
"crypto/tls"
"sync"
"testing"
"time"
2019-06-03 18:44:43 +01:00
"github.com/micro/go-micro/registry/memory"
"github.com/micro/go-micro/service"
2019-06-05 10:22:28 +01:00
hello "github.com/micro/go-micro/service/grpc/proto"
2019-06-03 18:44:43 +01:00
mls "github.com/micro/go-micro/util/tls"
)
type testHandler struct{}
2019-06-05 10:22:28 +01:00
func (t *testHandler) Call(ctx context.Context, req *hello.Request, rsp *hello.Response) error {
2019-06-03 18:44:43 +01:00
rsp.Msg = "Hello " + req.Name
return nil
}
func TestGRPCService(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// create memory registry
r := memory.NewRegistry()
// create GRPC service
service := NewService(
service.Name("test.service"),
service.Registry(r),
service.AfterStart(func() error {
2019-06-03 18:44:43 +01:00
wg.Done()
return nil
}),
service.Context(ctx),
2019-06-03 18:44:43 +01:00
)
// register test handler
2019-06-05 10:22:28 +01:00
hello.RegisterTestHandler(service.Server(), &testHandler{})
2019-06-03 18:44:43 +01:00
// run service
errCh := make(chan error, 1)
2019-06-03 18:44:43 +01:00
go func() {
defer close(errCh)
errCh <- service.Run()
2019-06-03 18:44:43 +01:00
}()
// wait for start
wg.Wait()
// create client
2019-06-05 10:22:28 +01:00
test := hello.NewTestService("test.service", service.Client())
2019-06-03 18:44:43 +01:00
// call service
ctx2, cancel2 := context.WithTimeout(context.Background(), time.Duration(time.Second))
defer cancel2()
rsp, err := test.Call(ctx2, &hello.Request{
2019-06-03 18:44:43 +01:00
Name: "John",
})
if err != nil {
t.Fatal(err)
}
// check server
select {
case err := <-errCh:
t.Fatal(err)
case <-time.After(time.Second):
break
}
2019-06-03 18:44:43 +01:00
// check message
if rsp.Msg != "Hello John" {
t.Fatalf("unexpected response %s", rsp.Msg)
}
}
func TestGRPCTLSService(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// create memory registry
r := memory.NewRegistry()
// create cert
cert, err := mls.Certificate("test.service")
if err != nil {
t.Fatal(err)
}
config := &tls.Config{
Certificates: []tls.Certificate{cert},
InsecureSkipVerify: true,
}
// create GRPC service
service := NewService(
service.Name("test.service"),
service.Registry(r),
service.AfterStart(func() error {
2019-06-03 18:44:43 +01:00
wg.Done()
return nil
}),
service.Context(ctx),
2019-06-03 18:44:43 +01:00
// set TLS config
WithTLS(config),
)
// register test handler
2019-06-05 10:22:28 +01:00
hello.RegisterTestHandler(service.Server(), &testHandler{})
2019-06-03 18:44:43 +01:00
// run service
errCh := make(chan error, 1)
2019-06-03 18:44:43 +01:00
go func() {
defer close(errCh)
errCh <- service.Run()
2019-06-03 18:44:43 +01:00
}()
// wait for start
wg.Wait()
// create client
2019-06-05 10:22:28 +01:00
test := hello.NewTestService("test.service", service.Client())
2019-06-03 18:44:43 +01:00
// call service
ctx2, cancel2 := context.WithTimeout(context.Background(), time.Duration(time.Second))
defer cancel2()
rsp, err := test.Call(ctx2, &hello.Request{
2019-06-03 18:44:43 +01:00
Name: "John",
})
if err != nil {
t.Fatal(err)
}
// check server
select {
case err := <-errCh:
t.Fatal(err)
case <-time.After(time.Second):
break
}
2019-06-03 18:44:43 +01:00
// check message
if rsp.Msg != "Hello John" {
t.Fatalf("unexpected response %s", rsp.Msg)
}
}