2020-07-27 15:22:00 +03:00
|
|
|
package mucp
|
2016-04-05 20:07:07 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/client"
|
|
|
|
"github.com/unistack-org/micro/v3/transport"
|
2016-04-05 20:07:07 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCallOptions(t *testing.T) {
|
|
|
|
testData := []struct {
|
|
|
|
set bool
|
|
|
|
retries int
|
|
|
|
rtimeout time.Duration
|
|
|
|
dtimeout time.Duration
|
|
|
|
}{
|
2020-07-27 15:22:00 +03:00
|
|
|
{false, client.DefaultRetries, client.DefaultRequestTimeout, transport.DefaultDialTimeout},
|
2016-04-05 20:07:07 +03:00
|
|
|
{true, 10, time.Second, time.Second * 2},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range testData {
|
2020-07-27 15:22:00 +03:00
|
|
|
var opts client.Options
|
|
|
|
var cl client.Client
|
2016-04-05 20:07:07 +03:00
|
|
|
|
|
|
|
if d.set {
|
2020-07-27 15:22:00 +03:00
|
|
|
opts = client.NewOptions(
|
|
|
|
client.Retries(d.retries),
|
|
|
|
client.RequestTimeout(d.rtimeout),
|
|
|
|
client.DialTimeout(d.dtimeout),
|
2016-04-05 20:07:07 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
cl = NewClient(
|
2020-07-27 15:22:00 +03:00
|
|
|
client.Retries(d.retries),
|
|
|
|
client.RequestTimeout(d.rtimeout),
|
|
|
|
client.DialTimeout(d.dtimeout),
|
2016-04-05 20:07:07 +03:00
|
|
|
)
|
|
|
|
} else {
|
2020-07-27 15:22:00 +03:00
|
|
|
opts = client.NewOptions()
|
2016-04-05 20:07:07 +03:00
|
|
|
cl = NewClient()
|
|
|
|
}
|
|
|
|
|
|
|
|
// test options and those set in client
|
2020-07-27 15:22:00 +03:00
|
|
|
for _, o := range []client.Options{opts, cl.Options()} {
|
2016-04-05 20:07:07 +03:00
|
|
|
if o.CallOptions.Retries != d.retries {
|
|
|
|
t.Fatalf("Expected retries %v got %v", d.retries, o.CallOptions.Retries)
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.CallOptions.RequestTimeout != d.rtimeout {
|
|
|
|
t.Fatalf("Expected request timeout %v got %v", d.rtimeout, o.CallOptions.RequestTimeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.CallOptions.DialTimeout != d.dtimeout {
|
|
|
|
t.Fatalf("Expected %v got %v", d.dtimeout, o.CallOptions.DialTimeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy CallOptions
|
|
|
|
callOpts := o.CallOptions
|
|
|
|
|
|
|
|
// create new opts
|
2020-07-27 15:22:00 +03:00
|
|
|
cretries := client.WithRetries(o.CallOptions.Retries * 10)
|
|
|
|
crtimeout := client.WithRequestTimeout(o.CallOptions.RequestTimeout * (time.Second * 10))
|
|
|
|
cdtimeout := client.WithDialTimeout(o.CallOptions.DialTimeout * (time.Second * 10))
|
2016-04-05 20:07:07 +03:00
|
|
|
|
|
|
|
// set call options
|
2020-07-27 15:22:00 +03:00
|
|
|
for _, opt := range []client.CallOption{cretries, crtimeout, cdtimeout} {
|
2016-04-05 20:07:07 +03:00
|
|
|
opt(&callOpts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check call options
|
|
|
|
if e := o.CallOptions.Retries * 10; callOpts.Retries != e {
|
|
|
|
t.Fatalf("Expected retries %v got %v", e, callOpts.Retries)
|
|
|
|
}
|
|
|
|
|
|
|
|
if e := o.CallOptions.RequestTimeout * (time.Second * 10); callOpts.RequestTimeout != e {
|
|
|
|
t.Fatalf("Expected request timeout %v got %v", e, callOpts.RequestTimeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
if e := o.CallOptions.DialTimeout * (time.Second * 10); callOpts.DialTimeout != e {
|
|
|
|
t.Fatalf("Expected %v got %v", e, callOpts.DialTimeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|