2016-04-05 22:04:37 +03:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2018-03-03 14:53:52 +03:00
|
|
|
"context"
|
2016-04-05 22:04:37 +03:00
|
|
|
"testing"
|
|
|
|
"time"
|
2020-08-23 16:13:45 +03:00
|
|
|
|
|
|
|
"github.com/micro/go-micro/v3/codec"
|
2016-04-05 22:04:37 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBackoff(t *testing.T) {
|
2020-01-30 20:08:03 +03:00
|
|
|
results := []time.Duration{
|
|
|
|
0 * time.Second,
|
|
|
|
100 * time.Millisecond,
|
|
|
|
600 * time.Millisecond,
|
|
|
|
1900 * time.Millisecond,
|
|
|
|
4300 * time.Millisecond,
|
|
|
|
7900 * time.Millisecond,
|
|
|
|
}
|
2016-04-05 22:04:37 +03:00
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
r := &testRequest{
|
|
|
|
service: "test",
|
|
|
|
method: "test",
|
|
|
|
}
|
2018-04-14 20:15:09 +03:00
|
|
|
|
2016-04-05 22:04:37 +03:00
|
|
|
for i := 0; i < 5; i++ {
|
2020-07-27 15:22:00 +03:00
|
|
|
d, err := exponentialBackoff(context.TODO(), r, i)
|
2016-04-05 22:04:37 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-01-30 19:43:03 +03:00
|
|
|
if d != results[i] {
|
|
|
|
t.Fatalf("Expected equal than %v, got %v", results[i], d)
|
2016-04-05 22:04:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-23 16:13:45 +03:00
|
|
|
|
|
|
|
type testRequest struct {
|
|
|
|
service string
|
|
|
|
method string
|
|
|
|
endpoint string
|
|
|
|
contentType string
|
|
|
|
codec codec.Codec
|
|
|
|
body interface{}
|
|
|
|
opts RequestOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRequest(service, endpoint string, request interface{}, contentType string, reqOpts ...RequestOption) Request {
|
|
|
|
var opts RequestOptions
|
|
|
|
|
|
|
|
for _, o := range reqOpts {
|
|
|
|
o(&opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the content-type specified
|
|
|
|
if len(opts.ContentType) > 0 {
|
|
|
|
contentType = opts.ContentType
|
|
|
|
}
|
|
|
|
|
|
|
|
return &testRequest{
|
|
|
|
service: service,
|
|
|
|
method: endpoint,
|
|
|
|
endpoint: endpoint,
|
|
|
|
body: request,
|
|
|
|
contentType: contentType,
|
|
|
|
opts: opts,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *testRequest) ContentType() string {
|
|
|
|
return r.contentType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *testRequest) Service() string {
|
|
|
|
return r.service
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *testRequest) Method() string {
|
|
|
|
return r.method
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *testRequest) Endpoint() string {
|
|
|
|
return r.endpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *testRequest) Body() interface{} {
|
|
|
|
return r.body
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *testRequest) Codec() codec.Writer {
|
|
|
|
return r.codec
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *testRequest) Stream() bool {
|
|
|
|
return r.opts.Stream
|
|
|
|
}
|