xsql_processor_test.go 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238
  1. package processors
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/emqx/kuiper/common"
  6. "github.com/emqx/kuiper/xsql"
  7. "github.com/emqx/kuiper/xstream"
  8. "github.com/emqx/kuiper/xstream/api"
  9. "github.com/emqx/kuiper/xstream/nodes"
  10. "github.com/emqx/kuiper/xstream/test"
  11. "path"
  12. "reflect"
  13. "strings"
  14. "testing"
  15. "time"
  16. )
  17. var DbDir = getDbDir()
  18. func getDbDir() string {
  19. dbDir, err := common.GetAndCreateDataLoc("test")
  20. if err != nil {
  21. log.Panic(err)
  22. }
  23. log.Infof("db location is %s", dbDir)
  24. return dbDir
  25. }
  26. func TestStreamCreateProcessor(t *testing.T) {
  27. var tests = []struct {
  28. s string
  29. r []string
  30. err string
  31. }{
  32. {
  33. s: `SHOW STREAMS;`,
  34. r: []string{"No stream definitions are found."},
  35. },
  36. {
  37. s: `EXPLAIN STREAM topic1;`,
  38. err: "Stream topic1 is not found.",
  39. },
  40. {
  41. s: `CREATE STREAM topic1 (
  42. USERID BIGINT,
  43. FIRST_NAME STRING,
  44. LAST_NAME STRING,
  45. NICKNAMES ARRAY(STRING),
  46. Gender BOOLEAN,
  47. ADDRESS STRUCT(STREET_NAME STRING, NUMBER BIGINT),
  48. ) WITH (DATASOURCE="users", FORMAT="AVRO", KEY="USERID");`,
  49. r: []string{"Stream topic1 is created."},
  50. },
  51. {
  52. s: `CREATE STREAM topic1 (
  53. USERID BIGINT,
  54. ) WITH (DATASOURCE="users", FORMAT="AVRO", KEY="USERID");`,
  55. err: "Create stream fails: Item topic1 already exists.",
  56. },
  57. {
  58. s: `EXPLAIN STREAM topic1;`,
  59. r: []string{"TO BE SUPPORTED"},
  60. },
  61. {
  62. s: `DESCRIBE STREAM topic1;`,
  63. r: []string{"Fields\n--------------------------------------------------------------------------------\nUSERID\tbigint\nFIRST_NAME\tstring\nLAST_NAME\tstring\nNICKNAMES\t" +
  64. "array(string)\nGender\tboolean\nADDRESS\tstruct(STREET_NAME string, NUMBER bigint)\n\n" +
  65. "DATASOURCE: users\nFORMAT: AVRO\nKEY: USERID\n"},
  66. },
  67. {
  68. s: `SHOW STREAMS;`,
  69. r: []string{"topic1"},
  70. },
  71. {
  72. s: `DROP STREAM topic1;`,
  73. r: []string{"Stream topic1 is dropped."},
  74. },
  75. {
  76. s: `DESCRIBE STREAM topic1;`,
  77. err: "Stream topic1 is not found.",
  78. },
  79. {
  80. s: `DROP STREAM topic1;`,
  81. err: "Drop stream fails: topic1 is not found.",
  82. },
  83. }
  84. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  85. streamDB := path.Join(getDbDir(), "streamTest")
  86. for i, tt := range tests {
  87. results, err := NewStreamProcessor(streamDB).ExecStmt(tt.s)
  88. if !reflect.DeepEqual(tt.err, errstring(err)) {
  89. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  90. } else if tt.err == "" {
  91. if !reflect.DeepEqual(tt.r, results) {
  92. t.Errorf("%d. %q\n\nstmt mismatch:\nexp=%s\ngot=%#v\n\n", i, tt.s, tt.r, results)
  93. }
  94. }
  95. }
  96. }
  97. func createStreams(t *testing.T) {
  98. p := NewStreamProcessor(path.Join(DbDir, "stream"))
  99. demo := `CREATE STREAM demo (
  100. color STRING,
  101. size BIGINT,
  102. ts BIGINT
  103. ) WITH (DATASOURCE="demo", FORMAT="json", KEY="ts");`
  104. _, err := p.ExecStmt(demo)
  105. if err != nil {
  106. t.Log(err)
  107. }
  108. demoE := `CREATE STREAM demoE (
  109. color STRING,
  110. size BIGINT,
  111. ts BIGINT
  112. ) WITH (DATASOURCE="demoE", FORMAT="json", KEY="ts");`
  113. _, err = p.ExecStmt(demoE)
  114. if err != nil {
  115. t.Log(err)
  116. }
  117. demo1 := `CREATE STREAM demo1 (
  118. temp FLOAT,
  119. hum BIGINT,
  120. ts BIGINT
  121. ) WITH (DATASOURCE="demo1", FORMAT="json", KEY="ts");`
  122. _, err = p.ExecStmt(demo1)
  123. if err != nil {
  124. t.Log(err)
  125. }
  126. sessionDemo := `CREATE STREAM sessionDemo (
  127. temp FLOAT,
  128. hum BIGINT,
  129. ts BIGINT
  130. ) WITH (DATASOURCE="sessionDemo", FORMAT="json", KEY="ts");`
  131. _, err = p.ExecStmt(sessionDemo)
  132. if err != nil {
  133. t.Log(err)
  134. }
  135. }
  136. func dropStreams(t *testing.T) {
  137. p := NewStreamProcessor(path.Join(DbDir, "stream"))
  138. demo := `DROP STREAM demo`
  139. _, err := p.ExecStmt(demo)
  140. if err != nil {
  141. t.Log(err)
  142. }
  143. demoE := `DROP STREAM demoE`
  144. _, err = p.ExecStmt(demoE)
  145. if err != nil {
  146. t.Log(err)
  147. }
  148. demo1 := `DROP STREAM demo1`
  149. _, err = p.ExecStmt(demo1)
  150. if err != nil {
  151. t.Log(err)
  152. }
  153. sessionDemo := `DROP STREAM sessionDemo`
  154. _, err = p.ExecStmt(sessionDemo)
  155. if err != nil {
  156. t.Log(err)
  157. }
  158. }
  159. func createSchemalessStreams(t *testing.T) {
  160. p := NewStreamProcessor(path.Join(DbDir, "stream"))
  161. demo := `CREATE STREAM ldemo (
  162. ) WITH (DATASOURCE="ldemo", FORMAT="json");`
  163. _, err := p.ExecStmt(demo)
  164. if err != nil {
  165. t.Log(err)
  166. }
  167. demo1 := `CREATE STREAM ldemo1 (
  168. ) WITH (DATASOURCE="ldemo1", FORMAT="json");`
  169. _, err = p.ExecStmt(demo1)
  170. if err != nil {
  171. t.Log(err)
  172. }
  173. sessionDemo := `CREATE STREAM lsessionDemo (
  174. ) WITH (DATASOURCE="lsessionDemo", FORMAT="json");`
  175. _, err = p.ExecStmt(sessionDemo)
  176. if err != nil {
  177. t.Log(err)
  178. }
  179. }
  180. func dropSchemalessStreams(t *testing.T) {
  181. p := NewStreamProcessor(path.Join(DbDir, "stream"))
  182. demo := `DROP STREAM ldemo`
  183. _, err := p.ExecStmt(demo)
  184. if err != nil {
  185. t.Log(err)
  186. }
  187. demo1 := `DROP STREAM ldemo1`
  188. _, err = p.ExecStmt(demo1)
  189. if err != nil {
  190. t.Log(err)
  191. }
  192. sessionDemo := `DROP STREAM lsessionDemo`
  193. _, err = p.ExecStmt(sessionDemo)
  194. if err != nil {
  195. t.Log(err)
  196. }
  197. }
  198. func getMockSource(name string, done <-chan int, size int) *nodes.SourceNode {
  199. var data []*xsql.Tuple
  200. switch name {
  201. case "demo":
  202. data = []*xsql.Tuple{
  203. {
  204. Emitter: name,
  205. Message: map[string]interface{}{
  206. "color": "red",
  207. "size": 3,
  208. "ts": 1541152486013,
  209. },
  210. Timestamp: 1541152486013,
  211. },
  212. {
  213. Emitter: name,
  214. Message: map[string]interface{}{
  215. "color": "blue",
  216. "size": 6,
  217. "ts": 1541152486822,
  218. },
  219. Timestamp: 1541152486822,
  220. },
  221. {
  222. Emitter: name,
  223. Message: map[string]interface{}{
  224. "color": "blue",
  225. "size": 2,
  226. "ts": 1541152487632,
  227. },
  228. Timestamp: 1541152487632,
  229. },
  230. {
  231. Emitter: name,
  232. Message: map[string]interface{}{
  233. "color": "yellow",
  234. "size": 4,
  235. "ts": 1541152488442,
  236. },
  237. Timestamp: 1541152488442,
  238. },
  239. {
  240. Emitter: name,
  241. Message: map[string]interface{}{
  242. "color": "red",
  243. "size": 1,
  244. "ts": 1541152489252,
  245. },
  246. Timestamp: 1541152489252,
  247. },
  248. }
  249. case "demoE":
  250. data = []*xsql.Tuple{
  251. {
  252. Emitter: name,
  253. Message: map[string]interface{}{
  254. "color": 3,
  255. "size": "red",
  256. "ts": 1541152486013,
  257. },
  258. Timestamp: 1541152486013,
  259. },
  260. {
  261. Emitter: name,
  262. Message: map[string]interface{}{
  263. "color": "blue",
  264. "size": 6,
  265. "ts": "1541152486822",
  266. },
  267. Timestamp: 1541152486822,
  268. },
  269. {
  270. Emitter: name,
  271. Message: map[string]interface{}{
  272. "color": "blue",
  273. "size": 2,
  274. "ts": 1541152487632,
  275. },
  276. Timestamp: 1541152487632,
  277. },
  278. {
  279. Emitter: name,
  280. Message: map[string]interface{}{
  281. "color": 7,
  282. "size": 4,
  283. "ts": 1541152488442,
  284. },
  285. Timestamp: 1541152488442,
  286. },
  287. {
  288. Emitter: name,
  289. Message: map[string]interface{}{
  290. "color": "red",
  291. "size": "blue",
  292. "ts": 1541152489252,
  293. },
  294. Timestamp: 1541152489252,
  295. },
  296. }
  297. case "demo1":
  298. data = []*xsql.Tuple{
  299. {
  300. Emitter: name,
  301. Message: map[string]interface{}{
  302. "temp": 25.5,
  303. "hum": 65,
  304. "ts": 1541152486013,
  305. },
  306. Timestamp: 1541152486013,
  307. },
  308. {
  309. Emitter: name,
  310. Message: map[string]interface{}{
  311. "temp": 27.5,
  312. "hum": 59,
  313. "ts": 1541152486823,
  314. },
  315. Timestamp: 1541152486823,
  316. },
  317. {
  318. Emitter: name,
  319. Message: map[string]interface{}{
  320. "temp": 28.1,
  321. "hum": 75,
  322. "ts": 1541152487632,
  323. },
  324. Timestamp: 1541152487632,
  325. },
  326. {
  327. Emitter: name,
  328. Message: map[string]interface{}{
  329. "temp": 27.4,
  330. "hum": 80,
  331. "ts": 1541152488442,
  332. },
  333. Timestamp: 1541152488442,
  334. },
  335. {
  336. Emitter: name,
  337. Message: map[string]interface{}{
  338. "temp": 25.5,
  339. "hum": 62,
  340. "ts": 1541152489252,
  341. },
  342. Timestamp: 1541152489252,
  343. },
  344. }
  345. case "sessionDemo":
  346. data = []*xsql.Tuple{
  347. {
  348. Emitter: name,
  349. Message: map[string]interface{}{
  350. "temp": 25.5,
  351. "hum": 65,
  352. "ts": 1541152486013,
  353. },
  354. Timestamp: 1541152486013,
  355. },
  356. {
  357. Emitter: name,
  358. Message: map[string]interface{}{
  359. "temp": 27.5,
  360. "hum": 59,
  361. "ts": 1541152486823,
  362. },
  363. Timestamp: 1541152486823,
  364. },
  365. {
  366. Emitter: name,
  367. Message: map[string]interface{}{
  368. "temp": 28.1,
  369. "hum": 75,
  370. "ts": 1541152487932,
  371. },
  372. Timestamp: 1541152487932,
  373. },
  374. {
  375. Emitter: name,
  376. Message: map[string]interface{}{
  377. "temp": 27.4,
  378. "hum": 80,
  379. "ts": 1541152488442,
  380. },
  381. Timestamp: 1541152488442,
  382. },
  383. {
  384. Emitter: name,
  385. Message: map[string]interface{}{
  386. "temp": 25.5,
  387. "hum": 62,
  388. "ts": 1541152489252,
  389. },
  390. Timestamp: 1541152489252,
  391. },
  392. {
  393. Emitter: name,
  394. Message: map[string]interface{}{
  395. "temp": 26.2,
  396. "hum": 63,
  397. "ts": 1541152490062,
  398. },
  399. Timestamp: 1541152490062,
  400. },
  401. {
  402. Emitter: name,
  403. Message: map[string]interface{}{
  404. "temp": 26.8,
  405. "hum": 71,
  406. "ts": 1541152490872,
  407. },
  408. Timestamp: 1541152490872,
  409. },
  410. {
  411. Emitter: name,
  412. Message: map[string]interface{}{
  413. "temp": 28.9,
  414. "hum": 85,
  415. "ts": 1541152491682,
  416. },
  417. Timestamp: 1541152491682,
  418. },
  419. {
  420. Emitter: name,
  421. Message: map[string]interface{}{
  422. "temp": 29.1,
  423. "hum": 92,
  424. "ts": 1541152492492,
  425. },
  426. Timestamp: 1541152492492,
  427. },
  428. {
  429. Emitter: name,
  430. Message: map[string]interface{}{
  431. "temp": 32.2,
  432. "hum": 99,
  433. "ts": 1541152493202,
  434. },
  435. Timestamp: 1541152493202,
  436. },
  437. {
  438. Emitter: name,
  439. Message: map[string]interface{}{
  440. "temp": 30.9,
  441. "hum": 87,
  442. "ts": 1541152494112,
  443. },
  444. Timestamp: 1541152494112,
  445. },
  446. }
  447. }
  448. return nodes.NewSourceNodeWithSource(name, test.NewMockSource(data[:size], done, false), map[string]string{
  449. "DATASOURCE": name,
  450. })
  451. }
  452. func TestSingleSQL(t *testing.T) {
  453. var tests = []struct {
  454. name string
  455. sql string
  456. r [][]map[string]interface{}
  457. s string
  458. m map[string]interface{}
  459. }{
  460. {
  461. name: `rule1`,
  462. sql: `SELECT * FROM demo`,
  463. r: [][]map[string]interface{}{
  464. {{
  465. "color": "red",
  466. "size": float64(3),
  467. "ts": float64(1541152486013),
  468. }},
  469. {{
  470. "color": "blue",
  471. "size": float64(6),
  472. "ts": float64(1541152486822),
  473. }},
  474. {{
  475. "color": "blue",
  476. "size": float64(2),
  477. "ts": float64(1541152487632),
  478. }},
  479. {{
  480. "color": "yellow",
  481. "size": float64(4),
  482. "ts": float64(1541152488442),
  483. }},
  484. {{
  485. "color": "red",
  486. "size": float64(1),
  487. "ts": float64(1541152489252),
  488. }},
  489. },
  490. m: map[string]interface{}{
  491. "op_preprocessor_demo_0_exceptions_total": int64(0),
  492. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  493. "op_preprocessor_demo_0_records_in_total": int64(5),
  494. "op_preprocessor_demo_0_records_out_total": int64(5),
  495. "op_project_0_exceptions_total": int64(0),
  496. "op_project_0_process_latency_ms": int64(0),
  497. "op_project_0_records_in_total": int64(5),
  498. "op_project_0_records_out_total": int64(5),
  499. "sink_mockSink_0_exceptions_total": int64(0),
  500. "sink_mockSink_0_records_in_total": int64(5),
  501. "sink_mockSink_0_records_out_total": int64(5),
  502. "source_demo_0_exceptions_total": int64(0),
  503. "source_demo_0_records_in_total": int64(5),
  504. "source_demo_0_records_out_total": int64(5),
  505. },
  506. s: "sink_mockSink_0_records_out_total",
  507. }, {
  508. name: `rule2`,
  509. sql: `SELECT color, ts FROM demo where size > 3`,
  510. r: [][]map[string]interface{}{
  511. {{
  512. "color": "blue",
  513. "ts": float64(1541152486822),
  514. }},
  515. {{
  516. "color": "yellow",
  517. "ts": float64(1541152488442),
  518. }},
  519. },
  520. s: "op_filter_0_records_in_total",
  521. m: map[string]interface{}{
  522. "op_preprocessor_demo_0_exceptions_total": int64(0),
  523. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  524. "op_preprocessor_demo_0_records_in_total": int64(5),
  525. "op_preprocessor_demo_0_records_out_total": int64(5),
  526. "op_project_0_exceptions_total": int64(0),
  527. "op_project_0_process_latency_ms": int64(0),
  528. "op_project_0_records_in_total": int64(2),
  529. "op_project_0_records_out_total": int64(2),
  530. "sink_mockSink_0_exceptions_total": int64(0),
  531. "sink_mockSink_0_records_in_total": int64(2),
  532. "sink_mockSink_0_records_out_total": int64(2),
  533. "source_demo_0_exceptions_total": int64(0),
  534. "source_demo_0_records_in_total": int64(5),
  535. "source_demo_0_records_out_total": int64(5),
  536. "op_filter_0_exceptions_total": int64(0),
  537. "op_filter_0_process_latency_ms": int64(0),
  538. "op_filter_0_records_in_total": int64(5),
  539. "op_filter_0_records_out_total": int64(2),
  540. },
  541. }, {
  542. name: `rule3`,
  543. sql: `SELECT size as Int8, ts FROM demo where size > 3`,
  544. r: [][]map[string]interface{}{
  545. {{
  546. "Int8": float64(6),
  547. "ts": float64(1541152486822),
  548. }},
  549. {{
  550. "Int8": float64(4),
  551. "ts": float64(1541152488442),
  552. }},
  553. },
  554. s: "op_filter_0_records_in_total",
  555. m: map[string]interface{}{
  556. "op_preprocessor_demo_0_exceptions_total": int64(0),
  557. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  558. "op_preprocessor_demo_0_records_in_total": int64(5),
  559. "op_preprocessor_demo_0_records_out_total": int64(5),
  560. "op_project_0_exceptions_total": int64(0),
  561. "op_project_0_process_latency_ms": int64(0),
  562. "op_project_0_records_in_total": int64(2),
  563. "op_project_0_records_out_total": int64(2),
  564. "sink_mockSink_0_exceptions_total": int64(0),
  565. "sink_mockSink_0_records_in_total": int64(2),
  566. "sink_mockSink_0_records_out_total": int64(2),
  567. "source_demo_0_exceptions_total": int64(0),
  568. "source_demo_0_records_in_total": int64(5),
  569. "source_demo_0_records_out_total": int64(5),
  570. "op_filter_0_exceptions_total": int64(0),
  571. "op_filter_0_process_latency_ms": int64(0),
  572. "op_filter_0_records_in_total": int64(5),
  573. "op_filter_0_records_out_total": int64(2),
  574. },
  575. }, {
  576. name: `rule4`,
  577. sql: `SELECT size as Int8, ts FROM demoE where size > 3`,
  578. r: [][]map[string]interface{}{
  579. {{
  580. "error": "error in preprocessor: invalid data type for color, expect string but found int(3)",
  581. }},
  582. {{
  583. "Int8": float64(6),
  584. "ts": float64(1541152486822),
  585. }},
  586. {{
  587. "error": "error in preprocessor: invalid data type for color, expect string but found int(7)",
  588. }},
  589. {{
  590. "error": "error in preprocessor: invalid data type for size, expect bigint but found string(blue)",
  591. }},
  592. },
  593. s: "op_filter_0_records_in_total",
  594. m: map[string]interface{}{
  595. "op_preprocessor_demoE_0_exceptions_total": int64(3),
  596. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  597. "op_preprocessor_demoE_0_records_in_total": int64(5),
  598. "op_preprocessor_demoE_0_records_out_total": int64(2),
  599. "op_project_0_exceptions_total": int64(3),
  600. "op_project_0_process_latency_ms": int64(0),
  601. "op_project_0_records_in_total": int64(4),
  602. "op_project_0_records_out_total": int64(1),
  603. "sink_mockSink_0_exceptions_total": int64(0),
  604. "sink_mockSink_0_records_in_total": int64(4),
  605. "sink_mockSink_0_records_out_total": int64(4),
  606. "source_demoE_0_exceptions_total": int64(0),
  607. "source_demoE_0_records_in_total": int64(5),
  608. "source_demoE_0_records_out_total": int64(5),
  609. "op_filter_0_exceptions_total": int64(3),
  610. "op_filter_0_process_latency_ms": int64(0),
  611. "op_filter_0_records_in_total": int64(5),
  612. "op_filter_0_records_out_total": int64(1),
  613. },
  614. }, {
  615. name: `rule4`,
  616. sql: `SELECT size as Int8, ts FROM demoE where size > 3`,
  617. r: [][]map[string]interface{}{
  618. {{
  619. "error": "error in preprocessor: invalid data type for color, expect string but found int(3)",
  620. }},
  621. {{
  622. "Int8": float64(6),
  623. "ts": float64(1541152486822),
  624. }},
  625. {{
  626. "error": "error in preprocessor: invalid data type for color, expect string but found int(7)",
  627. }},
  628. {{
  629. "error": "error in preprocessor: invalid data type for size, expect bigint but found string(blue)",
  630. }},
  631. },
  632. s: "op_filter_0_records_in_total",
  633. m: map[string]interface{}{
  634. "op_preprocessor_demoE_0_exceptions_total": int64(3),
  635. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  636. "op_preprocessor_demoE_0_records_in_total": int64(5),
  637. "op_preprocessor_demoE_0_records_out_total": int64(2),
  638. "op_project_0_exceptions_total": int64(3),
  639. "op_project_0_process_latency_ms": int64(0),
  640. "op_project_0_records_in_total": int64(4),
  641. "op_project_0_records_out_total": int64(1),
  642. "sink_mockSink_0_exceptions_total": int64(0),
  643. "sink_mockSink_0_records_in_total": int64(4),
  644. "sink_mockSink_0_records_out_total": int64(4),
  645. "source_demoE_0_exceptions_total": int64(0),
  646. "source_demoE_0_records_in_total": int64(5),
  647. "source_demoE_0_records_out_total": int64(5),
  648. "op_filter_0_exceptions_total": int64(3),
  649. "op_filter_0_process_latency_ms": int64(0),
  650. "op_filter_0_records_in_total": int64(5),
  651. "op_filter_0_records_out_total": int64(1),
  652. },
  653. }, {
  654. name: `rule5`,
  655. sql: `SELECT meta(topic) as m, ts FROM demo`,
  656. r: [][]map[string]interface{}{
  657. {{
  658. "m": "mock",
  659. "ts": float64(1541152486013),
  660. }},
  661. {{
  662. "m": "mock",
  663. "ts": float64(1541152486822),
  664. }},
  665. {{
  666. "m": "mock",
  667. "ts": float64(1541152487632),
  668. }},
  669. {{
  670. "m": "mock",
  671. "ts": float64(1541152488442),
  672. }},
  673. {{
  674. "m": "mock",
  675. "ts": float64(1541152489252),
  676. }},
  677. },
  678. m: map[string]interface{}{
  679. "op_preprocessor_demo_0_exceptions_total": int64(0),
  680. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  681. "op_preprocessor_demo_0_records_in_total": int64(5),
  682. "op_preprocessor_demo_0_records_out_total": int64(5),
  683. "op_project_0_exceptions_total": int64(0),
  684. "op_project_0_process_latency_ms": int64(0),
  685. "op_project_0_records_in_total": int64(5),
  686. "op_project_0_records_out_total": int64(5),
  687. "sink_mockSink_0_exceptions_total": int64(0),
  688. "sink_mockSink_0_records_in_total": int64(5),
  689. "sink_mockSink_0_records_out_total": int64(5),
  690. "source_demo_0_exceptions_total": int64(0),
  691. "source_demo_0_records_in_total": int64(5),
  692. "source_demo_0_records_out_total": int64(5),
  693. },
  694. s: "sink_mockSink_0_records_out_total",
  695. }, {
  696. name: `rule6`,
  697. sql: `SELECT color, ts FROM demo where size > 3 and meta(topic)="mock"`,
  698. r: [][]map[string]interface{}{
  699. {{
  700. "color": "blue",
  701. "ts": float64(1541152486822),
  702. }},
  703. {{
  704. "color": "yellow",
  705. "ts": float64(1541152488442),
  706. }},
  707. },
  708. s: "op_filter_0_records_in_total",
  709. m: map[string]interface{}{
  710. "op_preprocessor_demo_0_exceptions_total": int64(0),
  711. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  712. "op_preprocessor_demo_0_records_in_total": int64(5),
  713. "op_preprocessor_demo_0_records_out_total": int64(5),
  714. "op_project_0_exceptions_total": int64(0),
  715. "op_project_0_process_latency_ms": int64(0),
  716. "op_project_0_records_in_total": int64(2),
  717. "op_project_0_records_out_total": int64(2),
  718. "sink_mockSink_0_exceptions_total": int64(0),
  719. "sink_mockSink_0_records_in_total": int64(2),
  720. "sink_mockSink_0_records_out_total": int64(2),
  721. "source_demo_0_exceptions_total": int64(0),
  722. "source_demo_0_records_in_total": int64(5),
  723. "source_demo_0_records_out_total": int64(5),
  724. "op_filter_0_exceptions_total": int64(0),
  725. "op_filter_0_process_latency_ms": int64(0),
  726. "op_filter_0_records_in_total": int64(5),
  727. "op_filter_0_records_out_total": int64(2),
  728. },
  729. },
  730. }
  731. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  732. createStreams(t)
  733. defer dropStreams(t)
  734. //defer close(done)
  735. for i, tt := range tests {
  736. test.ResetClock(1541152486000)
  737. p := NewRuleProcessor(DbDir)
  738. parser := xsql.NewParser(strings.NewReader(tt.sql))
  739. var (
  740. sources []*nodes.SourceNode
  741. syncs []chan int
  742. )
  743. if stmt, err := xsql.Language.Parse(parser); err != nil {
  744. t.Errorf("parse sql %s error: %s", tt.sql, err)
  745. } else {
  746. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  747. t.Errorf("sql %s is not a select statement", tt.sql)
  748. } else {
  749. streams := xsql.GetStreams(selectStmt)
  750. for _, stream := range streams {
  751. next := make(chan int)
  752. syncs = append(syncs, next)
  753. source := getMockSource(stream, next, 5)
  754. sources = append(sources, source)
  755. }
  756. }
  757. }
  758. tp, inputs, err := p.createTopoWithSources(&api.Rule{Id: tt.name, Sql: tt.sql, Options: map[string]interface{}{
  759. "bufferLength": float64(100),
  760. }}, sources)
  761. if err != nil {
  762. t.Error(err)
  763. }
  764. mockSink := test.NewMockSink()
  765. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink, nil)
  766. tp.AddSink(inputs, sink)
  767. errCh := tp.Open()
  768. func() {
  769. for i := 0; i < 5; i++ {
  770. syncs[i%len(syncs)] <- i
  771. select {
  772. case err = <-errCh:
  773. t.Log(err)
  774. tp.Cancel()
  775. return
  776. default:
  777. }
  778. }
  779. for retry := 100; retry > 0; retry-- {
  780. if err := compareMetrics(tp, tt.m, tt.sql); err == nil {
  781. break
  782. }
  783. time.Sleep(time.Duration(retry) * time.Millisecond)
  784. }
  785. }()
  786. results := mockSink.GetResults()
  787. var maps [][]map[string]interface{}
  788. for _, v := range results {
  789. var mapRes []map[string]interface{}
  790. err := json.Unmarshal(v, &mapRes)
  791. if err != nil {
  792. t.Errorf("Failed to parse the input into map")
  793. continue
  794. }
  795. maps = append(maps, mapRes)
  796. }
  797. if !reflect.DeepEqual(tt.r, maps) {
  798. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, maps)
  799. continue
  800. }
  801. if err := compareMetrics(tp, tt.m, tt.sql); err != nil {
  802. t.Errorf("%d. %q\n\n%v", i, tt.sql, err)
  803. }
  804. tp.Cancel()
  805. }
  806. }
  807. func TestSingleSQLTemplate(t *testing.T) {
  808. var tests = []struct {
  809. name string
  810. sql string
  811. r []map[string]interface{}
  812. s string
  813. m map[string]interface{}
  814. }{
  815. {
  816. name: `rule1`,
  817. sql: `SELECT * FROM demo`,
  818. r: []map[string]interface{}{
  819. {
  820. "c": "red",
  821. "wrapper": "w1",
  822. },
  823. {
  824. "c": "blue",
  825. "wrapper": "w1",
  826. },
  827. {
  828. "c": "blue",
  829. "wrapper": "w1",
  830. },
  831. {
  832. "c": "yellow",
  833. "wrapper": "w1",
  834. },
  835. {
  836. "c": "red",
  837. "wrapper": "w1",
  838. },
  839. },
  840. m: map[string]interface{}{
  841. "op_preprocessor_demo_0_exceptions_total": int64(0),
  842. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  843. "op_preprocessor_demo_0_records_in_total": int64(5),
  844. "op_preprocessor_demo_0_records_out_total": int64(5),
  845. "op_project_0_exceptions_total": int64(0),
  846. "op_project_0_process_latency_ms": int64(0),
  847. "op_project_0_records_in_total": int64(5),
  848. "op_project_0_records_out_total": int64(5),
  849. "sink_mockSink_0_exceptions_total": int64(0),
  850. "sink_mockSink_0_records_in_total": int64(5),
  851. "sink_mockSink_0_records_out_total": int64(5),
  852. "source_demo_0_exceptions_total": int64(0),
  853. "source_demo_0_records_in_total": int64(5),
  854. "source_demo_0_records_out_total": int64(5),
  855. },
  856. s: "sink_mockSink_0_records_out_total",
  857. },
  858. }
  859. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  860. createStreams(t)
  861. defer dropStreams(t)
  862. //defer close(done)
  863. for i, tt := range tests {
  864. test.ResetClock(1541152486000)
  865. p := NewRuleProcessor(DbDir)
  866. parser := xsql.NewParser(strings.NewReader(tt.sql))
  867. var (
  868. sources []*nodes.SourceNode
  869. syncs []chan int
  870. )
  871. if stmt, err := xsql.Language.Parse(parser); err != nil {
  872. t.Errorf("parse sql %s error: %s", tt.sql, err)
  873. } else {
  874. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  875. t.Errorf("sql %s is not a select statement", tt.sql)
  876. } else {
  877. streams := xsql.GetStreams(selectStmt)
  878. for _, stream := range streams {
  879. next := make(chan int)
  880. syncs = append(syncs, next)
  881. source := getMockSource(stream, next, 5)
  882. sources = append(sources, source)
  883. }
  884. }
  885. }
  886. tp, inputs, err := p.createTopoWithSources(&api.Rule{Id: tt.name, Sql: tt.sql, Options: map[string]interface{}{
  887. "bufferLength": float64(100),
  888. }}, sources)
  889. if err != nil {
  890. t.Error(err)
  891. }
  892. mockSink := test.NewMockSink()
  893. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink, map[string]interface{}{
  894. "dataTemplate": `{"wrapper":"w1", "c":"{{.color}}"}`,
  895. "sendSingle": true,
  896. })
  897. tp.AddSink(inputs, sink)
  898. errCh := tp.Open()
  899. func() {
  900. for i := 0; i < 5; i++ {
  901. syncs[i%len(syncs)] <- i
  902. select {
  903. case err = <-errCh:
  904. t.Log(err)
  905. tp.Cancel()
  906. return
  907. default:
  908. }
  909. }
  910. for retry := 100; retry > 0; retry-- {
  911. if err := compareMetrics(tp, tt.m, tt.sql); err == nil {
  912. break
  913. }
  914. time.Sleep(time.Duration(retry) * time.Millisecond)
  915. }
  916. }()
  917. results := mockSink.GetResults()
  918. var maps []map[string]interface{}
  919. for _, v := range results {
  920. var mapRes map[string]interface{}
  921. err := json.Unmarshal(v, &mapRes)
  922. if err != nil {
  923. t.Errorf("Failed to parse the input into map")
  924. continue
  925. }
  926. maps = append(maps, mapRes)
  927. }
  928. if !reflect.DeepEqual(tt.r, maps) {
  929. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, maps)
  930. continue
  931. }
  932. if err := compareMetrics(tp, tt.m, tt.sql); err != nil {
  933. t.Errorf("%d. %q\n\n%v", i, tt.sql, err)
  934. }
  935. tp.Cancel()
  936. }
  937. }
  938. func TestNoneSingleSQLTemplate(t *testing.T) {
  939. var tests = []struct {
  940. name string
  941. sql string
  942. r [][]byte
  943. s string
  944. m map[string]interface{}
  945. }{
  946. {
  947. name: `rule1`,
  948. sql: `SELECT * FROM demo`,
  949. r: [][]byte{
  950. []byte("<div>results</div><ul><li>red - 3</li></ul>"),
  951. []byte("<div>results</div><ul><li>blue - 6</li></ul>"),
  952. []byte("<div>results</div><ul><li>blue - 2</li></ul>"),
  953. []byte("<div>results</div><ul><li>yellow - 4</li></ul>"),
  954. []byte("<div>results</div><ul><li>red - 1</li></ul>"),
  955. },
  956. m: map[string]interface{}{
  957. "op_preprocessor_demo_0_exceptions_total": int64(0),
  958. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  959. "op_preprocessor_demo_0_records_in_total": int64(5),
  960. "op_preprocessor_demo_0_records_out_total": int64(5),
  961. "op_project_0_exceptions_total": int64(0),
  962. "op_project_0_process_latency_ms": int64(0),
  963. "op_project_0_records_in_total": int64(5),
  964. "op_project_0_records_out_total": int64(5),
  965. "sink_mockSink_0_exceptions_total": int64(0),
  966. "sink_mockSink_0_records_in_total": int64(5),
  967. "sink_mockSink_0_records_out_total": int64(5),
  968. "source_demo_0_exceptions_total": int64(0),
  969. "source_demo_0_records_in_total": int64(5),
  970. "source_demo_0_records_out_total": int64(5),
  971. },
  972. s: "sink_mockSink_0_records_out_total",
  973. },
  974. }
  975. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  976. createStreams(t)
  977. defer dropStreams(t)
  978. //defer close(done)
  979. for i, tt := range tests {
  980. test.ResetClock(1541152486000)
  981. p := NewRuleProcessor(DbDir)
  982. parser := xsql.NewParser(strings.NewReader(tt.sql))
  983. var (
  984. sources []*nodes.SourceNode
  985. syncs []chan int
  986. )
  987. if stmt, err := xsql.Language.Parse(parser); err != nil {
  988. t.Errorf("parse sql %s error: %s", tt.sql, err)
  989. } else {
  990. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  991. t.Errorf("sql %s is not a select statement", tt.sql)
  992. } else {
  993. streams := xsql.GetStreams(selectStmt)
  994. for _, stream := range streams {
  995. next := make(chan int)
  996. syncs = append(syncs, next)
  997. source := getMockSource(stream, next, 5)
  998. sources = append(sources, source)
  999. }
  1000. }
  1001. }
  1002. tp, inputs, err := p.createTopoWithSources(&api.Rule{Id: tt.name, Sql: tt.sql, Options: map[string]interface{}{
  1003. "bufferLength": float64(100),
  1004. }}, sources)
  1005. if err != nil {
  1006. t.Error(err)
  1007. }
  1008. mockSink := test.NewMockSink()
  1009. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink, map[string]interface{}{
  1010. "dataTemplate": `<div>results</div><ul>{{range .}}<li>{{.color}} - {{.size}}</li>{{end}}</ul>`,
  1011. })
  1012. tp.AddSink(inputs, sink)
  1013. errCh := tp.Open()
  1014. func() {
  1015. for i := 0; i < 5; i++ {
  1016. syncs[i%len(syncs)] <- i
  1017. select {
  1018. case err = <-errCh:
  1019. t.Log(err)
  1020. tp.Cancel()
  1021. return
  1022. default:
  1023. }
  1024. }
  1025. for retry := 100; retry > 0; retry-- {
  1026. if err := compareMetrics(tp, tt.m, tt.sql); err == nil {
  1027. break
  1028. }
  1029. time.Sleep(time.Duration(retry) * time.Millisecond)
  1030. }
  1031. }()
  1032. results := mockSink.GetResults()
  1033. if !reflect.DeepEqual(tt.r, results) {
  1034. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, results)
  1035. continue
  1036. }
  1037. if err := compareMetrics(tp, tt.m, tt.sql); err != nil {
  1038. t.Errorf("%d. %q\n\n%v", i, tt.sql, err)
  1039. }
  1040. tp.Cancel()
  1041. }
  1042. }
  1043. func getMockSourceL(name string, done <-chan int, size int) *nodes.SourceNode {
  1044. var data []*xsql.Tuple
  1045. switch name {
  1046. case "ldemo":
  1047. data = []*xsql.Tuple{
  1048. {
  1049. Emitter: name,
  1050. Message: map[string]interface{}{
  1051. "color": "red",
  1052. "size": 3,
  1053. "ts": 1541152486013,
  1054. },
  1055. Timestamp: 1541152486013,
  1056. },
  1057. {
  1058. Emitter: name,
  1059. Message: map[string]interface{}{
  1060. "color": "blue",
  1061. "size": "string",
  1062. "ts": 1541152486822,
  1063. },
  1064. Timestamp: 1541152486822,
  1065. },
  1066. {
  1067. Emitter: name,
  1068. Message: map[string]interface{}{
  1069. "size": 3,
  1070. "ts": 1541152487632,
  1071. },
  1072. Timestamp: 1541152487632,
  1073. },
  1074. {
  1075. Emitter: name,
  1076. Message: map[string]interface{}{
  1077. "color": 49,
  1078. "size": 2,
  1079. "ts": 1541152488442,
  1080. },
  1081. Timestamp: 1541152488442,
  1082. },
  1083. {
  1084. Emitter: name,
  1085. Message: map[string]interface{}{
  1086. "color": "red",
  1087. "ts": 1541152489252,
  1088. },
  1089. Timestamp: 1541152489252,
  1090. },
  1091. }
  1092. case "ldemo1":
  1093. data = []*xsql.Tuple{
  1094. {
  1095. Emitter: name,
  1096. Message: map[string]interface{}{
  1097. "temp": 25.5,
  1098. "hum": 65,
  1099. "ts": 1541152486013,
  1100. },
  1101. Timestamp: 1541152486013,
  1102. },
  1103. {
  1104. Emitter: name,
  1105. Message: map[string]interface{}{
  1106. "temp": 27.5,
  1107. "hum": 59,
  1108. "ts": 1541152486823,
  1109. },
  1110. Timestamp: 1541152486823,
  1111. },
  1112. {
  1113. Emitter: name,
  1114. Message: map[string]interface{}{
  1115. "temp": 28.1,
  1116. "hum": 75,
  1117. "ts": 1541152487632,
  1118. },
  1119. Timestamp: 1541152487632,
  1120. },
  1121. {
  1122. Emitter: name,
  1123. Message: map[string]interface{}{
  1124. "temp": 27.4,
  1125. "hum": 80,
  1126. "ts": "1541152488442",
  1127. },
  1128. Timestamp: 1541152488442,
  1129. },
  1130. {
  1131. Emitter: name,
  1132. Message: map[string]interface{}{
  1133. "temp": 25.5,
  1134. "hum": 62,
  1135. "ts": 1541152489252,
  1136. },
  1137. Timestamp: 1541152489252,
  1138. },
  1139. }
  1140. case "lsessionDemo":
  1141. data = []*xsql.Tuple{
  1142. {
  1143. Emitter: name,
  1144. Message: map[string]interface{}{
  1145. "temp": 25.5,
  1146. "hum": 65,
  1147. "ts": 1541152486013,
  1148. },
  1149. Timestamp: 1541152486013,
  1150. },
  1151. {
  1152. Emitter: name,
  1153. Message: map[string]interface{}{
  1154. "temp": 27.5,
  1155. "hum": 59,
  1156. "ts": 1541152486823,
  1157. },
  1158. Timestamp: 1541152486823,
  1159. },
  1160. {
  1161. Emitter: name,
  1162. Message: map[string]interface{}{
  1163. "temp": 28.1,
  1164. "hum": 75,
  1165. "ts": 1541152487932,
  1166. },
  1167. Timestamp: 1541152487932,
  1168. },
  1169. {
  1170. Emitter: name,
  1171. Message: map[string]interface{}{
  1172. "temp": 27.4,
  1173. "hum": 80,
  1174. "ts": 1541152488442,
  1175. },
  1176. Timestamp: 1541152488442,
  1177. },
  1178. {
  1179. Emitter: name,
  1180. Message: map[string]interface{}{
  1181. "temp": 25.5,
  1182. "hum": 62,
  1183. "ts": 1541152489252,
  1184. },
  1185. Timestamp: 1541152489252,
  1186. },
  1187. {
  1188. Emitter: name,
  1189. Message: map[string]interface{}{
  1190. "temp": 26.2,
  1191. "hum": 63,
  1192. "ts": 1541152490062,
  1193. },
  1194. Timestamp: 1541152490062,
  1195. },
  1196. {
  1197. Emitter: name,
  1198. Message: map[string]interface{}{
  1199. "temp": 26.8,
  1200. "hum": 71,
  1201. "ts": 1541152490872,
  1202. },
  1203. Timestamp: 1541152490872,
  1204. },
  1205. {
  1206. Emitter: name,
  1207. Message: map[string]interface{}{
  1208. "temp": 28.9,
  1209. "hum": 85,
  1210. "ts": 1541152491682,
  1211. },
  1212. Timestamp: 1541152491682,
  1213. },
  1214. {
  1215. Emitter: name,
  1216. Message: map[string]interface{}{
  1217. "temp": 29.1,
  1218. "hum": 92,
  1219. "ts": 1541152492492,
  1220. },
  1221. Timestamp: 1541152492492,
  1222. },
  1223. {
  1224. Emitter: name,
  1225. Message: map[string]interface{}{
  1226. "temp": 2.2,
  1227. "hum": 99,
  1228. "ts": 1541152493202,
  1229. },
  1230. Timestamp: 1541152493202,
  1231. },
  1232. {
  1233. Emitter: name,
  1234. Message: map[string]interface{}{
  1235. "temp": 30.9,
  1236. "hum": 87,
  1237. "ts": 1541152494112,
  1238. },
  1239. Timestamp: 1541152494112,
  1240. },
  1241. }
  1242. }
  1243. return nodes.NewSourceNodeWithSource(name, test.NewMockSource(data[:size], done, false), map[string]string{
  1244. "DATASOURCE": name,
  1245. })
  1246. }
  1247. func TestSingleSQLError(t *testing.T) {
  1248. var tests = []struct {
  1249. name string
  1250. sql string
  1251. r [][]map[string]interface{}
  1252. s string
  1253. m map[string]interface{}
  1254. }{
  1255. {
  1256. name: `rule1`,
  1257. sql: `SELECT color, ts FROM ldemo where size >= 3`,
  1258. r: [][]map[string]interface{}{
  1259. {{
  1260. "color": "red",
  1261. "ts": float64(1541152486013),
  1262. }},
  1263. {{
  1264. "error": "run Where error: invalid operation string(string) >= int64(3)",
  1265. }},
  1266. {{
  1267. "ts": float64(1541152487632),
  1268. }},
  1269. },
  1270. s: "op_filter_0_records_in_total",
  1271. m: map[string]interface{}{
  1272. "op_preprocessor_ldemo_0_exceptions_total": int64(0),
  1273. "op_preprocessor_ldemo_0_process_latency_ms": int64(0),
  1274. "op_preprocessor_ldemo_0_records_in_total": int64(5),
  1275. "op_preprocessor_ldemo_0_records_out_total": int64(5),
  1276. "op_project_0_exceptions_total": int64(1),
  1277. "op_project_0_process_latency_ms": int64(0),
  1278. "op_project_0_records_in_total": int64(3),
  1279. "op_project_0_records_out_total": int64(2),
  1280. "sink_mockSink_0_exceptions_total": int64(0),
  1281. "sink_mockSink_0_records_in_total": int64(3),
  1282. "sink_mockSink_0_records_out_total": int64(3),
  1283. "source_ldemo_0_exceptions_total": int64(0),
  1284. "source_ldemo_0_records_in_total": int64(5),
  1285. "source_ldemo_0_records_out_total": int64(5),
  1286. "op_filter_0_exceptions_total": int64(1),
  1287. "op_filter_0_process_latency_ms": int64(0),
  1288. "op_filter_0_records_in_total": int64(5),
  1289. "op_filter_0_records_out_total": int64(2),
  1290. },
  1291. }, {
  1292. name: `rule2`,
  1293. sql: `SELECT size * 5 FROM ldemo`,
  1294. r: [][]map[string]interface{}{
  1295. {{
  1296. "rengine_field_0": float64(15),
  1297. }},
  1298. {{
  1299. "error": "run Select error: invalid operation string(string) * int64(5)",
  1300. }},
  1301. {{
  1302. "rengine_field_0": float64(15),
  1303. }},
  1304. {{
  1305. "rengine_field_0": float64(10),
  1306. }},
  1307. {{}},
  1308. },
  1309. s: "op_filter_0_records_in_total",
  1310. m: map[string]interface{}{
  1311. "op_preprocessor_ldemo_0_exceptions_total": int64(0),
  1312. "op_preprocessor_ldemo_0_process_latency_ms": int64(0),
  1313. "op_preprocessor_ldemo_0_records_in_total": int64(5),
  1314. "op_preprocessor_ldemo_0_records_out_total": int64(5),
  1315. "op_project_0_exceptions_total": int64(1),
  1316. "op_project_0_process_latency_ms": int64(0),
  1317. "op_project_0_records_in_total": int64(5),
  1318. "op_project_0_records_out_total": int64(4),
  1319. "sink_mockSink_0_exceptions_total": int64(0),
  1320. "sink_mockSink_0_records_in_total": int64(5),
  1321. "sink_mockSink_0_records_out_total": int64(5),
  1322. "source_ldemo_0_exceptions_total": int64(0),
  1323. "source_ldemo_0_records_in_total": int64(5),
  1324. "source_ldemo_0_records_out_total": int64(5),
  1325. },
  1326. },
  1327. }
  1328. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1329. createSchemalessStreams(t)
  1330. defer dropSchemalessStreams(t)
  1331. //defer close(done)
  1332. for i, tt := range tests {
  1333. test.ResetClock(1541152486000)
  1334. p := NewRuleProcessor(DbDir)
  1335. parser := xsql.NewParser(strings.NewReader(tt.sql))
  1336. var (
  1337. sources []*nodes.SourceNode
  1338. syncs []chan int
  1339. )
  1340. if stmt, err := xsql.Language.Parse(parser); err != nil {
  1341. t.Errorf("parse sql %s error: %s", tt.sql, err)
  1342. } else {
  1343. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  1344. t.Errorf("sql %s is not a select statement", tt.sql)
  1345. } else {
  1346. streams := xsql.GetStreams(selectStmt)
  1347. for _, stream := range streams {
  1348. next := make(chan int)
  1349. syncs = append(syncs, next)
  1350. source := getMockSourceL(stream, next, 5)
  1351. sources = append(sources, source)
  1352. }
  1353. }
  1354. }
  1355. tp, inputs, err := p.createTopoWithSources(&api.Rule{Id: tt.name, Sql: tt.sql, Options: map[string]interface{}{
  1356. "bufferLength": float64(100),
  1357. }}, sources)
  1358. if err != nil {
  1359. t.Error(err)
  1360. }
  1361. mockSink := test.NewMockSink()
  1362. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink, nil)
  1363. tp.AddSink(inputs, sink)
  1364. errCh := tp.Open()
  1365. func() {
  1366. for i := 0; i < 5; i++ {
  1367. syncs[i%len(syncs)] <- i
  1368. select {
  1369. case err = <-errCh:
  1370. t.Log(err)
  1371. tp.Cancel()
  1372. return
  1373. default:
  1374. }
  1375. }
  1376. for retry := 100; retry > 0; retry-- {
  1377. if err := compareMetrics(tp, tt.m, tt.sql); err == nil {
  1378. break
  1379. }
  1380. time.Sleep(time.Duration(retry) * time.Millisecond)
  1381. }
  1382. }()
  1383. results := mockSink.GetResults()
  1384. var maps [][]map[string]interface{}
  1385. for _, v := range results {
  1386. var mapRes []map[string]interface{}
  1387. err := json.Unmarshal(v, &mapRes)
  1388. if err != nil {
  1389. t.Errorf("Failed to parse the input into map")
  1390. continue
  1391. }
  1392. maps = append(maps, mapRes)
  1393. }
  1394. if !reflect.DeepEqual(tt.r, maps) {
  1395. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, maps)
  1396. continue
  1397. }
  1398. if err := compareMetrics(tp, tt.m, tt.sql); err != nil {
  1399. t.Errorf("%d. %q\n\n%v", i, tt.sql, err)
  1400. }
  1401. tp.Cancel()
  1402. }
  1403. }
  1404. func TestWindow(t *testing.T) {
  1405. var tests = []struct {
  1406. name string
  1407. sql string
  1408. size int
  1409. r [][]map[string]interface{}
  1410. m map[string]interface{}
  1411. }{
  1412. {
  1413. name: `rule1`,
  1414. sql: `SELECT * FROM demo GROUP BY HOPPINGWINDOW(ss, 2, 1)`,
  1415. size: 5,
  1416. r: [][]map[string]interface{}{
  1417. {{
  1418. "color": "red",
  1419. "size": float64(3),
  1420. "ts": float64(1541152486013),
  1421. }, {
  1422. "color": "blue",
  1423. "size": float64(6),
  1424. "ts": float64(1541152486822),
  1425. }},
  1426. {{
  1427. "color": "red",
  1428. "size": float64(3),
  1429. "ts": float64(1541152486013),
  1430. }, {
  1431. "color": "blue",
  1432. "size": float64(6),
  1433. "ts": float64(1541152486822),
  1434. }, {
  1435. "color": "blue",
  1436. "size": float64(2),
  1437. "ts": float64(1541152487632),
  1438. }},
  1439. {{
  1440. "color": "blue",
  1441. "size": float64(2),
  1442. "ts": float64(1541152487632),
  1443. }, {
  1444. "color": "yellow",
  1445. "size": float64(4),
  1446. "ts": float64(1541152488442),
  1447. }},
  1448. },
  1449. m: map[string]interface{}{
  1450. "op_preprocessor_demo_0_exceptions_total": int64(0),
  1451. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  1452. "op_preprocessor_demo_0_records_in_total": int64(5),
  1453. "op_preprocessor_demo_0_records_out_total": int64(5),
  1454. "op_project_0_exceptions_total": int64(0),
  1455. "op_project_0_process_latency_ms": int64(0),
  1456. "op_project_0_records_in_total": int64(3),
  1457. "op_project_0_records_out_total": int64(3),
  1458. "sink_mockSink_0_exceptions_total": int64(0),
  1459. "sink_mockSink_0_records_in_total": int64(3),
  1460. "sink_mockSink_0_records_out_total": int64(3),
  1461. "source_demo_0_exceptions_total": int64(0),
  1462. "source_demo_0_records_in_total": int64(5),
  1463. "source_demo_0_records_out_total": int64(5),
  1464. "op_window_0_exceptions_total": int64(0),
  1465. "op_window_0_process_latency_ms": int64(0),
  1466. "op_window_0_records_in_total": int64(5),
  1467. "op_window_0_records_out_total": int64(3),
  1468. },
  1469. }, {
  1470. name: `rule2`,
  1471. sql: `SELECT color, ts FROM demo where size > 2 GROUP BY tumblingwindow(ss, 1)`,
  1472. size: 5,
  1473. r: [][]map[string]interface{}{
  1474. {{
  1475. "color": "red",
  1476. "ts": float64(1541152486013),
  1477. }, {
  1478. "color": "blue",
  1479. "ts": float64(1541152486822),
  1480. }},
  1481. {{
  1482. "color": "yellow",
  1483. "ts": float64(1541152488442),
  1484. }},
  1485. },
  1486. m: map[string]interface{}{
  1487. "op_preprocessor_demo_0_exceptions_total": int64(0),
  1488. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  1489. "op_preprocessor_demo_0_records_in_total": int64(5),
  1490. "op_preprocessor_demo_0_records_out_total": int64(5),
  1491. "op_project_0_exceptions_total": int64(0),
  1492. "op_project_0_process_latency_ms": int64(0),
  1493. "op_project_0_records_in_total": int64(2),
  1494. "op_project_0_records_out_total": int64(2),
  1495. "sink_mockSink_0_exceptions_total": int64(0),
  1496. "sink_mockSink_0_records_in_total": int64(2),
  1497. "sink_mockSink_0_records_out_total": int64(2),
  1498. "source_demo_0_exceptions_total": int64(0),
  1499. "source_demo_0_records_in_total": int64(5),
  1500. "source_demo_0_records_out_total": int64(5),
  1501. "op_window_0_exceptions_total": int64(0),
  1502. "op_window_0_process_latency_ms": int64(0),
  1503. "op_window_0_records_in_total": int64(5),
  1504. "op_window_0_records_out_total": int64(3),
  1505. "op_filter_0_exceptions_total": int64(0),
  1506. "op_filter_0_process_latency_ms": int64(0),
  1507. "op_filter_0_records_in_total": int64(3),
  1508. "op_filter_0_records_out_total": int64(2),
  1509. },
  1510. }, {
  1511. name: `rule3`,
  1512. sql: `SELECT color, temp, ts FROM demo INNER JOIN demo1 ON demo.ts = demo1.ts GROUP BY SlidingWindow(ss, 1)`,
  1513. size: 5,
  1514. r: [][]map[string]interface{}{
  1515. {{
  1516. "color": "red",
  1517. "temp": 25.5,
  1518. "ts": float64(1541152486013),
  1519. }}, {{
  1520. "color": "red",
  1521. "temp": 25.5,
  1522. "ts": float64(1541152486013),
  1523. }}, {{
  1524. "color": "red",
  1525. "temp": 25.5,
  1526. "ts": float64(1541152486013),
  1527. }}, {{
  1528. "color": "blue",
  1529. "temp": 28.1,
  1530. "ts": float64(1541152487632),
  1531. }}, {{
  1532. "color": "blue",
  1533. "temp": 28.1,
  1534. "ts": float64(1541152487632),
  1535. }}, {{
  1536. "color": "blue",
  1537. "temp": 28.1,
  1538. "ts": float64(1541152487632),
  1539. }, {
  1540. "color": "yellow",
  1541. "temp": 27.4,
  1542. "ts": float64(1541152488442),
  1543. }}, {{
  1544. "color": "yellow",
  1545. "temp": 27.4,
  1546. "ts": float64(1541152488442),
  1547. }}, {{
  1548. "color": "yellow",
  1549. "temp": 27.4,
  1550. "ts": float64(1541152488442),
  1551. }, {
  1552. "color": "red",
  1553. "temp": 25.5,
  1554. "ts": float64(1541152489252),
  1555. }},
  1556. },
  1557. m: map[string]interface{}{
  1558. "op_preprocessor_demo_0_exceptions_total": int64(0),
  1559. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  1560. "op_preprocessor_demo_0_records_in_total": int64(5),
  1561. "op_preprocessor_demo_0_records_out_total": int64(5),
  1562. "op_preprocessor_demo1_0_exceptions_total": int64(0),
  1563. "op_preprocessor_demo1_0_process_latency_ms": int64(0),
  1564. "op_preprocessor_demo1_0_records_in_total": int64(5),
  1565. "op_preprocessor_demo1_0_records_out_total": int64(5),
  1566. "op_project_0_exceptions_total": int64(0),
  1567. "op_project_0_process_latency_ms": int64(0),
  1568. "op_project_0_records_in_total": int64(8),
  1569. "op_project_0_records_out_total": int64(8),
  1570. "sink_mockSink_0_exceptions_total": int64(0),
  1571. "sink_mockSink_0_records_in_total": int64(8),
  1572. "sink_mockSink_0_records_out_total": int64(8),
  1573. "source_demo_0_exceptions_total": int64(0),
  1574. "source_demo_0_records_in_total": int64(5),
  1575. "source_demo_0_records_out_total": int64(5),
  1576. "source_demo1_0_exceptions_total": int64(0),
  1577. "source_demo1_0_records_in_total": int64(5),
  1578. "source_demo1_0_records_out_total": int64(5),
  1579. "op_window_0_exceptions_total": int64(0),
  1580. "op_window_0_process_latency_ms": int64(0),
  1581. "op_window_0_records_in_total": int64(10),
  1582. "op_window_0_records_out_total": int64(10),
  1583. "op_join_0_exceptions_total": int64(0),
  1584. "op_join_0_process_latency_ms": int64(0),
  1585. "op_join_0_records_in_total": int64(10),
  1586. "op_join_0_records_out_total": int64(8),
  1587. },
  1588. }, {
  1589. name: `rule4`,
  1590. sql: `SELECT color FROM demo GROUP BY SlidingWindow(ss, 2), color ORDER BY color`,
  1591. size: 5,
  1592. r: [][]map[string]interface{}{
  1593. {{
  1594. "color": "red",
  1595. }}, {{
  1596. "color": "blue",
  1597. }, {
  1598. "color": "red",
  1599. }}, {{
  1600. "color": "blue",
  1601. }, {
  1602. "color": "red",
  1603. }}, {{
  1604. "color": "blue",
  1605. }, {
  1606. "color": "yellow",
  1607. }}, {{
  1608. "color": "blue",
  1609. }, {
  1610. "color": "red",
  1611. }, {
  1612. "color": "yellow",
  1613. }},
  1614. },
  1615. m: map[string]interface{}{
  1616. "op_preprocessor_demo_0_exceptions_total": int64(0),
  1617. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  1618. "op_preprocessor_demo_0_records_in_total": int64(5),
  1619. "op_preprocessor_demo_0_records_out_total": int64(5),
  1620. "op_project_0_exceptions_total": int64(0),
  1621. "op_project_0_process_latency_ms": int64(0),
  1622. "op_project_0_records_in_total": int64(5),
  1623. "op_project_0_records_out_total": int64(5),
  1624. "sink_mockSink_0_exceptions_total": int64(0),
  1625. "sink_mockSink_0_records_in_total": int64(5),
  1626. "sink_mockSink_0_records_out_total": int64(5),
  1627. "source_demo_0_exceptions_total": int64(0),
  1628. "source_demo_0_records_in_total": int64(5),
  1629. "source_demo_0_records_out_total": int64(5),
  1630. "op_window_0_exceptions_total": int64(0),
  1631. "op_window_0_process_latency_ms": int64(0),
  1632. "op_window_0_records_in_total": int64(5),
  1633. "op_window_0_records_out_total": int64(5),
  1634. "op_aggregate_0_exceptions_total": int64(0),
  1635. "op_aggregate_0_process_latency_ms": int64(0),
  1636. "op_aggregate_0_records_in_total": int64(5),
  1637. "op_aggregate_0_records_out_total": int64(5),
  1638. "op_order_0_exceptions_total": int64(0),
  1639. "op_order_0_process_latency_ms": int64(0),
  1640. "op_order_0_records_in_total": int64(5),
  1641. "op_order_0_records_out_total": int64(5),
  1642. },
  1643. }, {
  1644. name: `rule5`,
  1645. sql: `SELECT temp FROM sessionDemo GROUP BY SessionWindow(ss, 2, 1) `,
  1646. size: 11,
  1647. r: [][]map[string]interface{}{
  1648. {{
  1649. "temp": 25.5,
  1650. }, {
  1651. "temp": 27.5,
  1652. }}, {{
  1653. "temp": 28.1,
  1654. }, {
  1655. "temp": 27.4,
  1656. }, {
  1657. "temp": 25.5,
  1658. }}, {{
  1659. "temp": 26.2,
  1660. }, {
  1661. "temp": 26.8,
  1662. }, {
  1663. "temp": 28.9,
  1664. }, {
  1665. "temp": 29.1,
  1666. }, {
  1667. "temp": 32.2,
  1668. }},
  1669. },
  1670. m: map[string]interface{}{
  1671. "op_preprocessor_sessionDemo_0_exceptions_total": int64(0),
  1672. "op_preprocessor_sessionDemo_0_process_latency_ms": int64(0),
  1673. "op_preprocessor_sessionDemo_0_records_in_total": int64(11),
  1674. "op_preprocessor_sessionDemo_0_records_out_total": int64(11),
  1675. "op_project_0_exceptions_total": int64(0),
  1676. "op_project_0_process_latency_ms": int64(0),
  1677. "op_project_0_records_in_total": int64(3),
  1678. "op_project_0_records_out_total": int64(3),
  1679. "sink_mockSink_0_exceptions_total": int64(0),
  1680. "sink_mockSink_0_records_in_total": int64(3),
  1681. "sink_mockSink_0_records_out_total": int64(3),
  1682. "source_sessionDemo_0_exceptions_total": int64(0),
  1683. "source_sessionDemo_0_records_in_total": int64(11),
  1684. "source_sessionDemo_0_records_out_total": int64(11),
  1685. "op_window_0_exceptions_total": int64(0),
  1686. "op_window_0_process_latency_ms": int64(0),
  1687. "op_window_0_records_in_total": int64(11),
  1688. "op_window_0_records_out_total": int64(3),
  1689. },
  1690. }, {
  1691. name: `rule6`,
  1692. sql: `SELECT max(temp) as m, count(color) as c FROM demo INNER JOIN demo1 ON demo.ts = demo1.ts GROUP BY SlidingWindow(ss, 1)`,
  1693. size: 5,
  1694. r: [][]map[string]interface{}{
  1695. {{
  1696. "m": 25.5,
  1697. "c": float64(1),
  1698. }}, {{
  1699. "m": 25.5,
  1700. "c": float64(1),
  1701. }}, {{
  1702. "m": 25.5,
  1703. "c": float64(1),
  1704. }}, {{
  1705. "m": 28.1,
  1706. "c": float64(1),
  1707. }}, {{
  1708. "m": 28.1,
  1709. "c": float64(1),
  1710. }}, {{
  1711. "m": 28.1,
  1712. "c": float64(2),
  1713. }}, {{
  1714. "m": 27.4,
  1715. "c": float64(1),
  1716. }}, {{
  1717. "m": 27.4,
  1718. "c": float64(2),
  1719. }},
  1720. },
  1721. m: map[string]interface{}{
  1722. "op_preprocessor_demo_0_exceptions_total": int64(0),
  1723. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  1724. "op_preprocessor_demo_0_records_in_total": int64(5),
  1725. "op_preprocessor_demo_0_records_out_total": int64(5),
  1726. "op_preprocessor_demo1_0_exceptions_total": int64(0),
  1727. "op_preprocessor_demo1_0_process_latency_ms": int64(0),
  1728. "op_preprocessor_demo1_0_records_in_total": int64(5),
  1729. "op_preprocessor_demo1_0_records_out_total": int64(5),
  1730. "op_project_0_exceptions_total": int64(0),
  1731. "op_project_0_process_latency_ms": int64(0),
  1732. "op_project_0_records_in_total": int64(8),
  1733. "op_project_0_records_out_total": int64(8),
  1734. "sink_mockSink_0_exceptions_total": int64(0),
  1735. "sink_mockSink_0_records_in_total": int64(8),
  1736. "sink_mockSink_0_records_out_total": int64(8),
  1737. "source_demo_0_exceptions_total": int64(0),
  1738. "source_demo_0_records_in_total": int64(5),
  1739. "source_demo_0_records_out_total": int64(5),
  1740. "source_demo1_0_exceptions_total": int64(0),
  1741. "source_demo1_0_records_in_total": int64(5),
  1742. "source_demo1_0_records_out_total": int64(5),
  1743. "op_window_0_exceptions_total": int64(0),
  1744. "op_window_0_process_latency_ms": int64(0),
  1745. "op_window_0_records_in_total": int64(10),
  1746. "op_window_0_records_out_total": int64(10),
  1747. "op_join_0_exceptions_total": int64(0),
  1748. "op_join_0_process_latency_ms": int64(0),
  1749. "op_join_0_records_in_total": int64(10),
  1750. "op_join_0_records_out_total": int64(8),
  1751. },
  1752. }, {
  1753. name: `rule7`,
  1754. sql: `SELECT * FROM demoE GROUP BY HOPPINGWINDOW(ss, 2, 1)`,
  1755. size: 5,
  1756. r: [][]map[string]interface{}{
  1757. {{
  1758. "error": "error in preprocessor: invalid data type for color, expect string but found int(3)",
  1759. }},
  1760. {{
  1761. "color": "blue",
  1762. "size": float64(6),
  1763. "ts": float64(1541152486822),
  1764. }},
  1765. {{
  1766. "color": "blue",
  1767. "size": float64(6),
  1768. "ts": float64(1541152486822),
  1769. }, {
  1770. "color": "blue",
  1771. "size": float64(2),
  1772. "ts": float64(1541152487632),
  1773. }},
  1774. {{
  1775. "error": "error in preprocessor: invalid data type for color, expect string but found int(7)",
  1776. }},
  1777. {{
  1778. "color": "blue",
  1779. "size": float64(2),
  1780. "ts": float64(1541152487632),
  1781. }},
  1782. {{
  1783. "error": "error in preprocessor: invalid data type for size, expect bigint but found string(blue)",
  1784. }},
  1785. },
  1786. m: map[string]interface{}{
  1787. "op_preprocessor_demoE_0_exceptions_total": int64(3),
  1788. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  1789. "op_preprocessor_demoE_0_records_in_total": int64(5),
  1790. "op_preprocessor_demoE_0_records_out_total": int64(2),
  1791. "op_project_0_exceptions_total": int64(3),
  1792. "op_project_0_process_latency_ms": int64(0),
  1793. "op_project_0_records_in_total": int64(6),
  1794. "op_project_0_records_out_total": int64(3),
  1795. "sink_mockSink_0_exceptions_total": int64(0),
  1796. "sink_mockSink_0_records_in_total": int64(6),
  1797. "sink_mockSink_0_records_out_total": int64(6),
  1798. "source_demoE_0_exceptions_total": int64(0),
  1799. "source_demoE_0_records_in_total": int64(5),
  1800. "source_demoE_0_records_out_total": int64(5),
  1801. "op_window_0_exceptions_total": int64(3),
  1802. "op_window_0_process_latency_ms": int64(0),
  1803. "op_window_0_records_in_total": int64(5),
  1804. "op_window_0_records_out_total": int64(3),
  1805. },
  1806. },
  1807. }
  1808. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1809. createStreams(t)
  1810. defer dropStreams(t)
  1811. for i, tt := range tests {
  1812. test.ResetClock(1541152486000)
  1813. p := NewRuleProcessor(DbDir)
  1814. parser := xsql.NewParser(strings.NewReader(tt.sql))
  1815. var (
  1816. sources []*nodes.SourceNode
  1817. syncs []chan int
  1818. )
  1819. if stmt, err := xsql.Language.Parse(parser); err != nil {
  1820. t.Errorf("parse sql %s error: %s", tt.sql, err)
  1821. } else {
  1822. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  1823. t.Errorf("sql %s is not a select statement", tt.sql)
  1824. } else {
  1825. streams := xsql.GetStreams(selectStmt)
  1826. for _, stream := range streams {
  1827. next := make(chan int)
  1828. syncs = append(syncs, next)
  1829. source := getMockSource(stream, next, tt.size)
  1830. sources = append(sources, source)
  1831. }
  1832. }
  1833. }
  1834. tp, inputs, err := p.createTopoWithSources(&api.Rule{Id: tt.name, Sql: tt.sql}, sources)
  1835. if err != nil {
  1836. t.Error(err)
  1837. }
  1838. mockSink := test.NewMockSink()
  1839. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink, nil)
  1840. tp.AddSink(inputs, sink)
  1841. errCh := tp.Open()
  1842. func() {
  1843. for i := 0; i < tt.size*len(syncs); i++ {
  1844. syncs[i%len(syncs)] <- i
  1845. for {
  1846. time.Sleep(1)
  1847. if getMetric(tp, "op_window_0_records_in_total") == (i + 1) {
  1848. break
  1849. }
  1850. }
  1851. select {
  1852. case err = <-errCh:
  1853. t.Log(err)
  1854. tp.Cancel()
  1855. return
  1856. default:
  1857. }
  1858. }
  1859. retry := 100
  1860. for ; retry > 0; retry-- {
  1861. if err := compareMetrics(tp, tt.m, tt.sql); err == nil {
  1862. break
  1863. }
  1864. t.Logf("wait to try another %d times", retry)
  1865. time.Sleep(time.Duration(retry) * time.Millisecond)
  1866. }
  1867. if retry == 0 {
  1868. err := compareMetrics(tp, tt.m, tt.sql)
  1869. t.Errorf("could not get correct metrics: %v", err)
  1870. }
  1871. }()
  1872. results := mockSink.GetResults()
  1873. var maps [][]map[string]interface{}
  1874. for _, v := range results {
  1875. var mapRes []map[string]interface{}
  1876. err := json.Unmarshal(v, &mapRes)
  1877. if err != nil {
  1878. t.Errorf("Failed to parse the input into map")
  1879. continue
  1880. }
  1881. maps = append(maps, mapRes)
  1882. }
  1883. if !reflect.DeepEqual(tt.r, maps) {
  1884. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, maps)
  1885. }
  1886. if err := compareMetrics(tp, tt.m, tt.sql); err != nil {
  1887. t.Errorf("%d. %q\n\n%v", i, tt.sql, err)
  1888. }
  1889. tp.Cancel()
  1890. }
  1891. }
  1892. func TestWindowError(t *testing.T) {
  1893. var tests = []struct {
  1894. name string
  1895. sql string
  1896. size int
  1897. r [][]map[string]interface{}
  1898. m map[string]interface{}
  1899. }{
  1900. {
  1901. name: `rule1`,
  1902. sql: `SELECT size * 3 FROM ldemo GROUP BY TUMBLINGWINDOW(ss, 2)`,
  1903. size: 5,
  1904. r: [][]map[string]interface{}{
  1905. {{
  1906. "error": "run Select error: invalid operation string(string) * int64(3)",
  1907. }},
  1908. },
  1909. m: map[string]interface{}{
  1910. "op_preprocessor_ldemo_0_exceptions_total": int64(0),
  1911. "op_preprocessor_ldemo_0_process_latency_ms": int64(0),
  1912. "op_preprocessor_ldemo_0_records_in_total": int64(5),
  1913. "op_preprocessor_ldemo_0_records_out_total": int64(5),
  1914. "op_project_0_exceptions_total": int64(1),
  1915. "op_project_0_process_latency_ms": int64(0),
  1916. "op_project_0_records_in_total": int64(1),
  1917. "op_project_0_records_out_total": int64(0),
  1918. "sink_mockSink_0_exceptions_total": int64(0),
  1919. "sink_mockSink_0_records_in_total": int64(1),
  1920. "sink_mockSink_0_records_out_total": int64(1),
  1921. "source_ldemo_0_exceptions_total": int64(0),
  1922. "source_ldemo_0_records_in_total": int64(5),
  1923. "source_ldemo_0_records_out_total": int64(5),
  1924. "op_window_0_exceptions_total": int64(0),
  1925. "op_window_0_process_latency_ms": int64(0),
  1926. "op_window_0_records_in_total": int64(5),
  1927. "op_window_0_records_out_total": int64(1),
  1928. },
  1929. }, {
  1930. name: `rule2`,
  1931. sql: `SELECT color, ts FROM ldemo where size > 2 GROUP BY tumblingwindow(ss, 1)`,
  1932. size: 5,
  1933. r: [][]map[string]interface{}{
  1934. {{
  1935. "error": "run Where error: invalid operation string(string) > int64(2)",
  1936. }}, {{
  1937. "ts": float64(1541152487632),
  1938. }},
  1939. },
  1940. m: map[string]interface{}{
  1941. "op_preprocessor_ldemo_0_exceptions_total": int64(0),
  1942. "op_preprocessor_ldemo_0_process_latency_ms": int64(0),
  1943. "op_preprocessor_ldemo_0_records_in_total": int64(5),
  1944. "op_preprocessor_ldemo_0_records_out_total": int64(5),
  1945. "op_project_0_exceptions_total": int64(1),
  1946. "op_project_0_process_latency_ms": int64(0),
  1947. "op_project_0_records_in_total": int64(2),
  1948. "op_project_0_records_out_total": int64(1),
  1949. "sink_mockSink_0_exceptions_total": int64(0),
  1950. "sink_mockSink_0_records_in_total": int64(2),
  1951. "sink_mockSink_0_records_out_total": int64(2),
  1952. "source_ldemo_0_exceptions_total": int64(0),
  1953. "source_ldemo_0_records_in_total": int64(5),
  1954. "source_ldemo_0_records_out_total": int64(5),
  1955. "op_window_0_exceptions_total": int64(0),
  1956. "op_window_0_process_latency_ms": int64(0),
  1957. "op_window_0_records_in_total": int64(5),
  1958. "op_window_0_records_out_total": int64(3),
  1959. "op_filter_0_exceptions_total": int64(1),
  1960. "op_filter_0_process_latency_ms": int64(0),
  1961. "op_filter_0_records_in_total": int64(3),
  1962. "op_filter_0_records_out_total": int64(1),
  1963. },
  1964. }, {
  1965. name: `rule3`,
  1966. sql: `SELECT color, temp, ts FROM ldemo INNER JOIN ldemo1 ON ldemo.ts = ldemo1.ts GROUP BY SlidingWindow(ss, 1)`,
  1967. size: 5,
  1968. r: [][]map[string]interface{}{
  1969. {{
  1970. "color": "red",
  1971. "temp": 25.5,
  1972. "ts": float64(1541152486013),
  1973. }}, {{
  1974. "color": "red",
  1975. "temp": 25.5,
  1976. "ts": float64(1541152486013),
  1977. }}, {{
  1978. "color": "red",
  1979. "temp": 25.5,
  1980. "ts": float64(1541152486013),
  1981. }}, {{
  1982. "temp": 28.1,
  1983. "ts": float64(1541152487632),
  1984. }}, {{
  1985. "temp": 28.1,
  1986. "ts": float64(1541152487632),
  1987. }}, {{
  1988. "error": "run Join error: invalid operation int64(1541152487632) = string(1541152488442)",
  1989. }}, {{
  1990. "error": "run Join error: invalid operation int64(1541152488442) = string(1541152488442)",
  1991. }}, {{
  1992. "error": "run Join error: invalid operation int64(1541152488442) = string(1541152488442)",
  1993. }},
  1994. },
  1995. m: map[string]interface{}{
  1996. "op_preprocessor_ldemo_0_exceptions_total": int64(0),
  1997. "op_preprocessor_ldemo_0_process_latency_ms": int64(0),
  1998. "op_preprocessor_ldemo_0_records_in_total": int64(5),
  1999. "op_preprocessor_ldemo_0_records_out_total": int64(5),
  2000. "op_preprocessor_ldemo1_0_exceptions_total": int64(0),
  2001. "op_preprocessor_ldemo1_0_process_latency_ms": int64(0),
  2002. "op_preprocessor_ldemo1_0_records_in_total": int64(5),
  2003. "op_preprocessor_ldemo1_0_records_out_total": int64(5),
  2004. "op_project_0_exceptions_total": int64(3),
  2005. "op_project_0_process_latency_ms": int64(0),
  2006. "op_project_0_records_in_total": int64(8),
  2007. "op_project_0_records_out_total": int64(5),
  2008. "sink_mockSink_0_exceptions_total": int64(0),
  2009. "sink_mockSink_0_records_in_total": int64(8),
  2010. "sink_mockSink_0_records_out_total": int64(8),
  2011. "source_ldemo_0_exceptions_total": int64(0),
  2012. "source_ldemo_0_records_in_total": int64(5),
  2013. "source_ldemo_0_records_out_total": int64(5),
  2014. "source_ldemo1_0_exceptions_total": int64(0),
  2015. "source_ldemo1_0_records_in_total": int64(5),
  2016. "source_ldemo1_0_records_out_total": int64(5),
  2017. "op_window_0_exceptions_total": int64(0),
  2018. "op_window_0_process_latency_ms": int64(0),
  2019. "op_window_0_records_in_total": int64(10),
  2020. "op_window_0_records_out_total": int64(10),
  2021. "op_join_0_exceptions_total": int64(3),
  2022. "op_join_0_process_latency_ms": int64(0),
  2023. "op_join_0_records_in_total": int64(10),
  2024. "op_join_0_records_out_total": int64(5),
  2025. },
  2026. }, {
  2027. name: `rule4`,
  2028. sql: `SELECT color FROM ldemo GROUP BY SlidingWindow(ss, 2), color having size >= 2 order by color`,
  2029. size: 5,
  2030. r: [][]map[string]interface{}{
  2031. {{
  2032. "color": "red",
  2033. }}, {{
  2034. "error": "run Having error: invalid operation string(string) >= int64(2)",
  2035. }}, {{
  2036. "error": "run Having error: invalid operation string(string) >= int64(2)",
  2037. }}, {{
  2038. "error": "run Having error: invalid operation string(string) >= int64(2)",
  2039. }}, {{
  2040. "color": float64(49),
  2041. }, {}},
  2042. },
  2043. m: map[string]interface{}{
  2044. "op_preprocessor_ldemo_0_exceptions_total": int64(0),
  2045. "op_preprocessor_ldemo_0_process_latency_ms": int64(0),
  2046. "op_preprocessor_ldemo_0_records_in_total": int64(5),
  2047. "op_preprocessor_ldemo_0_records_out_total": int64(5),
  2048. "op_project_0_exceptions_total": int64(3),
  2049. "op_project_0_process_latency_ms": int64(0),
  2050. "op_project_0_records_in_total": int64(5),
  2051. "op_project_0_records_out_total": int64(2),
  2052. "sink_mockSink_0_exceptions_total": int64(0),
  2053. "sink_mockSink_0_records_in_total": int64(5),
  2054. "sink_mockSink_0_records_out_total": int64(5),
  2055. "source_ldemo_0_exceptions_total": int64(0),
  2056. "source_ldemo_0_records_in_total": int64(5),
  2057. "source_ldemo_0_records_out_total": int64(5),
  2058. "op_window_0_exceptions_total": int64(0),
  2059. "op_window_0_process_latency_ms": int64(0),
  2060. "op_window_0_records_in_total": int64(5),
  2061. "op_window_0_records_out_total": int64(5),
  2062. "op_aggregate_0_exceptions_total": int64(0),
  2063. "op_aggregate_0_process_latency_ms": int64(0),
  2064. "op_aggregate_0_records_in_total": int64(5),
  2065. "op_aggregate_0_records_out_total": int64(5),
  2066. "op_having_0_exceptions_total": int64(3),
  2067. "op_having_0_process_latency_ms": int64(0),
  2068. "op_having_0_records_in_total": int64(5),
  2069. "op_having_0_records_out_total": int64(2),
  2070. },
  2071. }, {
  2072. name: `rule5`,
  2073. sql: `SELECT color, size FROM ldemo GROUP BY tumblingwindow(ss, 1) ORDER BY size`,
  2074. size: 5,
  2075. r: [][]map[string]interface{}{
  2076. {{
  2077. "error": "run Order By error: incompatible types for comparison: int and string",
  2078. }}, {{
  2079. "size": float64(3),
  2080. }}, {{
  2081. "color": float64(49),
  2082. "size": float64(2),
  2083. }},
  2084. },
  2085. m: map[string]interface{}{
  2086. "op_preprocessor_ldemo_0_exceptions_total": int64(0),
  2087. "op_preprocessor_ldemo_0_process_latency_ms": int64(0),
  2088. "op_preprocessor_ldemo_0_records_in_total": int64(5),
  2089. "op_preprocessor_ldemo_0_records_out_total": int64(5),
  2090. "op_project_0_exceptions_total": int64(1),
  2091. "op_project_0_process_latency_ms": int64(0),
  2092. "op_project_0_records_in_total": int64(3),
  2093. "op_project_0_records_out_total": int64(2),
  2094. "sink_mockSink_0_exceptions_total": int64(0),
  2095. "sink_mockSink_0_records_in_total": int64(3),
  2096. "sink_mockSink_0_records_out_total": int64(3),
  2097. "source_ldemo_0_exceptions_total": int64(0),
  2098. "source_ldemo_0_records_in_total": int64(5),
  2099. "source_ldemo_0_records_out_total": int64(5),
  2100. "op_window_0_exceptions_total": int64(0),
  2101. "op_window_0_process_latency_ms": int64(0),
  2102. "op_window_0_records_in_total": int64(5),
  2103. "op_window_0_records_out_total": int64(3),
  2104. "op_order_0_exceptions_total": int64(1),
  2105. "op_order_0_process_latency_ms": int64(0),
  2106. "op_order_0_records_in_total": int64(3),
  2107. "op_order_0_records_out_total": int64(2),
  2108. },
  2109. },
  2110. }
  2111. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2112. createSchemalessStreams(t)
  2113. defer dropSchemalessStreams(t)
  2114. for i, tt := range tests {
  2115. test.ResetClock(1541152486000)
  2116. p := NewRuleProcessor(DbDir)
  2117. parser := xsql.NewParser(strings.NewReader(tt.sql))
  2118. var (
  2119. sources []*nodes.SourceNode
  2120. syncs []chan int
  2121. )
  2122. if stmt, err := xsql.Language.Parse(parser); err != nil {
  2123. t.Errorf("parse sql %s error: %s", tt.sql, err)
  2124. } else {
  2125. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  2126. t.Errorf("sql %s is not a select statement", tt.sql)
  2127. } else {
  2128. streams := xsql.GetStreams(selectStmt)
  2129. for _, stream := range streams {
  2130. next := make(chan int)
  2131. syncs = append(syncs, next)
  2132. source := getMockSourceL(stream, next, tt.size)
  2133. sources = append(sources, source)
  2134. }
  2135. }
  2136. }
  2137. tp, inputs, err := p.createTopoWithSources(&api.Rule{Id: tt.name, Sql: tt.sql}, sources)
  2138. if err != nil {
  2139. t.Error(err)
  2140. }
  2141. mockSink := test.NewMockSink()
  2142. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink, nil)
  2143. tp.AddSink(inputs, sink)
  2144. errCh := tp.Open()
  2145. func() {
  2146. for i := 0; i < tt.size*len(syncs); i++ {
  2147. syncs[i%len(syncs)] <- i
  2148. for {
  2149. time.Sleep(1)
  2150. if getMetric(tp, "op_window_0_records_in_total") == (i + 1) {
  2151. break
  2152. }
  2153. }
  2154. select {
  2155. case err = <-errCh:
  2156. t.Log(err)
  2157. tp.Cancel()
  2158. return
  2159. default:
  2160. }
  2161. }
  2162. retry := 100
  2163. for ; retry > 0; retry-- {
  2164. if err := compareMetrics(tp, tt.m, tt.sql); err == nil {
  2165. break
  2166. }
  2167. t.Logf("wait to try another %d times", retry)
  2168. time.Sleep(time.Duration(retry) * time.Millisecond)
  2169. }
  2170. if retry == 0 {
  2171. err := compareMetrics(tp, tt.m, tt.sql)
  2172. t.Errorf("could not get correct metrics: %v", err)
  2173. }
  2174. }()
  2175. results := mockSink.GetResults()
  2176. var maps [][]map[string]interface{}
  2177. for _, v := range results {
  2178. var mapRes []map[string]interface{}
  2179. err := json.Unmarshal(v, &mapRes)
  2180. if err != nil {
  2181. t.Errorf("Failed to parse the input into map")
  2182. continue
  2183. }
  2184. maps = append(maps, mapRes)
  2185. }
  2186. if !reflect.DeepEqual(tt.r, maps) {
  2187. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, maps)
  2188. }
  2189. if err := compareMetrics(tp, tt.m, tt.sql); err != nil {
  2190. t.Errorf("%d. %q\n\n%v", i, tt.sql, err)
  2191. }
  2192. tp.Cancel()
  2193. }
  2194. }
  2195. func createEventStreams(t *testing.T) {
  2196. p := NewStreamProcessor(path.Join(DbDir, "stream"))
  2197. demo := `CREATE STREAM demoE (
  2198. color STRING,
  2199. size BIGINT,
  2200. ts BIGINT
  2201. ) WITH (DATASOURCE="demoE", FORMAT="json", KEY="ts", TIMESTAMP="ts");`
  2202. _, err := p.ExecStmt(demo)
  2203. if err != nil {
  2204. t.Log(err)
  2205. }
  2206. demo1 := `CREATE STREAM demo1E (
  2207. temp FLOAT,
  2208. hum BIGINT,
  2209. ts BIGINT
  2210. ) WITH (DATASOURCE="demo1E", FORMAT="json", KEY="ts", TIMESTAMP="ts");`
  2211. _, err = p.ExecStmt(demo1)
  2212. if err != nil {
  2213. t.Log(err)
  2214. }
  2215. sessionDemo := `CREATE STREAM sessionDemoE (
  2216. temp FLOAT,
  2217. hum BIGINT,
  2218. ts BIGINT
  2219. ) WITH (DATASOURCE="sessionDemoE", FORMAT="json", KEY="ts", TIMESTAMP="ts");`
  2220. _, err = p.ExecStmt(sessionDemo)
  2221. if err != nil {
  2222. t.Log(err)
  2223. }
  2224. demoErr := `CREATE STREAM demoErr (
  2225. color STRING,
  2226. size BIGINT,
  2227. ts BIGINT
  2228. ) WITH (DATASOURCE="demoErr", FORMAT="json", KEY="ts", TIMESTAMP="ts");`
  2229. _, err = p.ExecStmt(demoErr)
  2230. if err != nil {
  2231. t.Log(err)
  2232. }
  2233. }
  2234. func dropEventStreams(t *testing.T) {
  2235. p := NewStreamProcessor(path.Join(DbDir, "stream"))
  2236. demo := `DROP STREAM demoE`
  2237. _, err := p.ExecStmt(demo)
  2238. if err != nil {
  2239. t.Log(err)
  2240. }
  2241. demo1 := `DROP STREAM demo1E`
  2242. _, err = p.ExecStmt(demo1)
  2243. if err != nil {
  2244. t.Log(err)
  2245. }
  2246. sessionDemo := `DROP STREAM sessionDemoE`
  2247. _, err = p.ExecStmt(sessionDemo)
  2248. if err != nil {
  2249. t.Log(err)
  2250. }
  2251. demoErr := `DROP STREAM demoErr`
  2252. _, err = p.ExecStmt(demoErr)
  2253. if err != nil {
  2254. t.Log(err)
  2255. }
  2256. }
  2257. func getEventMockSource(name string, done <-chan int, size int) *nodes.SourceNode {
  2258. var data []*xsql.Tuple
  2259. switch name {
  2260. case "demoE":
  2261. data = []*xsql.Tuple{
  2262. {
  2263. Emitter: name,
  2264. Message: map[string]interface{}{
  2265. "color": "red",
  2266. "size": 3,
  2267. "ts": 1541152486013,
  2268. },
  2269. Timestamp: 1541152486013,
  2270. },
  2271. {
  2272. Emitter: name,
  2273. Message: map[string]interface{}{
  2274. "color": "blue",
  2275. "size": 2,
  2276. "ts": 1541152487632,
  2277. },
  2278. Timestamp: 1541152487632,
  2279. },
  2280. {
  2281. Emitter: name,
  2282. Message: map[string]interface{}{
  2283. "color": "red",
  2284. "size": 1,
  2285. "ts": 1541152489252,
  2286. },
  2287. Timestamp: 1541152489252,
  2288. },
  2289. { //dropped item
  2290. Emitter: name,
  2291. Message: map[string]interface{}{
  2292. "color": "blue",
  2293. "size": 6,
  2294. "ts": 1541152486822,
  2295. },
  2296. Timestamp: 1541152486822,
  2297. },
  2298. {
  2299. Emitter: name,
  2300. Message: map[string]interface{}{
  2301. "color": "yellow",
  2302. "size": 4,
  2303. "ts": 1541152488442,
  2304. },
  2305. Timestamp: 1541152488442,
  2306. },
  2307. { //To lift the watermark and issue all windows
  2308. Emitter: name,
  2309. Message: map[string]interface{}{
  2310. "color": "yellow",
  2311. "size": 4,
  2312. "ts": 1541152492342,
  2313. },
  2314. Timestamp: 1541152488442,
  2315. },
  2316. }
  2317. case "demo1E":
  2318. data = []*xsql.Tuple{
  2319. {
  2320. Emitter: name,
  2321. Message: map[string]interface{}{
  2322. "temp": 27.5,
  2323. "hum": 59,
  2324. "ts": 1541152486823,
  2325. },
  2326. Timestamp: 1541152486823,
  2327. },
  2328. {
  2329. Emitter: name,
  2330. Message: map[string]interface{}{
  2331. "temp": 25.5,
  2332. "hum": 65,
  2333. "ts": 1541152486013,
  2334. },
  2335. Timestamp: 1541152486013,
  2336. },
  2337. {
  2338. Emitter: name,
  2339. Message: map[string]interface{}{
  2340. "temp": 27.4,
  2341. "hum": 80,
  2342. "ts": 1541152488442,
  2343. },
  2344. Timestamp: 1541152488442,
  2345. },
  2346. {
  2347. Emitter: name,
  2348. Message: map[string]interface{}{
  2349. "temp": 28.1,
  2350. "hum": 75,
  2351. "ts": 1541152487632,
  2352. },
  2353. Timestamp: 1541152487632,
  2354. },
  2355. {
  2356. Emitter: name,
  2357. Message: map[string]interface{}{
  2358. "temp": 25.5,
  2359. "hum": 62,
  2360. "ts": 1541152489252,
  2361. },
  2362. Timestamp: 1541152489252,
  2363. },
  2364. {
  2365. Emitter: name,
  2366. Message: map[string]interface{}{
  2367. "temp": 25.5,
  2368. "hum": 62,
  2369. "ts": 1541152499252,
  2370. },
  2371. Timestamp: 1541152499252,
  2372. },
  2373. }
  2374. case "sessionDemoE":
  2375. data = []*xsql.Tuple{
  2376. {
  2377. Emitter: name,
  2378. Message: map[string]interface{}{
  2379. "temp": 25.5,
  2380. "hum": 65,
  2381. "ts": 1541152486013,
  2382. },
  2383. Timestamp: 1541152486013,
  2384. },
  2385. {
  2386. Emitter: name,
  2387. Message: map[string]interface{}{
  2388. "temp": 28.1,
  2389. "hum": 75,
  2390. "ts": 1541152487932,
  2391. },
  2392. Timestamp: 1541152487932,
  2393. },
  2394. {
  2395. Emitter: name,
  2396. Message: map[string]interface{}{
  2397. "temp": 27.5,
  2398. "hum": 59,
  2399. "ts": 1541152486823,
  2400. },
  2401. Timestamp: 1541152486823,
  2402. },
  2403. {
  2404. Emitter: name,
  2405. Message: map[string]interface{}{
  2406. "temp": 25.5,
  2407. "hum": 62,
  2408. "ts": 1541152489252,
  2409. },
  2410. Timestamp: 1541152489252,
  2411. },
  2412. {
  2413. Emitter: name,
  2414. Message: map[string]interface{}{
  2415. "temp": 27.4,
  2416. "hum": 80,
  2417. "ts": 1541152488442,
  2418. },
  2419. Timestamp: 1541152488442,
  2420. },
  2421. {
  2422. Emitter: name,
  2423. Message: map[string]interface{}{
  2424. "temp": 26.2,
  2425. "hum": 63,
  2426. "ts": 1541152490062,
  2427. },
  2428. Timestamp: 1541152490062,
  2429. },
  2430. {
  2431. Emitter: name,
  2432. Message: map[string]interface{}{
  2433. "temp": 28.9,
  2434. "hum": 85,
  2435. "ts": 1541152491682,
  2436. },
  2437. Timestamp: 1541152491682,
  2438. },
  2439. {
  2440. Emitter: name,
  2441. Message: map[string]interface{}{
  2442. "temp": 26.8,
  2443. "hum": 71,
  2444. "ts": 1541152490872,
  2445. },
  2446. Timestamp: 1541152490872,
  2447. },
  2448. {
  2449. Emitter: name,
  2450. Message: map[string]interface{}{
  2451. "temp": 29.1,
  2452. "hum": 92,
  2453. "ts": 1541152492492,
  2454. },
  2455. Timestamp: 1541152492492,
  2456. },
  2457. {
  2458. Emitter: name,
  2459. Message: map[string]interface{}{
  2460. "temp": 30.9,
  2461. "hum": 87,
  2462. "ts": 1541152494112,
  2463. },
  2464. Timestamp: 1541152494112,
  2465. },
  2466. {
  2467. Emitter: name,
  2468. Message: map[string]interface{}{
  2469. "temp": 32.2,
  2470. "hum": 99,
  2471. "ts": 1541152493202,
  2472. },
  2473. Timestamp: 1541152493202,
  2474. },
  2475. {
  2476. Emitter: name,
  2477. Message: map[string]interface{}{
  2478. "temp": 32.2,
  2479. "hum": 99,
  2480. "ts": 1541152499202,
  2481. },
  2482. Timestamp: 1541152499202,
  2483. },
  2484. }
  2485. case "demoErr":
  2486. data = []*xsql.Tuple{
  2487. {
  2488. Emitter: name,
  2489. Message: map[string]interface{}{
  2490. "color": "red",
  2491. "size": 3,
  2492. "ts": 1541152486013,
  2493. },
  2494. Timestamp: 1541152486013,
  2495. },
  2496. {
  2497. Emitter: name,
  2498. Message: map[string]interface{}{
  2499. "color": 2,
  2500. "size": "blue",
  2501. "ts": 1541152487632,
  2502. },
  2503. Timestamp: 1541152487632,
  2504. },
  2505. {
  2506. Emitter: name,
  2507. Message: map[string]interface{}{
  2508. "color": "red",
  2509. "size": 1,
  2510. "ts": 1541152489252,
  2511. },
  2512. Timestamp: 1541152489252,
  2513. },
  2514. { //dropped item
  2515. Emitter: name,
  2516. Message: map[string]interface{}{
  2517. "color": "blue",
  2518. "size": 6,
  2519. "ts": 1541152486822,
  2520. },
  2521. Timestamp: 1541152486822,
  2522. },
  2523. {
  2524. Emitter: name,
  2525. Message: map[string]interface{}{
  2526. "color": "yellow",
  2527. "size": 4,
  2528. "ts": 1541152488442,
  2529. },
  2530. Timestamp: 1541152488442,
  2531. },
  2532. { //To lift the watermark and issue all windows
  2533. Emitter: name,
  2534. Message: map[string]interface{}{
  2535. "color": "yellow",
  2536. "size": 4,
  2537. "ts": 1541152492342,
  2538. },
  2539. Timestamp: 1541152488442,
  2540. },
  2541. }
  2542. }
  2543. return nodes.NewSourceNodeWithSource(name, test.NewMockSource(data[:size], done, true), map[string]string{
  2544. "DATASOURCE": name,
  2545. })
  2546. }
  2547. func TestEventWindow(t *testing.T) {
  2548. var tests = []struct {
  2549. name string
  2550. sql string
  2551. size int
  2552. r [][]map[string]interface{}
  2553. m map[string]interface{}
  2554. }{
  2555. {
  2556. name: `rule1`,
  2557. sql: `SELECT * FROM demoE GROUP BY HOPPINGWINDOW(ss, 2, 1)`,
  2558. size: 6,
  2559. r: [][]map[string]interface{}{
  2560. {{
  2561. "color": "red",
  2562. "size": float64(3),
  2563. "ts": float64(1541152486013),
  2564. }},
  2565. {{
  2566. "color": "red",
  2567. "size": float64(3),
  2568. "ts": float64(1541152486013),
  2569. }, {
  2570. "color": "blue",
  2571. "size": float64(2),
  2572. "ts": float64(1541152487632),
  2573. }},
  2574. {{
  2575. "color": "blue",
  2576. "size": float64(2),
  2577. "ts": float64(1541152487632),
  2578. }, {
  2579. "color": "yellow",
  2580. "size": float64(4),
  2581. "ts": float64(1541152488442),
  2582. }}, {{
  2583. "color": "yellow",
  2584. "size": float64(4),
  2585. "ts": float64(1541152488442),
  2586. }, {
  2587. "color": "red",
  2588. "size": float64(1),
  2589. "ts": float64(1541152489252),
  2590. }}, {{
  2591. "color": "red",
  2592. "size": float64(1),
  2593. "ts": float64(1541152489252),
  2594. }},
  2595. },
  2596. m: map[string]interface{}{
  2597. "op_preprocessor_demoE_0_exceptions_total": int64(0),
  2598. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  2599. "op_preprocessor_demoE_0_records_in_total": int64(6),
  2600. "op_preprocessor_demoE_0_records_out_total": int64(6),
  2601. "op_project_0_exceptions_total": int64(0),
  2602. "op_project_0_process_latency_ms": int64(0),
  2603. "op_project_0_records_in_total": int64(5),
  2604. "op_project_0_records_out_total": int64(5),
  2605. "sink_mockSink_0_exceptions_total": int64(0),
  2606. "sink_mockSink_0_records_in_total": int64(5),
  2607. "sink_mockSink_0_records_out_total": int64(5),
  2608. "source_demoE_0_exceptions_total": int64(0),
  2609. "source_demoE_0_records_in_total": int64(6),
  2610. "source_demoE_0_records_out_total": int64(6),
  2611. "op_window_0_exceptions_total": int64(0),
  2612. "op_window_0_process_latency_ms": int64(0),
  2613. "op_window_0_records_in_total": int64(6),
  2614. "op_window_0_records_out_total": int64(5),
  2615. },
  2616. }, {
  2617. name: `rule2`,
  2618. sql: `SELECT color, ts FROM demoE where size > 2 GROUP BY tumblingwindow(ss, 1)`,
  2619. size: 6,
  2620. r: [][]map[string]interface{}{
  2621. {{
  2622. "color": "red",
  2623. "ts": float64(1541152486013),
  2624. }},
  2625. {{
  2626. "color": "yellow",
  2627. "ts": float64(1541152488442),
  2628. }},
  2629. },
  2630. m: map[string]interface{}{
  2631. "op_preprocessor_demoE_0_exceptions_total": int64(0),
  2632. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  2633. "op_preprocessor_demoE_0_records_in_total": int64(6),
  2634. "op_preprocessor_demoE_0_records_out_total": int64(6),
  2635. "op_project_0_exceptions_total": int64(0),
  2636. "op_project_0_process_latency_ms": int64(0),
  2637. "op_project_0_records_in_total": int64(2),
  2638. "op_project_0_records_out_total": int64(2),
  2639. "sink_mockSink_0_exceptions_total": int64(0),
  2640. "sink_mockSink_0_records_in_total": int64(2),
  2641. "sink_mockSink_0_records_out_total": int64(2),
  2642. "source_demoE_0_exceptions_total": int64(0),
  2643. "source_demoE_0_records_in_total": int64(6),
  2644. "source_demoE_0_records_out_total": int64(6),
  2645. "op_window_0_exceptions_total": int64(0),
  2646. "op_window_0_process_latency_ms": int64(0),
  2647. "op_window_0_records_in_total": int64(6),
  2648. "op_window_0_records_out_total": int64(4),
  2649. "op_filter_0_exceptions_total": int64(0),
  2650. "op_filter_0_process_latency_ms": int64(0),
  2651. "op_filter_0_records_in_total": int64(4),
  2652. "op_filter_0_records_out_total": int64(2),
  2653. },
  2654. }, {
  2655. name: `rule3`,
  2656. sql: `SELECT color, temp, ts FROM demoE INNER JOIN demo1E ON demoE.ts = demo1E.ts GROUP BY SlidingWindow(ss, 1)`,
  2657. size: 6,
  2658. r: [][]map[string]interface{}{
  2659. {{
  2660. "color": "red",
  2661. "temp": 25.5,
  2662. "ts": float64(1541152486013),
  2663. }}, {{
  2664. "color": "red",
  2665. "temp": 25.5,
  2666. "ts": float64(1541152486013),
  2667. }}, {{
  2668. "color": "blue",
  2669. "temp": 28.1,
  2670. "ts": float64(1541152487632),
  2671. }}, {{
  2672. "color": "blue",
  2673. "temp": 28.1,
  2674. "ts": float64(1541152487632),
  2675. }, {
  2676. "color": "yellow",
  2677. "temp": 27.4,
  2678. "ts": float64(1541152488442),
  2679. }}, {{
  2680. "color": "yellow",
  2681. "temp": 27.4,
  2682. "ts": float64(1541152488442),
  2683. }, {
  2684. "color": "red",
  2685. "temp": 25.5,
  2686. "ts": float64(1541152489252),
  2687. }},
  2688. },
  2689. m: map[string]interface{}{
  2690. "op_preprocessor_demoE_0_exceptions_total": int64(0),
  2691. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  2692. "op_preprocessor_demoE_0_records_in_total": int64(6),
  2693. "op_preprocessor_demoE_0_records_out_total": int64(6),
  2694. "op_preprocessor_demo1E_0_exceptions_total": int64(0),
  2695. "op_preprocessor_demo1E_0_process_latency_ms": int64(0),
  2696. "op_preprocessor_demo1E_0_records_in_total": int64(6),
  2697. "op_preprocessor_demo1E_0_records_out_total": int64(6),
  2698. "op_project_0_exceptions_total": int64(0),
  2699. "op_project_0_process_latency_ms": int64(0),
  2700. "op_project_0_records_in_total": int64(5),
  2701. "op_project_0_records_out_total": int64(5),
  2702. "sink_mockSink_0_exceptions_total": int64(0),
  2703. "sink_mockSink_0_records_in_total": int64(5),
  2704. "sink_mockSink_0_records_out_total": int64(5),
  2705. "source_demoE_0_exceptions_total": int64(0),
  2706. "source_demoE_0_records_in_total": int64(6),
  2707. "source_demoE_0_records_out_total": int64(6),
  2708. "source_demo1E_0_exceptions_total": int64(0),
  2709. "source_demo1E_0_records_in_total": int64(6),
  2710. "source_demo1E_0_records_out_total": int64(6),
  2711. "op_window_0_exceptions_total": int64(0),
  2712. "op_window_0_process_latency_ms": int64(0),
  2713. "op_window_0_records_in_total": int64(12),
  2714. "op_window_0_records_out_total": int64(5),
  2715. "op_join_0_exceptions_total": int64(0),
  2716. "op_join_0_process_latency_ms": int64(0),
  2717. "op_join_0_records_in_total": int64(5),
  2718. "op_join_0_records_out_total": int64(5),
  2719. },
  2720. }, {
  2721. name: `rule4`,
  2722. sql: `SELECT color FROM demoE GROUP BY SlidingWindow(ss, 2), color ORDER BY color`,
  2723. size: 6,
  2724. r: [][]map[string]interface{}{
  2725. {{
  2726. "color": "red",
  2727. }}, {{
  2728. "color": "blue",
  2729. }, {
  2730. "color": "red",
  2731. }}, {{
  2732. "color": "blue",
  2733. }, {
  2734. "color": "yellow",
  2735. }}, {{
  2736. "color": "blue",
  2737. }, {
  2738. "color": "red",
  2739. }, {
  2740. "color": "yellow",
  2741. }},
  2742. },
  2743. m: map[string]interface{}{
  2744. "op_preprocessor_demoE_0_exceptions_total": int64(0),
  2745. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  2746. "op_preprocessor_demoE_0_records_in_total": int64(6),
  2747. "op_preprocessor_demoE_0_records_out_total": int64(6),
  2748. "op_project_0_exceptions_total": int64(0),
  2749. "op_project_0_process_latency_ms": int64(0),
  2750. "op_project_0_records_in_total": int64(4),
  2751. "op_project_0_records_out_total": int64(4),
  2752. "sink_mockSink_0_exceptions_total": int64(0),
  2753. "sink_mockSink_0_records_in_total": int64(4),
  2754. "sink_mockSink_0_records_out_total": int64(4),
  2755. "source_demoE_0_exceptions_total": int64(0),
  2756. "source_demoE_0_records_in_total": int64(6),
  2757. "source_demoE_0_records_out_total": int64(6),
  2758. "op_window_0_exceptions_total": int64(0),
  2759. "op_window_0_process_latency_ms": int64(0),
  2760. "op_window_0_records_in_total": int64(6),
  2761. "op_window_0_records_out_total": int64(4),
  2762. "op_aggregate_0_exceptions_total": int64(0),
  2763. "op_aggregate_0_process_latency_ms": int64(0),
  2764. "op_aggregate_0_records_in_total": int64(4),
  2765. "op_aggregate_0_records_out_total": int64(4),
  2766. "op_order_0_exceptions_total": int64(0),
  2767. "op_order_0_process_latency_ms": int64(0),
  2768. "op_order_0_records_in_total": int64(4),
  2769. "op_order_0_records_out_total": int64(4),
  2770. },
  2771. }, {
  2772. name: `rule5`,
  2773. sql: `SELECT temp FROM sessionDemoE GROUP BY SessionWindow(ss, 2, 1) `,
  2774. size: 12,
  2775. r: [][]map[string]interface{}{
  2776. {{
  2777. "temp": 25.5,
  2778. }}, {{
  2779. "temp": 28.1,
  2780. }, {
  2781. "temp": 27.4,
  2782. }, {
  2783. "temp": 25.5,
  2784. }}, {{
  2785. "temp": 26.2,
  2786. }, {
  2787. "temp": 26.8,
  2788. }, {
  2789. "temp": 28.9,
  2790. }, {
  2791. "temp": 29.1,
  2792. }, {
  2793. "temp": 32.2,
  2794. }}, {{
  2795. "temp": 30.9,
  2796. }},
  2797. },
  2798. m: map[string]interface{}{
  2799. "op_preprocessor_sessionDemoE_0_exceptions_total": int64(0),
  2800. "op_preprocessor_sessionDemoE_0_process_latency_ms": int64(0),
  2801. "op_preprocessor_sessionDemoE_0_records_in_total": int64(12),
  2802. "op_preprocessor_sessionDemoE_0_records_out_total": int64(12),
  2803. "op_project_0_exceptions_total": int64(0),
  2804. "op_project_0_process_latency_ms": int64(0),
  2805. "op_project_0_records_in_total": int64(4),
  2806. "op_project_0_records_out_total": int64(4),
  2807. "sink_mockSink_0_exceptions_total": int64(0),
  2808. "sink_mockSink_0_records_in_total": int64(4),
  2809. "sink_mockSink_0_records_out_total": int64(4),
  2810. "source_sessionDemoE_0_exceptions_total": int64(0),
  2811. "source_sessionDemoE_0_records_in_total": int64(12),
  2812. "source_sessionDemoE_0_records_out_total": int64(12),
  2813. "op_window_0_exceptions_total": int64(0),
  2814. "op_window_0_process_latency_ms": int64(0),
  2815. "op_window_0_records_in_total": int64(12),
  2816. "op_window_0_records_out_total": int64(4),
  2817. },
  2818. }, {
  2819. name: `rule6`,
  2820. sql: `SELECT max(temp) as m, count(color) as c FROM demoE INNER JOIN demo1E ON demoE.ts = demo1E.ts GROUP BY SlidingWindow(ss, 1)`,
  2821. size: 6,
  2822. r: [][]map[string]interface{}{
  2823. {{
  2824. "m": 25.5,
  2825. "c": float64(1),
  2826. }}, {{
  2827. "m": 25.5,
  2828. "c": float64(1),
  2829. }}, {{
  2830. "m": 28.1,
  2831. "c": float64(1),
  2832. }}, {{
  2833. "m": 28.1,
  2834. "c": float64(2),
  2835. }}, {{
  2836. "m": 27.4,
  2837. "c": float64(2),
  2838. }},
  2839. },
  2840. m: map[string]interface{}{
  2841. "op_preprocessor_demoE_0_exceptions_total": int64(0),
  2842. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  2843. "op_preprocessor_demoE_0_records_in_total": int64(6),
  2844. "op_preprocessor_demoE_0_records_out_total": int64(6),
  2845. "op_preprocessor_demo1E_0_exceptions_total": int64(0),
  2846. "op_preprocessor_demo1E_0_process_latency_ms": int64(0),
  2847. "op_preprocessor_demo1E_0_records_in_total": int64(6),
  2848. "op_preprocessor_demo1E_0_records_out_total": int64(6),
  2849. "op_project_0_exceptions_total": int64(0),
  2850. "op_project_0_process_latency_ms": int64(0),
  2851. "op_project_0_records_in_total": int64(5),
  2852. "op_project_0_records_out_total": int64(5),
  2853. "sink_mockSink_0_exceptions_total": int64(0),
  2854. "sink_mockSink_0_records_in_total": int64(5),
  2855. "sink_mockSink_0_records_out_total": int64(5),
  2856. "source_demoE_0_exceptions_total": int64(0),
  2857. "source_demoE_0_records_in_total": int64(6),
  2858. "source_demoE_0_records_out_total": int64(6),
  2859. "source_demo1E_0_exceptions_total": int64(0),
  2860. "source_demo1E_0_records_in_total": int64(6),
  2861. "source_demo1E_0_records_out_total": int64(6),
  2862. "op_window_0_exceptions_total": int64(0),
  2863. "op_window_0_records_in_total": int64(12),
  2864. "op_window_0_records_out_total": int64(5),
  2865. "op_join_0_exceptions_total": int64(0),
  2866. "op_join_0_process_latency_ms": int64(0),
  2867. "op_join_0_records_in_total": int64(5),
  2868. "op_join_0_records_out_total": int64(5),
  2869. },
  2870. }, {
  2871. name: `rule7`,
  2872. sql: `SELECT * FROM demoErr GROUP BY HOPPINGWINDOW(ss, 2, 1)`,
  2873. size: 6,
  2874. r: [][]map[string]interface{}{
  2875. {{
  2876. "error": "error in preprocessor: invalid data type for color, expect string but found int(2)",
  2877. }},
  2878. {{
  2879. "color": "red",
  2880. "size": float64(3),
  2881. "ts": float64(1541152486013),
  2882. }},
  2883. {{
  2884. "color": "red",
  2885. "size": float64(3),
  2886. "ts": float64(1541152486013),
  2887. }},
  2888. {{
  2889. "color": "yellow",
  2890. "size": float64(4),
  2891. "ts": float64(1541152488442),
  2892. }}, {{
  2893. "color": "yellow",
  2894. "size": float64(4),
  2895. "ts": float64(1541152488442),
  2896. }, {
  2897. "color": "red",
  2898. "size": float64(1),
  2899. "ts": float64(1541152489252),
  2900. }}, {{
  2901. "color": "red",
  2902. "size": float64(1),
  2903. "ts": float64(1541152489252),
  2904. }},
  2905. },
  2906. m: map[string]interface{}{
  2907. "op_preprocessor_demoErr_0_exceptions_total": int64(1),
  2908. "op_preprocessor_demoErr_0_process_latency_ms": int64(0),
  2909. "op_preprocessor_demoErr_0_records_in_total": int64(6),
  2910. "op_preprocessor_demoErr_0_records_out_total": int64(5),
  2911. "op_project_0_exceptions_total": int64(1),
  2912. "op_project_0_process_latency_ms": int64(0),
  2913. "op_project_0_records_in_total": int64(6),
  2914. "op_project_0_records_out_total": int64(5),
  2915. "sink_mockSink_0_exceptions_total": int64(0),
  2916. "sink_mockSink_0_records_in_total": int64(6),
  2917. "sink_mockSink_0_records_out_total": int64(6),
  2918. "source_demoErr_0_exceptions_total": int64(0),
  2919. "source_demoErr_0_records_in_total": int64(6),
  2920. "source_demoErr_0_records_out_total": int64(6),
  2921. "op_window_0_exceptions_total": int64(1),
  2922. "op_window_0_process_latency_ms": int64(0),
  2923. "op_window_0_records_in_total": int64(6),
  2924. "op_window_0_records_out_total": int64(5),
  2925. },
  2926. },
  2927. }
  2928. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2929. createEventStreams(t)
  2930. defer dropEventStreams(t)
  2931. for i, tt := range tests {
  2932. test.ResetClock(1541152486000)
  2933. p := NewRuleProcessor(DbDir)
  2934. parser := xsql.NewParser(strings.NewReader(tt.sql))
  2935. var (
  2936. sources []*nodes.SourceNode
  2937. syncs []chan int
  2938. )
  2939. if stmt, err := xsql.Language.Parse(parser); err != nil {
  2940. t.Errorf("parse sql %s error: %s", tt.sql, err)
  2941. } else {
  2942. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  2943. t.Errorf("sql %s is not a select statement", tt.sql)
  2944. } else {
  2945. streams := xsql.GetStreams(selectStmt)
  2946. for _, stream := range streams {
  2947. next := make(chan int)
  2948. syncs = append(syncs, next)
  2949. source := getEventMockSource(stream, next, tt.size)
  2950. sources = append(sources, source)
  2951. }
  2952. }
  2953. }
  2954. tp, inputs, err := p.createTopoWithSources(&api.Rule{
  2955. Id: tt.name, Sql: tt.sql,
  2956. Options: map[string]interface{}{
  2957. "isEventTime": true,
  2958. "lateTolerance": float64(1000),
  2959. },
  2960. }, sources)
  2961. if err != nil {
  2962. t.Error(err)
  2963. }
  2964. mockSink := test.NewMockSink()
  2965. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink, nil)
  2966. tp.AddSink(inputs, sink)
  2967. errCh := tp.Open()
  2968. func() {
  2969. for i := 0; i < tt.size*len(syncs); i++ {
  2970. syncs[i%len(syncs)] <- i
  2971. for {
  2972. time.Sleep(1)
  2973. if getMetric(tp, "op_window_0_records_in_total") == (i + 1) {
  2974. break
  2975. }
  2976. }
  2977. select {
  2978. case err = <-errCh:
  2979. t.Log(err)
  2980. tp.Cancel()
  2981. return
  2982. default:
  2983. }
  2984. }
  2985. mockClock := test.GetMockClock()
  2986. mockClock.Add(1000 * time.Millisecond)
  2987. retry := 100
  2988. for ; retry > 0; retry-- {
  2989. if err := compareMetrics(tp, tt.m, tt.sql); err == nil {
  2990. break
  2991. }
  2992. t.Logf("wait to try another %d times", retry)
  2993. time.Sleep(time.Duration(retry) * time.Millisecond)
  2994. }
  2995. if retry == 0 {
  2996. err := compareMetrics(tp, tt.m, tt.sql)
  2997. t.Errorf("could not get correct metrics: %v", err)
  2998. }
  2999. }()
  3000. results := mockSink.GetResults()
  3001. var maps [][]map[string]interface{}
  3002. for _, v := range results {
  3003. var mapRes []map[string]interface{}
  3004. err := json.Unmarshal(v, &mapRes)
  3005. if err != nil {
  3006. t.Errorf("Failed to parse the input into map")
  3007. continue
  3008. }
  3009. maps = append(maps, mapRes)
  3010. }
  3011. if !reflect.DeepEqual(tt.r, maps) {
  3012. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, maps)
  3013. }
  3014. if err := compareMetrics(tp, tt.m, tt.sql); err != nil {
  3015. t.Errorf("%d. %q\n\n%v", i, tt.sql, err)
  3016. }
  3017. tp.Cancel()
  3018. }
  3019. }
  3020. func getMetric(tp *xstream.TopologyNew, name string) int {
  3021. keys, values := tp.GetMetrics()
  3022. for index, key := range keys {
  3023. if key == name {
  3024. return int(values[index].(int64))
  3025. }
  3026. }
  3027. fmt.Println("can't find " + name)
  3028. return 0
  3029. }
  3030. func compareMetrics(tp *xstream.TopologyNew, m map[string]interface{}, sql string) (err error) {
  3031. keys, values := tp.GetMetrics()
  3032. //for i, k := range keys {
  3033. // log.Printf("%s:%v", k, values[i])
  3034. //}
  3035. for k, v := range m {
  3036. var (
  3037. index int
  3038. key string
  3039. matched bool
  3040. )
  3041. for index, key = range keys {
  3042. if k == key {
  3043. if strings.HasSuffix(k, "process_latency_ms") {
  3044. if values[index].(int64) >= v.(int64) {
  3045. matched = true
  3046. continue
  3047. } else {
  3048. break
  3049. }
  3050. }
  3051. if values[index] == v {
  3052. matched = true
  3053. }
  3054. break
  3055. }
  3056. }
  3057. if matched {
  3058. continue
  3059. }
  3060. //do not find
  3061. if index < len(values) {
  3062. return fmt.Errorf("metrics mismatch for %s:\n\nexp=%#v(%t)\n\ngot=%#v(%t)\n\n", k, v, v, values[index], values[index])
  3063. } else {
  3064. return fmt.Errorf("metrics mismatch for %s:\n\nexp=%#v\n\ngot=nil\n\n", k, v)
  3065. }
  3066. }
  3067. return nil
  3068. }
  3069. func errstring(err error) string {
  3070. if err != nil {
  3071. return err.Error()
  3072. }
  3073. return ""
  3074. }