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