mirror of
https://github.com/go-micro/go-micro.git
synced 2025-09-16 08:36:30 +02:00
util/wrapper: Add Static Client wrapper (#1685)
* util/wrapper: Add Static Client wrapper * util/wrapper/static: pass address to stream too Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org> * add static client wrapper tests Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org> * server: fix error message spaces between words Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org> * server/{rpc,grpc}: replace log.Error with log.Errorf * server/grpc: fix log typo * server/rpc: fix log typo Co-authored-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
committed by
Dominic Wong
parent
e5136332e3
commit
8dfd93e915
@@ -292,3 +292,21 @@ func (c *cacheWrapper) Call(ctx context.Context, req client.Request, rsp interfa
|
||||
func CacheClient(cacheFn func() *client.Cache, c client.Client) client.Client {
|
||||
return &cacheWrapper{cacheFn, c}
|
||||
}
|
||||
|
||||
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 StaticClient(address string, c client.Client) client.Client {
|
||||
return &staticClient{address, c}
|
||||
}
|
||||
|
72
util/wrapper/wrapper_static_client_test.go
Normal file
72
util/wrapper/wrapper_static_client_test.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package wrapper_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/micro/go-micro/v2/broker"
|
||||
bmemory "github.com/micro/go-micro/v2/broker/memory"
|
||||
"github.com/micro/go-micro/v2/client"
|
||||
rmemory "github.com/micro/go-micro/v2/registry/memory"
|
||||
"github.com/micro/go-micro/v2/server"
|
||||
tmemory "github.com/micro/go-micro/v2/transport/memory"
|
||||
wrapper "github.com/micro/go-micro/v2/util/wrapper"
|
||||
)
|
||||
|
||||
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
|
||||
|
||||
req := client.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()
|
||||
|
||||
srv := server.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 := client.NewClient(
|
||||
client.Registry(reg),
|
||||
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)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user