redis_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package dao
  2. import (
  3. . "github.com/smartystreets/goconvey/convey"
  4. "go-common/app/service/live/wallet/model"
  5. "go-common/library/cache/redis"
  6. "testing"
  7. )
  8. func TestDao_Lock(t *testing.T) {
  9. Convey("Lock", t, func() {
  10. once.Do(startService)
  11. key := "test_lock_key" + randomString(5)
  12. ttl := 2000
  13. retryDelay := 10000
  14. err, gotLock, lockValue := d.Lock(ctx, key, ttl, 0, retryDelay)
  15. So(err, ShouldBeNil)
  16. So(gotLock, ShouldEqual, true)
  17. So(lockValue, ShouldNotBeNil)
  18. })
  19. Convey("Lock Twice", t, func() {
  20. once.Do(startService)
  21. key := "test_lock_key" + randomString(5)
  22. ttl := 2000
  23. retryDelay := 10000
  24. err, gotLock, lockValue := d.Lock(ctx, key, ttl, 0, retryDelay)
  25. So(err, ShouldBeNil)
  26. So(gotLock, ShouldEqual, true)
  27. So(lockValue, ShouldNotBeNil)
  28. err, gotLock, lockValue = d.Lock(ctx, key, ttl, 3, retryDelay)
  29. So(err, ShouldBeNil)
  30. So(gotLock, ShouldEqual, false)
  31. })
  32. }
  33. func TestDao_UnLock(t *testing.T) {
  34. Convey("UnLock", t, func() {
  35. once.Do(startService)
  36. key := "test_lock_key" + randomString(5)
  37. value := "test"
  38. err := d.UnLock(ctx, key, value)
  39. So(err, ShouldEqual, redis.ErrNil)
  40. })
  41. Convey("UnLock wrong value", t, func() {
  42. once.Do(startService)
  43. key := "test_lock_key" + randomString(5)
  44. ttl := 2000
  45. retryDelay := 10000
  46. err, gotLock, lockValue := d.Lock(ctx, key, ttl, 3, retryDelay)
  47. So(err, ShouldBeNil)
  48. So(gotLock, ShouldEqual, true)
  49. So(lockValue, ShouldNotBeNil)
  50. value := "test"
  51. err = d.UnLock(ctx, key, value)
  52. So(err, ShouldEqual, ErrUnLockGet)
  53. })
  54. Convey("LockAndUnLock", t, func() {
  55. once.Do(startService)
  56. key := "test_lock_key" + randomString(5)
  57. ttl := 2000
  58. retryDelay := 10000
  59. err, gotLock, lockValue := d.Lock(ctx, key, ttl, 0, retryDelay)
  60. So(err, ShouldBeNil)
  61. So(gotLock, ShouldEqual, true)
  62. So(lockValue, ShouldNotBeNil)
  63. err = d.UnLock(ctx, key, lockValue)
  64. So(err, ShouldBeNil)
  65. err, gotLock, lockValue = d.Lock(ctx, key, ttl, 0, retryDelay)
  66. So(err, ShouldBeNil)
  67. So(gotLock, ShouldEqual, true)
  68. So(lockValue, ShouldNotBeNil)
  69. })
  70. }
  71. func TestDao_ForceUnLock(t *testing.T) {
  72. Convey("ForceUnLock", t, func() {
  73. once.Do(startService)
  74. key := "test_lock_key" + randomString(5)
  75. err := d.ForceUnLock(ctx, key)
  76. So(err, ShouldBeNil)
  77. })
  78. Convey("LockAndForceUnLock", t, func() {
  79. once.Do(startService)
  80. key := "test_lock_key" + randomString(5)
  81. ttl := 2000
  82. retryDelay := 10000
  83. err, gotLock, lockValue := d.Lock(ctx, key, ttl, 0, retryDelay)
  84. So(err, ShouldBeNil)
  85. So(gotLock, ShouldEqual, true)
  86. So(lockValue, ShouldNotBeNil)
  87. err = d.ForceUnLock(ctx, key)
  88. So(err, ShouldBeNil)
  89. err, gotLock, lockValue = d.Lock(ctx, key, ttl, 0, retryDelay)
  90. So(err, ShouldBeNil)
  91. So(gotLock, ShouldEqual, true)
  92. So(lockValue, ShouldNotBeNil)
  93. })
  94. }
  95. func TestDao_LockTransactionId(t *testing.T) {
  96. Convey("LockTid Twice", t, func() {
  97. tid := GetTid(model.PAYTYPE, "test")
  98. err := d.LockTransactionId(ctx, tid)
  99. So(err, ShouldBeNil)
  100. err = d.LockTransactionId(ctx, tid)
  101. So(err, ShouldEqual, ErrLockFailed)
  102. })
  103. }
  104. func TestDao_LockUser(t *testing.T) {
  105. Convey("LockUser", t, func() {
  106. uid := r.Int63n(100000000)
  107. err, gotLock, localValue := d.LockUser(ctx, uid)
  108. So(err, ShouldBeNil)
  109. So(gotLock, ShouldEqual, true)
  110. err = d.UnLockUser(ctx, uid, localValue)
  111. So(err, ShouldBeNil)
  112. err, gotLock, localValue = d.LockUser(ctx, uid)
  113. So(err, ShouldBeNil)
  114. So(gotLock, ShouldEqual, true)
  115. })
  116. Convey("LockUser Twice", t, func() {
  117. uid := r.Int63n(100000000)
  118. err, gotLock, _ := d.LockUser(ctx, uid)
  119. So(err, ShouldBeNil)
  120. So(gotLock, ShouldEqual, true)
  121. err, gotLock, _ = d.LockUser(ctx, uid)
  122. So(err, ShouldBeNil)
  123. So(gotLock, ShouldEqual, false)
  124. })
  125. }