gf/os/gmlock/gmlock_unit_lock_test.go

158 lines
3.4 KiB
Go
Raw Normal View History

// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// 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 gmlock_test
import (
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-06-21 22:23:07 +08:00
func Test_Locker_Lock(t *testing.T) {
2019-06-19 09:06:52 +08:00
gtest.Case(t, func() {
2019-06-21 22:23:07 +08:00
key := "testLock"
array := garray.New(true)
2019-06-19 09:06:52 +08:00
go func() {
gmlock.Lock(key)
array.Append(1)
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)
gmlock.Lock(key)
2019-06-19 09:06:52 +08:00
array.Append(1)
gmlock.Unlock(key)
}()
time.Sleep(100 * time.Millisecond)
gtest.Assert(array.Len(), 1)
time.Sleep(100 * time.Millisecond)
2019-06-19 09:06:52 +08:00
gtest.Assert(array.Len(), 1)
time.Sleep(200 * time.Millisecond)
gtest.Assert(array.Len(), 2)
2019-06-24 09:27:31 +08:00
gmlock.Remove(key)
})
gtest.Case(t, func() {
key := "testLock"
array := garray.New(true)
2019-06-24 09:27:31 +08:00
lock := gmlock.New()
go func() {
lock.Lock(key)
array.Append(1)
time.Sleep(300 * time.Millisecond)
2019-06-24 09:27:31 +08:00
lock.Unlock(key)
}()
go func() {
time.Sleep(100 * time.Millisecond)
lock.Lock(key)
2019-06-24 09:27:31 +08:00
array.Append(1)
lock.Unlock(key)
}()
time.Sleep(100 * time.Millisecond)
gtest.Assert(array.Len(), 1)
time.Sleep(100 * time.Millisecond)
2019-06-24 09:27:31 +08:00
gtest.Assert(array.Len(), 1)
time.Sleep(200 * time.Millisecond)
gtest.Assert(array.Len(), 2)
2019-06-24 09:27:31 +08:00
lock.Clear()
})
}
func Test_Locker_TryLock(t *testing.T) {
gtest.Case(t, func() {
key := "testTryLock"
array := garray.New(true)
2019-06-24 09:27:31 +08:00
go func() {
gmlock.Lock(key)
array.Append(1)
time.Sleep(300 * time.Millisecond)
2019-06-24 09:27:31 +08:00
gmlock.Unlock(key)
}()
go func() {
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() {
time.Sleep(400 * time.Millisecond)
2019-06-24 09:27:31 +08:00
if gmlock.TryLock(key) {
array.Append(1)
gmlock.Unlock(key)
}
}()
time.Sleep(100 * time.Millisecond)
2019-06-24 09:27:31 +08:00
gtest.Assert(array.Len(), 1)
time.Sleep(100 * time.Millisecond)
2019-06-24 09:27:31 +08:00
gtest.Assert(array.Len(), 1)
time.Sleep(300 * time.Millisecond)
gtest.Assert(array.Len(), 2)
2019-06-19 09:06:52 +08:00
})
2019-06-21 22:23:07 +08:00
}
2019-06-21 22:23:07 +08:00
func Test_Locker_LockFunc(t *testing.T) {
//no expire
2019-06-19 09:06:52 +08:00
gtest.Case(t, func() {
2019-06-21 22:23:07 +08:00
key := "testLockFunc"
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)
time.Sleep(300 * time.Millisecond)
2019-06-21 22:23:07 +08:00
}) //
2019-06-19 09:06:52 +08:00
}()
go func() {
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
}()
time.Sleep(100 * time.Millisecond)
2019-06-19 09:06:52 +08:00
gtest.Assert(array.Len(), 1)
time.Sleep(100 * time.Millisecond)
2019-06-21 22:23:07 +08:00
gtest.Assert(array.Len(), 1) //
time.Sleep(200 * time.Millisecond)
2019-06-19 09:06:52 +08:00
gtest.Assert(array.Len(), 2)
})
}
2019-06-21 22:23:07 +08:00
func Test_Locker_TryLockFunc(t *testing.T) {
//no expire
2019-06-19 09:06:52 +08:00
gtest.Case(t, func() {
2019-06-21 22:23:07 +08:00
key := "testTryLockFunc"
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)
time.Sleep(200 * time.Millisecond)
2019-06-21 22:23:07 +08:00
})
2019-06-19 09:06:52 +08:00
}()
go func() {
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() {
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
}()
time.Sleep(150 * time.Millisecond)
2019-06-19 09:06:52 +08:00
gtest.Assert(array.Len(), 1)
time.Sleep(400 * time.Millisecond)
2019-06-19 09:06:52 +08:00
gtest.Assert(array.Len(), 2)
})
}