1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-24 10:07:04 +02:00
go-micro/client/rpc_client_test.go

162 lines
3.5 KiB
Go
Raw Normal View History

2016-11-07 20:51:25 +02:00
package client
import (
2018-03-03 13:53:52 +02:00
"context"
2016-11-07 20:51:25 +02:00
"fmt"
"testing"
2019-06-21 18:20:41 +02:00
"github.com/micro/go-micro/client/selector"
"github.com/micro/go-micro/errors"
2016-11-07 20:51:25 +02:00
"github.com/micro/go-micro/registry"
2019-01-14 17:27:25 +02:00
"github.com/micro/go-micro/registry/memory"
2016-11-07 20:51:25 +02:00
)
2019-01-14 17:27:25 +02:00
func newTestRegistry() registry.Registry {
r := memory.NewRegistry()
reg := r.(*memory.Registry)
reg.Services = testData
return reg
2019-01-14 17:27:25 +02:00
}
2018-04-14 17:16:58 +02:00
func TestCallAddress(t *testing.T) {
var called bool
service := "test.service"
2019-01-10 23:25:31 +02:00
endpoint := "Test.Endpoint"
2019-07-08 09:01:42 +02:00
address := "10.1.10.1:8080"
2018-04-14 17:16:58 +02:00
wrap := func(cf CallFunc) CallFunc {
2019-01-18 14:30:39 +02:00
return func(ctx context.Context, node *registry.Node, req Request, rsp interface{}, opts CallOptions) error {
2018-04-14 17:16:58 +02:00
called = true
if req.Service() != service {
return fmt.Errorf("expected service: %s got %s", service, req.Service())
}
2019-01-10 23:25:31 +02:00
if req.Endpoint() != endpoint {
return fmt.Errorf("expected service: %s got %s", endpoint, req.Endpoint())
2018-04-14 17:16:58 +02:00
}
2019-01-18 14:30:39 +02:00
if node.Address != address {
return fmt.Errorf("expected address: %s got %s", address, node.Address)
}
2018-04-14 17:16:58 +02:00
// don't do the call
return nil
}
}
2019-01-14 17:27:25 +02:00
r := newTestRegistry()
2018-04-14 17:16:58 +02:00
c := NewClient(
Registry(r),
WrapCall(wrap),
)
c.Options().Selector.Init(selector.Registry(r))
2019-01-10 23:25:31 +02:00
req := c.NewRequest(service, endpoint, nil)
2018-04-14 17:16:58 +02:00
// test calling remote address
2019-07-08 09:01:42 +02:00
if err := c.Call(context.Background(), req, nil, WithAddress(address)); err != nil {
2018-04-14 17:16:58 +02:00
t.Fatal("call with address error", err)
}
if !called {
t.Fatal("wrapper not called")
}
}
func TestCallRetry(t *testing.T) {
service := "test.service"
2019-01-10 23:25:31 +02:00
endpoint := "Test.Endpoint"
2019-01-18 14:30:39 +02:00
address := "10.1.10.1"
var called int
wrap := func(cf CallFunc) CallFunc {
2019-01-18 14:30:39 +02:00
return func(ctx context.Context, node *registry.Node, req Request, rsp interface{}, opts CallOptions) error {
called++
if called == 1 {
return errors.InternalServerError("test.error", "retry request")
}
// don't do the call
return nil
}
}
2019-01-14 17:27:25 +02:00
r := newTestRegistry()
c := NewClient(
Registry(r),
WrapCall(wrap),
)
c.Options().Selector.Init(selector.Registry(r))
2019-01-10 23:25:31 +02:00
req := c.NewRequest(service, endpoint, nil)
// test calling remote address
if err := c.Call(context.Background(), req, nil, WithAddress(address)); err != nil {
t.Fatal("call with address error", err)
}
// num calls
if called < c.Options().CallOptions.Retries+1 {
t.Fatal("request not retried")
}
}
2016-11-07 20:51:25 +02:00
func TestCallWrapper(t *testing.T) {
var called bool
id := "test.1"
service := "test.service"
2019-01-10 23:25:31 +02:00
endpoint := "Test.Endpoint"
2019-07-08 09:01:42 +02:00
address := "10.1.10.1:8080"
2016-11-07 20:51:25 +02:00
wrap := func(cf CallFunc) CallFunc {
2019-01-18 14:30:39 +02:00
return func(ctx context.Context, node *registry.Node, req Request, rsp interface{}, opts CallOptions) error {
2016-11-07 20:51:25 +02:00
called = true
if req.Service() != service {
return fmt.Errorf("expected service: %s got %s", service, req.Service())
}
2019-01-10 23:25:31 +02:00
if req.Endpoint() != endpoint {
return fmt.Errorf("expected service: %s got %s", endpoint, req.Endpoint())
2016-11-07 20:51:25 +02:00
}
2019-01-18 14:30:39 +02:00
if node.Address != address {
return fmt.Errorf("expected address: %s got %s", address, node.Address)
2016-11-07 20:51:25 +02:00
}
// don't do the call
return nil
}
}
2019-01-14 17:27:25 +02:00
r := newTestRegistry()
2016-11-07 20:51:25 +02:00
c := NewClient(
Registry(r),
WrapCall(wrap),
)
c.Options().Selector.Init(selector.Registry(r))
r.Register(&registry.Service{
Name: service,
Version: "latest",
Nodes: []*registry.Node{
&registry.Node{
Id: id,
2019-01-18 14:30:39 +02:00
Address: address,
2016-11-07 20:51:25 +02:00
},
},
})
2019-01-10 23:25:31 +02:00
req := c.NewRequest(service, endpoint, nil)
2016-11-07 20:51:25 +02:00
if err := c.Call(context.Background(), req, nil); err != nil {
t.Fatal("call wrapper error", err)
}
if !called {
t.Fatal("wrapper not called")
}
}