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