Always call unlock after successful lock

This commit is contained in:
Fedor Korotkiy 2023-03-27 23:30:25 +04:00
parent 0ee100730b
commit a85b1f6878

View file

@ -25,33 +25,36 @@ func TestKeyLock_Simple(t *testing.T) {
l := keylock.New() l := keylock.New()
canceled, unlock := l.LockKeys([]string{"a", "b"}, nil) canceled, unlock0 := l.LockKeys([]string{"a", "b"}, nil)
require.False(t, canceled) require.False(t, canceled)
canceled, _ = l.LockKeys([]string{"", "b", "c"}, timeout(time.Millisecond*10)) canceled, _ = l.LockKeys([]string{"", "b", "c"}, timeout(time.Millisecond*10))
require.True(t, canceled) require.True(t, canceled)
unlock() unlock0()
canceled, _ = l.LockKeys([]string{"", "b", "c"}, nil) canceled, unlock1 := l.LockKeys([]string{"", "b", "c"}, nil)
require.False(t, canceled) require.False(t, canceled)
unlock1()
} }
func TestKeyLock_Progress(t *testing.T) { func TestKeyLock_Progress(t *testing.T) {
defer goleak.VerifyNone(t) defer goleak.VerifyNone(t)
l := keylock.New() l := keylock.New()
canceled, unlock := l.LockKeys([]string{"a", "b"}, nil) canceled, unlock0 := l.LockKeys([]string{"a", "b"}, nil)
require.False(t, canceled) require.False(t, canceled)
defer unlock() defer unlock0()
go func() { go func() {
_, _ = l.LockKeys([]string{"b", "c"}, nil) _, unlock := l.LockKeys([]string{"b", "c"}, nil)
unlock()
}() }()
time.Sleep(time.Millisecond * 10) time.Sleep(time.Millisecond * 10)
canceled, _ = l.LockKeys([]string{"d"}, nil) canceled, unlock1 := l.LockKeys([]string{"d"}, nil)
require.False(t, canceled) require.False(t, canceled)
unlock1()
} }
func TestKeyLock_DeadlockFree(t *testing.T) { func TestKeyLock_DeadlockFree(t *testing.T) {