forked from TrueCloudLab/frostfs-node
Dmitrii Stepanov
e39378b1c3
To limit memory usage by background flush. Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
27 lines
522 B
Go
27 lines
522 B
Go
package writecache
|
|
|
|
import (
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
"golang.org/x/sync/errgroup"
|
|
)
|
|
|
|
func TestLimiter(t *testing.T) {
|
|
var maxSize uint64 = 10
|
|
var single uint64 = 3
|
|
l := newFlushLimiter(uint64(maxSize))
|
|
var currSize atomic.Int64
|
|
var eg errgroup.Group
|
|
for i := 0; i < 10_000; i++ {
|
|
eg.Go(func() error {
|
|
defer l.release(single)
|
|
defer currSize.Add(-1)
|
|
l.acquire(single)
|
|
require.True(t, currSize.Add(1) <= 3)
|
|
return nil
|
|
})
|
|
}
|
|
require.NoError(t, eg.Wait())
|
|
}
|