github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/client/rate_limit_interceptor_test.go (about)

     1  // Licensed to the LF AI & Data foundation under one
     2  // or more contributor license agreements. See the NOTICE file
     3  // distributed with this work for additional information
     4  // regarding copyright ownership. The ASF licenses this file
     5  // to you under the Apache License, Version 2.0 (the
     6  // "License"); you may not use this file except in compliance
     7  // with the License. You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package client
    18  
    19  import (
    20  	"context"
    21  	"math"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  	"google.golang.org/grpc"
    27  
    28  	"github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
    29  )
    30  
    31  var mockInvokerError error
    32  var mockInvokerReply interface{}
    33  var mockInvokeTimes = 0
    34  
    35  var mockInvoker grpc.UnaryInvoker = func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, opts ...grpc.CallOption) error {
    36  	mockInvokeTimes++
    37  	return mockInvokerError
    38  }
    39  
    40  func resetMockInvokeTimes() {
    41  	mockInvokeTimes = 0
    42  }
    43  
    44  func TestRateLimitInterceptor(t *testing.T) {
    45  	maxRetry := uint(3)
    46  	maxBackoff := 3 * time.Second
    47  	inter := RetryOnRateLimitInterceptor(maxRetry, maxBackoff, func(ctx context.Context, attempt uint) time.Duration {
    48  		return 60 * time.Millisecond * time.Duration(math.Pow(2, float64(attempt)))
    49  	})
    50  
    51  	ctx := context.Background()
    52  
    53  	// with retry
    54  	mockInvokerReply = &commonpb.Status{ErrorCode: commonpb.ErrorCode_RateLimit}
    55  	resetMockInvokeTimes()
    56  	err := inter(ctx, "", nil, mockInvokerReply, nil, mockInvoker)
    57  	assert.NoError(t, err)
    58  	assert.Equal(t, maxRetry, uint(mockInvokeTimes))
    59  
    60  	// without retry
    61  	ctx1 := context.WithValue(ctx, RetryOnRateLimit, false)
    62  	resetMockInvokeTimes()
    63  	err = inter(ctx1, "", nil, mockInvokerReply, nil, mockInvoker)
    64  	assert.NoError(t, err)
    65  	assert.Equal(t, uint(1), uint(mockInvokeTimes))
    66  }