mirror of
https://github.com/hanzo-fi/stack.git
synced 2026-10-30 20:34:43 +00:00
Co-authored-by: Antoine Wacheux <[email protected]> Co-authored-by: Maxence Maireaux <[email protected]> Co-authored-by: Brieuc Caillot <[email protected]>
45 lines
801 B
Go
45 lines
801 B
Go
package command
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/formancehq/stack/libs/go-libs/logging"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestLock(t *testing.T) {
|
|
locker := NewDefaultLocker()
|
|
var accounts []string
|
|
for i := 0; i < 10; i++ {
|
|
accounts = append(accounts, fmt.Sprintf("accounts:%d", i))
|
|
}
|
|
|
|
r := rand.New(rand.NewSource(time.Now().Unix()))
|
|
ctx := logging.TestingContext()
|
|
|
|
const nbLoop = 1000
|
|
wg := sync.WaitGroup{}
|
|
wg.Add(nbLoop)
|
|
|
|
for i := 0; i < nbLoop; i++ {
|
|
go func() {
|
|
unlock, err := locker.Lock(ctx, Accounts{
|
|
Read: []string{accounts[r.Int31n(10)]},
|
|
Write: []string{accounts[r.Int31n(10)]},
|
|
})
|
|
require.NoError(t, err)
|
|
defer unlock(ctx)
|
|
|
|
<-time.After(10 * time.Millisecond)
|
|
wg.Add(-1)
|
|
}()
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|