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 mutex_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2019-06-14 10:20:15 +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/mutex"
|
|
|
|
"github.com/gogf/gf/v2/test/gtest"
|
2019-06-14 00:03:13 +08:00
|
|
|
)
|
|
|
|
|
2019-06-14 10:28:24 +08:00
|
|
|
func TestMutexIsSafe(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 := mutex.New()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(lock.IsSafe(), false)
|
2019-06-14 00:03:13 +08:00
|
|
|
|
2019-06-14 10:20:15 +08:00
|
|
|
lock = mutex.New(false)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(lock.IsSafe(), false)
|
2019-06-14 10:20:15 +08:00
|
|
|
|
|
|
|
lock = mutex.New(false, false)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(lock.IsSafe(), false)
|
2019-06-14 10:20:15 +08:00
|
|
|
|
|
|
|
lock = mutex.New(true, false)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(lock.IsSafe(), true)
|
2019-06-14 10:20:15 +08:00
|
|
|
|
|
|
|
lock = mutex.New(true, true)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(lock.IsSafe(), true)
|
2019-06-14 10:20:15 +08:00
|
|
|
|
|
|
|
lock = mutex.New(true)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(lock.IsSafe(), true)
|
2019-06-14 10:20:15 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSafeMutex(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-23 23:40:57 +08:00
|
|
|
safeLock := mutex.New(true)
|
2019-07-23 23:20:27 +08:00
|
|
|
array := garray.New(true)
|
2019-06-14 10:20:15 +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-14 10:20:15 +08:00
|
|
|
time.Sleep(80 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(array.Len(), 3)
|
2019-06-14 10:20:15 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(array.Len(), 3)
|
2019-06-14 10:20:15 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(array.Len(), 4)
|
2019-06-14 10:20:15 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnsafeMutex(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-23 23:40:57 +08:00
|
|
|
unsafeLock := mutex.New()
|
2019-07-23 23:20:27 +08:00
|
|
|
array := garray.New(true)
|
2019-06-14 00:03:13 +08:00
|
|
|
|
2019-06-14 10:20:15 +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-14 10:20:15 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(array.Len(), 3)
|
2019-06-14 10:20:15 +08:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(array.Len(), 3)
|
2019-06-14 10:20:15 +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
|
|
|
})
|
|
|
|
}
|