default_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 context
  15. import (
  16. "github.com/lf-edge/ekuiper/internal/conf"
  17. "github.com/lf-edge/ekuiper/internal/pkg/store"
  18. "github.com/lf-edge/ekuiper/internal/topo/state"
  19. "github.com/lf-edge/ekuiper/pkg/api"
  20. "log"
  21. "os"
  22. "path"
  23. "reflect"
  24. "testing"
  25. )
  26. func TestState(t *testing.T) {
  27. err := store.SetupDefault()
  28. if err != nil {
  29. t.Error(err)
  30. }
  31. var (
  32. i = 0
  33. ruleId = "testStateRule"
  34. value1 = 21
  35. value2 = "hello"
  36. value3 = "world"
  37. s = map[string]interface{}{
  38. "key1": 21,
  39. "key3": "world",
  40. }
  41. )
  42. //initialization
  43. store, err := state.CreateStore(ruleId, api.AtLeastOnce)
  44. if err != nil {
  45. t.Errorf("Get store for rule %s error: %s", ruleId, err)
  46. return
  47. }
  48. ctx := Background().WithMeta("testStateRule", "op1", store).(*DefaultContext)
  49. defer cleanStateData()
  50. // Do state function
  51. _ = ctx.IncrCounter("key1", 20)
  52. _ = ctx.IncrCounter("key1", 1)
  53. v, err := ctx.GetCounter("key1")
  54. if err != nil {
  55. t.Errorf("%d.Get counter error: %s", i, err)
  56. return
  57. }
  58. if !reflect.DeepEqual(value1, v) {
  59. t.Errorf("%d.Get counter\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, value1, v)
  60. }
  61. err = ctx.PutState("key2", value2)
  62. if err != nil {
  63. t.Errorf("%d.Put state key2 error: %s", i, err)
  64. return
  65. }
  66. err = ctx.PutState("key3", value3)
  67. if err != nil {
  68. t.Errorf("%d.Put state key3 error: %s", i, err)
  69. return
  70. }
  71. v2, err := ctx.GetState("key2")
  72. if err != nil {
  73. t.Errorf("%d.Get state key2 error: %s", i, err)
  74. return
  75. }
  76. if !reflect.DeepEqual(value2, v2) {
  77. t.Errorf("%d.Get state\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, value2, v2)
  78. }
  79. err = ctx.DeleteState("key2")
  80. if err != nil {
  81. t.Errorf("%d.Delete state key2 error: %s", i, err)
  82. return
  83. }
  84. err = ctx.Snapshot()
  85. if err != nil {
  86. t.Errorf("%d.Snapshot error: %s", i, err)
  87. return
  88. }
  89. rs := ctx.snapshot
  90. if !reflect.DeepEqual(s, rs) {
  91. t.Errorf("%d.Snapshot\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, s, rs)
  92. }
  93. }
  94. func cleanStateData() {
  95. dbDir, err := conf.GetDataLoc()
  96. if err != nil {
  97. log.Panic(err)
  98. }
  99. c := path.Join(dbDir, state.CheckpointListKey)
  100. err = os.RemoveAll(c)
  101. if err != nil {
  102. conf.Log.Error(err)
  103. }
  104. }