-
Notifications
You must be signed in to change notification settings - Fork 11
/
cache_test.go
51 lines (41 loc) · 989 Bytes
/
cache_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
package main
import (
"fmt"
"testing"
"time"
)
func TestCache(t *testing.T) {
c := newCache(time.Millisecond * 50)
elem := "foo"
c.Add(elem)
if !c.Exists(elem) {
t.Errorf("Expected element not found in cache.")
}
// Wait until the element expired.
time.Sleep(time.Millisecond * 100)
if c.Exists(elem) {
t.Errorf("Element in cache despite being expired.")
}
// Now ask for a non-existing item.
if c.Exists("bar") {
t.Errorf("Non-existing element is supposed to exist.")
}
}
func TestCacheWithManyElems(t *testing.T) {
c := newCache(time.Millisecond * 50)
// Add 100 items.
for i := 0; i < 100; i++ {
c.Add(fmt.Sprintf("%d", i))
}
// Wait for those 100 items to expire.
time.Sleep(time.Millisecond * 100)
// Add another 100 items.
for i := 100; i < 200; i++ {
c.Add(fmt.Sprintf("%d", i))
}
// We now expect 100 items to remain in the cache.
count := c.Count()
if count != 100 {
t.Fatalf("Expected 100 but got %d elems in cache.", count)
}
}