lookup_node_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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/benbjohnson/clock"
  21. "github.com/lf-edge/ekuiper/internal/binder"
  22. "github.com/lf-edge/ekuiper/internal/binder/io"
  23. "github.com/lf-edge/ekuiper/internal/conf"
  24. "github.com/lf-edge/ekuiper/internal/plugin"
  25. "github.com/lf-edge/ekuiper/internal/topo/context"
  26. "github.com/lf-edge/ekuiper/internal/topo/lookup"
  27. "github.com/lf-edge/ekuiper/internal/xsql"
  28. "github.com/lf-edge/ekuiper/pkg/api"
  29. "github.com/lf-edge/ekuiper/pkg/ast"
  30. )
  31. type mockLookupSrc struct {
  32. data []api.SourceTuple // new mock data
  33. }
  34. func (m *mockLookupSrc) Open(_ api.StreamContext) error {
  35. return nil
  36. }
  37. func (m *mockLookupSrc) Configure(_ string, _ map[string]interface{}) error {
  38. return nil
  39. }
  40. // Lookup accept int value as the first array value
  41. func (m *mockLookupSrc) Lookup(_ api.StreamContext, fields []string, _ []string, values []interface{}) ([]api.SourceTuple, error) {
  42. mc := conf.Clock.(*clock.Mock)
  43. if len(fields) > 0 { // if fields is not empty, the value will be kept
  44. if m.data != nil {
  45. return m.data, nil
  46. } else {
  47. m.data = []api.SourceTuple{api.NewDefaultSourceTupleWithTime(map[string]interface{}{
  48. "newA": 1000,
  49. "newB": 1000,
  50. }, nil, mc.Now())}
  51. }
  52. }
  53. a1, ok := values[0].(int)
  54. if ok {
  55. var result []api.SourceTuple
  56. c := a1 % 2
  57. if c != 0 {
  58. result = append(result, api.NewDefaultSourceTupleWithTime(map[string]interface{}{
  59. "newA": c,
  60. "newB": c * 2,
  61. }, nil, mc.Now()))
  62. }
  63. c = a1 % 3
  64. if c != 0 {
  65. result = append(result, api.NewDefaultSourceTupleWithTime(map[string]interface{}{
  66. "newA": c,
  67. "newB": c * 2,
  68. }, nil, mc.Now()))
  69. }
  70. c = a1 % 5
  71. if c != 0 {
  72. result = append(result, api.NewDefaultSourceTupleWithTime(map[string]interface{}{
  73. "newA": c,
  74. "newB": c * 2,
  75. }, nil, mc.Now()))
  76. }
  77. c = a1 % 7
  78. if c != 0 {
  79. result = append(result, api.NewDefaultSourceTupleWithTime(map[string]interface{}{
  80. "newA": c,
  81. "newB": c * 2,
  82. }, nil, mc.Now()))
  83. }
  84. return result, nil
  85. } else {
  86. return []api.SourceTuple{
  87. api.NewDefaultSourceTupleWithTime(map[string]interface{}{
  88. "newA": 0,
  89. "newB": 0,
  90. }, nil, mc.Now()),
  91. }, nil
  92. }
  93. }
  94. func (m *mockLookupSrc) Close(_ api.StreamContext) error {
  95. // do nothing
  96. return nil
  97. }
  98. type mockFac struct{}
  99. func (m *mockFac) Source(_ string) (api.Source, error) {
  100. return nil, nil
  101. }
  102. func (m *mockFac) SourcePluginInfo(_ string) (plugin.EXTENSION_TYPE, string, string) {
  103. return plugin.INTERNAL, "", ""
  104. }
  105. func (m *mockFac) LookupSource(name string) (api.LookupSource, error) {
  106. if name == "mock" {
  107. return &mockLookupSrc{}, nil
  108. }
  109. return nil, nil
  110. }
  111. // init mock lookup source factory
  112. func init() {
  113. io.Initialize([]binder.FactoryEntry{
  114. {Name: "native plugin", Factory: &mockFac{}, Weight: 10},
  115. })
  116. }
  117. func TestLookup(t *testing.T) {
  118. tests := []struct {
  119. input interface{} // a tuple or a window
  120. output *xsql.JoinTuples
  121. }{
  122. {
  123. input: &xsql.Tuple{
  124. Emitter: "demo",
  125. Message: map[string]interface{}{
  126. "a": 6,
  127. "b": "aaaa",
  128. },
  129. },
  130. output: &xsql.JoinTuples{
  131. Content: []*xsql.JoinTuple{
  132. {
  133. Tuples: []xsql.TupleRow{
  134. &xsql.Tuple{
  135. Emitter: "demo",
  136. Message: map[string]interface{}{
  137. "a": 6,
  138. "b": "aaaa",
  139. },
  140. },
  141. &xsql.Tuple{
  142. Emitter: "mock",
  143. Message: map[string]interface{}{
  144. "newA": 1,
  145. "newB": 2,
  146. },
  147. },
  148. },
  149. }, {
  150. Tuples: []xsql.TupleRow{
  151. &xsql.Tuple{
  152. Emitter: "demo",
  153. Message: map[string]interface{}{
  154. "a": 6,
  155. "b": "aaaa",
  156. },
  157. },
  158. &xsql.Tuple{
  159. Emitter: "mock",
  160. Message: map[string]interface{}{
  161. "newA": 6,
  162. "newB": 12,
  163. },
  164. },
  165. },
  166. },
  167. },
  168. },
  169. },
  170. {
  171. input: &xsql.WindowTuples{
  172. Content: []xsql.TupleRow{
  173. &xsql.Tuple{
  174. Emitter: "demo",
  175. Message: map[string]interface{}{
  176. "a": 9,
  177. "b": "aaaa",
  178. },
  179. },
  180. &xsql.Tuple{
  181. Emitter: "demo",
  182. Message: map[string]interface{}{
  183. "a": 4,
  184. "b": "bbaa",
  185. },
  186. },
  187. },
  188. },
  189. output: &xsql.JoinTuples{
  190. Content: []*xsql.JoinTuple{
  191. {
  192. Tuples: []xsql.TupleRow{
  193. &xsql.Tuple{
  194. Emitter: "demo",
  195. Message: map[string]interface{}{
  196. "a": 9,
  197. "b": "aaaa",
  198. },
  199. },
  200. &xsql.Tuple{
  201. Emitter: "mock",
  202. Message: map[string]interface{}{
  203. "newA": 1,
  204. "newB": 2,
  205. },
  206. },
  207. },
  208. }, {
  209. Tuples: []xsql.TupleRow{
  210. &xsql.Tuple{
  211. Emitter: "demo",
  212. Message: map[string]interface{}{
  213. "a": 9,
  214. "b": "aaaa",
  215. },
  216. },
  217. &xsql.Tuple{
  218. Emitter: "mock",
  219. Message: map[string]interface{}{
  220. "newA": 4,
  221. "newB": 8,
  222. },
  223. },
  224. },
  225. }, {
  226. Tuples: []xsql.TupleRow{
  227. &xsql.Tuple{
  228. Emitter: "demo",
  229. Message: map[string]interface{}{
  230. "a": 9,
  231. "b": "aaaa",
  232. },
  233. },
  234. &xsql.Tuple{
  235. Emitter: "mock",
  236. Message: map[string]interface{}{
  237. "newA": 2,
  238. "newB": 4,
  239. },
  240. },
  241. },
  242. }, {
  243. Tuples: []xsql.TupleRow{
  244. &xsql.Tuple{
  245. Emitter: "demo",
  246. Message: map[string]interface{}{
  247. "a": 4,
  248. "b": "bbaa",
  249. },
  250. },
  251. &xsql.Tuple{
  252. Emitter: "mock",
  253. Message: map[string]interface{}{
  254. "newA": 1,
  255. "newB": 2,
  256. },
  257. },
  258. },
  259. }, {
  260. Tuples: []xsql.TupleRow{
  261. &xsql.Tuple{
  262. Emitter: "demo",
  263. Message: map[string]interface{}{
  264. "a": 4,
  265. "b": "bbaa",
  266. },
  267. },
  268. &xsql.Tuple{
  269. Emitter: "mock",
  270. Message: map[string]interface{}{
  271. "newA": 4,
  272. "newB": 8,
  273. },
  274. },
  275. },
  276. }, {
  277. Tuples: []xsql.TupleRow{
  278. &xsql.Tuple{
  279. Emitter: "demo",
  280. Message: map[string]interface{}{
  281. "a": 4,
  282. "b": "bbaa",
  283. },
  284. },
  285. &xsql.Tuple{
  286. Emitter: "mock",
  287. Message: map[string]interface{}{
  288. "newA": 4,
  289. "newB": 8,
  290. },
  291. },
  292. },
  293. },
  294. },
  295. },
  296. },
  297. }
  298. options := &ast.Options{
  299. DATASOURCE: "mock",
  300. TYPE: "mock",
  301. STRICT_VALIDATION: true,
  302. KIND: "lookup",
  303. }
  304. lookup.CreateInstance("mock", "mock", options)
  305. contextLogger := conf.Log.WithField("rule", "TestLookup")
  306. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  307. l, _ := NewLookupNode("mock", []string{}, []string{"a"}, ast.INNER_JOIN, []ast.Expr{&ast.FieldRef{
  308. StreamName: "",
  309. Name: "a",
  310. }}, options, &api.RuleOption{
  311. IsEventTime: false,
  312. LateTol: 0,
  313. Concurrency: 0,
  314. BufferLength: 0,
  315. SendMetaToSink: false,
  316. SendError: false,
  317. Qos: 0,
  318. CheckpointInterval: 0,
  319. })
  320. errCh := make(chan error)
  321. outputCh := make(chan interface{}, 1)
  322. l.outputs["mock"] = outputCh
  323. l.Exec(ctx, errCh)
  324. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  325. for i, tt := range tests {
  326. select {
  327. case err := <-errCh:
  328. t.Error(err)
  329. return
  330. case l.input <- tt.input:
  331. case <-time.After(1 * time.Second):
  332. t.Error("send message timeout")
  333. return
  334. }
  335. select {
  336. case err := <-errCh:
  337. t.Error(err)
  338. return
  339. case output := <-outputCh:
  340. if !reflect.DeepEqual(tt.output, output) {
  341. t.Errorf("\ncase %d: expect %v but got %v", i, tt.output, output)
  342. }
  343. case <-time.After(1 * time.Second):
  344. t.Error("send message timeout")
  345. return
  346. }
  347. }
  348. }
  349. func TestCachedLookup(t *testing.T) {
  350. options := &ast.Options{
  351. DATASOURCE: "mock",
  352. TYPE: "mock",
  353. STRICT_VALIDATION: true,
  354. KIND: "lookup",
  355. }
  356. lookup.CreateInstance("mock", "mock", options)
  357. contextLogger := conf.Log.WithField("rule", "TestLookup")
  358. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  359. l, _ := NewLookupNode("mock", []string{"fixed"}, []string{"a"}, ast.INNER_JOIN, []ast.Expr{&ast.FieldRef{
  360. StreamName: "",
  361. Name: "a",
  362. }}, options, &api.RuleOption{
  363. IsEventTime: false,
  364. LateTol: 0,
  365. Concurrency: 0,
  366. BufferLength: 0,
  367. SendMetaToSink: false,
  368. SendError: false,
  369. Qos: 0,
  370. CheckpointInterval: 0,
  371. })
  372. l.conf = &LookupConf{
  373. Cache: true,
  374. CacheTTL: 20,
  375. CacheMissingKey: false,
  376. }
  377. errCh := make(chan error)
  378. outputCh := make(chan interface{}, 1)
  379. l.outputs["mock"] = outputCh
  380. l.Exec(ctx, errCh)
  381. input := &xsql.Tuple{
  382. Emitter: "demo",
  383. Message: map[string]interface{}{
  384. "a": 6,
  385. "b": "aaaa",
  386. },
  387. }
  388. outputBefore := &xsql.JoinTuples{
  389. Content: []*xsql.JoinTuple{
  390. {
  391. Tuples: []xsql.TupleRow{
  392. &xsql.Tuple{
  393. Emitter: "demo",
  394. Message: map[string]interface{}{
  395. "a": 6,
  396. "b": "aaaa",
  397. },
  398. },
  399. &xsql.Tuple{
  400. Emitter: "mock",
  401. Message: map[string]interface{}{
  402. "newA": 1,
  403. "newB": 2,
  404. },
  405. Timestamp: 11000,
  406. },
  407. },
  408. }, {
  409. Tuples: []xsql.TupleRow{
  410. &xsql.Tuple{
  411. Emitter: "demo",
  412. Message: map[string]interface{}{
  413. "a": 6,
  414. "b": "aaaa",
  415. },
  416. },
  417. &xsql.Tuple{
  418. Emitter: "mock",
  419. Message: map[string]interface{}{
  420. "newA": 6,
  421. "newB": 12,
  422. },
  423. Timestamp: 11000,
  424. },
  425. },
  426. },
  427. },
  428. }
  429. outputAfter := &xsql.JoinTuples{
  430. Content: []*xsql.JoinTuple{
  431. {
  432. Tuples: []xsql.TupleRow{
  433. &xsql.Tuple{
  434. Emitter: "demo",
  435. Message: map[string]interface{}{
  436. "a": 6,
  437. "b": "aaaa",
  438. },
  439. },
  440. &xsql.Tuple{
  441. Emitter: "mock",
  442. Message: map[string]interface{}{
  443. "newA": 1000,
  444. "newB": 1000,
  445. },
  446. Timestamp: 22000,
  447. },
  448. },
  449. },
  450. },
  451. }
  452. // First run and the set mock result
  453. clock := conf.Clock.(*clock.Mock)
  454. select {
  455. case err := <-errCh:
  456. t.Error(err)
  457. return
  458. case l.input <- input:
  459. case <-time.After(1 * time.Second):
  460. t.Error("send message timeout")
  461. return
  462. }
  463. select {
  464. case err := <-errCh:
  465. t.Error(err)
  466. return
  467. case <-outputCh:
  468. //if !reflect.DeepEqual(output, outputBefore) {
  469. // t.Errorf("\nfirst case: expect %v but got %v", output, outputBefore)
  470. //}
  471. case <-time.After(1 * time.Second):
  472. t.Error("send message timeout")
  473. return
  474. }
  475. // Get cache
  476. clock.Add(11 * time.Second)
  477. select {
  478. case err := <-errCh:
  479. t.Error(err)
  480. return
  481. case l.input <- input:
  482. case <-time.After(1 * time.Second):
  483. t.Error("send message timeout")
  484. return
  485. }
  486. select {
  487. case err := <-errCh:
  488. t.Error(err)
  489. return
  490. case output := <-outputCh:
  491. if !reflect.DeepEqual(output, outputBefore) {
  492. t.Errorf("\ncached case: expect %v but got %v", output, outputBefore)
  493. }
  494. case <-time.After(1 * time.Second):
  495. t.Error("send message timeout")
  496. return
  497. }
  498. // Cache expired
  499. clock.Add(11 * time.Second)
  500. select {
  501. case err := <-errCh:
  502. t.Error(err)
  503. return
  504. case l.input <- input:
  505. case <-time.After(1 * time.Second):
  506. t.Error("send message timeout")
  507. return
  508. }
  509. select {
  510. case err := <-errCh:
  511. t.Error(err)
  512. return
  513. case output := <-outputCh:
  514. if !reflect.DeepEqual(output, outputAfter) {
  515. t.Errorf("\nexpired case: expect %v but got %v", output, outputAfter)
  516. }
  517. case <-time.After(1 * time.Second):
  518. t.Error("send message timeout")
  519. return
  520. }
  521. }