-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplettl_test.go
152 lines (135 loc) · 2.9 KB
/
simplettl_test.go
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
package simplettl_test
import (
"fmt"
"strconv"
"sync"
"testing"
"time"
"github.com/Konstantin8105/SimpleTTL"
)
func TestNewAtHour(t *testing.T) {
cache := simplettl.NewCache(time.Hour)
if cache == nil {
t.Errorf("Cannot create cache")
}
}
func TestGet(t *testing.T) {
cache := simplettl.NewCache(2 * time.Second)
key := "foo"
value := "bar"
cache.Add(key, value, time.Second)
// checking before
{
r, ok := cache.Get(key)
if !ok {
t.Errorf("Cannot take \"ok\" from cache")
}
if r != value {
t.Errorf("Returned value is not correct")
}
}
time.Sleep(time.Second)
// checking after
{
r, ok := cache.Get(key)
if ok {
t.Errorf("We have \"ok\" from cache after deadline")
}
if r == value {
t.Errorf("We have correct value after deadline")
}
}
}
func TestCount(t *testing.T) {
cache := simplettl.NewCache(time.Second / 10) // It will be the second
key := "foo"
value := "bar"
cache.Add(key, value, time.Second/2)
// checking before
{
count := cache.Count()
if count != 1 {
t.Errorf("Cannot take correct counts of map elements before deadline. Count = %v", count)
}
}
time.Sleep(2 * time.Second)
// checking after
{
count := cache.Count()
if count != 0 {
t.Errorf("Cannot take correct counts of map elements after deadline. Count = %v", count)
}
}
}
func TestGetKeys(t *testing.T) {
cache := simplettl.NewCache(time.Second)
key := "foo"
value := "bar"
cache.Add(key, value, time.Second)
// checking before
{
keys := cache.GetKeys()
if len(keys) != 1 {
t.Errorf("Not correct length of of keys before deadline. Len = %v", len(keys))
}
if keys[0] != key {
t.Errorf("Returned value is not correct")
}
}
time.Sleep(time.Second * 2)
// checking after
{
keys := cache.GetKeys()
if len(keys) != 0 {
t.Errorf("Not correct length of of keys after deadline. Len = %v", len(keys))
}
}
}
func TestCondition(t *testing.T) {
cache := simplettl.NewCache(time.Second)
var wg sync.WaitGroup
wg.Add(1)
go func() {
cache.Add("Dream", 42, time.Hour)
wg.Done()
}()
cache.Add("Dream", -1, time.Second)
wg.Wait()
if cache.Count() != 1 {
t.Errorf("Checking race condition")
}
}
func ExampleNewCache() {
cache := simplettl.NewCache(2 * time.Second)
key := "foo"
value := "bar"
cache.Add(key, value, time.Second)
if r, ok := cache.Get(key); ok {
fmt.Printf("Value for key %v is %v", key, r)
}
// Output: Value for key foo is bar
}
func BenchmarkCondition(b *testing.B) {
cache := simplettl.NewCache(2 * time.Second)
amount := 100
mapSize := 100
for i := 0; i < b.N; i++ {
var wg sync.WaitGroup
wg.Add(2 * amount)
for j := 0; j < amount; j++ {
go func(pos int) {
defer wg.Done()
for i := 0; i < mapSize; i++ {
_, _ = cache.Get(strconv.Itoa(i))
}
}(j)
go func(pos int) {
defer wg.Done()
for i := 0; i < mapSize; i++ {
cache.Add(strconv.Itoa(i), "foo", time.Second)
}
}(j)
}
wg.Wait()
}
}