Files
gf/internal/rwmutex/rwmutex_z_unit_test.go

140 lines
3.1 KiB
Go
Raw Permalink Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2019-06-14 00:03:13 +08:00
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package rwmutex_test
import (
"testing"
2019-06-15 22:19:37 +08:00
"time"
2019-07-29 21:01:19 +08:00
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/internal/rwmutex"
"github.com/gogf/gf/v2/test/gtest"
2019-06-14 00:03:13 +08:00
)
2019-06-15 22:19:37 +08:00
func TestRwmutexIsSafe(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-14 00:03:13 +08:00
lock := rwmutex.New()
2020-03-19 22:56:12 +08:00
t.Assert(lock.IsSafe(), false)
2019-06-14 00:03:13 +08:00
2019-06-15 22:19:37 +08:00
lock = rwmutex.New(false)
2020-03-19 22:56:12 +08:00
t.Assert(lock.IsSafe(), false)
2019-06-15 22:19:37 +08:00
lock = rwmutex.New(false, false)
2020-03-19 22:56:12 +08:00
t.Assert(lock.IsSafe(), false)
2019-06-15 22:19:37 +08:00
lock = rwmutex.New(true, false)
2020-03-19 22:56:12 +08:00
t.Assert(lock.IsSafe(), true)
2019-06-15 22:19:37 +08:00
lock = rwmutex.New(true, true)
2020-03-19 22:56:12 +08:00
t.Assert(lock.IsSafe(), true)
2019-06-15 22:19:37 +08:00
lock = rwmutex.New(true)
2020-03-19 22:56:12 +08:00
t.Assert(lock.IsSafe(), true)
2019-06-15 22:19:37 +08:00
})
}
func TestSafeRwmutex(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
safeLock := rwmutex.New(true)
array := garray.New(true)
2019-06-15 22:19:37 +08:00
go func() {
safeLock.Lock()
array.Append(1)
time.Sleep(100 * time.Millisecond)
array.Append(1)
safeLock.Unlock()
}()
go func() {
time.Sleep(10 * time.Millisecond)
safeLock.Lock()
array.Append(1)
time.Sleep(200 * time.Millisecond)
array.Append(1)
safeLock.Unlock()
}()
time.Sleep(50 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 1)
2019-06-15 22:19:37 +08:00
time.Sleep(80 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 3)
2019-06-15 22:19:37 +08:00
time.Sleep(100 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 3)
2019-06-15 22:19:37 +08:00
time.Sleep(100 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 4)
2019-06-15 22:19:37 +08:00
})
}
func TestSafeReaderRwmutex(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
safeLock := rwmutex.New(true)
array := garray.New(true)
2019-06-15 22:19:37 +08:00
go func() {
safeLock.RLock()
array.Append(1)
time.Sleep(100 * time.Millisecond)
array.Append(1)
safeLock.RUnlock()
}()
go func() {
time.Sleep(10 * time.Millisecond)
safeLock.RLock()
array.Append(1)
time.Sleep(200 * time.Millisecond)
array.Append(1)
time.Sleep(100 * time.Millisecond)
array.Append(1)
safeLock.RUnlock()
}()
go func() {
time.Sleep(50 * time.Millisecond)
safeLock.Lock()
array.Append(1)
safeLock.Unlock()
}()
time.Sleep(50 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 2)
2019-06-15 22:19:37 +08:00
time.Sleep(100 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 3)
2019-06-15 22:19:37 +08:00
time.Sleep(100 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 4)
2019-06-15 22:19:37 +08:00
time.Sleep(100 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 6)
2019-06-15 22:19:37 +08:00
})
}
func TestUnsafeRwmutex(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
unsafeLock := rwmutex.New()
array := garray.New(true)
2019-06-15 22:19:37 +08:00
go func() {
unsafeLock.Lock()
array.Append(1)
time.Sleep(100 * time.Millisecond)
array.Append(1)
unsafeLock.Unlock()
}()
go func() {
time.Sleep(10 * time.Millisecond)
unsafeLock.Lock()
array.Append(1)
time.Sleep(200 * time.Millisecond)
array.Append(1)
unsafeLock.Unlock()
}()
time.Sleep(50 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 2)
2019-06-15 22:19:37 +08:00
time.Sleep(100 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 3)
2019-06-15 22:19:37 +08:00
time.Sleep(50 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 3)
2019-06-15 22:19:37 +08:00
time.Sleep(100 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 4)
2019-06-14 00:03:13 +08:00
})
}