1
0
mirror of https://github.com/gogf/gf.git synced 2025-04-05 03:05:05 +08:00

improve ut case for package internal/rwmutex (#2364)

This commit is contained in:
John Guo 2022-12-20 15:56:29 +08:00 committed by GitHub
parent e254b4f3c0
commit e7b9e41a5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,7 +15,7 @@ import (
"github.com/gogf/gf/v2/test/gtest"
)
func TestRwmutexIsSafe(t *testing.T) {
func TestRWMutexIsSafe(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
lock := rwmutex.New()
t.Assert(lock.IsSafe(), false)
@ -37,7 +37,7 @@ func TestRwmutexIsSafe(t *testing.T) {
})
}
func TestSafeRwmutex(t *testing.T) {
func TestSafeRWMutex(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
safeLock := rwmutex.New(true)
array := garray.New(true)
@ -68,33 +68,34 @@ func TestSafeRwmutex(t *testing.T) {
})
}
func TestSafeReaderRwmutex(t *testing.T) {
func TestSafeReaderRWMutex(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
safeLock := rwmutex.New(true)
array := garray.New(true)
var (
localSafeLock = rwmutex.New(true)
array = garray.New(true)
)
go func() {
safeLock.RLock()
localSafeLock.RLock()
array.Append(1)
time.Sleep(100 * time.Millisecond)
array.Append(1)
safeLock.RUnlock()
localSafeLock.RUnlock()
}()
go func() {
time.Sleep(10 * time.Millisecond)
safeLock.RLock()
localSafeLock.RLock()
array.Append(1)
time.Sleep(200 * time.Millisecond)
array.Append(1)
time.Sleep(100 * time.Millisecond)
array.Append(1)
safeLock.RUnlock()
localSafeLock.RUnlock()
}()
go func() {
time.Sleep(50 * time.Millisecond)
safeLock.Lock()
localSafeLock.Lock()
array.Append(1)
safeLock.Unlock()
localSafeLock.Unlock()
}()
time.Sleep(50 * time.Millisecond)
t.Assert(array.Len(), 2)
@ -107,33 +108,34 @@ func TestSafeReaderRwmutex(t *testing.T) {
})
}
func TestUnsafeRwmutex(t *testing.T) {
func TestUnsafeRWMutex(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
unsafeLock := rwmutex.New()
array := garray.New(true)
var (
localUnsafeLock = rwmutex.New()
array = garray.New(true)
)
go func() {
unsafeLock.Lock()
localUnsafeLock.Lock()
array.Append(1)
time.Sleep(100 * time.Millisecond)
time.Sleep(2000 * time.Millisecond)
array.Append(1)
unsafeLock.Unlock()
localUnsafeLock.Unlock()
}()
go func() {
time.Sleep(10 * time.Millisecond)
unsafeLock.Lock()
time.Sleep(500 * time.Millisecond)
localUnsafeLock.Lock()
array.Append(1)
time.Sleep(200 * time.Millisecond)
time.Sleep(500 * time.Millisecond)
array.Append(1)
unsafeLock.Unlock()
localUnsafeLock.Unlock()
}()
time.Sleep(50 * time.Millisecond)
time.Sleep(800 * time.Millisecond)
t.Assert(array.Len(), 2)
time.Sleep(100 * time.Millisecond)
time.Sleep(800 * time.Millisecond)
t.Assert(array.Len(), 3)
time.Sleep(50 * time.Millisecond)
time.Sleep(200 * time.Millisecond)
t.Assert(array.Len(), 3)
time.Sleep(100 * time.Millisecond)
time.Sleep(500 * time.Millisecond)
t.Assert(array.Len(), 4)
})
}