-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_memory_test.go
More file actions
113 lines (89 loc) · 2.05 KB
/
cache_memory_test.go
File metadata and controls
113 lines (89 loc) · 2.05 KB
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
// Copyright 2017-2026 Allow2 Pty Ltd. All rights reserved.
// Use of this source code is governed by the Allow2 API and SDK Licence.
package allow2service
import (
"testing"
"time"
)
func TestMemoryCache_SetAndGet(t *testing.T) {
c := NewMemoryCache()
c.Set("key1", "value1", 60)
val, ok := c.Get("key1")
if !ok {
t.Fatal("Expected cache hit")
}
if val != "value1" {
t.Errorf("Got %q, want %q", val, "value1")
}
}
func TestMemoryCache_GetMiss(t *testing.T) {
c := NewMemoryCache()
_, ok := c.Get("nonexistent")
if ok {
t.Fatal("Expected cache miss")
}
}
func TestMemoryCache_Delete(t *testing.T) {
c := NewMemoryCache()
c.Set("key1", "value1", 60)
c.Delete("key1")
_, ok := c.Get("key1")
if ok {
t.Fatal("Expected cache miss after delete")
}
}
func TestMemoryCache_TTLExpiry(t *testing.T) {
c := NewMemoryCache()
// Set with 1-second TTL
c.Set("key1", "value1", 1)
// Should be accessible immediately
val, ok := c.Get("key1")
if !ok {
t.Fatal("Expected cache hit immediately after set")
}
if val != "value1" {
t.Errorf("Got %q, want %q", val, "value1")
}
// Wait for expiry
time.Sleep(1100 * time.Millisecond)
_, ok = c.Get("key1")
if ok {
t.Fatal("Expected cache miss after TTL expiry")
}
}
func TestMemoryCache_Overwrite(t *testing.T) {
c := NewMemoryCache()
c.Set("key1", "value1", 60)
c.Set("key1", "value2", 60)
val, ok := c.Get("key1")
if !ok {
t.Fatal("Expected cache hit")
}
if val != "value2" {
t.Errorf("Got %q, want %q after overwrite", val, "value2")
}
}
func TestMemoryCache_MultipleKeys(t *testing.T) {
c := NewMemoryCache()
c.Set("a", "1", 60)
c.Set("b", "2", 60)
c.Set("c", "3", 60)
val, ok := c.Get("b")
if !ok || val != "2" {
t.Errorf("Expected 'b'='2', got ok=%v val=%q", ok, val)
}
c.Delete("b")
_, ok = c.Get("b")
if ok {
t.Error("Expected miss for deleted 'b'")
}
// Others should still be there
val, ok = c.Get("a")
if !ok || val != "1" {
t.Error("Expected 'a' still present")
}
val, ok = c.Get("c")
if !ok || val != "3" {
t.Error("Expected 'c' still present")
}
}