window_op_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package nodes
  2. import (
  3. "fmt"
  4. "github.com/emqx/kuiper/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. xsql.WindowTuples{
  63. Emitter: "",
  64. Tuples: []xsql.Tuple{
  65. {
  66. Message: map[string]interface{}{
  67. "f1": "v1",
  68. },
  69. },
  70. {
  71. Message: map[string]interface{}{
  72. "f2": "v2",
  73. },
  74. },
  75. {
  76. Message: map[string]interface{}{
  77. "f3": "v3",
  78. },
  79. },
  80. {
  81. Message: map[string]interface{}{
  82. "f4": "v4",
  83. },
  84. },
  85. {
  86. Message: map[string]interface{}{
  87. "f5": "v5",
  88. },
  89. },
  90. },
  91. },
  92. },
  93. },
  94. expRestTuples: []*xsql.Tuple{
  95. {
  96. Message: map[string]interface{}{
  97. "f2": "v2",
  98. },
  99. },
  100. {
  101. Message: map[string]interface{}{
  102. "f3": "v3",
  103. },
  104. },
  105. {
  106. Message: map[string]interface{}{
  107. "f4": "v4",
  108. },
  109. },
  110. {
  111. Message: map[string]interface{}{
  112. "f5": "v5",
  113. },
  114. },
  115. },
  116. },
  117. {
  118. tuplelist: TupleList{
  119. tuples: fivet,
  120. size: 3,
  121. },
  122. expWinCount: 1,
  123. winTupleSets: []xsql.WindowTuplesSet{
  124. {
  125. xsql.WindowTuples{
  126. Emitter: "",
  127. Tuples: []xsql.Tuple{
  128. {
  129. Message: map[string]interface{}{
  130. "f3": "v3",
  131. },
  132. },
  133. {
  134. Message: map[string]interface{}{
  135. "f4": "v4",
  136. },
  137. },
  138. {
  139. Message: map[string]interface{}{
  140. "f5": "v5",
  141. },
  142. },
  143. },
  144. },
  145. },
  146. },
  147. expRestTuples: []*xsql.Tuple{
  148. {
  149. Message: map[string]interface{}{
  150. "f4": "v4",
  151. },
  152. },
  153. {
  154. Message: map[string]interface{}{
  155. "f5": "v5",
  156. },
  157. },
  158. },
  159. },
  160. {
  161. tuplelist: TupleList{
  162. tuples: fivet,
  163. size: 2,
  164. },
  165. expWinCount: 1,
  166. winTupleSets: []xsql.WindowTuplesSet{
  167. {
  168. xsql.WindowTuples{
  169. Emitter: "",
  170. Tuples: []xsql.Tuple{
  171. {
  172. Message: map[string]interface{}{
  173. "f4": "v4",
  174. },
  175. },
  176. {
  177. Message: map[string]interface{}{
  178. "f5": "v5",
  179. },
  180. },
  181. },
  182. },
  183. },
  184. },
  185. expRestTuples: []*xsql.Tuple{
  186. {
  187. Message: map[string]interface{}{
  188. "f5": "v5",
  189. },
  190. },
  191. },
  192. },
  193. {
  194. tuplelist: TupleList{
  195. tuples: fivet,
  196. size: 6,
  197. },
  198. expWinCount: 0,
  199. winTupleSets: nil,
  200. expRestTuples: []*xsql.Tuple{
  201. {
  202. Message: map[string]interface{}{
  203. "f1": "v1",
  204. },
  205. },
  206. {
  207. Message: map[string]interface{}{
  208. "f2": "v2",
  209. },
  210. },
  211. {
  212. Message: map[string]interface{}{
  213. "f3": "v3",
  214. },
  215. },
  216. {
  217. Message: map[string]interface{}{
  218. "f4": "v4",
  219. },
  220. },
  221. {
  222. Message: map[string]interface{}{
  223. "f5": "v5",
  224. },
  225. },
  226. },
  227. },
  228. }
  229. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  230. for i, tt := range tests {
  231. if tt.expWinCount == 0 {
  232. if tt.tuplelist.hasMoreCountWindow() {
  233. t.Errorf("%d \n Should not have more count window.", i)
  234. }
  235. } else {
  236. for j := 0; j < tt.expWinCount; j++ {
  237. if !tt.tuplelist.hasMoreCountWindow() {
  238. t.Errorf("%d \n Expect more element, but cannot find more element.", i)
  239. }
  240. cw := tt.tuplelist.nextCountWindow()
  241. if !reflect.DeepEqual(tt.winTupleSets[j], cw) {
  242. t.Errorf("%d. \nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.winTupleSets[j], cw)
  243. }
  244. }
  245. rest := tt.tuplelist.getRestTuples()
  246. if !reflect.DeepEqual(tt.expRestTuples, rest) {
  247. t.Errorf("%d. \nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.expRestTuples, rest)
  248. }
  249. }
  250. }
  251. }