manager_test.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. // Copyright 2021 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 memory
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "github.com/gdexlab/go-render/render"
  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/state"
  22. "github.com/lf-edge/ekuiper/pkg/api"
  23. "reflect"
  24. "regexp"
  25. "strings"
  26. "testing"
  27. "time"
  28. )
  29. func reset() {
  30. pubTopics = make(map[string]*pubConsumers)
  31. subExps = make(map[string]*subChan)
  32. }
  33. func TestSharedInmemoryNode(t *testing.T) {
  34. reset()
  35. id := "test_id"
  36. sinkProps := make(map[string]interface{})
  37. sinkProps[IdProperty] = id
  38. src := GetSource()
  39. snk := GetSink()
  40. contextLogger := conf.Log.WithField("rule", "test")
  41. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  42. consumer := make(chan api.SourceTuple)
  43. errorChannel := make(chan error)
  44. srcProps := make(map[string]interface{})
  45. srcProps["option"] = "value"
  46. err := snk.Configure(sinkProps)
  47. if err != nil {
  48. t.Error(err)
  49. return
  50. }
  51. err = snk.Open(ctx)
  52. if err != nil {
  53. t.Error(err)
  54. return
  55. }
  56. srcProps[IdProperty] = id
  57. err = src.Configure(id, srcProps)
  58. if err != nil {
  59. t.Error(err)
  60. }
  61. go func() {
  62. src.Open(ctx, consumer, errorChannel)
  63. }()
  64. if _, contains := pubTopics[id]; !contains {
  65. t.Errorf("there should be memory node for topic")
  66. }
  67. data := make(map[string]interface{})
  68. data["temperature"] = 33.0
  69. list := make([]map[string]interface{}, 0)
  70. list = append(list, data)
  71. go func() {
  72. err = snk.Collect(ctx, list)
  73. if err != nil {
  74. t.Error(err)
  75. }
  76. }()
  77. for {
  78. select {
  79. case res := <-consumer:
  80. expected := api.NewDefaultSourceTuple(data, make(map[string]interface{}))
  81. if !reflect.DeepEqual(expected, res) {
  82. t.Errorf("result %s should be equal to %s", res, expected)
  83. }
  84. return
  85. default:
  86. }
  87. }
  88. }
  89. func TestCreateAndClose(t *testing.T) {
  90. reset()
  91. var (
  92. sourceTopics = []string{"h/d1/c1/s2", "h/+/+/s1", "h/d3/#", "h/d1/c1/s2", "h/+/c1/s1"}
  93. sinkTopics = []string{"h/d1/c1/s1", "h/d1/c1/s2", "h/d2/c2/s1", "h/d3/c3/s1", "h/d1/c1/s1"}
  94. chans []chan map[string]interface{}
  95. )
  96. for i, topic := range sinkTopics {
  97. createPub(topic)
  98. var (
  99. r *regexp.Regexp
  100. err error
  101. )
  102. if strings.ContainsAny(sourceTopics[i], "+#") {
  103. r, err = getRegexp(sourceTopics[i])
  104. if err != nil {
  105. t.Error(err)
  106. return
  107. }
  108. }
  109. c := createSub(sourceTopics[i], r, fmt.Sprintf("%d", i))
  110. chans = append(chans, c)
  111. }
  112. expPub := map[string]*pubConsumers{
  113. "h/d1/c1/s1": {
  114. count: 2,
  115. consumers: map[string]chan map[string]interface{}{
  116. "1": chans[1],
  117. "4": chans[4],
  118. },
  119. },
  120. "h/d1/c1/s2": {
  121. count: 1,
  122. consumers: map[string]chan map[string]interface{}{
  123. "0": chans[0],
  124. "3": chans[3],
  125. },
  126. },
  127. "h/d2/c2/s1": {
  128. count: 1,
  129. consumers: map[string]chan map[string]interface{}{
  130. "1": chans[1],
  131. },
  132. },
  133. "h/d3/c3/s1": {
  134. count: 1,
  135. consumers: map[string]chan map[string]interface{}{
  136. "1": chans[1],
  137. "2": chans[2],
  138. },
  139. },
  140. }
  141. if !reflect.DeepEqual(expPub, pubTopics) {
  142. t.Errorf("Error adding: Expect\n\t%v\nbut got\n\t%v", render.AsCode(expPub), render.AsCode(pubTopics))
  143. return
  144. }
  145. i := 0
  146. for i < 3 {
  147. closeSourceConsumerChannel(sourceTopics[i], fmt.Sprintf("%d", i))
  148. closeSink(sinkTopics[i])
  149. i++
  150. }
  151. expPub = map[string]*pubConsumers{
  152. "h/d1/c1/s1": {
  153. count: 1,
  154. consumers: map[string]chan map[string]interface{}{
  155. "4": chans[4],
  156. },
  157. },
  158. "h/d1/c1/s2": {
  159. count: 0,
  160. consumers: map[string]chan map[string]interface{}{
  161. "3": chans[3],
  162. },
  163. },
  164. "h/d3/c3/s1": {
  165. count: 1,
  166. consumers: map[string]chan map[string]interface{}{},
  167. },
  168. }
  169. if !reflect.DeepEqual(expPub, pubTopics) {
  170. t.Errorf("Error closing: Expect\n\t%v\nbut got\n\t %v", render.AsCode(expPub), render.AsCode(pubTopics))
  171. }
  172. }
  173. func TestMultipleTopics(t *testing.T) {
  174. reset()
  175. var (
  176. sourceTopics = []string{"h/d1/c1/s2", "h/+/+/s1", "h/d3/#", "h/d1/c1/s2", "h/+/c1/s1"}
  177. sinkTopics = []string{"h/d1/c1/s1", "h/d1/c1/s2", "h/d2/c2/s1", "h/d3/c3/s1"}
  178. sinkData = [][]map[string]interface{}{
  179. {
  180. {
  181. "id": 1,
  182. "temp": 23,
  183. },
  184. {
  185. "id": 2,
  186. "temp": 34,
  187. },
  188. {
  189. "id": 3,
  190. "temp": 28,
  191. },
  192. }, {
  193. {
  194. "id": 4,
  195. "color": "red",
  196. },
  197. {
  198. "id": 5,
  199. "color": "red",
  200. },
  201. {
  202. "id": 6,
  203. "color": "green",
  204. },
  205. }, {
  206. {
  207. "id": 7,
  208. "hum": 67.5,
  209. },
  210. {
  211. "id": 8,
  212. "hum": 77.1,
  213. },
  214. {
  215. "id": 9,
  216. "hum": 90.3,
  217. },
  218. }, {
  219. {
  220. "id": 10,
  221. "status": "on",
  222. },
  223. {
  224. "id": 11,
  225. "status": "off",
  226. },
  227. {
  228. "id": 12,
  229. "status": "on",
  230. },
  231. },
  232. }
  233. expected = []api.SourceTuple{
  234. &api.DefaultSourceTuple{
  235. Mess: map[string]interface{}{
  236. "id": 1,
  237. "temp": 23,
  238. },
  239. M: make(map[string]interface{}),
  240. },
  241. &api.DefaultSourceTuple{
  242. Mess: map[string]interface{}{
  243. "id": 1,
  244. "temp": 23,
  245. },
  246. M: make(map[string]interface{}),
  247. },
  248. &api.DefaultSourceTuple{
  249. Mess: map[string]interface{}{
  250. "id": 4,
  251. "color": "red",
  252. },
  253. M: make(map[string]interface{}),
  254. },
  255. &api.DefaultSourceTuple{
  256. Mess: map[string]interface{}{
  257. "id": 4,
  258. "color": "red",
  259. },
  260. M: make(map[string]interface{}),
  261. },
  262. &api.DefaultSourceTuple{
  263. Mess: map[string]interface{}{
  264. "id": 7,
  265. "hum": 67.5,
  266. },
  267. M: make(map[string]interface{}),
  268. },
  269. &api.DefaultSourceTuple{
  270. Mess: map[string]interface{}{
  271. "id": 10,
  272. "status": "on",
  273. },
  274. M: make(map[string]interface{}),
  275. },
  276. &api.DefaultSourceTuple{
  277. Mess: map[string]interface{}{
  278. "id": 10,
  279. "status": "on",
  280. },
  281. M: make(map[string]interface{}),
  282. },
  283. &api.DefaultSourceTuple{
  284. Mess: map[string]interface{}{
  285. "id": 2,
  286. "temp": 34,
  287. },
  288. M: make(map[string]interface{}),
  289. },
  290. &api.DefaultSourceTuple{
  291. Mess: map[string]interface{}{
  292. "id": 2,
  293. "temp": 34,
  294. },
  295. M: make(map[string]interface{}),
  296. },
  297. &api.DefaultSourceTuple{
  298. Mess: map[string]interface{}{
  299. "id": 5,
  300. "color": "red",
  301. },
  302. M: make(map[string]interface{}),
  303. },
  304. &api.DefaultSourceTuple{
  305. Mess: map[string]interface{}{
  306. "id": 5,
  307. "color": "red",
  308. },
  309. M: make(map[string]interface{}),
  310. },
  311. &api.DefaultSourceTuple{
  312. Mess: map[string]interface{}{
  313. "id": 8,
  314. "hum": 77.1,
  315. },
  316. M: make(map[string]interface{}),
  317. },
  318. &api.DefaultSourceTuple{
  319. Mess: map[string]interface{}{
  320. "id": 11,
  321. "status": "off",
  322. },
  323. M: make(map[string]interface{}),
  324. },
  325. &api.DefaultSourceTuple{
  326. Mess: map[string]interface{}{
  327. "id": 11,
  328. "status": "off",
  329. },
  330. M: make(map[string]interface{}),
  331. },
  332. &api.DefaultSourceTuple{
  333. Mess: map[string]interface{}{
  334. "id": 3,
  335. "temp": 28,
  336. },
  337. M: make(map[string]interface{}),
  338. },
  339. &api.DefaultSourceTuple{
  340. Mess: map[string]interface{}{
  341. "id": 3,
  342. "temp": 28,
  343. },
  344. M: make(map[string]interface{}),
  345. },
  346. &api.DefaultSourceTuple{
  347. Mess: map[string]interface{}{
  348. "id": 6,
  349. "color": "green",
  350. },
  351. M: make(map[string]interface{}),
  352. },
  353. &api.DefaultSourceTuple{
  354. Mess: map[string]interface{}{
  355. "id": 6,
  356. "color": "green",
  357. },
  358. M: make(map[string]interface{}),
  359. },
  360. &api.DefaultSourceTuple{
  361. Mess: map[string]interface{}{
  362. "id": 9,
  363. "hum": 90.3,
  364. },
  365. M: make(map[string]interface{}),
  366. },
  367. &api.DefaultSourceTuple{
  368. Mess: map[string]interface{}{
  369. "id": 12,
  370. "status": "on",
  371. },
  372. M: make(map[string]interface{}),
  373. },
  374. &api.DefaultSourceTuple{
  375. Mess: map[string]interface{}{
  376. "id": 12,
  377. "status": "on",
  378. },
  379. M: make(map[string]interface{}),
  380. },
  381. }
  382. )
  383. contextLogger := conf.Log.WithField("rule", "test")
  384. ctx, cancel := context.WithValue(context.Background(), context.LoggerKey, contextLogger).WithCancel()
  385. consumer := make(chan api.SourceTuple)
  386. errorChannel := make(chan error)
  387. count := 0
  388. for _, topic := range sinkTopics {
  389. snk := GetSink()
  390. err := snk.Configure(map[string]interface{}{"topic": topic})
  391. if err != nil {
  392. t.Error(err)
  393. return
  394. }
  395. err = snk.Open(ctx)
  396. if err != nil {
  397. t.Error(err)
  398. return
  399. }
  400. src := GetSource()
  401. err = src.Configure(sourceTopics[count], make(map[string]interface{}))
  402. if err != nil {
  403. t.Error(err)
  404. return
  405. }
  406. go func(c int) {
  407. nc := ctx.WithMeta("rule1", fmt.Sprintf("op%d", c), &state.MemoryStore{})
  408. src.Open(nc, consumer, errorChannel)
  409. }(count)
  410. count++
  411. }
  412. for count < len(sourceTopics) {
  413. src := GetSource()
  414. err := src.Configure(sourceTopics[count], make(map[string]interface{}))
  415. if err != nil {
  416. t.Error(err)
  417. return
  418. }
  419. go func(c int) {
  420. nc := ctx.WithMeta("rule1", fmt.Sprintf("op%d", c), &state.MemoryStore{})
  421. src.Open(nc, consumer, errorChannel)
  422. }(count)
  423. count++
  424. }
  425. go func() {
  426. c := 0
  427. for c < 3 {
  428. for i, v := range sinkData {
  429. time.Sleep(10 * time.Millisecond)
  430. produce(ctx, sinkTopics[i], v[c])
  431. }
  432. c++
  433. }
  434. cancel()
  435. time.Sleep(100 * time.Millisecond)
  436. close(consumer)
  437. }()
  438. var results []api.SourceTuple
  439. for res := range consumer {
  440. results = append(results, res)
  441. }
  442. if !reflect.DeepEqual(expected, results) {
  443. t.Errorf("Expect\t %v\n but got\t\t\t %v", render.AsCode(expected), render.AsCode(results))
  444. }
  445. }
  446. func asJsonBytes(m []map[string]interface{}) ([]byte, error) {
  447. return json.Marshal(m)
  448. }