dubbo.apache.org/dubbo-go/v3@v3.1.1/filter/sentinel/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 sentinel
    19  
    20  import (
    21  	"context"
    22  	"sync"
    23  	"sync/atomic"
    24  	"testing"
    25  )
    26  
    27  import (
    28  	"github.com/alibaba/sentinel-golang/core/flow"
    29  
    30  	"github.com/stretchr/testify/assert"
    31  )
    32  
    33  import (
    34  	"dubbo.apache.org/dubbo-go/v3/common"
    35  	"dubbo.apache.org/dubbo-go/v3/protocol"
    36  	"dubbo.apache.org/dubbo-go/v3/protocol/invocation"
    37  )
    38  
    39  func TestSentinelFilter_QPS(t *testing.T) {
    40  	url, err := common.NewURL("dubbo://127.0.0.1:20000/UserProvider?anyhost=true&" +
    41  		"version=1.0.0&group=myGroup&" +
    42  		"application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&" +
    43  		"environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&" +
    44  		"module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&" +
    45  		"side=provider&timeout=3000&timestamp=1556509797245&bean.name=UserProvider")
    46  	assert.NoError(t, err)
    47  	mockInvoker := protocol.NewBaseInvoker(url)
    48  	interfaceResourceName, _ := getResourceName(mockInvoker,
    49  		invocation.NewRPCInvocation("hello", []interface{}{"OK"}, make(map[string]interface{})), "prefix_")
    50  	mockInvocation := invocation.NewRPCInvocation("hello", []interface{}{"OK"}, make(map[string]interface{}))
    51  
    52  	_, err = flow.LoadRules([]*flow.Rule{
    53  		{
    54  			Resource: interfaceResourceName,
    55  			// MetricType:             flow.QPS,
    56  			TokenCalculateStrategy: flow.Direct,
    57  			ControlBehavior:        flow.Reject,
    58  			Threshold:              100,
    59  			RelationStrategy:       flow.CurrentResource,
    60  		},
    61  	})
    62  	assert.NoError(t, err)
    63  
    64  	wg := sync.WaitGroup{}
    65  	wg.Add(10)
    66  	f := &sentinelProviderFilter{}
    67  	pass := int64(0)
    68  	block := int64(0)
    69  	for i := 0; i < 10; i++ {
    70  		go func() {
    71  			for j := 0; j < 30; j++ {
    72  				result := f.Invoke(context.TODO(), mockInvoker, mockInvocation)
    73  				if result.Error() == nil {
    74  					atomic.AddInt64(&pass, 1)
    75  				} else {
    76  					atomic.AddInt64(&block, 1)
    77  				}
    78  			}
    79  			wg.Done()
    80  		}()
    81  	}
    82  	wg.Wait()
    83  	// todo sentinel can't assure the passed count is 100, sometimes is 101
    84  	assert.True(t, atomic.LoadInt64(&pass) <= 105 && atomic.LoadInt64(&pass) >= 95)
    85  	assert.True(t, atomic.LoadInt64(&block) <= 205 && atomic.LoadInt64(&block) >= 195)
    86  }
    87  
    88  func TestConsumerFilter_Invoke(t *testing.T) {
    89  	f := &sentinelConsumerFilter{}
    90  	url, err := common.NewURL("dubbo://127.0.0.1:20000/UserProvider?anyhost=true&" +
    91  		"application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&" +
    92  		"environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&" +
    93  		"module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&" +
    94  		"side=provider&timeout=3000&timestamp=1556509797245&bean.name=UserProvider")
    95  	assert.NoError(t, err)
    96  	mockInvoker := protocol.NewBaseInvoker(url)
    97  	mockInvocation := invocation.NewRPCInvocation("hello", []interface{}{"OK"}, make(map[string]interface{}))
    98  	result := f.Invoke(context.TODO(), mockInvoker, mockInvocation)
    99  	assert.NoError(t, result.Error())
   100  }
   101  
   102  func TestProviderFilter_Invoke(t *testing.T) {
   103  	f := &sentinelProviderFilter{}
   104  	url, err := common.NewURL("dubbo://127.0.0.1:20000/UserProvider?anyhost=true&" +
   105  		"application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&" +
   106  		"environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&" +
   107  		"module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&" +
   108  		"side=provider&timeout=3000&timestamp=1556509797245&bean.name=UserProvider")
   109  	assert.NoError(t, err)
   110  	mockInvoker := protocol.NewBaseInvoker(url)
   111  	mockInvocation := invocation.NewRPCInvocation("hello", []interface{}{"OK"}, make(map[string]interface{}))
   112  	result := f.Invoke(context.TODO(), mockInvoker, mockInvocation)
   113  	assert.NoError(t, result.Error())
   114  }
   115  
   116  func TestGetResourceName(t *testing.T) {
   117  	url, err := common.NewURL("dubbo://127.0.0.1:20000/UserProvider?anyhost=true&" +
   118  		"version=1.0.0&group=myGroup&" +
   119  		"application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&" +
   120  		"environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&" +
   121  		"module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&" +
   122  		"side=provider&timeout=3000&timestamp=1556509797245&bean.name=UserProvider")
   123  	assert.NoError(t, err)
   124  	mockInvoker := protocol.NewBaseInvoker(url)
   125  	interfaceResourceName, methodResourceName := getResourceName(mockInvoker,
   126  		invocation.NewRPCInvocation("hello", []interface{}{"OK"}, make(map[string]interface{})), "prefix_")
   127  	assert.Equal(t, "com.ikurento.user.UserProvider:myGroup:1.0.0", interfaceResourceName)
   128  	assert.Equal(t, "prefix_com.ikurento.user.UserProvider:myGroup:1.0.0:hello()", methodResourceName)
   129  }