2019-06-03 20:44:43 +03:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
2019-12-11 19:24:26 +03:00
|
|
|
"time"
|
2019-06-03 20:44:43 +03:00
|
|
|
|
|
|
|
"github.com/micro/go-micro/registry/memory"
|
2019-12-30 00:07:55 +03:00
|
|
|
"github.com/micro/go-micro/service"
|
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(
|
2019-12-30 00:07:55 +03:00
|
|
|
service.Name("test.service"),
|
|
|
|
service.Registry(r),
|
|
|
|
service.AfterStart(func() error {
|
2019-06-03 20:44:43 +03:00
|
|
|
wg.Done()
|
|
|
|
return nil
|
|
|
|
}),
|
2019-12-30 00:07:55 +03:00
|
|
|
service.Context(ctx),
|
2019-06-03 20:44:43 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
2019-12-11 19:24:26 +03:00
|
|
|
errCh := make(chan error, 1)
|
2019-06-03 20:44:43 +03:00
|
|
|
go func() {
|
2019-12-11 19:24:26 +03:00
|
|
|
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
|
2019-12-11 19:24:26 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-12-11 19:24:26 +03:00
|
|
|
// 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 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(
|
2019-12-30 00:07:55 +03:00
|
|
|
service.Name("test.service"),
|
|
|
|
service.Registry(r),
|
|
|
|
service.AfterStart(func() error {
|
2019-06-03 20:44:43 +03:00
|
|
|
wg.Done()
|
|
|
|
return nil
|
|
|
|
}),
|
2019-12-30 00:07:55 +03:00
|
|
|
service.Context(ctx),
|
2019-06-03 20:44:43 +03:00
|
|
|
// 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
|
2019-12-11 19:28:49 +03:00
|
|
|
errCh := make(chan error, 1)
|
2019-06-03 20:44:43 +03:00
|
|
|
go func() {
|
2019-12-11 19:28:49 +03:00
|
|
|
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
|
2019-12-11 19:28:49 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-12-11 19:28:49 +03:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|