gitee.com/sasukebo/go-micro/v4@v4.7.1/client/rpc_client_test.go (about)

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