-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttlmap_test.go
183 lines (169 loc) · 3.97 KB
/
ttlmap_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//
// Copyright (c) Dmitri Toubelis
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ttlmap
import (
"context"
"crypto/rand"
"crypto/sha256"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func generateTestSet(l int) []string {
var s string
x := make(map[string]interface{}, l)
seed := make([]byte, 16)
if _, err := rand.Read(seed); err != nil {
panic(err.Error())
}
h := sha256.New()
h.Write(seed)
for i := 0; i < l; i++ {
for {
h.Write(h.Sum(nil))
s = fmt.Sprintf("%016x", h.Sum(nil))
if _, ok := x[s]; !ok {
break
}
}
x[s] = nil
}
ss := make([]string, l)
i := 0
for k := range x {
ss[i] = k
i++
}
return ss
}
func TestPut(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ttl := New(time.Second * 1)
keys := generateTestSet(10000)
count := 0
for _, k := range keys {
// first check
count++
n := count
ttl.Put(ctx, k, n)
v, ok := ttl.Get(k)
assert.True(t, ok)
assert.Equal(t, n, v)
assert.False(t, ttl.TestAndPut(ctx, k, nil))
}
for _, k := range keys {
// second check
count++
a := count
count++
b := count
// inseert data at quick succession in an attempt simulate a race condition
ttl.Put(ctx, k, a)
ttl.Put(ctx, k, b)
v, ok := ttl.Get(k)
assert.True(t, ok)
assert.Equal(t, b, v)
}
for _, k := range keys {
v, ok := ttl.Get(k)
assert.True(t, ok)
assert.NotEqual(t, 0, v)
assert.False(t, ttl.TestAndPut(ctx, k, nil))
}
time.Sleep(time.Second * 2)
for _, k := range keys {
v, ok := ttl.Get(k)
assert.Nil(t, v)
assert.False(t, ok)
assert.True(t, ttl.TestAndPut(ctx, k, nil))
}
}
func TestPutWithTTL(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ttl := New(time.Second * 1)
keys := generateTestSet(1000)
for i, k := range keys {
if i%2 == 0 {
ttl.Put(ctx, k, 0)
} else {
ttl.PutWithTTL(ctx, k, 1, time.Second*2)
}
}
assert.Equal(t, 1000, ttl.Len())
time.Sleep(time.Millisecond * 1200)
assert.Equal(t, 500, ttl.Len())
time.Sleep(time.Millisecond * 1200)
assert.Equal(t, 0, ttl.Len())
}
func TestContextCancel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
ttl := New(time.Second * 1)
keys := generateTestSet(1000)
for _, k := range keys {
ttl.Put(ctx, k, nil)
}
assert.Equal(t, 1000, ttl.Len())
cancel()
time.Sleep(time.Millisecond * 200)
assert.Equal(t, 0, ttl.Len())
}
func TestClear(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ttl := New(time.Second * 1)
keys := generateTestSet(1000)
for _, k := range keys {
ttl.Put(ctx, k, nil)
}
assert.Equal(t, 1000, ttl.Len())
ttl.Clear()
assert.Equal(t, 0, ttl.Len())
}
func BenchmarkGet(b *testing.B) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
keys := generateTestSet(b.N)
ttl := New(time.Second * 10)
for n := 0; n < b.N; n++ {
ttl.Put(ctx, keys[n], nil)
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
ttl.Get(keys[n])
}
}
func BenchmarkPut(b *testing.B) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
keys := generateTestSet(b.N)
ttl := New(time.Second * 10)
b.ResetTimer()
for n := 0; n < b.N; n++ {
ttl.Put(ctx, keys[n], nil)
}
}
func BenchmarkCheckAndPut(b *testing.B) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
keys := generateTestSet(b.N)
ttl := New(time.Second * 10)
b.ResetTimer()
for n := 0; n < b.N; n++ {
ttl.TestAndPut(ctx, keys[n], nil)
}
}