mirror of
https://gitee.com/johng/gf.git
synced 2024-12-01 03:38:35 +08:00
Improve gmlock unit testing.
This commit is contained in:
parent
322513f99b
commit
1154f9601b
@ -23,7 +23,7 @@ func Test_Locker_Lock(t *testing.T) {
|
||||
go func() {
|
||||
gmlock.Lock(key)
|
||||
array.Append(1)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
array.Append(1)
|
||||
gmlock.Unlock(key)
|
||||
}()
|
||||
@ -31,17 +31,17 @@ func Test_Locker_Lock(t *testing.T) {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
gmlock.Lock(key)
|
||||
array.Append(1)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
array.Append(1)
|
||||
gmlock.Unlock(key)
|
||||
}()
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(80 * time.Millisecond)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 3)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 3)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 4)
|
||||
})
|
||||
//expire
|
||||
@ -107,20 +107,20 @@ func Test_Locker_LockFunc(t *testing.T) {
|
||||
go func() {
|
||||
gmlock.LockFunc(key, func() {
|
||||
array.Append(1)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
}) //
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
gmlock.LockFunc(key, func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1) //
|
||||
time.Sleep(350 * time.Millisecond)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
})
|
||||
|
||||
@ -156,78 +156,78 @@ func Test_Locker_TryLockFunc(t *testing.T) {
|
||||
go func() {
|
||||
gmlock.TryLockFunc(key, func() {
|
||||
array.Append(1)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
gmlock.TryLockFunc(key, func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
time.Sleep(70 * time.Millisecond)
|
||||
gmlock.TryLockFunc(key, func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
})
|
||||
//expire1
|
||||
gtest.Case(t, func() {
|
||||
key := "testTryLockFuncExpire"
|
||||
key := "testTryLockFuncExpire1"
|
||||
array := garray.New()
|
||||
go func() {
|
||||
gmlock.TryLockFunc(key, func() {
|
||||
array.Append(1)
|
||||
}, 200*time.Millisecond)
|
||||
}, 50*time.Millisecond)
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
gmlock.TryLockFunc(key, func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
time.Sleep(70 * time.Millisecond)
|
||||
gmlock.TryLockFunc(key, func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 3)
|
||||
})
|
||||
|
||||
//expire2
|
||||
gtest.Case(t, func() {
|
||||
key := "testTryLockFuncExpire"
|
||||
key := "testTryLockFuncExpire2"
|
||||
array := garray.New()
|
||||
go func() {
|
||||
gmlock.TryLockFunc(key, func() {
|
||||
array.Append(1)
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
}, 200*time.Millisecond)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}, 50*time.Millisecond) //unlock after expire, before func finish.
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
gmlock.TryLockFunc(key, func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
time.Sleep(70 * time.Millisecond)
|
||||
gmlock.TryLockFunc(key, func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(70 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
})
|
||||
}
|
||||
|
@ -15,6 +15,61 @@ import (
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
)
|
||||
|
||||
func Test_Mutex_RUnlock(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
mu := gmlock.NewMutex()
|
||||
for index := 0; index < 1000; index++ {
|
||||
go func() {
|
||||
mu.RLockFunc(func() {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
})
|
||||
}()
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
gtest.Assert(mu.IsRLocked(), true)
|
||||
gtest.Assert(mu.IsLocked(), true)
|
||||
gtest.Assert(mu.IsWLocked(), false)
|
||||
for index := 0; index < 1000; index++ {
|
||||
go func() {
|
||||
mu.RUnlock()
|
||||
}()
|
||||
}
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
gtest.Assert(mu.IsRLocked(), false)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Mutex_IsLocked(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
mu := gmlock.NewMutex()
|
||||
go func() {
|
||||
mu.LockFunc(func() {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
})
|
||||
}()
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
gtest.Assert(mu.IsLocked(), true)
|
||||
gtest.Assert(mu.IsWLocked(), true)
|
||||
gtest.Assert(mu.IsRLocked(), false)
|
||||
time.Sleep(110 * time.Millisecond)
|
||||
gtest.Assert(mu.IsLocked(), false)
|
||||
gtest.Assert(mu.IsWLocked(), false)
|
||||
|
||||
go func() {
|
||||
mu.RLockFunc(func() {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
})
|
||||
}()
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
gtest.Assert(mu.IsRLocked(), true)
|
||||
gtest.Assert(mu.IsLocked(), true)
|
||||
gtest.Assert(mu.IsWLocked(), false)
|
||||
time.Sleep(110 * time.Millisecond)
|
||||
gtest.Assert(mu.IsRLocked(), false)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Mutex_Unlock(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
mu := gmlock.NewMutex()
|
||||
@ -172,28 +227,28 @@ func Test_Mutex_TryRLockFunc(t *testing.T) {
|
||||
go func() {
|
||||
mu.LockFunc(func() {
|
||||
array.Append(1)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
mu.TryRLockFunc(func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(110 * time.Millisecond)
|
||||
time.Sleep(70 * time.Millisecond)
|
||||
mu.TryRLockFunc(func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(110 * time.Millisecond)
|
||||
time.Sleep(70 * time.Millisecond)
|
||||
mu.TryRLockFunc(func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
|
@ -46,7 +46,7 @@ func Test_Locker_RLock(t *testing.T) {
|
||||
go func() {
|
||||
gmlock.Lock(key)
|
||||
array.Append(1)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gmlock.Unlock(key)
|
||||
}()
|
||||
go func() {
|
||||
@ -57,7 +57,7 @@ func Test_Locker_RLock(t *testing.T) {
|
||||
}()
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(120 * time.Millisecond)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
})
|
||||
|
||||
@ -68,21 +68,21 @@ func Test_Locker_RLock(t *testing.T) {
|
||||
go func() {
|
||||
gmlock.Lock(key)
|
||||
array.Append(1)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gmlock.Unlock(key)
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
gmlock.RLock(key)
|
||||
array.Append(1)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
time.Sleep(70 * time.Millisecond)
|
||||
gmlock.RUnlock(key)
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
gmlock.RLock(key)
|
||||
array.Append(1)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
time.Sleep(70 * time.Millisecond)
|
||||
gmlock.RUnlock(key)
|
||||
}()
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
@ -100,7 +100,7 @@ func Test_Locker_TryRLock(t *testing.T) {
|
||||
go func() {
|
||||
gmlock.Lock(key)
|
||||
array.Append(1)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gmlock.Unlock(key)
|
||||
}()
|
||||
go func() {
|
||||
@ -112,7 +112,7 @@ func Test_Locker_TryRLock(t *testing.T) {
|
||||
}()
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(120 * time.Millisecond)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
})
|
||||
|
||||
@ -123,7 +123,7 @@ func Test_Locker_TryRLock(t *testing.T) {
|
||||
go func() {
|
||||
gmlock.Lock(key)
|
||||
array.Append(1)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gmlock.Unlock(key)
|
||||
}()
|
||||
go func() {
|
||||
@ -134,7 +134,7 @@ func Test_Locker_TryRLock(t *testing.T) {
|
||||
}
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
time.Sleep(70 * time.Millisecond)
|
||||
if gmlock.TryRLock(key) {
|
||||
array.Append(1)
|
||||
gmlock.RUnlock(key)
|
||||
@ -142,7 +142,7 @@ func Test_Locker_TryRLock(t *testing.T) {
|
||||
}()
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
time.Sleep(80 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
})
|
||||
}
|
||||
@ -178,7 +178,7 @@ func Test_Locker_RLockFunc(t *testing.T) {
|
||||
go func() {
|
||||
gmlock.Lock(key)
|
||||
array.Append(1)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gmlock.Unlock(key)
|
||||
}()
|
||||
go func() {
|
||||
@ -189,7 +189,7 @@ func Test_Locker_RLockFunc(t *testing.T) {
|
||||
}()
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(120 * time.Millisecond)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
})
|
||||
|
||||
@ -200,26 +200,26 @@ func Test_Locker_RLockFunc(t *testing.T) {
|
||||
go func() {
|
||||
gmlock.Lock(key)
|
||||
array.Append(1)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gmlock.Unlock(key)
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
gmlock.RLockFunc(key, func() {
|
||||
array.Append(1)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
time.Sleep(70 * time.Millisecond)
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
gmlock.RLockFunc(key, func() {
|
||||
array.Append(1)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
time.Sleep(70 * time.Millisecond)
|
||||
})
|
||||
}()
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(120 * time.Millisecond)
|
||||
time.Sleep(70 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 3)
|
||||
})
|
||||
}
|
||||
@ -232,7 +232,7 @@ func Test_Locker_TryRLockFunc(t *testing.T) {
|
||||
go func() {
|
||||
gmlock.Lock(key)
|
||||
array.Append(1)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gmlock.Unlock(key)
|
||||
}()
|
||||
go func() {
|
||||
@ -243,7 +243,7 @@ func Test_Locker_TryRLockFunc(t *testing.T) {
|
||||
}()
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(120 * time.Millisecond)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
})
|
||||
|
||||
@ -254,7 +254,7 @@ func Test_Locker_TryRLockFunc(t *testing.T) {
|
||||
go func() {
|
||||
gmlock.Lock(key)
|
||||
array.Append(1)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gmlock.Unlock(key)
|
||||
}()
|
||||
go func() {
|
||||
@ -264,14 +264,14 @@ func Test_Locker_TryRLockFunc(t *testing.T) {
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
time.Sleep(70 * time.Millisecond)
|
||||
gmlock.TryRLockFunc(key, func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
time.Sleep(70 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user