-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathttl_set_test.go
More file actions
86 lines (72 loc) · 1.77 KB
/
ttl_set_test.go
File metadata and controls
86 lines (72 loc) · 1.77 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
package statsig
import (
"fmt"
"testing"
"time"
)
func TestTTLSet_AddAndContains(t *testing.T) {
set := NewTTLSet()
set.Add("key1")
if !set.Contains("key1") {
t.Errorf("key1 should exist in the set")
}
if set.Contains("key2") {
t.Errorf("key2 should not exist in the set")
}
}
func TestTTLSet_Reset(t *testing.T) {
set := NewTTLSet()
set.Add("key1")
set.Add("key2")
set.mu.Lock()
set.store = make(map[string]struct{})
set.mu.Unlock()
if set.Contains("key1") {
t.Errorf("key1 should not exist after reset")
}
if set.Contains("key2") {
t.Errorf("key2 should not exist after reset")
}
}
func TestTTLSet_MaxSize(t *testing.T) {
set := NewTTLSetWithMaxSize(minTTLSetMaxSize)
for i := 0; i < minTTLSetMaxSize; i++ {
set.Add(fmt.Sprintf("key_%d", i))
}
set.Add("overflow")
if set.Contains("key_0") || set.Contains("key_1") {
t.Errorf("existing keys should be cleared when max size is reached")
}
if !set.Contains("overflow") {
t.Errorf("new key should still exist after clear-on-overflow insertion")
}
}
func TestTTLSet_MinSize(t *testing.T) {
set := NewTTLSetWithMaxSize(1)
if set.maxSize != minTTLSetMaxSize {
t.Errorf("max size should not be less than minimum")
}
}
func TestTTLSet_StartResetThread(t *testing.T) {
set := NewTTLSet()
set.resetInterval = 10 * time.Millisecond
set.StartResetThread()
set.Add("key1")
time.Sleep(20 * time.Millisecond)
if set.Contains("key1") {
t.Errorf("key1 should not exist after automatic reset")
}
set.Shutdown()
}
func TestTTLSet_Shutdown(t *testing.T) {
set := NewTTLSet()
set.resetInterval = 10 * time.Millisecond
set.StartResetThread()
set.Add("key1")
set.Shutdown()
time.Sleep(20 * time.Millisecond)
set.Add("key2")
if !set.Contains("key2") {
t.Errorf("shutdown should prevent automatic reset")
}
}