switch_node_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 node
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/conf"
  18. "github.com/lf-edge/ekuiper/internal/topo/context"
  19. "github.com/lf-edge/ekuiper/internal/xsql"
  20. "github.com/lf-edge/ekuiper/pkg/api"
  21. "github.com/lf-edge/ekuiper/pkg/ast"
  22. "reflect"
  23. "testing"
  24. "time"
  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. fmt.Println("send input", i)
  177. case <-time.After(time.Second):
  178. t.Fatalf("Timeout sending input %d", i)
  179. }
  180. }
  181. }()
  182. actualOuts := make([][]*xsql.Tuple, 3)
  183. outterFor:
  184. for {
  185. select {
  186. case err := <-errCh:
  187. t.Fatalf("Error received: %v", err)
  188. case out1 := <-output1:
  189. actualOuts[0] = append(actualOuts[0], out1.(*xsql.Tuple))
  190. case out2 := <-output2:
  191. actualOuts[1] = append(actualOuts[1], out2.(*xsql.Tuple))
  192. case out3 := <-output3:
  193. actualOuts[2] = append(actualOuts[2], out3.(*xsql.Tuple))
  194. case <-time.After(100 * time.Millisecond):
  195. break outterFor
  196. }
  197. }
  198. if !reflect.DeepEqual(actualOuts, outputs) {
  199. t.Errorf("Expected: %v, actual: %v", outputs, actualOuts)
  200. }
  201. }
  202. func TestCollection(t *testing.T) {
  203. inputs := []*xsql.WindowTuples{
  204. {
  205. Content: []xsql.TupleRow{
  206. &xsql.Tuple{
  207. Message: map[string]interface{}{
  208. "f1": "v1",
  209. "f2": 45.6,
  210. },
  211. },
  212. &xsql.Tuple{
  213. Message: map[string]interface{}{
  214. "f1": "v1",
  215. "f2": 65.6,
  216. },
  217. },
  218. },
  219. }, {
  220. Content: []xsql.TupleRow{
  221. &xsql.Tuple{
  222. Message: map[string]interface{}{
  223. "f1": "v2",
  224. "f2": 46.6,
  225. },
  226. },
  227. &xsql.Tuple{
  228. Message: map[string]interface{}{
  229. "f1": "v2",
  230. "f2": 26.6,
  231. },
  232. },
  233. &xsql.Tuple{
  234. Message: map[string]interface{}{
  235. "f1": "v2",
  236. "f2": 54.3,
  237. },
  238. },
  239. },
  240. }, {
  241. Content: []xsql.TupleRow{
  242. &xsql.Tuple{
  243. Message: map[string]interface{}{
  244. "f1": "v1",
  245. "f2": 36.6,
  246. },
  247. },
  248. &xsql.Tuple{
  249. Message: map[string]interface{}{
  250. "f1": "v1",
  251. "f2": 76.6,
  252. },
  253. },
  254. &xsql.Tuple{
  255. Message: map[string]interface{}{
  256. "f1": "v2",
  257. "f2": 41.2,
  258. },
  259. },
  260. },
  261. },
  262. }
  263. outputs := [][]*xsql.WindowTuples{
  264. { // avg(f2) > 50
  265. {
  266. Content: []xsql.TupleRow{
  267. &xsql.Tuple{
  268. Message: map[string]interface{}{
  269. "f1": "v1",
  270. "f2": 45.6,
  271. },
  272. },
  273. &xsql.Tuple{
  274. Message: map[string]interface{}{
  275. "f1": "v1",
  276. "f2": 65.6,
  277. },
  278. },
  279. },
  280. }, {
  281. Content: []xsql.TupleRow{
  282. &xsql.Tuple{
  283. Message: map[string]interface{}{
  284. "f1": "v1",
  285. "f2": 36.6,
  286. },
  287. },
  288. &xsql.Tuple{
  289. Message: map[string]interface{}{
  290. "f1": "v1",
  291. "f2": 76.6,
  292. },
  293. },
  294. &xsql.Tuple{
  295. Message: map[string]interface{}{
  296. "f1": "v2",
  297. "f2": 41.2,
  298. },
  299. },
  300. },
  301. },
  302. },
  303. { // else
  304. {
  305. Content: []xsql.TupleRow{
  306. &xsql.Tuple{
  307. Message: map[string]interface{}{
  308. "f1": "v2",
  309. "f2": 46.6,
  310. },
  311. },
  312. &xsql.Tuple{
  313. Message: map[string]interface{}{
  314. "f1": "v2",
  315. "f2": 26.6,
  316. },
  317. },
  318. &xsql.Tuple{
  319. Message: map[string]interface{}{
  320. "f1": "v2",
  321. "f2": 54.3,
  322. },
  323. },
  324. },
  325. },
  326. },
  327. }
  328. sn, err := NewSwitchNode("test", &SwitchConfig{
  329. Cases: []ast.Expr{
  330. &ast.BinaryExpr{
  331. LHS: &ast.Call{
  332. Name: "avg",
  333. FuncId: 0,
  334. FuncType: ast.FuncTypeAgg,
  335. Args: []ast.Expr{&ast.FieldRef{Name: "f2"}},
  336. },
  337. OP: ast.GT,
  338. RHS: &ast.NumberLiteral{Val: 50},
  339. },
  340. &ast.BinaryExpr{
  341. LHS: &ast.Call{
  342. Name: "avg",
  343. FuncId: 0,
  344. FuncType: ast.FuncTypeAgg,
  345. Args: []ast.Expr{&ast.FieldRef{Name: "f2"}},
  346. },
  347. OP: ast.LTE,
  348. RHS: &ast.NumberLiteral{Val: 50},
  349. },
  350. },
  351. StopAtFirstMatch: true,
  352. }, &api.RuleOption{})
  353. if err != nil {
  354. t.Fatalf("Failed to create switch node: %v", err)
  355. }
  356. contextLogger := conf.Log.WithField("rule", "TestSwitchWindow")
  357. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  358. errCh := make(chan error)
  359. output1 := make(chan interface{}, 10)
  360. output2 := make(chan interface{}, 10)
  361. sn.outputNodes[0].AddOutput(output1, "output1")
  362. sn.outputNodes[1].AddOutput(output2, "output2")
  363. go sn.Exec(ctx, errCh)
  364. go func() {
  365. for i, input := range inputs {
  366. select {
  367. case sn.input <- input:
  368. fmt.Println("send input", i)
  369. case <-time.After(time.Second):
  370. t.Fatalf("Timeout sending input %d", i)
  371. }
  372. }
  373. }()
  374. actualOuts := make([][]*xsql.WindowTuples, 2)
  375. outterFor:
  376. for {
  377. select {
  378. case err := <-errCh:
  379. t.Fatalf("Error received: %v", err)
  380. case out1 := <-output1:
  381. actualOuts[0] = append(actualOuts[0], out1.(*xsql.WindowTuples))
  382. case out2 := <-output2:
  383. actualOuts[1] = append(actualOuts[1], out2.(*xsql.WindowTuples))
  384. case <-time.After(100 * time.Millisecond):
  385. break outterFor
  386. }
  387. }
  388. if !reflect.DeepEqual(actualOuts, outputs) {
  389. t.Errorf("Expected: %v, actual: %v", outputs, actualOuts)
  390. }
  391. }