2020-08-11 13:25:49 +03:00
|
|
|
package client_test
|
2016-04-15 18:45:59 +03:00
|
|
|
|
|
|
|
import (
|
2018-03-03 14:53:52 +03:00
|
|
|
"context"
|
2016-04-15 18:45:59 +03:00
|
|
|
"testing"
|
|
|
|
|
2020-07-31 13:36:33 +03:00
|
|
|
"github.com/micro/go-micro/v3/broker"
|
|
|
|
bmemory "github.com/micro/go-micro/v3/broker/memory"
|
2020-07-27 15:22:00 +03:00
|
|
|
"github.com/micro/go-micro/v3/client"
|
|
|
|
"github.com/micro/go-micro/v3/client/grpc"
|
2020-08-23 20:37:22 +03:00
|
|
|
tmemory "github.com/micro/go-micro/v3/network/transport/memory"
|
2020-07-31 13:36:33 +03:00
|
|
|
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"
|
2020-07-27 15:22:00 +03:00
|
|
|
"github.com/micro/go-micro/v3/server"
|
2020-07-31 13:36:33 +03:00
|
|
|
grpcsrv "github.com/micro/go-micro/v3/server/grpc"
|
2020-08-11 13:25:49 +03:00
|
|
|
cw "github.com/micro/go-micro/v3/util/client"
|
2016-04-15 18:45:59 +03:00
|
|
|
)
|
|
|
|
|
2020-07-31 13:36:33 +03:00
|
|
|
type TestFoo struct {
|
2020-05-22 12:44:18 +03:00
|
|
|
}
|
|
|
|
|
2020-07-31 13:36:33 +03:00
|
|
|
type TestReq struct{}
|
2020-05-22 12:44:18 +03:00
|
|
|
|
2020-07-31 13:36:33 +03:00
|
|
|
type TestRsp struct {
|
|
|
|
Data string
|
2020-05-22 12:44:18 +03:00
|
|
|
}
|
|
|
|
|
2020-07-31 13:36:33 +03:00
|
|
|
func (h *TestFoo) Bar(ctx context.Context, req *TestReq, rsp *TestRsp) error {
|
|
|
|
rsp.Data = "pass"
|
|
|
|
return nil
|
2020-05-22 12:44:18 +03:00
|
|
|
}
|
|
|
|
|
2020-08-11 13:25:49 +03:00
|
|
|
func TestStaticClient(t *testing.T) {
|
2020-07-31 13:36:33 +03:00
|
|
|
var err error
|
|
|
|
|
|
|
|
req := grpc.NewClient().NewRequest(
|
|
|
|
"go.micro.service.foo",
|
|
|
|
"TestFoo.Bar",
|
|
|
|
&TestReq{},
|
|
|
|
client.WithContentType("application/json"),
|
|
|
|
)
|
|
|
|
rsp := &TestRsp{}
|
|
|
|
|
|
|
|
reg := rmemory.NewRegistry()
|
|
|
|
brk := bmemory.NewBroker(broker.Registry(reg))
|
|
|
|
tr := tmemory.NewTransport()
|
|
|
|
rtr := rtreg.NewRouter(router.Registry(reg))
|
|
|
|
|
|
|
|
srv := grpcsrv.NewServer(
|
|
|
|
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)
|
2020-05-22 12:44:18 +03:00
|
|
|
}
|
|
|
|
|
2020-07-31 13:36:33 +03:00
|
|
|
if err = srv.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
2020-05-24 22:26:37 +03:00
|
|
|
}
|
|
|
|
|
2020-07-31 13:36:33 +03:00
|
|
|
cli := grpc.NewClient(
|
|
|
|
client.Router(rtr),
|
|
|
|
client.Broker(brk),
|
|
|
|
client.Transport(tr),
|
|
|
|
)
|
2020-05-24 22:26:37 +03:00
|
|
|
|
2020-08-11 13:25:49 +03:00
|
|
|
w1 := cw.Static("xxx_localhost:12345", cli)
|
2020-07-31 13:36:33 +03:00
|
|
|
if err = w1.Call(context.TODO(), req, nil); err == nil {
|
|
|
|
t.Fatal("address xxx_#localhost:12345 must not exists and call must be failed")
|
|
|
|
}
|
2020-05-24 22:26:37 +03:00
|
|
|
|
2020-08-11 13:25:49 +03:00
|
|
|
w2 := cw.Static(srv.Options().Address, cli)
|
2020-07-31 13:36:33 +03:00
|
|
|
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)
|
|
|
|
}
|
2016-04-15 18:45:59 +03:00
|
|
|
}
|