File tree 1 file changed +37
-0
lines changed
1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -2,7 +2,9 @@ package cache_test
2
2
3
3
import (
4
4
"math/rand"
5
+ "strconv"
5
6
"sync"
7
+ "sync/atomic"
6
8
"testing"
7
9
"time"
8
10
@@ -121,3 +123,38 @@ func TestCallJanitor(t *testing.T) {
121
123
t .Errorf ("want items is empty but got %d" , len (keys ))
122
124
}
123
125
}
126
+
127
+ func TestConcurrentDelete (t * testing.T ) {
128
+ c := cache .New [string , int ]()
129
+ exp := 5 * time .Second
130
+ for k := 1 ; k <= 10 ; k ++ {
131
+ c .Set (strconv .Itoa (k ), k , cache .WithExpiration (exp ))
132
+ }
133
+ var (
134
+ wg sync.WaitGroup
135
+ stop atomic.Bool
136
+ )
137
+
138
+ time .AfterFunc (10 * time .Second , func () {
139
+ stop .Store (true )
140
+ })
141
+
142
+ wg .Add (1 )
143
+ go func () {
144
+ defer wg .Done ()
145
+ for k := 1 ; ! stop .Load (); k ++ {
146
+ c .Set (strconv .Itoa (k ), k , cache .WithExpiration (0 ))
147
+ c .Delete (strconv .Itoa (k ))
148
+ }
149
+ }()
150
+
151
+ wg .Add (1 )
152
+ go func () {
153
+ defer wg .Done ()
154
+ for ! stop .Load () {
155
+ c .DeleteExpired ()
156
+ }
157
+ }()
158
+
159
+ wg .Wait ()
160
+ }
You can’t perform that action at this time.
0 commit comments