2020-06-05 12:18:35 +03:00
|
|
|
package wrapper_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
"github.com/micro/go-micro/v3/broker"
|
|
|
|
bmemory "github.com/micro/go-micro/v3/broker/memory"
|
|
|
|
"github.com/micro/go-micro/v3/client"
|
|
|
|
"github.com/micro/go-micro/v3/client/grpc"
|
|
|
|
rmemory "github.com/micro/go-micro/v3/registry/memory"
|
|
|
|
"github.com/micro/go-micro/v3/router"
|
|
|
|
rtreg "github.com/micro/go-micro/v3/router/registry"
|
|
|
|
"github.com/micro/go-micro/v3/server"
|
|
|
|
grpcsrv "github.com/micro/go-micro/v3/server/grpc"
|
|
|
|
tmemory "github.com/micro/go-micro/v3/transport/memory"
|
|
|
|
wrapper "github.com/micro/go-micro/v3/util/wrapper"
|
2020-06-05 12:18:35 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type TestFoo struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
type TestReq struct{}
|
|
|
|
|
|
|
|
type TestRsp struct {
|
|
|
|
Data string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *TestFoo) Bar(ctx context.Context, req *TestReq, rsp *TestRsp) error {
|
|
|
|
rsp.Data = "pass"
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStaticClientWrapper(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
req := grpc.NewClient().NewRequest(
|
|
|
|
"go.micro.service.foo",
|
|
|
|
"TestFoo.Bar",
|
|
|
|
&TestReq{},
|
|
|
|
client.WithContentType("application/json"),
|
|
|
|
)
|
2020-06-05 12:18:35 +03:00
|
|
|
rsp := &TestRsp{}
|
|
|
|
|
|
|
|
reg := rmemory.NewRegistry()
|
|
|
|
brk := bmemory.NewBroker(broker.Registry(reg))
|
|
|
|
tr := tmemory.NewTransport()
|
2020-07-27 15:22:00 +03:00
|
|
|
rtr := rtreg.NewRouter(router.Registry(reg))
|
2020-06-05 12:18:35 +03:00
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
srv := grpcsrv.NewServer(
|
2020-06-05 12:18:35 +03:00
|
|
|
server.Broker(brk),
|
|
|
|
server.Registry(reg),
|
|
|
|
server.Name("go.micro.service.foo"),
|
|
|
|
server.Address("127.0.0.1:0"),
|
|
|
|
server.Transport(tr),
|
|
|
|
)
|
|
|
|
if err = srv.Handle(srv.NewHandler(&TestFoo{})); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = srv.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
cli := grpc.NewClient(
|
2020-07-01 19:06:59 +03:00
|
|
|
client.Router(rtr),
|
2020-06-05 12:18:35 +03:00
|
|
|
client.Broker(brk),
|
|
|
|
client.Transport(tr),
|
|
|
|
)
|
|
|
|
|
|
|
|
w1 := wrapper.StaticClient("xxx_localhost:12345", cli)
|
|
|
|
if err = w1.Call(context.TODO(), req, nil); err == nil {
|
|
|
|
t.Fatal("address xxx_#localhost:12345 must not exists and call must be failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
w2 := wrapper.StaticClient(srv.Options().Address, cli)
|
|
|
|
if err = w2.Call(context.TODO(), req, rsp); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else if rsp.Data != "pass" {
|
|
|
|
t.Fatalf("something wrong with response: %#+v", rsp)
|
|
|
|
}
|
|
|
|
}
|