github.com/cloudwego/hertz@v0.9.3/pkg/app/client/option_test.go (about)

     1  /*
     2   * Copyright 2022 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package client
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/cloudwego/hertz/pkg/app/client/retry"
    24  	"github.com/cloudwego/hertz/pkg/common/config"
    25  	"github.com/cloudwego/hertz/pkg/common/test/assert"
    26  )
    27  
    28  func TestClientOptions(t *testing.T) {
    29  	opt := config.NewClientOptions([]config.ClientOption{
    30  		WithDialTimeout(100 * time.Millisecond),
    31  		WithMaxConnsPerHost(128),
    32  		WithMaxIdleConnDuration(5 * time.Second),
    33  		WithMaxConnDuration(10 * time.Second),
    34  		WithMaxConnWaitTimeout(5 * time.Second),
    35  		WithKeepAlive(false),
    36  		WithClientReadTimeout(1 * time.Second),
    37  		WithResponseBodyStream(true),
    38  		WithRetryConfig(
    39  			retry.WithMaxAttemptTimes(2),
    40  			retry.WithInitDelay(100*time.Millisecond),
    41  			retry.WithMaxDelay(5*time.Second),
    42  			retry.WithMaxJitter(1*time.Second),
    43  			retry.WithDelayPolicy(retry.CombineDelay(retry.DefaultDelayPolicy, retry.FixedDelayPolicy, retry.BackOffDelayPolicy)),
    44  		),
    45  		WithWriteTimeout(time.Second),
    46  		WithConnStateObserve(nil, time.Second),
    47  	})
    48  	assert.DeepEqual(t, 100*time.Millisecond, opt.DialTimeout)
    49  	assert.DeepEqual(t, 128, opt.MaxConnsPerHost)
    50  	assert.DeepEqual(t, 5*time.Second, opt.MaxIdleConnDuration)
    51  	assert.DeepEqual(t, 10*time.Second, opt.MaxConnDuration)
    52  	assert.DeepEqual(t, 5*time.Second, opt.MaxConnWaitTimeout)
    53  	assert.DeepEqual(t, false, opt.KeepAlive)
    54  	assert.DeepEqual(t, 1*time.Second, opt.ReadTimeout)
    55  	assert.DeepEqual(t, 1*time.Second, opt.WriteTimeout)
    56  	assert.DeepEqual(t, true, opt.ResponseBodyStream)
    57  	assert.DeepEqual(t, uint(2), opt.RetryConfig.MaxAttemptTimes)
    58  	assert.DeepEqual(t, 100*time.Millisecond, opt.RetryConfig.Delay)
    59  	assert.DeepEqual(t, 5*time.Second, opt.RetryConfig.MaxDelay)
    60  	assert.DeepEqual(t, 1*time.Second, opt.RetryConfig.MaxJitter)
    61  	assert.DeepEqual(t, 1*time.Second, opt.ObservationInterval)
    62  	for i := 0; i < 100; i++ {
    63  		assert.DeepEqual(t, opt.RetryConfig.DelayPolicy(uint(i), nil, opt.RetryConfig), retry.CombineDelay(retry.DefaultDelayPolicy, retry.FixedDelayPolicy, retry.BackOffDelayPolicy)(uint(i), nil, opt.RetryConfig))
    64  	}
    65  }