-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmap_test.go
116 lines (105 loc) · 2.37 KB
/
cmap_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
package go_concurrent_map
import (
"context"
"sync"
"testing"
"time"
)
func Test_concurrentmap_Set(t *testing.T) {
c := New().
Build()
type args struct {
key string
value []byte
}
tests := []struct {
name string
args args
}{
{
name: "test1",
args: args{
key: "DeckardCain",
value: []byte("Hello my friend. Stay awhile, and listen.."),
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
_, ok := c.Get(tc.args.key)
if ok {
t.Fatalf("Key should not be present before being set")
}
c.Set(tc.args.key, tc.args.value)
val, ok := c.Get(tc.args.key)
if !ok {
t.Fatalf("Key not found after being set")
}
if string(val) != string(tc.args.value) {
t.Fatalf("Expected %s got %s", string(tc.args.value), string(val))
}
c.Delete(tc.args.key)
_, ok = c.Get(tc.args.key)
if ok {
t.Fatalf("Key should not be present after deletion")
}
})
}
}
func TestConcurrentmap_PurgeExpiredEntries(t *testing.T) {
c := New().
WithDefaultExpiration(30 * time.Second).
WithPurgeInterval(20 * time.Millisecond).
Build()
tests := []struct {
name string
key string
existAfterPurge bool
entry Entry
}{
{
name: "test1",
key: "DeckardCain",
existAfterPurge: false,
entry: Entry{
Expiration: time.Duration(1 * time.Millisecond),
Value: []byte("Hello my friend. Stay awhile, and listen.."),
setTime: time.Now(),
},
},
{
name: "test2",
key: "Butcher",
existAfterPurge: true,
entry: Entry{
Expiration: time.Duration(20 * time.Second),
Value: []byte("Rarrgh Rarghhh!"),
setTime: time.Now(),
},
},
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(1*time.Second))
c.PurgeExpiredEntries(ctx)
defer cancel()
defer wg.Done()
}()
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
c.SetEntry(tc.key, tc.entry)
if _, ok := c.GetEntry(tc.key); !ok {
t.Fatalf("Key %s not found after being set", tc.key)
}
})
}
wg.Wait()
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if _, ok := c.GetEntry(tc.key); ok != tc.existAfterPurge {
t.Errorf("After PurgeExpiredEntries have %t but want %t", ok, tc.existAfterPurge)
}
})
}
}