github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/client/backoff_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/backoff_test.go 16 17 package client 18 19 import ( 20 "context" 21 "testing" 22 "time" 23 24 "github.com/tickoalcantara12/micro/v3/util/codec" 25 ) 26 27 func TestBackoff(t *testing.T) { 28 results := []time.Duration{ 29 0 * time.Second, 30 100 * time.Millisecond, 31 600 * time.Millisecond, 32 1900 * time.Millisecond, 33 4300 * time.Millisecond, 34 7900 * time.Millisecond, 35 } 36 37 r := &testRequest{ 38 service: "test", 39 method: "test", 40 } 41 42 for i := 0; i < 5; i++ { 43 d, err := exponentialBackoff(context.TODO(), r, i) 44 if err != nil { 45 t.Fatal(err) 46 } 47 48 if d != results[i] { 49 t.Fatalf("Expected equal than %v, got %v", results[i], d) 50 } 51 } 52 } 53 54 type testRequest struct { 55 service string 56 method string 57 endpoint string 58 contentType string 59 codec codec.Codec 60 body interface{} 61 opts RequestOptions 62 } 63 64 func newRequest(service, endpoint string, request interface{}, contentType string, reqOpts ...RequestOption) Request { 65 var opts RequestOptions 66 67 for _, o := range reqOpts { 68 o(&opts) 69 } 70 71 // set the content-type specified 72 if len(opts.ContentType) > 0 { 73 contentType = opts.ContentType 74 } 75 76 return &testRequest{ 77 service: service, 78 method: endpoint, 79 endpoint: endpoint, 80 body: request, 81 contentType: contentType, 82 opts: opts, 83 } 84 } 85 86 func (r *testRequest) ContentType() string { 87 return r.contentType 88 } 89 90 func (r *testRequest) Service() string { 91 return r.service 92 } 93 94 func (r *testRequest) Method() string { 95 return r.method 96 } 97 98 func (r *testRequest) Endpoint() string { 99 return r.endpoint 100 } 101 102 func (r *testRequest) Body() interface{} { 103 return r.body 104 } 105 106 func (r *testRequest) Codec() codec.Writer { 107 return r.codec 108 } 109 110 func (r *testRequest) Stream() bool { 111 return r.opts.Stream 112 }