redis_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package dao
  2. import (
  3. "context"
  4. "crypto/rand"
  5. "encoding/hex"
  6. "strings"
  7. "testing"
  8. . "github.com/smartystreets/goconvey/convey"
  9. )
  10. var (
  11. testAddMidRedis int64 = 15555180
  12. testLowScore int8 = 7
  13. testBlockNo int64 = 100000
  14. testKey = "test_lock"
  15. testMidList = []int64{15555180}
  16. )
  17. func Test_PingRedis(t *testing.T) {
  18. Convey("ping redis", t, func() {
  19. So(d.PingRedis(context.TODO()), ShouldBeNil)
  20. })
  21. }
  22. func Test_AddBlockCache(t *testing.T) {
  23. Convey("AddBlockCache redis ", t, func() {
  24. c := context.TODO()
  25. ret, err := d.BlockMidCache(c, testBlockNo, 10)
  26. So(ret, ShouldBeEmpty)
  27. So(err, ShouldBeNil)
  28. err = d.AddBlockCache(c, testAddMidRedis, testLowScore, testBlockNo)
  29. So(err, ShouldBeNil)
  30. ret, err = d.BlockMidCache(c, testBlockNo, 10)
  31. So(ret, ShouldContain, testAddMidRedis)
  32. So(err, ShouldBeNil)
  33. err = d.DelBlockCache(c, testBlockNo, testAddMidRedis)
  34. So(err, ShouldBeNil)
  35. ret, err = d.BlockMidCache(c, testBlockNo, 10)
  36. So(ret, ShouldNotContain, testAddMidRedis)
  37. So(err, ShouldBeNil)
  38. })
  39. }
  40. func Test_SetNXLockCache(t *testing.T) {
  41. Convey("SetNXLockCache", t, func() {
  42. ret, err := d.SetNXLockCache(c, testKey, 2)
  43. So(err, ShouldBeNil)
  44. So(ret, ShouldBeTrue)
  45. err = d.DelLockCache(c, testKey)
  46. So(err, ShouldBeNil)
  47. ret, err = d.SetNXLockCache(c, testKey, 2)
  48. So(err, ShouldBeNil)
  49. So(ret, ShouldBeTrue)
  50. ret, err = d.SetNXLockCache(c, testKey, 2)
  51. So(err, ShouldBeNil)
  52. So(ret, ShouldBeFalse)
  53. })
  54. }
  55. func Test_SetBlockCache(t *testing.T) {
  56. Convey("ping SetBlockCache", t, func() {
  57. err := d.SetBlockCache(context.TODO(), testMidList)
  58. So(err, ShouldBeNil)
  59. Convey("ping SetBlockCache", func() {
  60. mid, err := d.SPOPBlockCache(context.TODO())
  61. So(err, ShouldBeNil)
  62. So(testMidList, ShouldContain, mid)
  63. Convey("ping SetBlockCache 2", func() {
  64. mid, err := d.SPOPBlockCache(context.TODO())
  65. So(err, ShouldBeNil)
  66. So(mid == 0, ShouldBeTrue)
  67. })
  68. })
  69. })
  70. }
  71. // go test -test.v -test.run TestPfaddCache
  72. func TestPfaddCache(t *testing.T) {
  73. Convey("PfaddCache", t, func() {
  74. idx := strings.Replace(randHex(), "-", "", -1)
  75. ok, err := d.PfaddCache(context.TODO(), idx)
  76. So(err, ShouldBeNil)
  77. So(ok, ShouldBeTrue)
  78. ok, err = d.PfaddCache(context.TODO(), idx)
  79. So(err, ShouldBeNil)
  80. So(ok, ShouldBeFalse)
  81. })
  82. }
  83. // go test -test.v -test.run TestBatchPfaddCache
  84. func TestBatchPfaddCache(t *testing.T) {
  85. Convey("PfaddCache", t, func() {
  86. for i := 0; i < 10; i++ {
  87. idx := strings.Replace(randHex(), "-", "", -1)
  88. _, err := d.PfaddCache(context.TODO(), idx)
  89. So(err, ShouldBeNil)
  90. }
  91. })
  92. }
  93. func randHex() string {
  94. bs := make([]byte, 16)
  95. rand.Read(bs)
  96. return hex.EncodeToString(bs)
  97. }