2020-09-29 11:15:29 +03:00
|
|
|
package grpc_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-29 00:27:32 +03:00
|
|
|
"io"
|
|
|
|
"net/http"
|
2020-09-29 11:15:29 +03:00
|
|
|
"testing"
|
|
|
|
|
2021-10-27 01:24:35 +03:00
|
|
|
gclient "go.unistack.org/micro-client-grpc/v3"
|
|
|
|
protocodec "go.unistack.org/micro-codec-proto/v3"
|
|
|
|
regRouter "go.unistack.org/micro-router-register/v3"
|
|
|
|
gserver "go.unistack.org/micro-server-grpc/v3"
|
|
|
|
httpsrv "go.unistack.org/micro-server-http/v3"
|
|
|
|
gpb "go.unistack.org/micro-tests/server/grpc/gproto"
|
|
|
|
pb "go.unistack.org/micro-tests/server/grpc/proto"
|
|
|
|
"go.unistack.org/micro/v3/broker"
|
|
|
|
"go.unistack.org/micro/v3/client"
|
|
|
|
"go.unistack.org/micro/v3/codec"
|
|
|
|
"go.unistack.org/micro/v3/errors"
|
|
|
|
"go.unistack.org/micro/v3/register"
|
|
|
|
"go.unistack.org/micro/v3/router"
|
|
|
|
"go.unistack.org/micro/v3/server"
|
|
|
|
health "go.unistack.org/micro/v3/server/health"
|
2020-12-29 13:44:22 +03:00
|
|
|
jsonpb "google.golang.org/protobuf/encoding/protojson"
|
2020-09-29 11:15:29 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type testServer struct {
|
|
|
|
pb.UnimplementedTestServer
|
|
|
|
}
|
|
|
|
|
2021-02-06 19:08:01 +03:00
|
|
|
func NewServerHandlerWrapper() server.HandlerWrapper {
|
|
|
|
return func(fn server.HandlerFunc) server.HandlerFunc {
|
|
|
|
return func(ctx context.Context, req server.Request, rsp interface{}) error {
|
2021-10-01 01:37:54 +03:00
|
|
|
// fmt.Printf("wrap ctx: %#+v req: %#+v\n", ctx, req)
|
2021-02-06 19:08:01 +03:00
|
|
|
return fn(ctx, req, rsp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 11:15:29 +03:00
|
|
|
func (g *testServer) Call(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
|
|
|
|
if req.Name == "Error" {
|
2021-10-01 01:37:54 +03:00
|
|
|
return &errors.Error{ID: "id", Code: 99, Detail: "detail"}
|
2020-09-29 11:15:29 +03:00
|
|
|
}
|
|
|
|
rsp.Msg = "Hello " + req.Name
|
2020-12-29 13:44:22 +03:00
|
|
|
rsp.Broken = &pb.Broken{Field: "12345"}
|
|
|
|
|
2020-09-29 11:15:29 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGRPCServer(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
2021-03-29 00:27:32 +03:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2021-02-12 20:50:02 +03:00
|
|
|
r := register.NewRegister()
|
|
|
|
b := broker.NewBroker(broker.Register(r))
|
2021-03-29 00:27:32 +03:00
|
|
|
s := gserver.NewServer(
|
|
|
|
server.Codec("application/grpc+proto", protocodec.NewCodec()),
|
2021-07-16 23:30:58 +03:00
|
|
|
server.Address("127.0.0.1:0"),
|
|
|
|
server.Register(r),
|
|
|
|
server.Name("helloworld"),
|
|
|
|
gserver.Reflection(true),
|
2021-02-06 19:08:01 +03:00
|
|
|
server.WrapHandler(NewServerHandlerWrapper()),
|
|
|
|
)
|
2020-09-29 11:15:29 +03:00
|
|
|
// create router
|
2021-01-29 16:47:26 +03:00
|
|
|
rtr := regRouter.NewRouter(router.Register(r))
|
2020-09-29 11:15:29 +03:00
|
|
|
|
|
|
|
h := &testServer{}
|
2021-03-29 00:27:32 +03:00
|
|
|
if err = gpb.RegisterTestServer(s, h); err != nil {
|
2020-09-29 11:15:29 +03:00
|
|
|
t.Fatalf("can't register handler: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-03-29 00:27:32 +03:00
|
|
|
srv := httpsrv.NewServer(
|
2021-07-16 23:30:58 +03:00
|
|
|
server.Address("127.0.0.1:0"),
|
2021-03-29 00:27:32 +03:00
|
|
|
server.Codec("text/plain", codec.NewCodec()),
|
|
|
|
)
|
|
|
|
if err = health.RegisterHealthServer(srv, health.NewHandler(health.Version("0.0.1"))); err != nil {
|
|
|
|
t.Fatalf("cant register health handler: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = srv.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = srv.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-09-29 11:15:29 +03:00
|
|
|
if err = s.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = s.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
2021-03-29 00:27:32 +03:00
|
|
|
if err = srv.Stop(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-09-29 11:15:29 +03:00
|
|
|
if err = s.Stop(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-03-29 00:27:32 +03:00
|
|
|
hr, err := http.NewRequestWithContext(ctx, "GET", "http://"+srv.Options().Address+"/version", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
hr.Header.Set("Content-Type", "text/plain")
|
|
|
|
rsp, err := http.DefaultClient.Do(hr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer rsp.Body.Close()
|
|
|
|
buf, err := io.ReadAll(rsp.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else if string(buf) != "0.0.1" {
|
|
|
|
t.Fatalf("unknown version returned from health handler: %s", buf)
|
|
|
|
}
|
|
|
|
|
2020-09-29 11:15:29 +03:00
|
|
|
// create client
|
2021-01-29 16:47:26 +03:00
|
|
|
c := gclient.NewClient(client.Codec("application/grpc+proto", protocodec.NewCodec()), client.Router(rtr), client.Register(r), client.Broker(b))
|
2020-09-29 11:15:29 +03:00
|
|
|
|
|
|
|
testMethods := []string{
|
|
|
|
"Test.Call",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, method := range testMethods {
|
|
|
|
req := c.NewRequest("helloworld", method, &pb.Request{
|
|
|
|
Name: "John",
|
|
|
|
})
|
|
|
|
|
2020-12-29 13:44:22 +03:00
|
|
|
rsp := &pb.Response{}
|
2020-09-29 11:15:29 +03:00
|
|
|
|
2020-12-29 13:44:22 +03:00
|
|
|
err = c.Call(context.TODO(), req, rsp)
|
2020-09-29 11:15:29 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("method: %s err: %v", method, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rsp.Msg != "Hello John" {
|
|
|
|
t.Fatalf("Got unexpected response %v", rsp.Msg)
|
|
|
|
}
|
2020-12-29 13:44:22 +03:00
|
|
|
|
|
|
|
enc := &jsonpb.MarshalOptions{EmitUnpopulated: true}
|
2021-01-17 15:35:28 +03:00
|
|
|
_, err := enc.Marshal(rsp)
|
2020-12-29 13:44:22 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-09-29 11:15:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//rsp := rpb.ServerReflectionResponse{}
|
|
|
|
//req := c.NewRequest("helloworld", "Test.ServerReflectionInfo", &rpb.ServerReflectionRequest{}, client.StreamingRequest())
|
|
|
|
//if err := c.Call(context.TODO(), req, &rsp); err != nil {
|
|
|
|
// t.Fatal(err)
|
|
|
|
//}
|
|
|
|
|
|
|
|
// select {}
|
|
|
|
}
|