From 70a17dc10a2e965820d3c73c8dc3009e6737f31c Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Tue, 15 Dec 2020 23:09:51 +0300 Subject: [PATCH] client: allow to create new client with predefined call options Signed-off-by: Vasiliy Tolstov --- client/client_call_options.go | 23 ++++++++++ util/client/client.go | 25 ----------- util/client/client_test.go | 84 ----------------------------------- 3 files changed, 23 insertions(+), 109 deletions(-) create mode 100644 client/client_call_options.go delete mode 100644 util/client/client.go delete mode 100644 util/client/client_test.go diff --git a/client/client_call_options.go b/client/client_call_options.go new file mode 100644 index 00000000..903642a5 --- /dev/null +++ b/client/client_call_options.go @@ -0,0 +1,23 @@ +package client + +import ( + "context" +) + +type clientCallOptions struct { + Client + opts []CallOption +} + +func (s *clientCallOptions) Call(ctx context.Context, req Request, rsp interface{}, opts ...CallOption) error { + return s.Client.Call(ctx, req, rsp, append(s.opts, opts...)...) +} + +func (s *clientCallOptions) Stream(ctx context.Context, req Request, opts ...CallOption) (Stream, error) { + return s.Client.Stream(ctx, req, append(s.opts, opts...)...) +} + +// NewClientCallOptions add CallOption to every call +func NewClientCallOptions(c Client, opts ...CallOption) Client { + return &clientCallOptions{c, opts} +} diff --git a/util/client/client.go b/util/client/client.go deleted file mode 100644 index 0f680b80..00000000 --- a/util/client/client.go +++ /dev/null @@ -1,25 +0,0 @@ -package client - -import ( - "context" - - "github.com/unistack-org/micro/v3/client" -) - -type staticClient struct { - address string - client.Client -} - -func (s *staticClient) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error { - return s.Client.Call(ctx, req, rsp, append(opts, client.WithAddress(s.address))...) -} - -func (s *staticClient) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) { - return s.Client.Stream(ctx, req, append(opts, client.WithAddress(s.address))...) -} - -// StaticClient sets an address on every call -func Static(address string, c client.Client) client.Client { - return &staticClient{address, c} -} diff --git a/util/client/client_test.go b/util/client/client_test.go deleted file mode 100644 index 1d8f8447..00000000 --- a/util/client/client_test.go +++ /dev/null @@ -1,84 +0,0 @@ -// +build ignore - -package client_test - -import ( - "context" - "testing" - - "github.com/unistack-org/micro/v3/broker" - bmemory "github.com/unistack-org/micro/v3/broker/memory" - "github.com/unistack-org/micro/v3/client" - "github.com/unistack-org/micro/v3/client/grpc" - tmemory "github.com/unistack-org/micro/v3/network/transport/memory" - rmemory "github.com/unistack-org/micro/v3/registry/memory" - "github.com/unistack-org/micro/v3/router" - rtreg "github.com/unistack-org/micro/v3/router/registry" - "github.com/unistack-org/micro/v3/server" - grpcsrv "github.com/unistack-org/micro/v3/server/grpc" - cw "github.com/unistack-org/micro/v3/util/client" -) - -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 TestStaticClient(t *testing.T) { - 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) - } - - if err = srv.Start(); err != nil { - t.Fatal(err) - } - - cli := grpc.NewClient( - client.Router(rtr), - client.Broker(brk), - client.Transport(tr), - ) - - w1 := cw.Static("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 := cw.Static(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) - } -}