switch_node_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. // Copyright 2022-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 node
  15. import (
  16. "fmt"
  17. "reflect"
  18. "testing"
  19. "time"
  20. "github.com/lf-edge/ekuiper/internal/conf"
  21. "github.com/lf-edge/ekuiper/internal/topo/context"
  22. "github.com/lf-edge/ekuiper/internal/xsql"
  23. "github.com/lf-edge/ekuiper/pkg/api"
  24. "github.com/lf-edge/ekuiper/pkg/ast"
  25. )
  26. func TestTuple(t *testing.T) {
  27. inputs := []*xsql.Tuple{
  28. {
  29. Message: map[string]interface{}{
  30. "f1": "v1",
  31. "f2": 45.6,
  32. },
  33. }, {
  34. Message: map[string]interface{}{
  35. "f1": "v2",
  36. "f2": 46.6,
  37. },
  38. }, {
  39. Message: map[string]interface{}{
  40. "f1": "v2",
  41. "f2": 26.6,
  42. },
  43. }, {
  44. Message: map[string]interface{}{
  45. "f1": "v2",
  46. "f2": 54.3,
  47. },
  48. }, {
  49. Message: map[string]interface{}{
  50. "f1": "v1",
  51. "f2": 36.6,
  52. },
  53. }, {
  54. Message: map[string]interface{}{
  55. "f1": "v1",
  56. "f2": 76.6,
  57. },
  58. }, {
  59. Message: map[string]interface{}{
  60. "f1": "v2",
  61. "f2": 41.2,
  62. },
  63. }, {
  64. Message: map[string]interface{}{
  65. "f1": "v2",
  66. "f2": 86.6,
  67. },
  68. },
  69. }
  70. outputs := [][]*xsql.Tuple{
  71. { // f2 > 40
  72. {
  73. Message: map[string]interface{}{
  74. "f1": "v1",
  75. "f2": 45.6,
  76. },
  77. }, {
  78. Message: map[string]interface{}{
  79. "f1": "v2",
  80. "f2": 46.6,
  81. },
  82. }, {
  83. Message: map[string]interface{}{
  84. "f1": "v2",
  85. "f2": 54.3,
  86. },
  87. }, {
  88. Message: map[string]interface{}{
  89. "f1": "v1",
  90. "f2": 76.6,
  91. },
  92. }, {
  93. Message: map[string]interface{}{
  94. "f1": "v2",
  95. "f2": 41.2,
  96. },
  97. }, {
  98. Message: map[string]interface{}{
  99. "f1": "v2",
  100. "f2": 86.6,
  101. },
  102. },
  103. },
  104. { // f1 == v1
  105. {
  106. Message: map[string]interface{}{
  107. "f1": "v1",
  108. "f2": 45.6,
  109. },
  110. }, {
  111. Message: map[string]interface{}{
  112. "f1": "v1",
  113. "f2": 36.6,
  114. },
  115. }, {
  116. Message: map[string]interface{}{
  117. "f1": "v1",
  118. "f2": 76.6,
  119. },
  120. },
  121. },
  122. { // f1 == v2 && f2 < 40
  123. {
  124. Message: map[string]interface{}{
  125. "f1": "v2",
  126. "f2": 26.6,
  127. },
  128. },
  129. },
  130. }
  131. sn, err := NewSwitchNode("test", &SwitchConfig{
  132. Cases: []ast.Expr{
  133. &ast.BinaryExpr{
  134. LHS: &ast.FieldRef{Name: "f2"},
  135. OP: ast.GT,
  136. RHS: &ast.NumberLiteral{Val: 40},
  137. },
  138. &ast.BinaryExpr{
  139. LHS: &ast.FieldRef{Name: "f1"},
  140. OP: ast.EQ,
  141. RHS: &ast.StringLiteral{Val: "v1"},
  142. },
  143. &ast.BinaryExpr{
  144. LHS: &ast.BinaryExpr{
  145. LHS: &ast.FieldRef{Name: "f1"},
  146. OP: ast.EQ,
  147. RHS: &ast.StringLiteral{Val: "v2"},
  148. },
  149. OP: ast.AND,
  150. RHS: &ast.BinaryExpr{
  151. LHS: &ast.FieldRef{Name: "f2"},
  152. OP: ast.LT,
  153. RHS: &ast.NumberLiteral{Val: 40},
  154. },
  155. },
  156. },
  157. StopAtFirstMatch: false,
  158. }, &api.RuleOption{})
  159. if err != nil {
  160. t.Fatalf("Failed to create switch node: %v", err)
  161. }
  162. contextLogger := conf.Log.WithField("rule", "TestSwitchTuple")
  163. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  164. errCh := make(chan error)
  165. output1 := make(chan interface{}, 10)
  166. output2 := make(chan interface{}, 10)
  167. output3 := make(chan interface{}, 10)
  168. sn.outputNodes[0].AddOutput(output1, "output1")
  169. sn.outputNodes[1].AddOutput(output2, "output2")
  170. sn.outputNodes[2].AddOutput(output3, "output3")
  171. go sn.Exec(ctx, errCh)
  172. go func() {
  173. for i, input := range inputs {
  174. select {
  175. case sn.input <- input:
  176. t.Logf("send input %d", i)
  177. case <-time.After(time.Second):
  178. errCh <- fmt.Errorf("Timeout sending input %d", i)
  179. return
  180. }
  181. }
  182. }()
  183. actualOuts := make([][]*xsql.Tuple, 3)
  184. outterFor:
  185. for {
  186. select {
  187. case err := <-errCh:
  188. t.Fatalf("Error received: %v", err)
  189. case out1 := <-output1:
  190. actualOuts[0] = append(actualOuts[0], out1.(*xsql.Tuple))
  191. case out2 := <-output2:
  192. actualOuts[1] = append(actualOuts[1], out2.(*xsql.Tuple))
  193. case out3 := <-output3:
  194. actualOuts[2] = append(actualOuts[2], out3.(*xsql.Tuple))
  195. case <-time.After(100 * time.Millisecond):
  196. break outterFor
  197. }
  198. }
  199. if !reflect.DeepEqual(actualOuts, outputs) {
  200. t.Errorf("Expected: %v, actual: %v", outputs, actualOuts)
  201. }
  202. }
  203. func TestCollection(t *testing.T) {
  204. inputs := []*xsql.WindowTuples{
  205. {
  206. Content: []xsql.TupleRow{
  207. &xsql.Tuple{
  208. Message: map[string]interface{}{
  209. "f1": "v1",
  210. "f2": 45.6,
  211. },
  212. },
  213. &xsql.Tuple{
  214. Message: map[string]interface{}{
  215. "f1": "v1",
  216. "f2": 65.6,
  217. },
  218. },
  219. },
  220. }, {
  221. Content: []xsql.TupleRow{
  222. &xsql.Tuple{
  223. Message: map[string]interface{}{
  224. "f1": "v2",
  225. "f2": 46.6,
  226. },
  227. },
  228. &xsql.Tuple{
  229. Message: map[string]interface{}{
  230. "f1": "v2",
  231. "f2": 26.6,
  232. },
  233. },
  234. &xsql.Tuple{
  235. Message: map[string]interface{}{
  236. "f1": "v2",
  237. "f2": 54.3,
  238. },
  239. },
  240. },
  241. }, {
  242. Content: []xsql.TupleRow{
  243. &xsql.Tuple{
  244. Message: map[string]interface{}{
  245. "f1": "v1",
  246. "f2": 36.6,
  247. },
  248. },
  249. &xsql.Tuple{
  250. Message: map[string]interface{}{
  251. "f1": "v1",
  252. "f2": 76.6,
  253. },
  254. },
  255. &xsql.Tuple{
  256. Message: map[string]interface{}{
  257. "f1": "v2",
  258. "f2": 41.2,
  259. },
  260. },
  261. },
  262. },
  263. }
  264. outputs := [][]*xsql.WindowTuples{
  265. { // avg(f2) > 50
  266. {
  267. Content: []xsql.TupleRow{
  268. &xsql.Tuple{
  269. Message: map[string]interface{}{
  270. "f1": "v1",
  271. "f2": 45.6,
  272. },
  273. },
  274. &xsql.Tuple{
  275. Message: map[string]interface{}{
  276. "f1": "v1",
  277. "f2": 65.6,
  278. },
  279. },
  280. },
  281. }, {
  282. Content: []xsql.TupleRow{
  283. &xsql.Tuple{
  284. Message: map[string]interface{}{
  285. "f1": "v1",
  286. "f2": 36.6,
  287. },
  288. },
  289. &xsql.Tuple{
  290. Message: map[string]interface{}{
  291. "f1": "v1",
  292. "f2": 76.6,
  293. },
  294. },
  295. &xsql.Tuple{
  296. Message: map[string]interface{}{
  297. "f1": "v2",
  298. "f2": 41.2,
  299. },
  300. },
  301. },
  302. },
  303. },
  304. { // else
  305. {
  306. Content: []xsql.TupleRow{
  307. &xsql.Tuple{
  308. Message: map[string]interface{}{
  309. "f1": "v2",
  310. "f2": 46.6,
  311. },
  312. },
  313. &xsql.Tuple{
  314. Message: map[string]interface{}{
  315. "f1": "v2",
  316. "f2": 26.6,
  317. },
  318. },
  319. &xsql.Tuple{
  320. Message: map[string]interface{}{
  321. "f1": "v2",
  322. "f2": 54.3,
  323. },
  324. },
  325. },
  326. },
  327. },
  328. }
  329. sn, err := NewSwitchNode("test", &SwitchConfig{
  330. Cases: []ast.Expr{
  331. &ast.BinaryExpr{
  332. LHS: &ast.Call{
  333. Name: "avg",
  334. FuncId: 0,
  335. FuncType: ast.FuncTypeAgg,
  336. Args: []ast.Expr{&ast.FieldRef{Name: "f2"}},
  337. },
  338. OP: ast.GT,
  339. RHS: &ast.NumberLiteral{Val: 50},
  340. },
  341. &ast.BinaryExpr{
  342. LHS: &ast.Call{
  343. Name: "avg",
  344. FuncId: 0,
  345. FuncType: ast.FuncTypeAgg,
  346. Args: []ast.Expr{&ast.FieldRef{Name: "f2"}},
  347. },
  348. OP: ast.LTE,
  349. RHS: &ast.NumberLiteral{Val: 50},
  350. },
  351. },
  352. StopAtFirstMatch: true,
  353. }, &api.RuleOption{})
  354. if err != nil {
  355. t.Fatalf("Failed to create switch node: %v", err)
  356. }
  357. contextLogger := conf.Log.WithField("rule", "TestSwitchWindow")
  358. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  359. errCh := make(chan error)
  360. output1 := make(chan interface{}, 10)
  361. output2 := make(chan interface{}, 10)
  362. sn.outputNodes[0].AddOutput(output1, "output1")
  363. sn.outputNodes[1].AddOutput(output2, "output2")
  364. go sn.Exec(ctx, errCh)
  365. go func() {
  366. for i, input := range inputs {
  367. select {
  368. case sn.input <- input:
  369. t.Logf("send input %d", i)
  370. case <-time.After(time.Second):
  371. errCh <- fmt.Errorf("Timeout sending input %d", i)
  372. return
  373. }
  374. }
  375. }()
  376. actualOuts := make([][]*xsql.WindowTuples, 2)
  377. outterFor:
  378. for {
  379. select {
  380. case err := <-errCh:
  381. t.Fatalf("Error received: %v", err)
  382. case out1 := <-output1:
  383. actualOuts[0] = append(actualOuts[0], out1.(*xsql.WindowTuples))
  384. case out2 := <-output2:
  385. actualOuts[1] = append(actualOuts[1], out2.(*xsql.WindowTuples))
  386. case <-time.After(100 * time.Millisecond):
  387. break outterFor
  388. }
  389. }
  390. if !reflect.DeepEqual(actualOuts, outputs) {
  391. t.Errorf("Expected: %v, actual: %v", outputs, actualOuts)
  392. }
  393. }