mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-11-15 05:33:47 +08:00
发布v2.15.1版本,更新内容请查看:https://github.com/bufanyun/hotgo/blob/v2.0/docs/guide-zh-CN/start-update-log.md
This commit is contained in:
@@ -168,6 +168,7 @@ func (l *Lock) startWatchDog() {
|
||||
}
|
||||
case <-l.watchDog:
|
||||
// 已经解锁
|
||||
l.wg.Done()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,9 +6,12 @@
|
||||
package lock_test
|
||||
|
||||
import (
|
||||
_ "github.com/gogf/gf/contrib/nosql/redis/v2"
|
||||
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"hotgo/internal/library/hgrds/lock"
|
||||
"runtime"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -24,21 +27,43 @@ func TestDefaultLock(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
t.Log("A加锁成功")
|
||||
time.Sleep(lock.DefaultTTL)
|
||||
err = l.Unlock(context.Background())
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
t.Log("A已释放锁")
|
||||
}()
|
||||
|
||||
time.Sleep(time.Second)
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
l := lock.Mutex("test")
|
||||
err := l.TryLock(context.Background())
|
||||
if err != nil && !gerror.Is(err, lock.ErrLockFailed) {
|
||||
t.Error(err)
|
||||
for {
|
||||
l := lock.Mutex("test")
|
||||
err := l.TryLock(context.Background())
|
||||
if err == nil {
|
||||
t.Log("B加锁成功")
|
||||
|
||||
// 等待1s,模拟业务
|
||||
time.Sleep(time.Second)
|
||||
|
||||
err = l.Unlock(context.Background())
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
t.Log("B已释放锁")
|
||||
break
|
||||
}
|
||||
|
||||
if gerror.Is(err, lock.ErrLockFailed) {
|
||||
t.Log("B加锁失败,等待1s重试...")
|
||||
time.Sleep(time.Second)
|
||||
} else {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
wg.Wait()
|
||||
@@ -116,3 +141,12 @@ func TestNewLock2(t *testing.T) {
|
||||
t.Errorf("count = %d", count)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Fix_watchDogMemoryLeak(t *testing.T) {
|
||||
i := 0
|
||||
for i < 5 {
|
||||
TestDefaultLock(t)
|
||||
t.Log("current goroutine num:", runtime.NumGoroutine())
|
||||
i++
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user