From 03facd6d903085de1572059237fc674bb101dc46 Mon Sep 17 00:00:00 2001 From: Damien Tournoud Date: Fri, 28 Jul 2017 14:28:39 +0200 Subject: [PATCH] Fix bogus duration handling in IOContext.Lock* Signed-off-by: Damien Tournoud --- rados/ioctx.go | 4 ++-- rados/rados_test.go | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/rados/ioctx.go b/rados/ioctx.go index 34a1edb..336aeb3 100644 --- a/rados/ioctx.go +++ b/rados/ioctx.go @@ -649,7 +649,7 @@ func (ioctx *IOContext) LockExclusive(oid, name, cookie, desc string, duration t var c_duration C.struct_timeval if duration != 0 { - tv := syscall.NsecToTimeval(time.Now().Add(duration).UnixNano()) + tv := syscall.NsecToTimeval(duration.Nanoseconds()) c_duration = C.struct_timeval{tv_sec: C.__time_t(tv.Sec), tv_usec: C.__suseconds_t(tv.Usec)} } @@ -698,7 +698,7 @@ func (ioctx *IOContext) LockShared(oid, name, cookie, tag, desc string, duration var c_duration C.struct_timeval if duration != 0 { - tv := syscall.NsecToTimeval(time.Now().Add(duration).UnixNano()) + tv := syscall.NsecToTimeval(duration.Nanoseconds()) c_duration = C.struct_timeval{tv_sec: C.__time_t(tv.Sec), tv_usec: C.__suseconds_t(tv.Usec)} } diff --git a/rados/rados_test.go b/rados/rados_test.go index f8ffa71..dcd636e 100644 --- a/rados/rados_test.go +++ b/rados/rados_test.go @@ -976,6 +976,28 @@ func TestLocking(t *testing.T) { assert.NoError(t, err) assert.Equal(t, 0, len(info.Clients)) + // lock sh with duration + res, err = pool.LockShared("obj", "myLock", "myCookie", "", "a description", time.Millisecond, nil) + assert.NoError(t, err) + assert.Equal(t, 0, res) + + // verify lock sh expired + time.Sleep(time.Second) + info, err = pool.ListLockers("obj", "myLock") + assert.NoError(t, err) + assert.Equal(t, 0, len(info.Clients)) + + // lock sh with duration + res, err = pool.LockExclusive("obj", "myLock", "myCookie", "a description", time.Millisecond, nil) + assert.NoError(t, err) + assert.Equal(t, 0, res) + + // verify lock sh expired + time.Sleep(time.Second) + info, err = pool.ListLockers("obj", "myLock") + assert.NoError(t, err) + assert.Equal(t, 0, len(info.Clients)) + pool.Destroy() conn.Shutdown() }