dubbo.apache.org/dubbo-go/v3@v3.1.1/filter/tps/filter_test.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * 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  
    18  package tps
    19  
    20  import (
    21  	"context"
    22  	"net/url"
    23  	"testing"
    24  )
    25  
    26  import (
    27  	"github.com/golang/mock/gomock"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  )
    31  
    32  import (
    33  	"dubbo.apache.org/dubbo-go/v3/common"
    34  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    35  	"dubbo.apache.org/dubbo-go/v3/common/extension"
    36  	"dubbo.apache.org/dubbo-go/v3/filter"
    37  	"dubbo.apache.org/dubbo-go/v3/filter/handler"
    38  	"dubbo.apache.org/dubbo-go/v3/filter/tps/limiter"
    39  	"dubbo.apache.org/dubbo-go/v3/protocol"
    40  	"dubbo.apache.org/dubbo-go/v3/protocol/invocation"
    41  )
    42  
    43  func TestTpsLimitFilterInvokeWithNoTpsLimiter(t *testing.T) {
    44  	tpsFilter := &tpsLimitFilter{}
    45  	invokeUrl := common.NewURLWithOptions(
    46  		common.WithParams(url.Values{}),
    47  		common.WithParamsValue(constant.TPSLimiterKey, ""))
    48  	attch := make(map[string]interface{})
    49  
    50  	result := tpsFilter.Invoke(context.Background(),
    51  		protocol.NewBaseInvoker(invokeUrl),
    52  		invocation.NewRPCInvocation("MethodName",
    53  			[]interface{}{"OK"}, attch))
    54  	assert.Nil(t, result.Error())
    55  	assert.Nil(t, result.Result())
    56  }
    57  
    58  func TestGenericFilterInvokeWithDefaultTpsLimiter(t *testing.T) {
    59  	ctrl := gomock.NewController(t)
    60  	defer ctrl.Finish()
    61  	mockLimiter := limiter.NewMockTpsLimiter(ctrl)
    62  	mockLimiter.EXPECT().IsAllowable(gomock.Any(), gomock.Any()).Return(true).Times(1)
    63  	extension.SetTpsLimiter(constant.DefaultKey, func() filter.TpsLimiter {
    64  		return mockLimiter
    65  	})
    66  
    67  	tpsFilter := &tpsLimitFilter{}
    68  	invokeUrl := common.NewURLWithOptions(
    69  		common.WithParams(url.Values{}),
    70  		common.WithParamsValue(constant.TPSLimiterKey, constant.DefaultKey))
    71  	attch := make(map[string]interface{})
    72  
    73  	result := tpsFilter.Invoke(context.Background(),
    74  		protocol.NewBaseInvoker(invokeUrl),
    75  		invocation.NewRPCInvocation("MethodName",
    76  			[]interface{}{"OK"}, attch))
    77  	assert.Nil(t, result.Error())
    78  	assert.Nil(t, result.Result())
    79  }
    80  
    81  func TestGenericFilterInvokeWithDefaultTpsLimiterNotAllow(t *testing.T) {
    82  	ctrl := gomock.NewController(t)
    83  	defer ctrl.Finish()
    84  	mockLimiter := limiter.NewMockTpsLimiter(ctrl)
    85  	mockLimiter.EXPECT().IsAllowable(gomock.Any(), gomock.Any()).Return(false).Times(1)
    86  	extension.SetTpsLimiter(constant.DefaultKey, func() filter.TpsLimiter {
    87  		return mockLimiter
    88  	})
    89  
    90  	mockResult := &protocol.RPCResult{}
    91  	mockRejectedHandler := handler.NewMockRejectedExecutionHandler(ctrl)
    92  	mockRejectedHandler.EXPECT().RejectedExecution(gomock.Any(), gomock.Any()).Return(mockResult).Times(1)
    93  
    94  	extension.SetRejectedExecutionHandler(constant.DefaultKey, func() filter.RejectedExecutionHandler {
    95  		return mockRejectedHandler
    96  	})
    97  
    98  	tpsFilter := &tpsLimitFilter{}
    99  	invokeUrl := common.NewURLWithOptions(
   100  		common.WithParams(url.Values{}),
   101  		common.WithParamsValue(constant.TPSLimiterKey, constant.DefaultKey))
   102  	attch := make(map[string]interface{})
   103  
   104  	result := tpsFilter.Invoke(context.Background(),
   105  		protocol.NewBaseInvoker(invokeUrl),
   106  		invocation.NewRPCInvocation("MethodName", []interface{}{"OK"}, attch))
   107  	assert.Nil(t, result.Error())
   108  	assert.Nil(t, result.Result())
   109  }