mirror of
https://github.com/xiaoqidun/klock.git
synced 2026-09-03 08:30:22 +08:00
fix(修复错误): 修复锁超时失效问题
This commit is contained in:
2 files changed
+94
-28
No files matched your search
@@ -250,6 +250,30 @@ func TestLockWithTimeout(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestLockWithTimeoutDoesNotAcquireAfterDeadline 测试写锁在超时后被释放时不会再被获取。
|
||||
func TestLockWithTimeoutDoesNotAcquireAfterDeadline(t *testing.T) {
|
||||
kl := NewWithConfig(Config{
|
||||
MaxPollInterval: 50 * time.Millisecond,
|
||||
InitialPollInterval: 50 * time.Millisecond,
|
||||
})
|
||||
key := "test_key"
|
||||
released := make(chan struct{})
|
||||
kl.Lock(key)
|
||||
go func() {
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
kl.Unlock(key)
|
||||
close(released)
|
||||
}()
|
||||
acquired := kl.LockWithTimeout(key, 10*time.Millisecond)
|
||||
if acquired {
|
||||
kl.Unlock(key)
|
||||
}
|
||||
<-released
|
||||
if acquired {
|
||||
t.Fatal("LockWithTimeout 不应该在超时后获取锁")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRLockWithTimeout 测试带超时的读锁获取功能。
|
||||
func TestRLockWithTimeout(t *testing.T) {
|
||||
kl := New()
|
||||
@@ -270,6 +294,30 @@ func TestRLockWithTimeout(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestRLockWithTimeoutDoesNotAcquireAfterDeadline 测试读锁在超时后被释放时不会再被获取。
|
||||
func TestRLockWithTimeoutDoesNotAcquireAfterDeadline(t *testing.T) {
|
||||
kl := NewWithConfig(Config{
|
||||
MaxPollInterval: 50 * time.Millisecond,
|
||||
InitialPollInterval: 50 * time.Millisecond,
|
||||
})
|
||||
key := "test_key"
|
||||
released := make(chan struct{})
|
||||
kl.Lock(key)
|
||||
go func() {
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
kl.Unlock(key)
|
||||
close(released)
|
||||
}()
|
||||
acquired := kl.RLockWithTimeout(key, 10*time.Millisecond)
|
||||
if acquired {
|
||||
kl.RUnlock(key)
|
||||
}
|
||||
<-released
|
||||
if acquired {
|
||||
t.Fatal("RLockWithTimeout 不应该在超时后获取锁")
|
||||
}
|
||||
}
|
||||
|
||||
// TestPanicOnUnlockOfUnlockedKey 测试对未锁定的键执行 Unlock 操作是否会引发 panic。
|
||||
func TestPanicOnUnlockOfUnlockedKey(t *testing.T) {
|
||||
defer func() {
|
||||
|
||||
Reference in New Issue
Block a user