Skip to content

Commit de2247e

Browse files
committed
Add a concurrent test for GetOrCreate and Resize
1 parent 226d272 commit de2247e

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

‎lazycache_test.go‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,45 @@ func TestGetOrCreateConcurrent(t *testing.T) {
212212
wg.Wait()
213213
}
214214

215+
func TestGetOrCreateAndResizeConcurrent(t *testing.T) {
216+
c := qt.New(t)
217+
218+
cache := New(Options[int, int]{MaxEntries: 1000})
219+
220+
var counter atomic.Uint32
221+
create := func(key int) (int, error) {
222+
time.Sleep(time.Duration(rand.Intn(40)+1) * time.Millisecond)
223+
counter.Add(1)
224+
return int(counter.Load()), nil
225+
}
226+
227+
var wg sync.WaitGroup
228+
229+
for i := 0; i < 100; i++ {
230+
i := i
231+
wg.Add(1)
232+
go func() {
233+
defer wg.Done()
234+
for j := 0; j < 12; j++ {
235+
v, _, err := cache.GetOrCreate(i, create)
236+
c.Assert(err, qt.IsNil)
237+
c.Assert(v, qt.Not(qt.Equals), 0)
238+
}
239+
}()
240+
}
241+
242+
wg.Add(1)
243+
go func() {
244+
defer wg.Done()
245+
for i := 100; i >= 0; i-- {
246+
cache.Resize(i)
247+
time.Sleep(10 * time.Millisecond)
248+
}
249+
}()
250+
251+
wg.Wait()
252+
}
253+
215254
func TestGetOrCreateRecursive(t *testing.T) {
216255
c := qt.New(t)
217256

0 commit comments

Comments
 (0)