github.com/annwntech/go-micro/v2@v2.9.5/client/rpc_client_test.go (about)

     1  package client
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/annwntech/go-micro/v2/client/selector"
     9  	"github.com/annwntech/go-micro/v2/errors"
    10  	"github.com/annwntech/go-micro/v2/registry"
    11  	"github.com/annwntech/go-micro/v2/registry/memory"
    12  )
    13  
    14  func newTestRegistry() registry.Registry {
    15  	return memory.NewRegistry(memory.Services(testData))
    16  }
    17  
    18  func TestCallAddress(t *testing.T) {
    19  	var called bool
    20  	service := "test.service"
    21  	endpoint := "Test.Endpoint"
    22  	address := "10.1.10.1:8080"
    23  
    24  	wrap := func(cf CallFunc) CallFunc {
    25  		return func(ctx context.Context, node *registry.Node, req Request, rsp interface{}, opts CallOptions) error {
    26  			called = true
    27  
    28  			if req.Service() != service {
    29  				return fmt.Errorf("expected service: %s got %s", service, req.Service())
    30  			}
    31  
    32  			if req.Endpoint() != endpoint {
    33  				return fmt.Errorf("expected service: %s got %s", endpoint, req.Endpoint())
    34  			}
    35  
    36  			if node.Address != address {
    37  				return fmt.Errorf("expected address: %s got %s", address, node.Address)
    38  			}
    39  
    40  			// don't do the call
    41  			return nil
    42  		}
    43  	}
    44  
    45  	r := newTestRegistry()
    46  	c := NewClient(
    47  		Registry(r),
    48  		WrapCall(wrap),
    49  	)
    50  	c.Options().Selector.Init(selector.Registry(r))
    51  
    52  	req := c.NewRequest(service, endpoint, nil)
    53  
    54  	// test calling remote address
    55  	if err := c.Call(context.Background(), req, nil, WithAddress(address)); err != nil {
    56  		t.Fatal("call with address error", err)
    57  	}
    58  
    59  	if !called {
    60  		t.Fatal("wrapper not called")
    61  	}
    62  
    63  }
    64  
    65  func TestCallRetry(t *testing.T) {
    66  	service := "test.service"
    67  	endpoint := "Test.Endpoint"
    68  	address := "10.1.10.1"
    69  
    70  	var called int
    71  
    72  	wrap := func(cf CallFunc) CallFunc {
    73  		return func(ctx context.Context, node *registry.Node, req Request, rsp interface{}, opts CallOptions) error {
    74  			called++
    75  			if called == 1 {
    76  				return errors.InternalServerError("test.error", "retry request")
    77  			}
    78  
    79  			// don't do the call
    80  			return nil
    81  		}
    82  	}
    83  
    84  	r := newTestRegistry()
    85  	c := NewClient(
    86  		Registry(r),
    87  		WrapCall(wrap),
    88  	)
    89  	c.Options().Selector.Init(selector.Registry(r))
    90  
    91  	req := c.NewRequest(service, endpoint, nil)
    92  
    93  	// test calling remote address
    94  	if err := c.Call(context.Background(), req, nil, WithAddress(address)); err != nil {
    95  		t.Fatal("call with address error", err)
    96  	}
    97  
    98  	// num calls
    99  	if called < c.Options().CallOptions.Retries+1 {
   100  		t.Fatal("request not retried")
   101  	}
   102  }
   103  
   104  func TestCallWrapper(t *testing.T) {
   105  	var called bool
   106  	id := "test.1"
   107  	service := "test.service"
   108  	endpoint := "Test.Endpoint"
   109  	address := "10.1.10.1:8080"
   110  
   111  	wrap := func(cf CallFunc) CallFunc {
   112  		return func(ctx context.Context, node *registry.Node, req Request, rsp interface{}, opts CallOptions) error {
   113  			called = true
   114  
   115  			if req.Service() != service {
   116  				return fmt.Errorf("expected service: %s got %s", service, req.Service())
   117  			}
   118  
   119  			if req.Endpoint() != endpoint {
   120  				return fmt.Errorf("expected service: %s got %s", endpoint, req.Endpoint())
   121  			}
   122  
   123  			if node.Address != address {
   124  				return fmt.Errorf("expected address: %s got %s", address, node.Address)
   125  			}
   126  
   127  			// don't do the call
   128  			return nil
   129  		}
   130  	}
   131  
   132  	r := newTestRegistry()
   133  	c := NewClient(
   134  		Registry(r),
   135  		WrapCall(wrap),
   136  	)
   137  	c.Options().Selector.Init(selector.Registry(r))
   138  
   139  	r.Register(&registry.Service{
   140  		Name:    service,
   141  		Version: "latest",
   142  		Nodes: []*registry.Node{
   143  			{
   144  				Id:      id,
   145  				Address: address,
   146  				Metadata: map[string]string{
   147  					"protocol": "mucp",
   148  				},
   149  			},
   150  		},
   151  	})
   152  
   153  	req := c.NewRequest(service, endpoint, nil)
   154  	if err := c.Call(context.Background(), req, nil); err != nil {
   155  		t.Fatal("call wrapper error", err)
   156  	}
   157  
   158  	if !called {
   159  		t.Fatal("wrapper not called")
   160  	}
   161  }