Welcome to mirror list, hosted at ThFree Co, Russian Federation.

batch_cache_test.go « catfile « git « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c797e396afb60afea82da11cc087d61ac5f42be6 (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package catfile

import (
	"fmt"
	"testing"
	"time"

	"github.com/stretchr/testify/require"
)

func TestCacheAdd(t *testing.T) {
	const maxLen = 3
	bc := newCache(time.Hour, maxLen, defaultEvictionInterval)

	key0 := testKey(0)
	value0 := testValue()
	bc.add(key0, value0)
	requireCacheValid(t, bc)

	key1 := testKey(1)
	bc.add(key1, testValue())
	requireCacheValid(t, bc)

	key2 := testKey(2)
	bc.add(key2, testValue())
	requireCacheValid(t, bc)

	// Because maxLen is 3, and key0 is oldest, we expect that adding key3
	// will kick out key0.
	key3 := testKey(3)
	bc.add(key3, testValue())
	requireCacheValid(t, bc)

	require.Equal(t, maxLen, bc.len(), "length should be maxLen")
	require.True(t, value0.isClosed(), "value0 should be closed")
	require.Equal(t, []key{key1, key2, key3}, keys(bc))
}

func TestCacheAddTwice(t *testing.T) {
	bc := newCache(time.Hour, 10, defaultEvictionInterval)

	key0 := testKey(0)
	value0 := testValue()
	bc.add(key0, value0)
	requireCacheValid(t, bc)

	key1 := testKey(1)
	bc.add(key1, testValue())
	requireCacheValid(t, bc)

	require.Equal(t, key0, bc.head().key, "key0 should be oldest key")

	value2 := testValue()
	bc.add(key0, value2)
	requireCacheValid(t, bc)

	require.Equal(t, key1, bc.head().key, "key1 should be oldest key")
	require.Equal(t, value2, bc.head().value)

	require.True(t, value0.isClosed(), "value0 should be closed")
}

func TestCacheCheckout(t *testing.T) {
	bc := newCache(time.Hour, 10, defaultEvictionInterval)

	key0 := testKey(0)
	value0 := testValue()
	bc.add(key0, value0)

	v, ok := bc.checkout(key{sessionID: "foo"})
	requireCacheValid(t, bc)
	require.Nil(t, v, "expect nil value when key not found")
	require.False(t, ok, "ok flag")

	v, ok = bc.checkout(key0)
	requireCacheValid(t, bc)

	require.Equal(t, value0, v)
	require.True(t, ok, "ok flag")

	require.False(t, v.isClosed(), "value should not be closed after checkout")

	v, ok = bc.checkout(key0)
	require.False(t, ok, "ok flag after second checkout")
	require.Nil(t, v, "value from second checkout")
}

func TestCacheEnforceTTL(t *testing.T) {
	ttl := time.Hour
	bc := newCache(ttl, 10, defaultEvictionInterval)

	sleep := func() { time.Sleep(2 * time.Millisecond) }

	key0 := testKey(0)
	value0 := testValue()
	bc.add(key0, value0)
	sleep()

	key1 := testKey(1)
	value1 := testValue()
	bc.add(key1, value1)
	sleep()

	cutoff := time.Now().Add(ttl)
	sleep()

	key2 := testKey(2)
	bc.add(key2, testValue())
	sleep()

	key3 := testKey(3)
	bc.add(key3, testValue())
	sleep()

	requireCacheValid(t, bc)

	// We expect this cutoff to cause eviction of key0 and key1 but no other keys.
	bc.enforceTTL(cutoff)

	requireCacheValid(t, bc)

	for i, v := range []*batch{value0, value1} {
		require.True(t, v.isClosed(), "value %d %v should be closed", i, v)
	}

	require.Equal(t, []key{key2, key3}, keys(bc), "remaining keys after EnforceTTL")

	bc.enforceTTL(cutoff)

	requireCacheValid(t, bc)
	require.Equal(t, []key{key2, key3}, keys(bc), "remaining keys after second EnforceTTL")
}

func TestAutoExpiry(t *testing.T) {
	ttl := 5 * time.Millisecond
	refresh := 1 * time.Millisecond
	bc := newCache(ttl, 10, refresh)

	key0 := testKey(0)
	value0 := testValue()
	bc.add(key0, value0)
	requireCacheValid(t, bc)

	require.Contains(t, keys(bc), key0, "key should still be in map")
	require.False(t, value0.isClosed(), "value should not have been closed")

	// Wait for the monitor goroutine to do its thing
	for i := 0; i < 100; i++ {
		if len(keys(bc)) == 0 {
			break
		}

		time.Sleep(refresh)
	}

	require.Empty(t, keys(bc), "key should no longer be in map")
	require.True(t, value0.isClosed(), "value should be closed after eviction")
}

func requireCacheValid(t *testing.T, bc *BatchCache) {
	bc.Lock()
	defer bc.Unlock()

	for _, ent := range bc.entries {
		v := ent.value
		require.False(t, v.isClosed(), "values in cache should not be closed: %v %v", ent, v)
	}
}

func testValue() *batch { return &batch{} }

func testKey(i int) key { return key{sessionID: fmt.Sprintf("key-%d", i)} }

func keys(bc *BatchCache) []key {
	bc.Lock()
	defer bc.Unlock()

	var result []key
	for _, ent := range bc.entries {
		result = append(result, ent.key)
	}

	return result
}