istio

Форк
0
/
ttlCache_test.go 
98 строк · 2.5 Кб
1
// Copyright 2017 Istio 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 implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
package cache
16

17
import (
18
	"sync/atomic"
19
	"testing"
20
	"time"
21
)
22

23
func TestTTLBasic(t *testing.T) {
24
	ttl := NewTTL(5*time.Second, 1*time.Millisecond)
25
	testCacheBasic(ttl, t)
26
}
27

28
func TestTTLConcurrent(t *testing.T) {
29
	ttl := NewTTL(5*time.Second, 1*time.Second)
30
	testCacheConcurrent(ttl, t)
31
}
32

33
func TestTTLExpiration(t *testing.T) {
34
	ttl := NewTTL(5*time.Second, 0).(*ttlCache)
35
	testCacheExpiration(ttl, ttl.evictExpired, t)
36
}
37

38
func TestTTLEvicter(t *testing.T) {
39
	ttl := NewTTL(5*time.Second, 1*time.Millisecond)
40
	testCacheEvicter(ttl)
41
}
42

43
func TestTTLEvictExpired(t *testing.T) {
44
	ttl := NewTTL(5*time.Second, 0).(*ttlCache)
45
	testCacheEvictExpired(ttl, t)
46
}
47

48
type callbackRecorder struct {
49
	callbacks int64
50
}
51

52
func (c *callbackRecorder) callback(key, value any) {
53
	atomic.AddInt64(&c.callbacks, 1)
54
}
55

56
func TestTTLEvictionCallback(t *testing.T) {
57
	c := &callbackRecorder{callbacks: 0}
58
	ttl := NewTTLWithCallback(50*time.Millisecond, time.Millisecond, c.callback)
59
	testCacheEvicter(ttl)
60
	if atomic.LoadInt64(&c.callbacks) != 1 {
61
		t.Errorf("evictExpired() => failed to invoke EvictionCallback: got %d callbacks, wanted 1", c.callbacks)
62
	}
63
}
64

65
func TestTTLFinalizer(t *testing.T) {
66
	ttl := NewTTL(5*time.Second, 1*time.Millisecond).(*ttlWrapper)
67
	testCacheFinalizer(&ttl.evicterTerminated)
68
}
69

70
func BenchmarkTTLGet(b *testing.B) {
71
	c := NewTTL(5*time.Minute, 1*time.Minute)
72
	benchmarkCacheGet(c, b)
73
}
74

75
func BenchmarkTTLGetConcurrent(b *testing.B) {
76
	c := NewTTL(5*time.Minute, 1*time.Minute)
77
	benchmarkCacheGetConcurrent(c, b)
78
}
79

80
func BenchmarkTTLSet(b *testing.B) {
81
	c := NewTTL(5*time.Minute, 1*time.Minute)
82
	benchmarkCacheSet(c, b)
83
}
84

85
func BenchmarkTTLSetConcurrent(b *testing.B) {
86
	c := NewTTL(5*time.Minute, 1*time.Minute)
87
	benchmarkCacheSetConcurrent(c, b)
88
}
89

90
func BenchmarkTTLGetSetConcurrent(b *testing.B) {
91
	c := NewTTL(5*time.Minute, 1*time.Minute)
92
	benchmarkCacheGetSetConcurrent(c, b)
93
}
94

95
func BenchmarkTTLSetRemove(b *testing.B) {
96
	c := NewTTL(5*time.Minute, 1*time.Minute)
97
	benchmarkCacheSetRemove(c, b)
98
}
99

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

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

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

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