MCPcopy
hub / github.com/rclone/rclone / TestPoolMaxBufferMemory

Function TestPoolMaxBufferMemory

lib/pool/pool_test.go:259–316  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

257}
258
259func TestPoolMaxBufferMemory(t *testing.T) {
260 ctx := context.Background()
261 ci := fs.GetConfig(ctx)
262 ci.MaxBufferMemory = 4 * 4096
263 defer func() {
264 ci.MaxBufferMemory = 0
265 totalMemory = nil
266 }()
267 totalMemoryInit = sync.Once{} // reset the sync.Once as it likely has been used
268 totalMemory = nil
269 bp := New(60*time.Second, 4096, 2, true)
270 assert.NotNil(t, totalMemory)
271
272 assert.Equal(t, bp.alloced, 0)
273 buf := bp.Get()
274 bp.Put(buf)
275 assert.Equal(t, bp.alloced, 1)
276
277 var (
278 wg sync.WaitGroup
279 mu sync.Mutex
280 bufs int
281 maxBufs int
282 countBuf = func(i int) {
283 mu.Lock()
284 defer mu.Unlock()
285 bufs += i
286 if bufs > maxBufs {
287 maxBufs = bufs
288 }
289 }
290 )
291 const trials = 50
292 for i := range trials {
293 wg.Go(func() {
294 if i < trials/2 {
295 n := i%4 + 1
296 buf := bp.GetN(n)
297 countBuf(n)
298 time.Sleep(1 * time.Millisecond)
299 countBuf(-n)
300 bp.PutN(buf)
301 } else {
302 buf := bp.Get()
303 countBuf(1)
304 time.Sleep(1 * time.Millisecond)
305 countBuf(-1)
306 bp.Put(buf)
307 }
308 })
309 }
310
311 wg.Wait()
312
313 assert.Equal(t, bufs, 0)
314 assert.Equal(t, maxBufs, 4)
315 assert.Equal(t, bp.alloced, 2)
316}

Callers

nothing calls this directly

Calls 10

GetConfigFunction · 0.92
GetNMethod · 0.80
PutNMethod · 0.80
NewFunction · 0.70
GetMethod · 0.65
PutMethod · 0.65
LockMethod · 0.65
UnlockMethod · 0.65
WaitMethod · 0.65
EqualMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…