redislock_test.go 726 B

123456789101112131415161718192021222324252627282930313233
  1. package redis
  2. import (
  3. "testing"
  4. "git.i2edu.net/i2/go-zero/core/stringx"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestRedisLock(t *testing.T) {
  8. runOnRedis(t, func(client *Redis) {
  9. key := stringx.Rand()
  10. firstLock := NewRedisLock(client, key)
  11. firstLock.SetExpire(5)
  12. firstAcquire, err := firstLock.Acquire()
  13. assert.Nil(t, err)
  14. assert.True(t, firstAcquire)
  15. secondLock := NewRedisLock(client, key)
  16. secondLock.SetExpire(5)
  17. againAcquire, err := secondLock.Acquire()
  18. assert.Nil(t, err)
  19. assert.False(t, againAcquire)
  20. release, err := firstLock.Release()
  21. assert.Nil(t, err)
  22. assert.True(t, release)
  23. endAcquire, err := secondLock.Acquire()
  24. assert.Nil(t, err)
  25. assert.True(t, endAcquire)
  26. })
  27. }