-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache-map-lru_test.go
71 lines (58 loc) · 1.35 KB
/
cache-map-lru_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
package gofnext
import (
"errors"
"testing"
"time"
)
func TestCacheLru_StoreAndLoad(t *testing.T) {
m := NewCacheLru(100).SetTTL(time.Second)
// Store a value
m.Store("key1", "value1", nil)
// Load the value
value, existed, err := m.Load("key1")
if !existed {
t.Errorf("Expected key1 to exist")
}
if err != nil {
t.Errorf("Expected no error, got: %v", err)
}
if value != "value1" {
t.Errorf("Expected value1, got: %v", value)
}
// Load a non-existent key
_, existed, _ = m.Load("key2")
if existed {
t.Fatal("Expected key2 to not exist")
}
// Store a value with an error
m.Store("key3", nil, errors.New("some error"))
m.SetErrTTL(-1)
// Load the value with an error
_, _, err = m.Load("key3")
if err == nil {
t.Fatal("Expected an error, got nil")
}
if err.Error() != "some error" {
t.Fatalf("Expected 'some error', got: %v", err)
}
}
func TestCacheLru_SetTTL(t *testing.T) {
m := NewCacheLru(100)
m.SetTTL(time.Second)
// Store a value
m.Store("key1", "value1", nil)
// Load the value before ttl
_, existed, _ := m.Load("key1")
if !existed {
t.Errorf("Expected key1 to exist")
}
// Set a shorter ttl
m.SetTTL(time.Millisecond)
// Wait for the ttl to expire
time.Sleep(time.Millisecond * 10)
// Load the value after ttl
_, existed, _ = m.Load("key1")
if existed {
t.Errorf("Expected key1 to not exist after ttl")
}
}