Skip to content

Commit a6de649

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

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

cache_test.go

Lines changed: 37 additions & 0 deletions
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+
var (
130+
wg sync.WaitGroup
131+
stop atomic.Bool
132+
timeout = 10 * time.Second
133+
)
134+
135+
if testing.Short() {
136+
timeout = 100 * time.Millisecond
137+
}
138+
time.AfterFunc(timeout, 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)