2019-06-03 20:44:43 +03:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-09-18 18:27:29 +03:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-06-03 20:44:43 +03:00
|
|
|
"net"
|
|
|
|
"testing"
|
2020-09-18 18:27:29 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
pb "github.com/unistack-org/micro-client-grpc/proto"
|
|
|
|
rmemory "github.com/unistack-org/micro-registry-memory"
|
|
|
|
regRouter "github.com/unistack-org/micro-router-registry"
|
|
|
|
pberr "github.com/unistack-org/micro-server-grpc/errors"
|
|
|
|
"github.com/unistack-org/micro/v3/client"
|
|
|
|
"github.com/unistack-org/micro/v3/errors"
|
|
|
|
"github.com/unistack-org/micro/v3/registry"
|
|
|
|
"github.com/unistack-org/micro/v3/router"
|
2019-06-03 20:44:43 +03:00
|
|
|
pgrpc "google.golang.org/grpc"
|
2020-09-18 18:27:29 +03:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2019-06-03 20:44:43 +03:00
|
|
|
)
|
|
|
|
|
2020-09-18 18:27:29 +03:00
|
|
|
type testServer struct {
|
|
|
|
pb.UnimplementedTestServer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *testServer) CallNative(ctx context.Context, in *pb.Request) (*pb.Response, error) {
|
|
|
|
if in.Name == "Error" {
|
|
|
|
st := status.New(codes.InvalidArgument, "error request")
|
|
|
|
st, err := st.WithDetails(&pberr.Error{Id: "id", Code: 99, Detail: "detail"})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, st.Err()
|
|
|
|
}
|
|
|
|
return &pb.Response{Msg: "Hello " + in.Name}, nil
|
|
|
|
}
|
2019-06-03 20:44:43 +03:00
|
|
|
|
2020-09-18 18:27:29 +03:00
|
|
|
func (g *testServer) CallMicro(ctx context.Context, in *pb.Request) (*pb.Response, error) {
|
2020-02-06 13:18:33 +03:00
|
|
|
if in.Name == "Error" {
|
2020-09-18 18:27:29 +03:00
|
|
|
return nil, &pberr.Error{Id: "id", Code: 99, Detail: "detail"}
|
2020-02-06 13:18:33 +03:00
|
|
|
}
|
2020-09-18 18:27:29 +03:00
|
|
|
return &pb.Response{Msg: "Hello " + in.Name}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *testServer) Stream(stream pb.Test_StreamServer) error {
|
|
|
|
rsp := &pb.Response{}
|
|
|
|
for {
|
|
|
|
req, err := stream.Recv()
|
|
|
|
if err != nil && err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rsp.Msg = req.Name
|
|
|
|
if err = stream.Send(rsp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGRPCClient(t *testing.T) {
|
2020-09-18 18:27:29 +03:00
|
|
|
l, err := net.Listen("tcp", "127.0.0.1:0")
|
2019-06-03 20:44:43 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to listen: %v", err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
s := pgrpc.NewServer()
|
2020-09-18 18:27:29 +03:00
|
|
|
pb.RegisterTestServer(s, &testServer{})
|
2019-06-03 20:44:43 +03:00
|
|
|
|
2020-09-18 18:27:29 +03:00
|
|
|
go func() {
|
|
|
|
if err := s.Serve(l); err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
}
|
|
|
|
}()
|
2019-06-03 20:44:43 +03:00
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
// create mock registry
|
2020-09-18 18:27:29 +03:00
|
|
|
r := rmemory.NewRegistry()
|
2019-06-03 20:44:43 +03:00
|
|
|
|
|
|
|
// register service
|
2020-09-18 18:27:29 +03:00
|
|
|
if err := r.Register(®istry.Service{
|
2019-06-19 14:34:45 +03:00
|
|
|
Name: "helloworld",
|
2019-06-03 20:44:43 +03:00
|
|
|
Version: "test",
|
|
|
|
Nodes: []*registry.Node{
|
2019-11-01 18:07:53 +03:00
|
|
|
{
|
2019-06-03 20:44:43 +03:00
|
|
|
Id: "test-1",
|
2019-07-17 10:38:50 +03:00
|
|
|
Address: l.Addr().String(),
|
2019-11-25 19:31:43 +03:00
|
|
|
Metadata: map[string]string{
|
|
|
|
"protocol": "grpc",
|
|
|
|
},
|
2019-06-03 20:44:43 +03:00
|
|
|
},
|
|
|
|
},
|
2020-09-18 18:27:29 +03:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-06-03 20:44:43 +03:00
|
|
|
|
2020-07-01 19:06:59 +03:00
|
|
|
// create router
|
2020-07-27 15:22:00 +03:00
|
|
|
rtr := regRouter.NewRouter(router.Registry(r))
|
2019-06-03 20:44:43 +03:00
|
|
|
|
|
|
|
// create client
|
2020-07-01 19:06:59 +03:00
|
|
|
c := NewClient(client.Router(rtr))
|
2019-06-03 20:44:43 +03:00
|
|
|
|
|
|
|
testMethods := []string{
|
2020-09-18 18:27:29 +03:00
|
|
|
"/helloworld.Test/CallNative",
|
|
|
|
"Test.CallNative",
|
|
|
|
"/helloworld.Test/CallMicro",
|
|
|
|
"Test.CallMicro",
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, method := range testMethods {
|
2020-09-18 18:27:29 +03:00
|
|
|
req := c.NewRequest("helloworld", method, &pb.Request{
|
2019-06-03 20:44:43 +03:00
|
|
|
Name: "John",
|
|
|
|
})
|
|
|
|
|
2020-09-18 18:27:29 +03:00
|
|
|
rsp := pb.Response{}
|
2019-06-03 20:44:43 +03:00
|
|
|
|
|
|
|
err = c.Call(context.TODO(), req, &rsp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-09-18 18:27:29 +03:00
|
|
|
if rsp.Msg != "Hello John" {
|
|
|
|
t.Fatalf("Got unexpected response %v", rsp.Msg)
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
|
|
|
}
|
2020-02-06 13:18:33 +03:00
|
|
|
|
2020-09-18 18:27:29 +03:00
|
|
|
for _, method := range testMethods {
|
|
|
|
req := c.NewRequest("helloworld", method, &pb.Request{
|
|
|
|
Name: "Error",
|
|
|
|
})
|
|
|
|
|
|
|
|
rsp := pb.Response{}
|
2020-02-06 13:18:33 +03:00
|
|
|
|
2020-09-18 18:27:29 +03:00
|
|
|
err = c.Call(context.TODO(), req, &rsp)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("nil error received")
|
|
|
|
}
|
|
|
|
|
|
|
|
verr, ok := err.(*errors.Error)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("invalid error received %#+v\n", err)
|
|
|
|
}
|
2020-02-06 13:18:33 +03:00
|
|
|
|
2020-09-18 18:27:29 +03:00
|
|
|
if verr.Code != 99 && verr.Id != "id" && verr.Detail != "detail" {
|
|
|
|
t.Fatalf("invalid error received %#+v\n", verr)
|
|
|
|
}
|
2020-02-06 13:18:33 +03:00
|
|
|
}
|
|
|
|
|
2020-09-18 18:27:29 +03:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
req := c.NewRequest("helloworld", "Test.Stream", &pb.Request{}, client.StreamingRequest())
|
|
|
|
stream, err := c.Stream(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2020-02-06 13:18:33 +03:00
|
|
|
}
|
2020-09-18 18:27:29 +03:00
|
|
|
defer stream.Close()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
fmt.Printf("send to stream\n")
|
|
|
|
if err = stream.Send(&pb.Request{Name: "test name"}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2020-02-06 13:18:33 +03:00
|
|
|
|
2020-09-18 18:27:29 +03:00
|
|
|
rsp := &pb.Response{}
|
|
|
|
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
fmt.Printf("recv from stream\n")
|
|
|
|
if err = stream.Recv(rsp); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if rsp.Msg != "test name" {
|
|
|
|
t.Fatalf("invalid msg: %v", rsp)
|
|
|
|
}
|
2020-02-06 13:18:33 +03:00
|
|
|
}
|
|
|
|
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|