cubefs

Форк
0
/
keyratelimit_test.go 
51 строка · 1.4 Кб
1
// Copyright 2023 The CubeFS Authors.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12
// implied. See the License for the specific language governing
13
// permissions and limitations under the License.
14

15
package ratelimit
16

17
import (
18
	"testing"
19

20
	"github.com/stretchr/testify/assert"
21
)
22

23
func TestKeyController(t *testing.T) {
24
	k := NewKeyRateLimit()
25
	key1, key2, key3 := "10001", "10002", "10003"
26
	k.Acquire(key1, 1024)
27
	assert.Equal(t, 1, k.current[key1].refCount)
28
	k.Acquire(key1, 1024)
29
	assert.Equal(t, 2, k.current[key1].refCount)
30

31
	k.Acquire(key2, 1024)
32
	assert.Equal(t, 2, len(k.current))
33
	k.Acquire(key3, 1024)
34
	assert.Equal(t, 3, len(k.current))
35

36
	k.Release(key1)
37
	assert.Equal(t, 1, k.current[key1].refCount)
38
	assert.Equal(t, 3, len(k.current))
39
	k.Release(key1)
40
	_, ok := k.current[key1]
41
	assert.Equal(t, false, ok)
42
	assert.Equal(t, 2, len(k.current))
43
	k.Release(key2)
44
	assert.Equal(t, 1, len(k.current))
45
	k.Release(key3)
46
	assert.Equal(t, 0, len(k.current))
47

48
	assert.Panics(t, func() {
49
		k.Release(key3)
50
	})
51
}
52

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.