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())
}