move implementation to internal/ directory (#828)

This commit is contained in:
Christian Schwarz
2024-10-18 19:21:17 +02:00
committed by GitHub
parent b9b9ad10cf
commit 908807bd59
360 changed files with 507 additions and 507 deletions
+52
View File
@@ -0,0 +1,52 @@
package semaphore
import (
"context"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zrepl/zrepl/internal/daemon/logging/trace"
)
func TestSemaphore(t *testing.T) {
const numGoroutines = 10
const concurrentSemaphore = 6
const sleepTime = 1 * time.Second
begin := time.Now()
sem := New(concurrentSemaphore)
var acquisitions struct {
beforeT, afterT uint32
}
rootCtx, endRoot := trace.WithTaskFromStack(context.Background())
defer endRoot()
_, add, waitEnd := trace.WithTaskGroup(rootCtx, "TestSemaphore")
for i := 0; i < numGoroutines; i++ {
// not capturing i so no need for local copy
add(func(ctx context.Context) {
res, err := sem.Acquire(ctx)
require.NoError(t, err)
defer res.Release()
if time.Since(begin) > sleepTime {
atomic.AddUint32(&acquisitions.afterT, 1)
} else {
atomic.AddUint32(&acquisitions.beforeT, 1)
}
time.Sleep(sleepTime)
})
}
waitEnd()
assert.True(t, acquisitions.beforeT == concurrentSemaphore)
assert.True(t, acquisitions.afterT == numGoroutines-concurrentSemaphore)
}