2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-01-18 11:30:52 +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,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2019-01-18 11:30:52 +08:00
|
|
|
|
|
|
|
package gmlock_test
|
|
|
|
|
|
|
|
import (
|
2020-12-02 21:33:07 +08:00
|
|
|
"sync"
|
2019-06-21 22:23:07 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-07-29 21:01:19 +08:00
|
|
|
"github.com/gogf/gf/container/garray"
|
|
|
|
"github.com/gogf/gf/os/gmlock"
|
|
|
|
"github.com/gogf/gf/test/gtest"
|
2019-01-18 11:30:52 +08:00
|
|
|
)
|
|
|
|
|
2019-06-21 22:23:07 +08:00
|
|
|
func Test_Locker_Lock(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-21 22:23:07 +08:00
|
|
|
key := "testLock"
|
2019-07-23 23:20:27 +08:00
|
|
|
array := garray.New(true)
|
2019-06-19 09:06:52 +08:00
|
|
|
go func() {
|
|
|
|
gmlock.Lock(key)
|
|
|
|
array.Append(1)
|
2019-06-24 19:34:53 +08:00
|
|
|
time.Sleep(300 * time.Millisecond)
|
2019-06-19 09:06:52 +08:00
|
|
|
gmlock.Unlock(key)
|
|
|
|
}()
|
|
|
|
go func() {
|
2019-06-21 22:23:07 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2019-06-24 19:34:53 +08:00
|
|
|
gmlock.Lock(key)
|
2019-06-19 09:06:52 +08:00
|
|
|
array.Append(1)
|
|
|
|
gmlock.Unlock(key)
|
|
|
|
}()
|
2019-06-24 19:34:53 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(array.Len(), 1)
|
2019-06-24 19:34:53 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(array.Len(), 1)
|
2019-06-24 19:34:53 +08:00
|
|
|
time.Sleep(200 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(array.Len(), 2)
|
2019-06-24 09:27:31 +08:00
|
|
|
gmlock.Remove(key)
|
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-24 09:27:31 +08:00
|
|
|
key := "testLock"
|
2019-07-23 23:20:27 +08:00
|
|
|
array := garray.New(true)
|
2019-06-24 09:27:31 +08:00
|
|
|
lock := gmlock.New()
|
|
|
|
go func() {
|
|
|
|
lock.Lock(key)
|
|
|
|
array.Append(1)
|
2019-06-24 19:34:53 +08:00
|
|
|
time.Sleep(300 * time.Millisecond)
|
2019-06-24 09:27:31 +08:00
|
|
|
lock.Unlock(key)
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2019-06-24 19:34:53 +08:00
|
|
|
lock.Lock(key)
|
2019-06-24 09:27:31 +08:00
|
|
|
array.Append(1)
|
|
|
|
lock.Unlock(key)
|
|
|
|
}()
|
2019-06-24 19:34:53 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(array.Len(), 1)
|
2019-06-24 19:34:53 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(array.Len(), 1)
|
2019-06-24 19:34:53 +08:00
|
|
|
time.Sleep(200 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(array.Len(), 2)
|
2019-06-24 09:27:31 +08:00
|
|
|
lock.Clear()
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Locker_TryLock(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-24 09:27:31 +08:00
|
|
|
key := "testTryLock"
|
2019-07-23 23:20:27 +08:00
|
|
|
array := garray.New(true)
|
2019-06-24 09:27:31 +08:00
|
|
|
go func() {
|
|
|
|
gmlock.Lock(key)
|
|
|
|
array.Append(1)
|
2019-06-24 19:34:53 +08:00
|
|
|
time.Sleep(300 * time.Millisecond)
|
2019-06-24 09:27:31 +08:00
|
|
|
gmlock.Unlock(key)
|
|
|
|
}()
|
|
|
|
go func() {
|
2019-06-24 19:34:53 +08:00
|
|
|
time.Sleep(150 * time.Millisecond)
|
2019-06-24 09:27:31 +08:00
|
|
|
if gmlock.TryLock(key) {
|
|
|
|
array.Append(1)
|
|
|
|
gmlock.Unlock(key)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
go func() {
|
2019-06-24 19:34:53 +08:00
|
|
|
time.Sleep(400 * time.Millisecond)
|
2019-06-24 09:27:31 +08:00
|
|
|
if gmlock.TryLock(key) {
|
|
|
|
array.Append(1)
|
|
|
|
gmlock.Unlock(key)
|
|
|
|
}
|
|
|
|
}()
|
2019-06-24 19:34:53 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(array.Len(), 1)
|
2019-06-24 19:34:53 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(array.Len(), 1)
|
2019-06-24 09:27:31 +08:00
|
|
|
time.Sleep(300 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(array.Len(), 2)
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
2019-06-21 22:23:07 +08:00
|
|
|
|
2019-01-18 11:30:52 +08:00
|
|
|
}
|
|
|
|
|
2019-06-21 22:23:07 +08:00
|
|
|
func Test_Locker_LockFunc(t *testing.T) {
|
|
|
|
//no expire
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-21 22:23:07 +08:00
|
|
|
key := "testLockFunc"
|
2019-07-23 23:20:27 +08:00
|
|
|
array := garray.New(true)
|
2019-06-19 09:06:52 +08:00
|
|
|
go func() {
|
2019-06-21 22:23:07 +08:00
|
|
|
gmlock.LockFunc(key, func() {
|
|
|
|
array.Append(1)
|
2019-06-24 19:34:53 +08:00
|
|
|
time.Sleep(300 * time.Millisecond)
|
2019-06-21 22:23:07 +08:00
|
|
|
}) //
|
2019-06-19 09:06:52 +08:00
|
|
|
}()
|
|
|
|
go func() {
|
2019-06-24 19:34:53 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2019-06-21 22:23:07 +08:00
|
|
|
gmlock.LockFunc(key, func() {
|
|
|
|
array.Append(1)
|
|
|
|
})
|
2019-06-19 09:06:52 +08:00
|
|
|
}()
|
2019-06-24 19:34:53 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(array.Len(), 1)
|
2019-06-24 19:34:53 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(array.Len(), 1) //
|
2019-06-24 19:34:53 +08:00
|
|
|
time.Sleep(200 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(array.Len(), 2)
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
2019-01-18 11:30:52 +08:00
|
|
|
}
|
2019-06-21 22:23:07 +08:00
|
|
|
func Test_Locker_TryLockFunc(t *testing.T) {
|
|
|
|
//no expire
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-21 22:23:07 +08:00
|
|
|
key := "testTryLockFunc"
|
2019-07-23 23:20:27 +08:00
|
|
|
array := garray.New(true)
|
2019-06-19 09:06:52 +08:00
|
|
|
go func() {
|
2019-06-21 22:23:07 +08:00
|
|
|
gmlock.TryLockFunc(key, func() {
|
|
|
|
array.Append(1)
|
2019-06-24 19:34:53 +08:00
|
|
|
time.Sleep(200 * time.Millisecond)
|
2019-06-21 22:23:07 +08:00
|
|
|
})
|
2019-06-19 09:06:52 +08:00
|
|
|
}()
|
|
|
|
go func() {
|
2019-06-24 19:34:53 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2019-06-21 22:23:07 +08:00
|
|
|
gmlock.TryLockFunc(key, func() {
|
2019-06-19 09:06:52 +08:00
|
|
|
array.Append(1)
|
2019-06-21 22:23:07 +08:00
|
|
|
})
|
2019-06-19 09:06:52 +08:00
|
|
|
}()
|
|
|
|
go func() {
|
2019-06-24 19:34:53 +08:00
|
|
|
time.Sleep(300 * time.Millisecond)
|
2019-06-21 22:23:07 +08:00
|
|
|
gmlock.TryLockFunc(key, func() {
|
2019-06-19 09:06:52 +08:00
|
|
|
array.Append(1)
|
2019-06-21 22:23:07 +08:00
|
|
|
})
|
2019-06-19 09:06:52 +08:00
|
|
|
}()
|
2019-06-24 19:34:53 +08:00
|
|
|
time.Sleep(150 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(array.Len(), 1)
|
2019-06-24 19:34:53 +08:00
|
|
|
time.Sleep(400 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(array.Len(), 2)
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
2019-01-18 11:30:52 +08:00
|
|
|
}
|
2020-12-02 21:33:07 +08:00
|
|
|
|
|
|
|
func Test_Multiple_Goroutine(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
ch := make(chan struct{}, 0)
|
|
|
|
num := 1000
|
|
|
|
wait := sync.WaitGroup{}
|
|
|
|
wait.Add(num)
|
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
go func() {
|
|
|
|
defer wait.Done()
|
|
|
|
<-ch
|
|
|
|
gmlock.Lock("test")
|
|
|
|
defer gmlock.Unlock("test")
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
close(ch)
|
|
|
|
wait.Wait()
|
|
|
|
})
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
ch := make(chan struct{}, 0)
|
|
|
|
num := 100
|
|
|
|
wait := sync.WaitGroup{}
|
|
|
|
wait.Add(num * 2)
|
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
go func() {
|
|
|
|
defer wait.Done()
|
|
|
|
<-ch
|
|
|
|
gmlock.Lock("test")
|
|
|
|
defer gmlock.Unlock("test")
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
go func() {
|
|
|
|
defer wait.Done()
|
|
|
|
<-ch
|
|
|
|
gmlock.RLock("test")
|
|
|
|
defer gmlock.RUnlock("test")
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
close(ch)
|
|
|
|
wait.Wait()
|
|
|
|
})
|
|
|
|
}
|