Improve gmlock unit testing.

This commit is contained in:
hailaz
2019-06-24 09:27:31 +08:00
parent 4b9db0c794
commit 1434800982

View File

@ -16,7 +16,6 @@ import (
)
func Test_Locker_Lock(t *testing.T) {
//no expire
gtest.Case(t, func() {
key := "testLock"
array := garray.New()
@ -43,6 +42,71 @@ func Test_Locker_Lock(t *testing.T) {
gtest.Assert(array.Len(), 3)
time.Sleep(50 * time.Millisecond)
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)
})
}