micro/service/grpc/grpc_test.go

190 lines
3.4 KiB
Go
Raw Normal View History

2019-06-03 20:44:43 +03:00
package grpc
import (
"context"
"crypto/tls"
"sync"
"testing"
"time"
2019-06-03 20:44:43 +03:00
"github.com/micro/go-micro"
"github.com/micro/go-micro/registry/memory"
2019-06-05 12:22:28 +03:00
hello "github.com/micro/go-micro/service/grpc/proto"
2019-06-03 20:44:43 +03:00
mls "github.com/micro/go-micro/util/tls"
)
type testHandler struct{}
2019-06-05 12:22:28 +03:00
func (t *testHandler) Call(ctx context.Context, req *hello.Request, rsp *hello.Response) error {
2019-06-03 20:44:43 +03: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(
micro.Name("test.service"),
micro.Registry(r),
micro.AfterStart(func() error {
wg.Done()
return nil
}),
micro.Context(ctx),
)
// register test handler
2019-06-05 12:22:28 +03:00
hello.RegisterTestHandler(service.Server(), &testHandler{})
2019-06-03 20:44:43 +03:00
// run service
errCh := make(chan error, 1)
2019-06-03 20:44:43 +03:00
go func() {
defer close(errCh)
errCh <- service.Run()
2019-06-03 20:44:43 +03:00
}()
// wait for start
wg.Wait()
// create client
2019-06-05 12:22:28 +03:00
test := hello.NewTestService("test.service", service.Client())
2019-06-03 20:44:43 +03: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 20:44:43 +03: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 20:44:43 +03:00
// check message
if rsp.Msg != "Hello John" {
t.Fatalf("unexpected response %s", rsp.Msg)
}
}
func TestGRPCFunction(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// create service
fn := NewFunction(
micro.Name("test.function"),
micro.Registry(memory.NewRegistry()),
micro.AfterStart(func() error {
wg.Done()
return nil
}),
micro.Context(ctx),
)
// register test handler
2019-06-05 12:22:28 +03:00
hello.RegisterTestHandler(fn.Server(), &testHandler{})
2019-06-03 20:44:43 +03:00
// run service
go fn.Run()
// wait for start
wg.Wait()
// create client
2019-06-05 12:22:28 +03:00
test := hello.NewTestService("test.function", fn.Client())
2019-06-03 20:44:43 +03:00
// call service
2019-06-05 12:22:28 +03:00
rsp, err := test.Call(context.Background(), &hello.Request{
2019-06-03 20:44:43 +03:00
Name: "John",
})
if err != nil {
t.Fatal(err)
}
// 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(
micro.Name("test.service"),
micro.Registry(r),
micro.AfterStart(func() error {
wg.Done()
return nil
}),
micro.Context(ctx),
// set TLS config
WithTLS(config),
)
// register test handler
2019-06-05 12:22:28 +03:00
hello.RegisterTestHandler(service.Server(), &testHandler{})
2019-06-03 20:44:43 +03:00
// run service
go func() {
if err := service.Run(); err != nil {
t.Fatal(err)
}
}()
// wait for start
wg.Wait()
// create client
2019-06-05 12:22:28 +03:00
test := hello.NewTestService("test.service", service.Client())
2019-06-03 20:44:43 +03:00
// call service
2019-06-05 12:22:28 +03:00
rsp, err := test.Call(context.Background(), &hello.Request{
2019-06-03 20:44:43 +03:00
Name: "John",
})
if err != nil {
t.Fatal(err)
}
// check message
if rsp.Msg != "Hello John" {
t.Fatalf("unexpected response %s", rsp.Msg)
}
}