redisKv_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2021 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. package store
  15. import (
  16. "github.com/alicebob/miniredis/v2"
  17. "github.com/lf-edge/ekuiper/internal/pkg/db/redis"
  18. rb "github.com/lf-edge/ekuiper/internal/pkg/store/redis"
  19. "github.com/lf-edge/ekuiper/internal/pkg/store/test/common"
  20. "github.com/lf-edge/ekuiper/pkg/kv"
  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 TestRedisKvGet(t *testing.T) {
  35. ks, db, minRedis := setupRedisKv()
  36. defer cleanRedisKv(db, minRedis)
  37. common.TestKvGet(ks, t)
  38. }
  39. func TestRedisKvKeys(t *testing.T) {
  40. ks, db, minRedis := setupRedisKv()
  41. defer cleanRedisKv(db, minRedis)
  42. length := 10
  43. common.TestKvKeys(length, ks, t)
  44. }
  45. func setupRedisKv() (kv.KeyValue, *redis.Instance, *miniredis.Miniredis) {
  46. minRedis, err := miniredis.Run()
  47. if err != nil {
  48. panic(err)
  49. }
  50. redisDB := redis.NewRedis("localhost", stringToInt(minRedis.Port()))
  51. err = redisDB.Connect()
  52. if err != nil {
  53. panic(err)
  54. }
  55. builder := rb.NewStoreBuilder(redisDB)
  56. var ks kv.KeyValue
  57. ks, err = builder.CreateStore("test")
  58. if err != nil {
  59. panic(err)
  60. }
  61. return ks, &redisDB, minRedis
  62. }
  63. func cleanRedisKv(instance *redis.Instance, minRedis *miniredis.Miniredis) {
  64. instance.Disconnect()
  65. minRedis.Close()
  66. }
  67. func stringToInt(svalue string) int {
  68. ivalue, err := strconv.Atoi(svalue)
  69. if err != nil {
  70. panic(err)
  71. }
  72. return ivalue
  73. }