1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 08:32:40 +08:00
This commit is contained in:
2022-07-26 14:19:40 +08:00
parent 23865214c8
commit 792458a185
5 changed files with 144 additions and 14 deletions

View File

@ -3,10 +3,15 @@ package locker
import (
"sync"
"testing"
"time"
)
var sourcekey = "u-0001"
func TestTryLock(t *testing.T) {
}
func TestSourceLocker(t *testing.T) {
l := NewSourceLocker()
@ -27,6 +32,32 @@ func TestSourceLocker(t *testing.T) {
wg.Wait()
t.Log("n:", n)
t.Logf("%+v", l)
}
func TestSourceTryLock(t *testing.T) {
c := 5
n := 0
wg := new(sync.WaitGroup)
wg.Add(c)
l := NewSourceLocker()
for i := 0; i < c; i++ {
go func() {
defer wg.Done()
if l.TryLock(sourcekey) {
n++
time.Sleep(time.Second)
l.Unlock(sourcekey)
}
}()
}
wg.Wait()
t.Log("n:", n)
t.Logf("%+v", l)
}
func BenchmarkSourceLocker(b *testing.B) {