mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
gtimer, gmlock updates
This commit is contained in:
@ -16,20 +16,22 @@ import (
|
||||
|
||||
func TestLocker_Lock_Unlock(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
key := "test1"
|
||||
array := garray.New(0, 0)
|
||||
go func() {
|
||||
gmlock.Lock("test")
|
||||
gmlock.Lock(key)
|
||||
array.Append(1)
|
||||
time.Sleep(100*time.Millisecond)
|
||||
array.Append(1)
|
||||
gmlock.Unlock("test")
|
||||
gmlock.Unlock(key)
|
||||
}()
|
||||
go func() {
|
||||
gmlock.Lock("test")
|
||||
time.Sleep(10*time.Millisecond)
|
||||
gmlock.Lock(key)
|
||||
array.Append(1)
|
||||
time.Sleep(200*time.Millisecond)
|
||||
array.Append(1)
|
||||
gmlock.Unlock("test")
|
||||
gmlock.Unlock(key)
|
||||
}()
|
||||
time.Sleep(50*time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
@ -44,48 +46,50 @@ func TestLocker_Lock_Unlock(t *testing.T) {
|
||||
|
||||
func TestLocker_Lock_Expire(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
key := "test2"
|
||||
array := garray.New(0, 0)
|
||||
go func() {
|
||||
gmlock.Lock("test", 50*time.Millisecond)
|
||||
gmlock.Lock(key, 100*time.Millisecond)
|
||||
array.Append(1)
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(10*time.Millisecond)
|
||||
gmlock.Lock("test")
|
||||
gmlock.Lock(key)
|
||||
time.Sleep(100*time.Millisecond)
|
||||
array.Append(1)
|
||||
gmlock.Unlock("test")
|
||||
gmlock.Unlock(key)
|
||||
}()
|
||||
time.Sleep(80*time.Millisecond)
|
||||
time.Sleep(150*time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(80*time.Millisecond)
|
||||
time.Sleep(250*time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestLocker_TryLock_Expire(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
key := "test3"
|
||||
array := garray.New(0, 0)
|
||||
go func() {
|
||||
gmlock.Lock("test", 200*time.Millisecond)
|
||||
gmlock.Lock(key, 200*time.Millisecond)
|
||||
array.Append(1)
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(50*time.Millisecond)
|
||||
if !gmlock.TryLock("test") {
|
||||
time.Sleep(100*time.Millisecond)
|
||||
if !gmlock.TryLock(key) {
|
||||
array.Append(1)
|
||||
} else {
|
||||
gmlock.Unlock("test")
|
||||
gmlock.Unlock(key)
|
||||
}
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(300*time.Millisecond)
|
||||
if gmlock.TryLock("test") {
|
||||
if gmlock.TryLock(key) {
|
||||
array.Append(1)
|
||||
gmlock.Unlock("test")
|
||||
gmlock.Unlock(key)
|
||||
}
|
||||
}()
|
||||
time.Sleep(20*time.Millisecond)
|
||||
time.Sleep(50*time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(80*time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
|
||||
@ -16,19 +16,20 @@ import (
|
||||
|
||||
func TestLocker_RLock1(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
key := "test100"
|
||||
array := garray.New(0, 0)
|
||||
go func() {
|
||||
gmlock.RLock("test")
|
||||
gmlock.RLock(key)
|
||||
array.Append(1)
|
||||
time.Sleep(50*time.Millisecond)
|
||||
array.Append(1)
|
||||
gmlock.RUnlock("test")
|
||||
gmlock.RUnlock(key)
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(10*time.Millisecond)
|
||||
gmlock.Lock("test")
|
||||
gmlock.Lock(key)
|
||||
array.Append(1)
|
||||
gmlock.Unlock("test")
|
||||
gmlock.Unlock(key)
|
||||
}()
|
||||
time.Sleep(20*time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
@ -39,18 +40,19 @@ func TestLocker_RLock1(t *testing.T) {
|
||||
|
||||
func TestLocker_RLock2(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
key := "test200"
|
||||
array := garray.New(0, 0)
|
||||
go func() {
|
||||
gmlock.Lock("test")
|
||||
gmlock.Lock(key)
|
||||
array.Append(1)
|
||||
time.Sleep(100*time.Millisecond)
|
||||
gmlock.Unlock("test")
|
||||
gmlock.Unlock(key)
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(10*time.Millisecond)
|
||||
gmlock.RLock("test")
|
||||
gmlock.RLock(key)
|
||||
array.Append(1)
|
||||
gmlock.RUnlock("test")
|
||||
gmlock.RUnlock(key)
|
||||
}()
|
||||
|
||||
time.Sleep(20*time.Millisecond)
|
||||
|
||||
@ -80,27 +80,27 @@ func (t *Timer) newWheel(level int, slot int, interval time.Duration) *wheel {
|
||||
|
||||
// 添加循环任务
|
||||
func (t *Timer) Add(interval time.Duration, job JobFunc) *Entry {
|
||||
return t.doAddEntry(interval, job, false, gDEFAULT_TIMES, STATUS_READY, nil)
|
||||
return t.doAddEntry(interval, job, false, gDEFAULT_TIMES, STATUS_READY)
|
||||
}
|
||||
|
||||
// 添加定时任务
|
||||
func (t *Timer) AddEntry(interval time.Duration, job JobFunc, singleton bool, times int, status int) *Entry {
|
||||
return t.doAddEntry(interval, job, singleton, times, STATUS_READY, nil)
|
||||
return t.doAddEntry(interval, job, singleton, times, status)
|
||||
}
|
||||
|
||||
// 添加单例运行循环任务
|
||||
func (t *Timer) AddSingleton(interval time.Duration, job JobFunc) *Entry {
|
||||
return t.doAddEntry(interval, job, true, gDEFAULT_TIMES, STATUS_READY, nil)
|
||||
return t.doAddEntry(interval, job, true, gDEFAULT_TIMES, STATUS_READY)
|
||||
}
|
||||
|
||||
// 添加只运行一次的循环任务
|
||||
func (t *Timer) AddOnce(interval time.Duration, job JobFunc) *Entry {
|
||||
return t.doAddEntry(interval, job, true, 1, STATUS_READY, nil)
|
||||
return t.doAddEntry(interval, job, true, 1, STATUS_READY)
|
||||
}
|
||||
|
||||
// 添加运行指定次数的循环任务。
|
||||
func (t *Timer) AddTimes(interval time.Duration, times int, job JobFunc) *Entry {
|
||||
return t.doAddEntry(interval, job, true, times, STATUS_READY, nil)
|
||||
return t.doAddEntry(interval, job, true, times, STATUS_READY)
|
||||
}
|
||||
|
||||
// 延迟添加循环任务。
|
||||
@ -154,7 +154,7 @@ func (t *Timer) Close() {
|
||||
}
|
||||
|
||||
// 添加定时任务
|
||||
func (t *Timer) doAddEntry(interval time.Duration, job JobFunc, singleton bool, times int, status int, parent *Entry) *Entry {
|
||||
func (t *Timer) doAddEntry(interval time.Duration, job JobFunc, singleton bool, times int, status int) *Entry {
|
||||
return t.wheels[t.getLevelByIntervalMs(interval.Nanoseconds()/1e6)].addEntry(interval, job, singleton, times, status)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user