portable_rule_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 test
  15. import (
  16. "bufio"
  17. "context"
  18. "encoding/json"
  19. "fmt"
  20. "github.com/lf-edge/ekuiper/internal/binder"
  21. "github.com/lf-edge/ekuiper/internal/binder/function"
  22. "github.com/lf-edge/ekuiper/internal/binder/io"
  23. "github.com/lf-edge/ekuiper/internal/plugin/portable"
  24. "github.com/lf-edge/ekuiper/internal/plugin/portable/runtime"
  25. "github.com/lf-edge/ekuiper/internal/processor"
  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. "os"
  31. "reflect"
  32. "testing"
  33. "time"
  34. )
  35. func init() {
  36. m, err := portable.InitManager()
  37. if err != nil {
  38. panic(err)
  39. }
  40. entry := binder.FactoryEntry{Name: "portable plugin", Factory: m}
  41. err = function.Initialize([]binder.FactoryEntry{entry})
  42. if err != nil {
  43. panic(err)
  44. }
  45. err = io.Initialize([]binder.FactoryEntry{entry})
  46. if err != nil {
  47. panic(err)
  48. }
  49. }
  50. const CACHE_FILE = "cache2"
  51. func TestSourceAndFunc(t *testing.T) {
  52. streamList := []string{"ext"}
  53. topotest.HandleStream(false, streamList, t)
  54. var tests = []struct {
  55. Name string
  56. Rule string
  57. R [][]map[string]interface{}
  58. M map[string]interface{}
  59. }{
  60. {
  61. Name: `TestPortableRule1`,
  62. Rule: `{"sql":"SELECT echo(count) as ee FROM ext","actions":[{"file":{"path":"` + CACHE_FILE + `"}}]}`,
  63. R: [][]map[string]interface{}{
  64. {{
  65. "ee": float64(50),
  66. }},
  67. {{
  68. "ee": float64(50),
  69. }},
  70. {{
  71. "ee": float64(50),
  72. }},
  73. },
  74. M: map[string]interface{}{
  75. "source_ext_0_exceptions_total": int64(0),
  76. "source_ext_0_records_in_total": int64(3),
  77. "source_ext_0_records_out_total": int64(3),
  78. "sink_file_0_0_records_out_total": int64(3),
  79. },
  80. },
  81. }
  82. topotest.HandleStream(true, streamList, t)
  83. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  84. defer runtime.GetPluginInsManager().KillAll()
  85. for i, tt := range tests {
  86. _ = os.Remove(CACHE_FILE)
  87. rs, err := CreateRule(tt.Name, tt.Rule)
  88. if err != nil {
  89. t.Errorf("failed to create rule: %s.", err)
  90. continue
  91. }
  92. tp, err := planner.Plan(rs)
  93. if err != nil {
  94. t.Errorf("fail to init rule: %v", err)
  95. continue
  96. }
  97. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  98. go func(ctx context.Context) {
  99. select {
  100. case err := <-tp.Open():
  101. log.Println(err)
  102. tp.Cancel()
  103. case <-ctx.Done():
  104. log.Printf("ctx done %v\n", ctx.Err())
  105. tp.Cancel()
  106. }
  107. fmt.Println("all exit")
  108. }(ctx)
  109. for {
  110. if ctx.Err() != nil {
  111. t.Errorf("Exiting with error %v", ctx.Err())
  112. break
  113. }
  114. time.Sleep(10 * time.Millisecond)
  115. if err := topotest.CompareMetrics(tp, tt.M); err == nil {
  116. cancel()
  117. // need to wait for file results
  118. time.Sleep(10 * time.Millisecond)
  119. results := getResults()
  120. fmt.Printf("get results %v\n", results)
  121. time.Sleep(10 * time.Millisecond)
  122. var mm [][]map[string]interface{}
  123. for i, v := range results {
  124. if i >= 3 {
  125. break
  126. }
  127. var mapRes []map[string]interface{}
  128. err := json.Unmarshal([]byte(v), &mapRes)
  129. if err != nil {
  130. t.Errorf("Failed to parse the input into map")
  131. continue
  132. }
  133. mm = append(mm, mapRes)
  134. }
  135. if !reflect.DeepEqual(tt.R, mm) {
  136. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.Rule, tt.R, results)
  137. }
  138. break
  139. }
  140. }
  141. }
  142. topotest.HandleStream(false, streamList, t)
  143. // wait for rule clean up
  144. time.Sleep(1 * time.Second)
  145. }
  146. func getResults() []string {
  147. f, err := os.Open(CACHE_FILE)
  148. if err != nil {
  149. panic(err)
  150. }
  151. result := make([]string, 0)
  152. scanner := bufio.NewScanner(f)
  153. for scanner.Scan() {
  154. result = append(result, scanner.Text())
  155. }
  156. if err := scanner.Err(); err != nil {
  157. panic(err)
  158. }
  159. f.Close()
  160. return result
  161. }
  162. func CreateRule(name, sql string) (*api.Rule, error) {
  163. p := processor.NewRuleProcessor()
  164. p.ExecDrop(name)
  165. return p.ExecCreate(name, sql)
  166. }