aboutsummaryrefslogtreecommitdiffstats
path: root/library/go/yandex/tvm/tvmtool/internal/cache/cache_test.go
blob: d9a1780108c4f109260206d0cb54f2425320412e (plain) (blame)
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
package cache_test

import (
	"sort"
	"testing"
	"time"

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

	"a.yandex-team.ru/library/go/yandex/tvm"
	"a.yandex-team.ru/library/go/yandex/tvm/tvmtool/internal/cache"
)

var (
	testDst      = "test_dst"
	testDstAlias = "test_dst_alias"
	testDstID    = tvm.ClientID(1)
	testValue    = "test_val"
)

func TestNewAtHour(t *testing.T) {
	c := cache.New(time.Hour, 11*time.Hour)
	assert.NotNil(t, c, "failed to create cache")
}

func TestCache_Load(t *testing.T) {

	c := cache.New(time.Second, time.Hour)
	c.Store(testDstID, testDst, &testValue)
	// checking before
	{
		r, hit := c.Load(testDstID)
		assert.Equal(t, cache.Hit, hit, "failed to get '%d' from cache before deadline", testDstID)
		assert.NotNil(t, r, "failed to get '%d' from cache before deadline", testDstID)
		assert.Equal(t, testValue, *r)

		r, hit = c.LoadByAlias(testDst)
		assert.Equal(t, cache.Hit, hit, "failed to get '%s' from cache before deadline", testDst)
		assert.NotNil(t, r, "failed to get %q from tickets before deadline", testDst)
		assert.Equal(t, testValue, *r)
	}
	{
		r, hit := c.Load(999833321)
		assert.Equal(t, cache.Miss, hit, "got tickets for '999833321', but that key must be never existed")
		assert.Nil(t, r, "got tickets for '999833321', but that key must be never existed")

		r, hit = c.LoadByAlias("kek")
		assert.Equal(t, cache.Miss, hit, "got tickets for 'kek', but that key must be never existed")
		assert.Nil(t, r, "got tickets for 'kek', but that key must be never existed")
	}

	time.Sleep(3 * time.Second)
	// checking after
	{
		r, hit := c.Load(testDstID)
		assert.Equal(t, cache.GonnaMissy, hit)
		assert.Equal(t, testValue, *r)

		r, hit = c.LoadByAlias(testDst)
		assert.Equal(t, cache.GonnaMissy, hit)
		assert.Equal(t, testValue, *r)
	}
}

func TestCache_Keys(t *testing.T) {
	c := cache.New(time.Second, time.Hour)
	c.Store(testDstID, testDst, &testValue)
	c.Store(testDstID, testDstAlias, &testValue)

	t.Run("aliases", func(t *testing.T) {
		aliases := c.Aliases()
		sort.Strings(aliases)
		require.Equal(t, 2, len(aliases), "not correct length of aliases")
		require.EqualValues(t, []string{testDst, testDstAlias}, aliases)
	})

	t.Run("client_ids", func(t *testing.T) {
		ids := c.ClientIDs()
		require.Equal(t, 1, len(ids), "not correct length of client ids")
		require.EqualValues(t, []tvm.ClientID{testDstID}, ids)
	})
}

func TestCache_ExpiredKeys(t *testing.T) {
	c := cache.New(time.Second, 10*time.Second)
	c.Store(testDstID, testDst, &testValue)
	c.Store(testDstID, testDstAlias, &testValue)

	time.Sleep(3 * time.Second)
	c.Gc()

	var (
		newDst   = "new_dst"
		newDstID = tvm.ClientID(2)
	)
	c.Store(newDstID, newDst, &testValue)

	t.Run("aliases", func(t *testing.T) {
		aliases := c.Aliases()
		require.Equal(t, 3, len(aliases), "not correct length of aliases")
		require.ElementsMatch(t, []string{testDst, testDstAlias, newDst}, aliases)
	})

	t.Run("client_ids", func(t *testing.T) {
		ids := c.ClientIDs()
		require.Equal(t, 2, len(ids), "not correct length of client ids")
		require.ElementsMatch(t, []tvm.ClientID{testDstID, newDstID}, ids)
	})

	time.Sleep(8 * time.Second)
	c.Gc()

	t.Run("aliases", func(t *testing.T) {
		aliases := c.Aliases()
		require.Equal(t, 1, len(aliases), "not correct length of aliases")
		require.ElementsMatch(t, []string{newDst}, aliases)
	})

	t.Run("client_ids", func(t *testing.T) {
		ids := c.ClientIDs()
		require.Equal(t, 1, len(ids), "not correct length of client ids")
		require.ElementsMatch(t, []tvm.ClientID{newDstID}, ids)
	})
}