window_op_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package node
  2. import (
  3. "fmt"
  4. "github.com/emqx/kuiper/internal/xsql"
  5. "reflect"
  6. "testing"
  7. )
  8. var fivet = []*xsql.Tuple{
  9. {
  10. Message: map[string]interface{}{
  11. "f1": "v1",
  12. },
  13. },
  14. {
  15. Message: map[string]interface{}{
  16. "f2": "v2",
  17. },
  18. },
  19. {
  20. Message: map[string]interface{}{
  21. "f3": "v3",
  22. },
  23. },
  24. {
  25. Message: map[string]interface{}{
  26. "f4": "v4",
  27. },
  28. },
  29. {
  30. Message: map[string]interface{}{
  31. "f5": "v5",
  32. },
  33. },
  34. }
  35. func TestNewTupleList(t *testing.T) {
  36. _, e := NewTupleList(nil, 0)
  37. es1 := "Window size should not be less than zero."
  38. if !reflect.DeepEqual(es1, e.Error()) {
  39. t.Errorf("error mismatch:\n exp=%s\n got=%s\n\n", es1, e)
  40. }
  41. _, e = NewTupleList(nil, 2)
  42. es1 = "The tuples should not be nil or empty."
  43. if !reflect.DeepEqual(es1, e.Error()) {
  44. t.Errorf("error mismatch:\n exp=%s\n got=%s\n\n", es1, e)
  45. }
  46. }
  47. func TestCountWindow(t *testing.T) {
  48. var tests = []struct {
  49. tuplelist TupleList
  50. expWinCount int
  51. winTupleSets []xsql.WindowTuplesSet
  52. expRestTuples []*xsql.Tuple
  53. }{
  54. {
  55. tuplelist: TupleList{
  56. tuples: fivet,
  57. size: 5,
  58. },
  59. expWinCount: 1,
  60. winTupleSets: []xsql.WindowTuplesSet{
  61. {
  62. Content: []xsql.WindowTuples{
  63. {
  64. Emitter: "",
  65. Tuples: []xsql.Tuple{
  66. {
  67. Message: map[string]interface{}{
  68. "f1": "v1",
  69. },
  70. },
  71. {
  72. Message: map[string]interface{}{
  73. "f2": "v2",
  74. },
  75. },
  76. {
  77. Message: map[string]interface{}{
  78. "f3": "v3",
  79. },
  80. },
  81. {
  82. Message: map[string]interface{}{
  83. "f4": "v4",
  84. },
  85. },
  86. {
  87. Message: map[string]interface{}{
  88. "f5": "v5",
  89. },
  90. },
  91. },
  92. },
  93. },
  94. },
  95. },
  96. expRestTuples: []*xsql.Tuple{
  97. {
  98. Message: map[string]interface{}{
  99. "f2": "v2",
  100. },
  101. },
  102. {
  103. Message: map[string]interface{}{
  104. "f3": "v3",
  105. },
  106. },
  107. {
  108. Message: map[string]interface{}{
  109. "f4": "v4",
  110. },
  111. },
  112. {
  113. Message: map[string]interface{}{
  114. "f5": "v5",
  115. },
  116. },
  117. },
  118. },
  119. {
  120. tuplelist: TupleList{
  121. tuples: fivet,
  122. size: 3,
  123. },
  124. expWinCount: 1,
  125. winTupleSets: []xsql.WindowTuplesSet{
  126. {
  127. Content: []xsql.WindowTuples{
  128. {
  129. Emitter: "",
  130. Tuples: []xsql.Tuple{
  131. {
  132. Message: map[string]interface{}{
  133. "f3": "v3",
  134. },
  135. },
  136. {
  137. Message: map[string]interface{}{
  138. "f4": "v4",
  139. },
  140. },
  141. {
  142. Message: map[string]interface{}{
  143. "f5": "v5",
  144. },
  145. },
  146. },
  147. },
  148. },
  149. },
  150. },
  151. expRestTuples: []*xsql.Tuple{
  152. {
  153. Message: map[string]interface{}{
  154. "f4": "v4",
  155. },
  156. },
  157. {
  158. Message: map[string]interface{}{
  159. "f5": "v5",
  160. },
  161. },
  162. },
  163. },
  164. {
  165. tuplelist: TupleList{
  166. tuples: fivet,
  167. size: 2,
  168. },
  169. expWinCount: 1,
  170. winTupleSets: []xsql.WindowTuplesSet{
  171. {
  172. Content: []xsql.WindowTuples{
  173. {
  174. Emitter: "",
  175. Tuples: []xsql.Tuple{
  176. {
  177. Message: map[string]interface{}{
  178. "f4": "v4",
  179. },
  180. },
  181. {
  182. Message: map[string]interface{}{
  183. "f5": "v5",
  184. },
  185. },
  186. },
  187. },
  188. },
  189. },
  190. },
  191. expRestTuples: []*xsql.Tuple{
  192. {
  193. Message: map[string]interface{}{
  194. "f5": "v5",
  195. },
  196. },
  197. },
  198. },
  199. {
  200. tuplelist: TupleList{
  201. tuples: fivet,
  202. size: 6,
  203. },
  204. expWinCount: 0,
  205. winTupleSets: nil,
  206. expRestTuples: []*xsql.Tuple{
  207. {
  208. Message: map[string]interface{}{
  209. "f1": "v1",
  210. },
  211. },
  212. {
  213. Message: map[string]interface{}{
  214. "f2": "v2",
  215. },
  216. },
  217. {
  218. Message: map[string]interface{}{
  219. "f3": "v3",
  220. },
  221. },
  222. {
  223. Message: map[string]interface{}{
  224. "f4": "v4",
  225. },
  226. },
  227. {
  228. Message: map[string]interface{}{
  229. "f5": "v5",
  230. },
  231. },
  232. },
  233. },
  234. }
  235. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  236. for i, tt := range tests {
  237. if tt.expWinCount == 0 {
  238. if tt.tuplelist.hasMoreCountWindow() {
  239. t.Errorf("%d \n Should not have more count window.", i)
  240. }
  241. } else {
  242. for j := 0; j < tt.expWinCount; j++ {
  243. if !tt.tuplelist.hasMoreCountWindow() {
  244. t.Errorf("%d \n Expect more element, but cannot find more element.", i)
  245. }
  246. cw := tt.tuplelist.nextCountWindow()
  247. if !reflect.DeepEqual(tt.winTupleSets[j], cw) {
  248. t.Errorf("%d. \nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.winTupleSets[j], cw)
  249. }
  250. }
  251. rest := tt.tuplelist.getRestTuples()
  252. if !reflect.DeepEqual(tt.expRestTuples, rest) {
  253. t.Errorf("%d. \nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.expRestTuples, rest)
  254. }
  255. }
  256. }
  257. }