multi_rate_limiter_test.go 3.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
// Licensed to the LF AI & Data foundation under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package proxy

import (
	"math"
	"testing"

	"github.com/milvus-io/milvus/internal/proto/internalpb"
	"github.com/milvus-io/milvus/internal/util/ratelimitutil"
	"github.com/stretchr/testify/assert"
)

func TestMultiRateLimiter(t *testing.T) {
	Params.Init()
	t.Run("test multiRateLimiter", func(t *testing.T) {
B
bigsheeper 已提交
31 32
		bak := Params.QuotaConfig.QuotaAndLimitsEnabled
		Params.QuotaConfig.QuotaAndLimitsEnabled = true
33 34 35 36 37 38 39 40 41 42 43 44
		multiLimiter := NewMultiRateLimiter()
		for _, rt := range internalpb.RateType_value {
			multiLimiter.globalRateLimiter.limiters[internalpb.RateType(rt)] = ratelimitutil.NewLimiter(ratelimitutil.Limit(1000), 1)
		}
		for _, rt := range internalpb.RateType_value {
			ok, _ := multiLimiter.Limit(internalpb.RateType(rt), 1)
			assert.False(t, ok)
			ok, _ = multiLimiter.Limit(internalpb.RateType(rt), math.MaxInt)
			assert.False(t, ok)
			ok, _ = multiLimiter.Limit(internalpb.RateType(rt), math.MaxInt)
			assert.True(t, ok)
		}
B
bigsheeper 已提交
45
		Params.QuotaConfig.QuotaAndLimitsEnabled = bak
46 47 48 49
	})

	t.Run("not enable quotaAndLimit", func(t *testing.T) {
		multiLimiter := NewMultiRateLimiter()
B
bigsheeper 已提交
50 51
		bak := Params.QuotaConfig.QuotaAndLimitsEnabled
		Params.QuotaConfig.QuotaAndLimitsEnabled = false
52 53 54
		ok, r := multiLimiter.Limit(internalpb.RateType(0), 1)
		assert.False(t, ok)
		assert.NotEqual(t, float64(0), r)
B
bigsheeper 已提交
55
		Params.QuotaConfig.QuotaAndLimitsEnabled = bak
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	})
}

func TestRateLimiter(t *testing.T) {
	Params.Init()
	t.Run("test limit", func(t *testing.T) {
		limiter := newRateLimiter()
		limiter.registerLimiters()
		for _, rt := range internalpb.RateType_value {
			limiter.limiters[internalpb.RateType(rt)] = ratelimitutil.NewLimiter(ratelimitutil.Limit(1000), 1)
		}
		for _, rt := range internalpb.RateType_value {
			ok, _ := limiter.limit(internalpb.RateType(rt), 1)
			assert.False(t, ok)
			ok, _ = limiter.limit(internalpb.RateType(rt), math.MaxInt)
			assert.False(t, ok)
			ok, _ = limiter.limit(internalpb.RateType(rt), math.MaxInt)
			assert.True(t, ok)
		}
	})

	t.Run("test setRates", func(t *testing.T) {
		limiter := newRateLimiter()
		for _, rt := range internalpb.RateType_value {
			limiter.limiters[internalpb.RateType(rt)] = ratelimitutil.NewLimiter(ratelimitutil.Limit(1000), 1)
		}

		zeroRates := make([]*internalpb.Rate, 0, len(internalpb.RateType_value))
		for _, rt := range internalpb.RateType_value {
			zeroRates = append(zeroRates, &internalpb.Rate{
				Rt: internalpb.RateType(rt), R: 0,
			})
		}
		err := limiter.setRates(zeroRates)
		assert.NoError(t, err)
		for _, rt := range internalpb.RateType_value {
			for i := 0; i < 100; i++ {
				if i == 0 {
					// Consume token initialed in bucket
					ok, _ := limiter.limit(internalpb.RateType(rt), 1)
					assert.False(t, ok)
				} else {
					ok, _ := limiter.limit(internalpb.RateType(rt), 1)
					assert.True(t, ok)
				}
			}
		}
	})
}