Skip to content

Commit 274db6d

Browse files
committed
add concurrent delete test
1 parent 2601bb0 commit 274db6d

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

cache_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package cache_test
22

33
import (
44
"math/rand"
5+
"strconv"
56
"sync"
7+
"sync/atomic"
68
"testing"
79
"time"
810

@@ -121,3 +123,38 @@ func TestCallJanitor(t *testing.T) {
121123
t.Errorf("want items is empty but got %d", len(keys))
122124
}
123125
}
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+
}

0 commit comments

Comments
 (0)