2024-09-10 09:56:29 +00:00
|
|
|
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
|
2024-10-30 11:42:09 +00:00
|
|
|
for range 10_000 {
|
2024-09-10 09:56:29 +00:00
|
|
|
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())
|
|
|
|
}
|