redisKv_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2021-2022 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //go:build redisdb || !core
  15. package redis
  16. import (
  17. "github.com/alicebob/miniredis/v2"
  18. "github.com/lf-edge/ekuiper/internal/pkg/store/test/common"
  19. "github.com/lf-edge/ekuiper/pkg/kv"
  20. "github.com/redis/go-redis/v9"
  21. "strconv"
  22. "testing"
  23. )
  24. func TestRedisKvSetnx(t *testing.T) {
  25. ks, db, minRedis := setupRedisKv()
  26. defer cleanRedisKv(db, minRedis)
  27. common.TestKvSetnx(ks, t)
  28. }
  29. func TestRedisKvSet(t *testing.T) {
  30. ks, db, minRedis := setupRedisKv()
  31. defer cleanRedisKv(db, minRedis)
  32. common.TestKvSet(ks, t)
  33. }
  34. func TestRedisKvSetGet(t *testing.T) {
  35. ks, db, minRedis := setupRedisKv()
  36. defer cleanRedisKv(db, minRedis)
  37. common.TestKvSetGet(ks, t)
  38. }
  39. func TestRedisKvGet(t *testing.T) {
  40. ks, db, minRedis := setupRedisKv()
  41. defer cleanRedisKv(db, minRedis)
  42. common.TestKvGet(ks, t)
  43. }
  44. func TestRedisKvKeys(t *testing.T) {
  45. ks, db, minRedis := setupRedisKv()
  46. defer cleanRedisKv(db, minRedis)
  47. length := 10
  48. common.TestKvKeys(length, ks, t)
  49. }
  50. func TestRedisKvAll(t *testing.T) {
  51. ks, db, minRedis := setupRedisKv()
  52. defer cleanRedisKv(db, minRedis)
  53. length := 10
  54. common.TestKvAll(length, ks, t)
  55. }
  56. func TestRedisKvGetKeyedState(t *testing.T) {
  57. ks, db, minRedis := setupRedisKv()
  58. defer cleanRedisKv(db, minRedis)
  59. common.TestKvGetKeyedState(ks, t)
  60. }
  61. func setupRedisKv() (kv.KeyValue, *redis.Client, *miniredis.Miniredis) {
  62. minRedis, err := miniredis.Run()
  63. if err != nil {
  64. panic(err)
  65. }
  66. redisDB := redis.NewClient(&redis.Options{
  67. Addr: minRedis.Addr(),
  68. })
  69. builder := NewStoreBuilder(redisDB)
  70. var ks kv.KeyValue
  71. ks, err = builder.CreateStore("test")
  72. if err != nil {
  73. panic(err)
  74. }
  75. return ks, redisDB, minRedis
  76. }
  77. func cleanRedisKv(instance *redis.Client, minRedis *miniredis.Miniredis) {
  78. instance.Close()
  79. minRedis.Close()
  80. }
  81. func stringToInt(svalue string) int {
  82. ivalue, err := strconv.Atoi(svalue)
  83. if err != nil {
  84. panic(err)
  85. }
  86. return ivalue
  87. }