(t *testing.T)
| 257 | } |
| 258 | |
| 259 | func 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 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…