ruleState_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Copyright 2022 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 rule
  15. import (
  16. "errors"
  17. "fmt"
  18. "github.com/lf-edge/ekuiper/internal/processor"
  19. "github.com/lf-edge/ekuiper/internal/testx"
  20. "github.com/lf-edge/ekuiper/pkg/api"
  21. "reflect"
  22. "testing"
  23. "time"
  24. )
  25. var defaultOption = &api.RuleOption{
  26. IsEventTime: false,
  27. LateTol: 1000,
  28. Concurrency: 1,
  29. BufferLength: 1024,
  30. SendMetaToSink: false,
  31. SendError: true,
  32. Qos: api.AtMostOnce,
  33. CheckpointInterval: 300000,
  34. Restart: &api.RestartStrategy{
  35. Attempts: 0,
  36. Delay: 1000,
  37. Multiplier: 2,
  38. MaxDelay: 30000,
  39. JitterFactor: 0.1,
  40. },
  41. }
  42. func init() {
  43. testx.InitEnv()
  44. }
  45. func TestCreate(t *testing.T) {
  46. sp := processor.NewStreamProcessor()
  47. sp.ExecStmt(`CREATE STREAM demo () WITH (DATASOURCE="users", FORMAT="JSON")`)
  48. defer sp.ExecStmt(`DROP STREAM demo`)
  49. var tests = []struct {
  50. r *api.Rule
  51. e error
  52. }{
  53. {
  54. r: &api.Rule{
  55. Triggered: false,
  56. Id: "test",
  57. Sql: "SELECT ts FROM demo",
  58. Actions: []map[string]interface{}{
  59. {
  60. "log": map[string]interface{}{},
  61. },
  62. },
  63. Options: defaultOption,
  64. },
  65. e: nil,
  66. }, {
  67. r: &api.Rule{
  68. Triggered: false,
  69. Id: "test",
  70. Sql: "SELECT timestamp FROM demo",
  71. Actions: []map[string]interface{}{
  72. {
  73. "log": map[string]interface{}{},
  74. },
  75. },
  76. Options: defaultOption,
  77. },
  78. e: errors.New("Parse SQL SELECT timestamp FROM demo error: found \"TIMESTAMP\", expected expression.."),
  79. },
  80. {
  81. r: &api.Rule{
  82. Triggered: false,
  83. Id: "test",
  84. Sql: "SELECT * FROM demo1",
  85. Actions: []map[string]interface{}{
  86. {
  87. "log": map[string]interface{}{},
  88. },
  89. },
  90. Options: defaultOption,
  91. },
  92. e: errors.New("fail to get stream demo1, please check if stream is created"),
  93. },
  94. }
  95. for i, tt := range tests {
  96. _, err := NewRuleState(tt.r)
  97. if !reflect.DeepEqual(err, tt.e) {
  98. t.Errorf("%d.\n\nerror mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.e, err)
  99. }
  100. }
  101. }
  102. func TestUpdate(t *testing.T) {
  103. sp := processor.NewStreamProcessor()
  104. sp.ExecStmt(`CREATE STREAM demo () WITH (DATASOURCE="users", FORMAT="JSON")`)
  105. defer sp.ExecStmt(`DROP STREAM demo`)
  106. rs, err := NewRuleState(&api.Rule{
  107. Triggered: false,
  108. Id: "test",
  109. Sql: "SELECT ts FROM demo",
  110. Actions: []map[string]interface{}{
  111. {
  112. "log": map[string]interface{}{},
  113. },
  114. },
  115. Options: defaultOption,
  116. })
  117. if err != nil {
  118. t.Error(err)
  119. return
  120. }
  121. defer rs.Close()
  122. err = rs.Start()
  123. if err != nil {
  124. t.Error(err)
  125. return
  126. }
  127. var tests = []struct {
  128. r *api.Rule
  129. e error
  130. }{
  131. {
  132. r: &api.Rule{
  133. Triggered: false,
  134. Id: "test",
  135. Sql: "SELECT timestamp FROM demo",
  136. Actions: []map[string]interface{}{
  137. {
  138. "log": map[string]interface{}{},
  139. },
  140. },
  141. Options: defaultOption,
  142. },
  143. e: errors.New("Parse SQL SELECT timestamp FROM demo error: found \"TIMESTAMP\", expected expression.."),
  144. },
  145. {
  146. r: &api.Rule{
  147. Triggered: false,
  148. Id: "test",
  149. Sql: "SELECT * FROM demo1",
  150. Actions: []map[string]interface{}{
  151. {
  152. "log": map[string]interface{}{},
  153. },
  154. },
  155. Options: defaultOption,
  156. },
  157. e: errors.New("fail to get stream demo1, please check if stream is created"),
  158. },
  159. {
  160. r: &api.Rule{
  161. Triggered: false,
  162. Id: "test",
  163. Sql: "SELECT * FROM demo",
  164. Actions: []map[string]interface{}{
  165. {
  166. "log": map[string]interface{}{},
  167. },
  168. },
  169. Options: defaultOption,
  170. },
  171. e: nil,
  172. },
  173. }
  174. for i, tt := range tests {
  175. err = rs.UpdateTopo(tt.r)
  176. if !reflect.DeepEqual(err, tt.e) {
  177. t.Errorf("%d.\n\nerror mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.e, err)
  178. }
  179. }
  180. }
  181. func TestMultipleAccess(t *testing.T) {
  182. sp := processor.NewStreamProcessor()
  183. sp.ExecStmt(`CREATE STREAM demo () WITH (DATASOURCE="users", FORMAT="JSON")`)
  184. defer sp.ExecStmt(`DROP STREAM demo`)
  185. rs, err := NewRuleState(&api.Rule{
  186. Triggered: false,
  187. Id: "test",
  188. Sql: "SELECT ts FROM demo",
  189. Actions: []map[string]interface{}{
  190. {
  191. "log": map[string]interface{}{},
  192. },
  193. },
  194. Options: defaultOption,
  195. })
  196. if err != nil {
  197. t.Error(err)
  198. return
  199. }
  200. defer rs.Close()
  201. err = rs.Start()
  202. if err != nil {
  203. t.Error(err)
  204. return
  205. }
  206. for i := 0; i < 10; i++ {
  207. if i%3 == 0 {
  208. go func(i int) {
  209. rs.Stop()
  210. fmt.Printf("%d:%d\n", i, rs.triggered)
  211. }(i)
  212. } else {
  213. go func(i int) {
  214. rs.Start()
  215. fmt.Printf("%d:%d\n", i, rs.triggered)
  216. }(i)
  217. }
  218. }
  219. time.Sleep(1 * time.Millisecond)
  220. rs.Start()
  221. time.Sleep(10 * time.Millisecond)
  222. if rs.triggered != 1 {
  223. t.Errorf("triggered mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", 1, rs.triggered)
  224. }
  225. }