funcs_global_state_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2023 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 function
  15. import (
  16. "testing"
  17. "github.com/stretchr/testify/assert"
  18. "github.com/lf-edge/ekuiper/internal/conf"
  19. kctx "github.com/lf-edge/ekuiper/internal/topo/context"
  20. "github.com/lf-edge/ekuiper/internal/topo/state"
  21. "github.com/lf-edge/ekuiper/pkg/api"
  22. )
  23. func TestHitFuncs(t *testing.T) {
  24. f1, ok := builtins["last_hit_count"]
  25. if !ok {
  26. t.Fatal("builtin last_hit_count not found")
  27. }
  28. f2, ok := builtins["last_hit_time"]
  29. if !ok {
  30. t.Fatal("builtin last_hit_time not found")
  31. }
  32. f3, ok := builtins["last_agg_hit_count"]
  33. if !ok {
  34. t.Fatal("builtin last_agg_hit_count not found")
  35. }
  36. f4, ok := builtins["last_agg_hit_time"]
  37. if !ok {
  38. t.Fatal("builtin last_agg_hit_time not found")
  39. }
  40. funcs := []builtinFunc{f1, f2, f3, f4}
  41. contextLogger := conf.Log.WithField("rule", "testExec")
  42. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  43. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  44. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 1)
  45. tests := []struct {
  46. name string
  47. args []any
  48. result []any
  49. }{
  50. {
  51. name: "first hit",
  52. args: []any{
  53. true,
  54. int64(10100),
  55. },
  56. result: []any{
  57. 0, 0, 0, 0,
  58. },
  59. },
  60. {
  61. name: "second hit",
  62. args: []any{
  63. true,
  64. int64(10200),
  65. },
  66. result: []any{
  67. 1, int64(10100), 1, int64(10100),
  68. },
  69. },
  70. {
  71. name: "third hit but not update",
  72. args: []any{
  73. false,
  74. int64(10300),
  75. },
  76. result: []any{
  77. 2, int64(10200), 2, int64(10200),
  78. },
  79. },
  80. {
  81. name: "fourth hit",
  82. args: []any{
  83. true,
  84. int64(10400),
  85. },
  86. result: []any{
  87. 2, int64(10200), 2, int64(10200),
  88. },
  89. },
  90. {
  91. name: "fifth hit, no update",
  92. args: []any{
  93. false,
  94. int64(10500),
  95. },
  96. result: []any{
  97. 3, int64(10400), 3, int64(10400),
  98. },
  99. },
  100. {
  101. name: "sixth hit",
  102. args: []any{
  103. true,
  104. int64(10600),
  105. },
  106. result: []any{
  107. 3, int64(10400), 3, int64(10400),
  108. },
  109. },
  110. }
  111. for _, tt := range tests {
  112. t.Run(tt.name, func(t *testing.T) {
  113. for i, re := range tt.result {
  114. result, _ := funcs[i].exec(fctx, tt.args)
  115. assert.Equal(t, re, result, "failed on %d", i)
  116. }
  117. })
  118. }
  119. }