portable_rule_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright 2021-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 test
  15. import (
  16. "context"
  17. "fmt"
  18. "github.com/lf-edge/ekuiper/internal/binder"
  19. "github.com/lf-edge/ekuiper/internal/binder/function"
  20. "github.com/lf-edge/ekuiper/internal/binder/io"
  21. "github.com/lf-edge/ekuiper/internal/io/memory/pubsub"
  22. "github.com/lf-edge/ekuiper/internal/plugin/portable"
  23. "github.com/lf-edge/ekuiper/internal/plugin/portable/runtime"
  24. "github.com/lf-edge/ekuiper/internal/processor"
  25. "github.com/lf-edge/ekuiper/internal/topo"
  26. "github.com/lf-edge/ekuiper/internal/topo/planner"
  27. "github.com/lf-edge/ekuiper/internal/topo/topotest"
  28. "github.com/lf-edge/ekuiper/pkg/api"
  29. "log"
  30. "reflect"
  31. "testing"
  32. "time"
  33. )
  34. func init() {
  35. m, err := portable.InitManager()
  36. if err != nil {
  37. panic(err)
  38. }
  39. entry := binder.FactoryEntry{Name: "portable plugin", Factory: m}
  40. err = function.Initialize([]binder.FactoryEntry{entry})
  41. if err != nil {
  42. panic(err)
  43. }
  44. err = io.Initialize([]binder.FactoryEntry{entry})
  45. if err != nil {
  46. panic(err)
  47. }
  48. }
  49. func TestSourceAndFunc(t *testing.T) {
  50. streamList := []string{"ext", "extpy"}
  51. topotest.HandleStream(false, streamList, t)
  52. var tests = []struct {
  53. Name string
  54. Rule string
  55. R [][]map[string]interface{}
  56. M map[string]interface{}
  57. }{
  58. {
  59. Name: `TestPortableRule1`,
  60. Rule: `{"sql":"SELECT count as ee FROM ext","actions":[{"memory":{"topic":"cache"}}]}`,
  61. R: [][]map[string]interface{}{
  62. {{
  63. "ee": int64(50),
  64. }},
  65. {{
  66. "ee": int64(50),
  67. }},
  68. {{
  69. "ee": int64(50),
  70. }},
  71. },
  72. M: map[string]interface{}{
  73. "sink_memory_0_0_records_out_total": int64(3),
  74. },
  75. }, {
  76. Name: `TestPythonRule`,
  77. Rule: `{"sql":"SELECT revert(name) as ee FROM extpy","actions":[{"memory":{"topic":"cache"}},{"print":{}}]}`,
  78. R: [][]map[string]interface{}{
  79. {{
  80. "ee": "nosjyp",
  81. }},
  82. {{
  83. "ee": "nosjyp",
  84. }},
  85. {{
  86. "ee": "nosjyp",
  87. }},
  88. },
  89. M: map[string]interface{}{
  90. "sink_memory_0_0_records_out_total": int64(3),
  91. },
  92. },
  93. }
  94. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  95. defer runtime.GetPluginInsManager().KillAll()
  96. for i, tt := range tests {
  97. topotest.HandleStream(true, streamList[i:i+1], t)
  98. rs, err := CreateRule(tt.Name, tt.Rule)
  99. if err != nil {
  100. t.Errorf("failed to create rule: %s.", err)
  101. continue
  102. }
  103. tp, err := planner.Plan(rs)
  104. if err != nil {
  105. t.Errorf("fail to init rule: %v", err)
  106. continue
  107. }
  108. var mm [][]map[string]interface{}
  109. ch := pubsub.CreateSub("cache", nil, fmt.Sprintf("cache%d", i+1), 10)
  110. ctx, cancel := context.WithTimeout(context.Background(), 10000*time.Second)
  111. go func(ctx context.Context) {
  112. select {
  113. case err := <-tp.Open():
  114. log.Println(err)
  115. tp.Cancel()
  116. case <-ctx.Done():
  117. log.Printf("ctx done %v\n", ctx.Err())
  118. tp.Cancel()
  119. }
  120. fmt.Println("all exit")
  121. }(ctx)
  122. go func(ctx context.Context) {
  123. for {
  124. select {
  125. case s := <-ch:
  126. log.Printf("get %v", s)
  127. mm = append(mm, []map[string]interface{}{s.Message()})
  128. case <-ctx.Done():
  129. log.Printf("ctx done %v\n", ctx.Err())
  130. return
  131. }
  132. }
  133. }(ctx)
  134. topotest.HandleStream(false, streamList[i:i+1], t)
  135. for {
  136. if ctx.Err() != nil {
  137. t.Errorf("Exiting with error %v", ctx.Err())
  138. break
  139. }
  140. time.Sleep(10 * time.Millisecond)
  141. if compareMetrics(tp, tt.M) {
  142. cancel()
  143. if !reflect.DeepEqual(tt.R, mm) {
  144. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.Rule, tt.R, mm)
  145. }
  146. break
  147. }
  148. }
  149. }
  150. }
  151. func compareMetrics(tp *topo.Topo, m map[string]interface{}) bool {
  152. keys, values := tp.GetMetrics()
  153. for k, v := range m {
  154. var (
  155. index int
  156. key string
  157. matched bool
  158. )
  159. for index, key = range keys {
  160. if k == key {
  161. va, ok := values[index].(int64)
  162. if !ok {
  163. continue
  164. }
  165. ve := v.(int64)
  166. if va < ve {
  167. return false
  168. }
  169. matched = true
  170. }
  171. }
  172. if !matched {
  173. return false
  174. }
  175. }
  176. return true
  177. }
  178. func CreateRule(name, sql string) (*api.Rule, error) {
  179. p := processor.NewRuleProcessor()
  180. p.ExecDrop(name)
  181. return p.ExecCreateWithValidation(name, sql)
  182. }