github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/client/mucp/options_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/options_test.go
    16  
    17  package mucp
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/tickoalcantara12/micro/v3/service/client"
    24  	"github.com/tickoalcantara12/micro/v3/service/network/transport"
    25  )
    26  
    27  func TestCallOptions(t *testing.T) {
    28  	testData := []struct {
    29  		set      bool
    30  		retries  int
    31  		rtimeout time.Duration
    32  		dtimeout time.Duration
    33  	}{
    34  		{false, client.DefaultRetries, client.DefaultRequestTimeout, transport.DefaultDialTimeout},
    35  		{true, 10, time.Second, time.Second * 2},
    36  	}
    37  
    38  	for _, d := range testData {
    39  		var opts client.Options
    40  		var cl client.Client
    41  
    42  		if d.set {
    43  			opts = client.NewOptions(
    44  				client.Retries(d.retries),
    45  				client.RequestTimeout(d.rtimeout),
    46  				client.DialTimeout(d.dtimeout),
    47  			)
    48  
    49  			cl = NewClient(
    50  				client.Retries(d.retries),
    51  				client.RequestTimeout(d.rtimeout),
    52  				client.DialTimeout(d.dtimeout),
    53  			)
    54  		} else {
    55  			opts = client.NewOptions()
    56  			cl = NewClient()
    57  		}
    58  
    59  		// test options and those set in client
    60  		for _, o := range []client.Options{opts, cl.Options()} {
    61  			if o.CallOptions.Retries != d.retries {
    62  				t.Fatalf("Expected retries %v got %v", d.retries, o.CallOptions.Retries)
    63  			}
    64  
    65  			if o.CallOptions.RequestTimeout != d.rtimeout {
    66  				t.Fatalf("Expected request timeout %v got %v", d.rtimeout, o.CallOptions.RequestTimeout)
    67  			}
    68  
    69  			if o.CallOptions.DialTimeout != d.dtimeout {
    70  				t.Fatalf("Expected %v got %v", d.dtimeout, o.CallOptions.DialTimeout)
    71  			}
    72  
    73  			// copy CallOptions
    74  			callOpts := o.CallOptions
    75  
    76  			// create new opts
    77  			cretries := client.WithRetries(o.CallOptions.Retries * 10)
    78  			crtimeout := client.WithRequestTimeout(o.CallOptions.RequestTimeout * (time.Second * 10))
    79  			cdtimeout := client.WithDialTimeout(o.CallOptions.DialTimeout * (time.Second * 10))
    80  
    81  			// set call options
    82  			for _, opt := range []client.CallOption{cretries, crtimeout, cdtimeout} {
    83  				opt(&callOpts)
    84  			}
    85  
    86  			// check call options
    87  			if e := o.CallOptions.Retries * 10; callOpts.Retries != e {
    88  				t.Fatalf("Expected retries %v got %v", e, callOpts.Retries)
    89  			}
    90  
    91  			if e := o.CallOptions.RequestTimeout * (time.Second * 10); callOpts.RequestTimeout != e {
    92  				t.Fatalf("Expected request timeout %v got %v", e, callOpts.RequestTimeout)
    93  			}
    94  
    95  			if e := o.CallOptions.DialTimeout * (time.Second * 10); callOpts.DialTimeout != e {
    96  				t.Fatalf("Expected %v got %v", e, callOpts.DialTimeout)
    97  			}
    98  
    99  		}
   100  
   101  	}
   102  }