redisTs_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2021 INTECH Process Automation 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 ts
  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/ts/redis"
  19. "github.com/lf-edge/ekuiper/internal/pkg/ts/test/common"
  20. st "github.com/lf-edge/ekuiper/pkg/kv/stores"
  21. "strconv"
  22. "testing"
  23. )
  24. func TestRedisTsSet(t *testing.T) {
  25. ks, db, minRedis := setupRedisKv()
  26. defer cleanRedisKv(db, minRedis)
  27. common.TestTsSet(ks, t)
  28. }
  29. func TestRedisTsLast(t *testing.T) {
  30. ks, db, minRedis := setupRedisKv()
  31. defer cleanRedisKv(db, minRedis)
  32. common.TestTsLast(ks, t)
  33. }
  34. func TestRedisTsGet(t *testing.T) {
  35. ks, db, minRedis := setupRedisKv()
  36. defer cleanRedisKv(db, minRedis)
  37. common.TestTsGet(ks, t)
  38. }
  39. func TestRedisTsDelete(t *testing.T) {
  40. ks, db, minRedis := setupRedisKv()
  41. defer cleanRedisKv(db, minRedis)
  42. common.TestTsDelete(ks, t)
  43. }
  44. func TestRedisTsDeleteBefore(t *testing.T) {
  45. ks, db, minRedis := setupRedisKv()
  46. defer cleanRedisKv(db, minRedis)
  47. common.TestTsDeleteBefore(ks, t)
  48. }
  49. func setupRedisKv() (st.Tskv, *redis.Instance, *miniredis.Miniredis) {
  50. minRedis, err := miniredis.Run()
  51. if err != nil {
  52. panic(err)
  53. }
  54. redisDB := redis.NewRedis("localhost", stringToInt(minRedis.Port()))
  55. err = redisDB.Connect()
  56. if err != nil {
  57. panic(err)
  58. }
  59. builder := rb.NewTsBuilder(redisDB)
  60. var ks st.Tskv
  61. err, ks = builder.CreateTs("test")
  62. if err != nil {
  63. panic(err)
  64. }
  65. return ks, &redisDB, minRedis
  66. }
  67. func cleanRedisKv(instance *redis.Instance, minRedis *miniredis.Miniredis) {
  68. instance.Disconnect()
  69. minRedis.Close()
  70. }
  71. func stringToInt(svalue string) int {
  72. ivalue, err := strconv.Atoi(svalue)
  73. if err != nil {
  74. panic(err)
  75. }
  76. return ivalue
  77. }