forked from TrueCloudLab/frostfs-node
Dmitrii Stepanov
04be9415d9
Use key locker to lock by key. Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
32 lines
578 B
Go
32 lines
578 B
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
"golang.org/x/sync/errgroup"
|
|
)
|
|
|
|
func TestKeyLocker(t *testing.T) {
|
|
taken := false
|
|
eg, _ := errgroup.WithContext(context.Background())
|
|
keyLocker := newKeyLocker[int]()
|
|
for i := 0; i < 100; i++ {
|
|
eg.Go(func() error {
|
|
keyLocker.LockKey(0)
|
|
defer keyLocker.UnlockKey(0)
|
|
|
|
require.False(t, taken)
|
|
taken = true
|
|
require.True(t, taken)
|
|
time.Sleep(10 * time.Millisecond)
|
|
taken = false
|
|
require.False(t, taken)
|
|
|
|
return nil
|
|
})
|
|
}
|
|
require.NoError(t, eg.Wait())
|
|
}
|