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