1
0
mirror of https://github.com/gogf/gf.git synced 2025-04-05 11:18:50 +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" "github.com/gogf/gf/v2/test/gtest"
) )
func TestRwmutexIsSafe(t *testing.T) { func TestRWMutexIsSafe(t *testing.T) {
gtest.C(t, func(t *gtest.T) { gtest.C(t, func(t *gtest.T) {
lock := rwmutex.New() lock := rwmutex.New()
t.Assert(lock.IsSafe(), false) 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) { gtest.C(t, func(t *gtest.T) {
safeLock := rwmutex.New(true) safeLock := rwmutex.New(true)
array := garray.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) { gtest.C(t, func(t *gtest.T) {
safeLock := rwmutex.New(true) var (
array := garray.New(true) localSafeLock = rwmutex.New(true)
array = garray.New(true)
)
go func() { go func() {
safeLock.RLock() localSafeLock.RLock()
array.Append(1) array.Append(1)
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
array.Append(1) array.Append(1)
safeLock.RUnlock() localSafeLock.RUnlock()
}() }()
go func() { go func() {
time.Sleep(10 * time.Millisecond) time.Sleep(10 * time.Millisecond)
safeLock.RLock() localSafeLock.RLock()
array.Append(1) array.Append(1)
time.Sleep(200 * time.Millisecond) time.Sleep(200 * time.Millisecond)
array.Append(1) array.Append(1)
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
array.Append(1) array.Append(1)
safeLock.RUnlock() localSafeLock.RUnlock()
}() }()
go func() { go func() {
time.Sleep(50 * time.Millisecond) time.Sleep(50 * time.Millisecond)
safeLock.Lock() localSafeLock.Lock()
array.Append(1) array.Append(1)
safeLock.Unlock() localSafeLock.Unlock()
}() }()
time.Sleep(50 * time.Millisecond) time.Sleep(50 * time.Millisecond)
t.Assert(array.Len(), 2) 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) { gtest.C(t, func(t *gtest.T) {
unsafeLock := rwmutex.New() var (
array := garray.New(true) localUnsafeLock = rwmutex.New()
array = garray.New(true)
)
go func() { go func() {
unsafeLock.Lock() localUnsafeLock.Lock()
array.Append(1) array.Append(1)
time.Sleep(100 * time.Millisecond) time.Sleep(2000 * time.Millisecond)
array.Append(1) array.Append(1)
unsafeLock.Unlock() localUnsafeLock.Unlock()
}() }()
go func() { go func() {
time.Sleep(10 * time.Millisecond) time.Sleep(500 * time.Millisecond)
unsafeLock.Lock() localUnsafeLock.Lock()
array.Append(1) array.Append(1)
time.Sleep(200 * time.Millisecond) time.Sleep(500 * time.Millisecond)
array.Append(1) array.Append(1)
unsafeLock.Unlock() localUnsafeLock.Unlock()
}() }()
time.Sleep(50 * time.Millisecond) time.Sleep(800 * time.Millisecond)
t.Assert(array.Len(), 2) t.Assert(array.Len(), 2)
time.Sleep(100 * time.Millisecond) time.Sleep(800 * time.Millisecond)
t.Assert(array.Len(), 3) t.Assert(array.Len(), 3)
time.Sleep(50 * time.Millisecond) time.Sleep(200 * time.Millisecond)
t.Assert(array.Len(), 3) t.Assert(array.Len(), 3)
time.Sleep(100 * time.Millisecond) time.Sleep(500 * time.Millisecond)
t.Assert(array.Len(), 4) t.Assert(array.Len(), 4)
}) })
} }