github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/client/mucp/mucp_test.go (about) 1 // Copyright 2020 Asim Aslam 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // Original source: github.com/micro/go-micro/v3/client/mucp/mucp_test.go 16 17 package mucp 18 19 import ( 20 "context" 21 "fmt" 22 "testing" 23 24 "github.com/tickoalcantara12/micro/v3/service/client" 25 "github.com/tickoalcantara12/micro/v3/service/errors" 26 "github.com/tickoalcantara12/micro/v3/service/registry" 27 "github.com/tickoalcantara12/micro/v3/service/registry/memory" 28 "github.com/tickoalcantara12/micro/v3/service/router" 29 regRouter "github.com/tickoalcantara12/micro/v3/service/router/registry" 30 ) 31 32 func newTestRouter() router.Router { 33 reg := memory.NewRegistry(memory.Services(testData)) 34 return regRouter.NewRouter(router.Registry(reg)) 35 } 36 37 func TestCallAddress(t *testing.T) { 38 var called bool 39 service := "test.service" 40 endpoint := "Test.Endpoint" 41 address := "10.1.10.1:8080" 42 43 wrap := func(cf client.CallFunc) client.CallFunc { 44 return func(ctx context.Context, node string, req client.Request, rsp interface{}, opts client.CallOptions) error { 45 called = true 46 47 if req.Service() != service { 48 return fmt.Errorf("expected service: %s got %s", service, req.Service()) 49 } 50 51 if req.Endpoint() != endpoint { 52 return fmt.Errorf("expected service: %s got %s", endpoint, req.Endpoint()) 53 } 54 55 if node != address { 56 return fmt.Errorf("expected address: %s got %s", address, node) 57 } 58 59 // don't do the call 60 return nil 61 } 62 } 63 64 r := newTestRouter() 65 66 c := NewClient( 67 client.Router(r), 68 client.WrapCall(wrap), 69 ) 70 71 req := c.NewRequest(service, endpoint, nil) 72 73 // test calling remote address 74 if err := c.Call(context.Background(), req, nil, client.WithAddress(address)); err != nil { 75 t.Fatal("call with address error", err) 76 } 77 78 if !called { 79 t.Fatal("wrapper not called") 80 } 81 82 } 83 84 func TestCallRetry(t *testing.T) { 85 service := "test.service" 86 endpoint := "Test.Endpoint" 87 address := "10.1.10.1" 88 89 var called int 90 91 wrap := func(cf client.CallFunc) client.CallFunc { 92 return func(ctx context.Context, node string, req client.Request, rsp interface{}, opts client.CallOptions) error { 93 called++ 94 if called == 1 { 95 return errors.InternalServerError("test.error", "retry request") 96 } 97 98 // don't do the call 99 return nil 100 } 101 } 102 103 r := newTestRouter() 104 c := NewClient( 105 client.Router(r), 106 client.WrapCall(wrap), 107 client.Retry(client.RetryOnError), 108 ) 109 110 req := c.NewRequest(service, endpoint, nil) 111 112 // test calling remote address 113 if err := c.Call(context.Background(), req, nil, client.WithAddress(address)); err != nil { 114 t.Fatal("call with address error", err) 115 } 116 117 // num calls 118 if called < c.Options().CallOptions.Retries+1 { 119 t.Fatal("request not retried") 120 } 121 } 122 123 func TestCallWrapper(t *testing.T) { 124 var called bool 125 id := "test.1" 126 service := "test.service" 127 endpoint := "Test.Endpoint" 128 address := "10.1.10.1:8080" 129 130 wrap := func(cf client.CallFunc) client.CallFunc { 131 return func(ctx context.Context, node string, req client.Request, rsp interface{}, opts client.CallOptions) error { 132 called = true 133 134 if req.Service() != service { 135 return fmt.Errorf("expected service: %s got %s", service, req.Service()) 136 } 137 138 if req.Endpoint() != endpoint { 139 return fmt.Errorf("expected service: %s got %s", endpoint, req.Endpoint()) 140 } 141 142 if node != address { 143 return fmt.Errorf("expected address: %s got %s", address, node) 144 } 145 146 // don't do the call 147 return nil 148 } 149 } 150 151 r := newTestRouter() 152 c := NewClient( 153 client.Router(r), 154 client.WrapCall(wrap), 155 ) 156 157 r.Options().Registry.Register(®istry.Service{ 158 Name: service, 159 Version: "latest", 160 Nodes: []*registry.Node{ 161 { 162 Id: id, 163 Address: address, 164 }, 165 }, 166 }) 167 168 req := c.NewRequest(service, endpoint, nil) 169 if err := c.Call(context.Background(), req, nil); err != nil { 170 t.Fatal("call wrapper error", err) 171 } 172 173 if !called { 174 t.Fatal("wrapper not called") 175 } 176 }