mirror of
https://gitee.com/johng/gf.git
synced 2024-12-03 04:37:49 +08:00
Improve gmlock, gmutex unit testing sleep time.
This commit is contained in:
parent
3806f9db07
commit
dcf7772589
@ -23,7 +23,7 @@ func Test_Locker_RLock(t *testing.T) {
|
||||
go func() {
|
||||
gmlock.RLock(key)
|
||||
array.Append(1)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
array.Append(1)
|
||||
gmlock.RUnlock(key)
|
||||
}()
|
||||
@ -35,7 +35,7 @@ func Test_Locker_RLock(t *testing.T) {
|
||||
}()
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(80 * time.Millisecond)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 3)
|
||||
})
|
||||
|
||||
@ -46,7 +46,7 @@ func Test_Locker_RLock(t *testing.T) {
|
||||
go func() {
|
||||
gmlock.Lock(key)
|
||||
array.Append(1)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
time.Sleep(100 * 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(50 * time.Millisecond)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
})
|
||||
|
||||
@ -68,26 +68,26 @@ func Test_Locker_RLock(t *testing.T) {
|
||||
go func() {
|
||||
gmlock.Lock(key)
|
||||
array.Append(1)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gmlock.Unlock(key)
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
gmlock.RLock(key)
|
||||
array.Append(1)
|
||||
time.Sleep(70 * time.Millisecond)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gmlock.RUnlock(key)
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
gmlock.RLock(key)
|
||||
array.Append(1)
|
||||
time.Sleep(70 * time.Millisecond)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gmlock.RUnlock(key)
|
||||
}()
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(120 * time.Millisecond)
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 3)
|
||||
})
|
||||
}
|
||||
@ -100,7 +100,7 @@ func Test_Locker_TryRLock(t *testing.T) {
|
||||
go func() {
|
||||
gmlock.Lock(key)
|
||||
array.Append(1)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
time.Sleep(100 * 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(50 * time.Millisecond)
|
||||
time.Sleep(200 * 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(50 * time.Millisecond)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gmlock.Unlock(key)
|
||||
}()
|
||||
go func() {
|
||||
@ -134,7 +134,7 @@ func Test_Locker_TryRLock(t *testing.T) {
|
||||
}
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(70 * time.Millisecond)
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
if gmlock.TryRLock(key) {
|
||||
array.Append(1)
|
||||
gmlock.RUnlock(key)
|
||||
@ -142,12 +142,12 @@ func Test_Locker_TryRLock(t *testing.T) {
|
||||
}()
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(80 * time.Millisecond)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Locker_RLockFunc1(t *testing.T) {
|
||||
func Test_Locker_RLockFunc(t *testing.T) {
|
||||
//RLockFunc before Lock
|
||||
gtest.Case(t, func() {
|
||||
key := "testRLockFuncBeforeLock"
|
||||
@ -155,7 +155,7 @@ func Test_Locker_RLockFunc1(t *testing.T) {
|
||||
go func() {
|
||||
gmlock.RLockFunc(key, func() {
|
||||
array.Append(1)
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
@ -165,9 +165,9 @@ func Test_Locker_RLockFunc1(t *testing.T) {
|
||||
array.Append(1)
|
||||
gmlock.Unlock(key)
|
||||
}()
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(800 * time.Millisecond)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 3)
|
||||
})
|
||||
|
||||
@ -178,7 +178,7 @@ func Test_Locker_RLockFunc1(t *testing.T) {
|
||||
go func() {
|
||||
gmlock.Lock(key)
|
||||
array.Append(1)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gmlock.Unlock(key)
|
||||
}()
|
||||
go func() {
|
||||
@ -189,13 +189,10 @@ func Test_Locker_RLockFunc1(t *testing.T) {
|
||||
}()
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func Test_Locker_RLockFunc2(t *testing.T) {
|
||||
//Lock before RLockFuncs
|
||||
gtest.Case(t, func() {
|
||||
key := "testLockBeforeRLockFuncs"
|
||||
@ -203,29 +200,26 @@ func Test_Locker_RLockFunc2(t *testing.T) {
|
||||
go func() {
|
||||
gmlock.Lock(key)
|
||||
array.Append(1)
|
||||
//glog.Println("add1")
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gmlock.Unlock(key)
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
gmlock.RLockFunc(key, func() {
|
||||
array.Append(1)
|
||||
//glog.Println("add2")
|
||||
time.Sleep(700 * time.Millisecond)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
gmlock.RLockFunc(key, func() {
|
||||
array.Append(1)
|
||||
//glog.Println("add3")
|
||||
time.Sleep(700 * time.Millisecond)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
})
|
||||
}()
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(700 * time.Millisecond)
|
||||
time.Sleep(400 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 3)
|
||||
})
|
||||
}
|
||||
@ -238,7 +232,7 @@ func Test_Locker_TryRLockFunc(t *testing.T) {
|
||||
go func() {
|
||||
gmlock.Lock(key)
|
||||
array.Append(1)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gmlock.Unlock(key)
|
||||
}()
|
||||
go func() {
|
||||
@ -249,7 +243,7 @@ func Test_Locker_TryRLockFunc(t *testing.T) {
|
||||
}()
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
})
|
||||
|
||||
@ -260,7 +254,7 @@ func Test_Locker_TryRLockFunc(t *testing.T) {
|
||||
go func() {
|
||||
gmlock.Lock(key)
|
||||
array.Append(1)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gmlock.Unlock(key)
|
||||
}()
|
||||
go func() {
|
||||
@ -270,14 +264,14 @@ func Test_Locker_TryRLockFunc(t *testing.T) {
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(70 * time.Millisecond)
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
gmlock.TryRLockFunc(key, func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(70 * time.Millisecond)
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
})
|
||||
}
|
||||
|
@ -95,34 +95,33 @@ func Test_Mutex_Unlock(t *testing.T) {
|
||||
go func() {
|
||||
mu.LockFunc(func() {
|
||||
array.Append(1)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
mu.LockFunc(func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
mu.LockFunc(func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
|
||||
go func() {
|
||||
time.Sleep(60 * time.Millisecond)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
mu.Unlock()
|
||||
mu.Unlock()
|
||||
mu.Unlock()
|
||||
mu.Unlock()
|
||||
}()
|
||||
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 3)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
time.Sleep(400 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 3)
|
||||
})
|
||||
}
|
||||
@ -134,20 +133,20 @@ func Test_Mutex_LockFunc(t *testing.T) {
|
||||
go func() {
|
||||
mu.LockFunc(func() {
|
||||
array.Append(1)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
mu.LockFunc(func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
})
|
||||
}
|
||||
@ -159,26 +158,26 @@ func Test_Mutex_TryLockFunc(t *testing.T) {
|
||||
go func() {
|
||||
mu.LockFunc(func() {
|
||||
array.Append(1)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
mu.TryLockFunc(func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(110 * time.Millisecond)
|
||||
time.Sleep(400 * time.Millisecond)
|
||||
mu.TryLockFunc(func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
})
|
||||
}
|
||||
@ -190,7 +189,7 @@ func Test_Mutex_RLockFunc(t *testing.T) {
|
||||
go func() {
|
||||
mu.LockFunc(func() {
|
||||
array.Append(1)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
@ -200,11 +199,11 @@ func Test_Mutex_RLockFunc(t *testing.T) {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
})
|
||||
}()
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
})
|
||||
|
||||
@ -233,7 +232,7 @@ func Test_Mutex_RLockFunc(t *testing.T) {
|
||||
})
|
||||
}()
|
||||
gtest.Assert(array.Len(), 0)
|
||||
time.Sleep(80 * time.Millisecond)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 3)
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user