feat(redislock): support set context (#2208)
* feat(redislock): support set context * chore: fix testmaster
parent
b18479dd43
commit
4879d4dfcd
@ -1,33 +1,51 @@
|
|||||||
package redis
|
package redis
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/stringx"
|
"github.com/zeromicro/go-zero/core/stringx"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRedisLock(t *testing.T) {
|
func TestRedisLock(t *testing.T) {
|
||||||
runOnRedis(t, func(client *Redis) {
|
testFn := func(ctx context.Context) func(client *Redis) {
|
||||||
key := stringx.Rand()
|
return func(client *Redis) {
|
||||||
firstLock := NewRedisLock(client, key)
|
key := stringx.Rand()
|
||||||
firstLock.SetExpire(5)
|
firstLock := NewRedisLock(client, key)
|
||||||
firstAcquire, err := firstLock.Acquire()
|
if ctx != nil {
|
||||||
assert.Nil(t, err)
|
firstLock.WithContext(ctx)
|
||||||
assert.True(t, firstAcquire)
|
}
|
||||||
|
firstLock.SetExpire(5)
|
||||||
secondLock := NewRedisLock(client, key)
|
firstAcquire, err := firstLock.Acquire()
|
||||||
secondLock.SetExpire(5)
|
assert.Nil(t, err)
|
||||||
againAcquire, err := secondLock.Acquire()
|
assert.True(t, firstAcquire)
|
||||||
assert.Nil(t, err)
|
|
||||||
assert.False(t, againAcquire)
|
secondLock := NewRedisLock(client, key)
|
||||||
|
if ctx != nil {
|
||||||
release, err := firstLock.Release()
|
secondLock.WithContext(ctx)
|
||||||
assert.Nil(t, err)
|
}
|
||||||
assert.True(t, release)
|
secondLock.SetExpire(5)
|
||||||
|
againAcquire, err := secondLock.Acquire()
|
||||||
endAcquire, err := secondLock.Acquire()
|
assert.Nil(t, err)
|
||||||
assert.Nil(t, err)
|
assert.False(t, againAcquire)
|
||||||
assert.True(t, endAcquire)
|
|
||||||
|
release, err := firstLock.Release()
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.True(t, release)
|
||||||
|
|
||||||
|
endAcquire, err := secondLock.Acquire()
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.True(t, endAcquire)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run("normal", func(t *testing.T) {
|
||||||
|
runOnRedis(t, testFn(nil))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("withContext", func(t *testing.T) {
|
||||||
|
runOnRedis(t, testFn(context.Background()))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue