lookup_node_test.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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/binder"
  18. "github.com/lf-edge/ekuiper/internal/binder/io"
  19. "github.com/lf-edge/ekuiper/internal/conf"
  20. "github.com/lf-edge/ekuiper/internal/topo/context"
  21. "github.com/lf-edge/ekuiper/internal/topo/lookup"
  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. "reflect"
  26. "testing"
  27. "time"
  28. )
  29. type mockLookupSrc struct {
  30. }
  31. func (m *mockLookupSrc) Open(_ api.StreamContext) error {
  32. return nil
  33. }
  34. func (m *mockLookupSrc) Configure(_ string, _ map[string]interface{}) error {
  35. return nil
  36. }
  37. // Lookup accept int value as the first array value
  38. func (m *mockLookupSrc) Lookup(_ api.StreamContext, _ []string, _ []string, values []interface{}) ([]api.SourceTuple, error) {
  39. a1, ok := values[0].(int)
  40. if ok {
  41. var result []api.SourceTuple
  42. c := a1 % 2
  43. if c != 0 {
  44. result = append(result, api.NewDefaultSourceTuple(map[string]interface{}{
  45. "newA": c,
  46. "newB": c * 2,
  47. }, nil))
  48. }
  49. c = a1 % 3
  50. if c != 0 {
  51. result = append(result, api.NewDefaultSourceTuple(map[string]interface{}{
  52. "newA": c,
  53. "newB": c * 2,
  54. }, nil))
  55. }
  56. c = a1 % 5
  57. if c != 0 {
  58. result = append(result, api.NewDefaultSourceTuple(map[string]interface{}{
  59. "newA": c,
  60. "newB": c * 2,
  61. }, nil))
  62. }
  63. c = a1 % 7
  64. if c != 0 {
  65. result = append(result, api.NewDefaultSourceTuple(map[string]interface{}{
  66. "newA": c,
  67. "newB": c * 2,
  68. }, nil))
  69. }
  70. return result, nil
  71. } else {
  72. return []api.SourceTuple{
  73. api.NewDefaultSourceTuple(map[string]interface{}{
  74. "newA": 0,
  75. "newB": 0,
  76. }, nil),
  77. }, nil
  78. }
  79. }
  80. func (m *mockLookupSrc) Close(_ api.StreamContext) error {
  81. // do nothing
  82. return nil
  83. }
  84. type mockFac struct{}
  85. func (m *mockFac) Source(_ string) (api.Source, error) {
  86. return nil, nil
  87. }
  88. func (m *mockFac) LookupSource(name string) (api.LookupSource, error) {
  89. if name == "mock" {
  90. return &mockLookupSrc{}, nil
  91. }
  92. return nil, nil
  93. }
  94. // init mock lookup source factory
  95. func init() {
  96. io.Initialize([]binder.FactoryEntry{
  97. {Name: "native plugin", Factory: &mockFac{}, Weight: 10},
  98. })
  99. }
  100. func TestLookup(t *testing.T) {
  101. var tests = []struct {
  102. input interface{} // a tuple or a window
  103. output *xsql.JoinTuples
  104. }{
  105. {
  106. input: &xsql.Tuple{
  107. Emitter: "demo",
  108. Message: map[string]interface{}{
  109. "a": 6,
  110. "b": "aaaa",
  111. },
  112. },
  113. output: &xsql.JoinTuples{
  114. Content: []*xsql.JoinTuple{
  115. {
  116. Tuples: []xsql.TupleRow{
  117. &xsql.Tuple{
  118. Emitter: "demo",
  119. Message: map[string]interface{}{
  120. "a": 6,
  121. "b": "aaaa",
  122. },
  123. },
  124. &xsql.Tuple{
  125. Emitter: "mock",
  126. Message: map[string]interface{}{
  127. "newA": 1,
  128. "newB": 2,
  129. },
  130. },
  131. },
  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": 6,
  145. "newB": 12,
  146. },
  147. },
  148. },
  149. },
  150. },
  151. },
  152. },
  153. {
  154. input: &xsql.WindowTuples{
  155. Content: []xsql.TupleRow{
  156. &xsql.Tuple{
  157. Emitter: "demo",
  158. Message: map[string]interface{}{
  159. "a": 9,
  160. "b": "aaaa",
  161. },
  162. },
  163. &xsql.Tuple{
  164. Emitter: "demo",
  165. Message: map[string]interface{}{
  166. "a": 4,
  167. "b": "bbaa",
  168. },
  169. },
  170. },
  171. },
  172. output: &xsql.JoinTuples{
  173. Content: []*xsql.JoinTuple{
  174. {
  175. Tuples: []xsql.TupleRow{
  176. &xsql.Tuple{
  177. Emitter: "demo",
  178. Message: map[string]interface{}{
  179. "a": 9,
  180. "b": "aaaa",
  181. },
  182. },
  183. &xsql.Tuple{
  184. Emitter: "mock",
  185. Message: map[string]interface{}{
  186. "newA": 1,
  187. "newB": 2,
  188. },
  189. },
  190. },
  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": 4,
  204. "newB": 8,
  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": 2,
  221. "newB": 4,
  222. },
  223. },
  224. },
  225. }, {
  226. Tuples: []xsql.TupleRow{
  227. &xsql.Tuple{
  228. Emitter: "demo",
  229. Message: map[string]interface{}{
  230. "a": 4,
  231. "b": "bbaa",
  232. },
  233. },
  234. &xsql.Tuple{
  235. Emitter: "mock",
  236. Message: map[string]interface{}{
  237. "newA": 1,
  238. "newB": 2,
  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": 4,
  255. "newB": 8,
  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. },
  278. },
  279. },
  280. }
  281. options := &ast.Options{
  282. DATASOURCE: "mock",
  283. TYPE: "mock",
  284. STRICT_VALIDATION: true,
  285. KIND: "lookup",
  286. }
  287. lookup.CreateInstance("mock", "mock", options)
  288. contextLogger := conf.Log.WithField("rule", "TestLookup")
  289. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  290. l, _ := NewLookupNode("mock", []string{}, []string{"a"}, ast.INNER_JOIN, []ast.Expr{&ast.FieldRef{
  291. StreamName: "",
  292. Name: "a",
  293. }}, options, &api.RuleOption{
  294. IsEventTime: false,
  295. LateTol: 0,
  296. Concurrency: 0,
  297. BufferLength: 0,
  298. SendMetaToSink: false,
  299. SendError: false,
  300. Qos: 0,
  301. CheckpointInterval: 0,
  302. })
  303. errCh := make(chan error)
  304. outputCh := make(chan interface{}, 1)
  305. l.outputs["mock"] = outputCh
  306. l.Exec(ctx, errCh)
  307. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  308. for i, tt := range tests {
  309. select {
  310. case err := <-errCh:
  311. t.Error(err)
  312. return
  313. case l.input <- tt.input:
  314. case <-time.After(1 * time.Second):
  315. t.Error("send message timeout")
  316. return
  317. }
  318. select {
  319. case err := <-errCh:
  320. t.Error(err)
  321. return
  322. case output := <-outputCh:
  323. if !reflect.DeepEqual(tt.output, output) {
  324. t.Errorf("\ncase %d: expect %v but got %v", i, tt.output, output)
  325. }
  326. case <-time.After(1 * time.Second):
  327. t.Error("send message timeout")
  328. return
  329. }
  330. }
  331. }