Skip to content

Commit

Permalink
redisext: fix always return false bug of LockWithTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
niubell committed Jul 6, 2020
1 parent 0e5e02d commit b1a9f62
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 2 additions & 0 deletions cache/redisext/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func (m *RedisExt) LockWithTimeout(ctx context.Context, key string, value interf
if err != nil || !r {
time.Sleep(retryTimeGap)
timeout = timeout - retryTimeGap
} else {
return r, err
}
}
return r, err
Expand Down
7 changes: 5 additions & 2 deletions cache/redisext/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ func TestLockWithTimeout(t *testing.T) {
value := "76c8c07d15c32fa90dd2b89f141246e8"
ctx := context.Background()
m := NewRedisExt("test/test", "test")
m.Lock(ctx, key, value, time.Second*5)
r, err := m.LockWithTimeout(ctx, key, value, time.Second, time.Second*1)
m.Del(ctx, key)
r, err := m.LockWithTimeout(ctx, key, value, time.Second*100, time.Second*1)
assert.Nil(t, err)
assert.Equal(t, true, r)
r, err = m.LockWithTimeout(ctx, key, value, time.Second, time.Second*3)
assert.Nil(t, err)
assert.Equal(t, false, r)
}
Expand Down

0 comments on commit b1a9f62

Please sign in to comment.