mirror of
https://gitee.com/johng/gf
synced 2026-07-01 03:21:22 +08:00
Improve gmlock unit testing.
This commit is contained in:
@ -16,7 +16,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Test_Locker_Lock(t *testing.T) {
|
func Test_Locker_Lock(t *testing.T) {
|
||||||
//no expire
|
|
||||||
gtest.Case(t, func() {
|
gtest.Case(t, func() {
|
||||||
key := "testLock"
|
key := "testLock"
|
||||||
array := garray.New()
|
array := garray.New()
|
||||||
@ -43,6 +42,71 @@ func Test_Locker_Lock(t *testing.T) {
|
|||||||
gtest.Assert(array.Len(), 3)
|
gtest.Assert(array.Len(), 3)
|
||||||
time.Sleep(50 * time.Millisecond)
|
time.Sleep(50 * time.Millisecond)
|
||||||
gtest.Assert(array.Len(), 4)
|
gtest.Assert(array.Len(), 4)
|
||||||
|
gmlock.Remove(key)
|
||||||
|
})
|
||||||
|
|
||||||
|
gtest.Case(t, func() {
|
||||||
|
key := "testLock"
|
||||||
|
array := garray.New()
|
||||||
|
lock := gmlock.New()
|
||||||
|
go func() {
|
||||||
|
lock.Lock(key)
|
||||||
|
array.Append(1)
|
||||||
|
time.Sleep(50 * time.Millisecond)
|
||||||
|
array.Append(1)
|
||||||
|
lock.Unlock(key)
|
||||||
|
}()
|
||||||
|
go func() {
|
||||||
|
time.Sleep(10 * time.Millisecond)
|
||||||
|
lock.Lock(key)
|
||||||
|
array.Append(1)
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
array.Append(1)
|
||||||
|
lock.Unlock(key)
|
||||||
|
}()
|
||||||
|
time.Sleep(10 * time.Millisecond)
|
||||||
|
gtest.Assert(array.Len(), 1)
|
||||||
|
time.Sleep(50 * time.Millisecond)
|
||||||
|
gtest.Assert(array.Len(), 3)
|
||||||
|
time.Sleep(50 * time.Millisecond)
|
||||||
|
gtest.Assert(array.Len(), 3)
|
||||||
|
time.Sleep(50 * time.Millisecond)
|
||||||
|
gtest.Assert(array.Len(), 4)
|
||||||
|
lock.Clear()
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_Locker_TryLock(t *testing.T) {
|
||||||
|
gtest.Case(t, func() {
|
||||||
|
key := "testTryLock"
|
||||||
|
array := garray.New()
|
||||||
|
go func() {
|
||||||
|
gmlock.Lock(key)
|
||||||
|
array.Append(1)
|
||||||
|
time.Sleep(200 * time.Millisecond)
|
||||||
|
gmlock.Unlock(key)
|
||||||
|
}()
|
||||||
|
go func() {
|
||||||
|
time.Sleep(10 * time.Millisecond)
|
||||||
|
if gmlock.TryLock(key) {
|
||||||
|
array.Append(1)
|
||||||
|
gmlock.Unlock(key)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
go func() {
|
||||||
|
time.Sleep(300 * time.Millisecond)
|
||||||
|
if gmlock.TryLock(key) {
|
||||||
|
array.Append(1)
|
||||||
|
gmlock.Unlock(key)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
time.Sleep(50 * time.Millisecond)
|
||||||
|
gtest.Assert(array.Len(), 1)
|
||||||
|
time.Sleep(50 * time.Millisecond)
|
||||||
|
gtest.Assert(array.Len(), 1)
|
||||||
|
time.Sleep(300 * time.Millisecond)
|
||||||
|
gtest.Assert(array.Len(), 2)
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user