sync_cache_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // Copyright 2022 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package cache
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/conf"
  18. "github.com/lf-edge/ekuiper/internal/testx"
  19. "github.com/lf-edge/ekuiper/internal/topo/context"
  20. "github.com/lf-edge/ekuiper/internal/topo/node/metric"
  21. "github.com/lf-edge/ekuiper/internal/topo/state"
  22. "github.com/lf-edge/ekuiper/pkg/api"
  23. "os"
  24. "path/filepath"
  25. "reflect"
  26. "testing"
  27. "time"
  28. )
  29. func TestPage(t *testing.T) {
  30. p := newPage(2)
  31. if !p.isEmpty() {
  32. t.Errorf("page is not empty")
  33. }
  34. if !p.append([]map[string]interface{}{
  35. {"a": 1},
  36. }) {
  37. t.Fatal("append failed")
  38. }
  39. if !p.append([]map[string]interface{}{
  40. {"a": 2},
  41. }) {
  42. t.Fatal("append failed")
  43. }
  44. if p.append([]map[string]interface{}{
  45. {"a": 3},
  46. }) {
  47. t.Fatal("should append fail")
  48. }
  49. v, ok := p.peak()
  50. if !ok {
  51. t.Fatal("peak failed")
  52. }
  53. if !reflect.DeepEqual(v, []map[string]interface{}{
  54. {"a": 1},
  55. }) {
  56. t.Fatalf("peak value mismatch, expect 1 but got %v", v)
  57. }
  58. if p.append([]map[string]interface{}{
  59. {"a": 4},
  60. }) {
  61. t.Fatal("should append failed")
  62. }
  63. if !p.delete() {
  64. t.Fatal("delete failed")
  65. }
  66. v, ok = p.peak()
  67. if !ok {
  68. t.Fatal("peak failed")
  69. }
  70. if !reflect.DeepEqual(v, []map[string]interface{}{
  71. {"a": 2},
  72. }) {
  73. t.Fatalf("peak value mismatch, expect 2 but got %v", v)
  74. }
  75. p.reset()
  76. if !p.append([]map[string]interface{}{
  77. {"a": 5},
  78. }) {
  79. t.Fatal("append failed")
  80. }
  81. if p.isEmpty() {
  82. t.Fatal("page should not empty")
  83. }
  84. if !p.delete() {
  85. t.Fatal("delete failed")
  86. }
  87. if !p.append([]map[string]interface{}{
  88. {"a": 5},
  89. }) {
  90. t.Fatal("append failed")
  91. }
  92. if !p.append([]map[string]interface{}{
  93. {"a": 6},
  94. }) {
  95. t.Fatal("append failed")
  96. }
  97. if !p.delete() {
  98. t.Fatal("delete failed")
  99. }
  100. if !p.delete() {
  101. t.Fatal("delete failed")
  102. }
  103. if p.delete() {
  104. t.Fatal("should delete failed")
  105. }
  106. if !p.isEmpty() {
  107. t.Fatal("page should be empty")
  108. }
  109. }
  110. // TestRun test for
  111. // 1. cache in memory only
  112. // 2. cache in memory and disk buffer only
  113. // 3. cache in memory and disk
  114. // 4. cache in memory and disk buffer and overflow
  115. // Each flow test rule restart
  116. // Each flow use slightly different config like bufferPageSize
  117. func TestRun(t *testing.T) {
  118. var tests = []struct {
  119. sconf *conf.SinkConf
  120. dataIn [][]map[string]interface{}
  121. dataOut [][]map[string]interface{}
  122. stopPt int // restart the rule in this point
  123. }{
  124. { // 0
  125. sconf: &conf.SinkConf{
  126. MemoryCacheThreshold: 4,
  127. MaxDiskCache: 12,
  128. BufferPageSize: 2,
  129. EnableCache: true,
  130. ResendInterval: 0,
  131. CleanCacheAtStop: false,
  132. },
  133. dataIn: [][]map[string]interface{}{
  134. {{"a": 1}}, {{"a": 2}}, {{"a": 3}}, {{"a": 4}}, {{"a": 5}},
  135. },
  136. stopPt: 4,
  137. },
  138. { // 1
  139. sconf: &conf.SinkConf{
  140. MemoryCacheThreshold: 4,
  141. MaxDiskCache: 8,
  142. BufferPageSize: 2,
  143. EnableCache: true,
  144. ResendInterval: 0,
  145. CleanCacheAtStop: false,
  146. },
  147. dataIn: [][]map[string]interface{}{
  148. {{"a": 1}}, {{"a": 2}}, {{"a": 3}}, {{"a": 4}}, {{"a": 5}}, {{"a": 6}},
  149. },
  150. stopPt: 5,
  151. },
  152. { // 2
  153. sconf: &conf.SinkConf{
  154. MemoryCacheThreshold: 1,
  155. MaxDiskCache: 8,
  156. BufferPageSize: 1,
  157. EnableCache: true,
  158. ResendInterval: 0,
  159. CleanCacheAtStop: false,
  160. },
  161. dataIn: [][]map[string]interface{}{
  162. {{"a": 1}}, {{"a": 2}}, {{"a": 3}}, {{"a": 4}}, {{"a": 5}}, {{"a": 6}},
  163. },
  164. stopPt: 4,
  165. },
  166. { // 3
  167. sconf: &conf.SinkConf{
  168. MemoryCacheThreshold: 2,
  169. MaxDiskCache: 4,
  170. BufferPageSize: 2,
  171. EnableCache: true,
  172. ResendInterval: 0,
  173. CleanCacheAtStop: false,
  174. },
  175. dataIn: [][]map[string]interface{}{
  176. {{"a": 1}}, {{"a": 2}}, {{"a": 3}}, {{"a": 4}}, {{"a": 5}}, {{"a": 6}}, {{"a": 7}}, {{"a": 8}}, {{"a": 9}}, {{"a": 10}}, {{"a": 11}}, {{"a": 12}}, {{"a": 13}},
  177. },
  178. dataOut: [][]map[string]interface{}{
  179. {{"a": 1}}, {{"a": 6}}, {{"a": 7}}, {{"a": 8}}, {{"a": 9}}, {{"a": 10}}, {{"a": 11}}, {{"a": 12}}, {{"a": 13}},
  180. },
  181. stopPt: 4,
  182. },
  183. }
  184. testx.InitEnv()
  185. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  186. tempStore, _ := state.CreateStore("mock", api.AtMostOnce)
  187. deleteCachedb()
  188. for i, tt := range tests {
  189. contextLogger := conf.Log.WithField("rule", fmt.Sprintf("TestRun-%d", i))
  190. ctx, cancel := context.WithValue(context.Background(), context.LoggerKey, contextLogger).WithMeta(fmt.Sprintf("rule%d", i), fmt.Sprintf("op%d", i), tempStore).WithCancel()
  191. stats, err := metric.NewStatManager(ctx, "sink")
  192. if err != nil {
  193. t.Fatal(err)
  194. return
  195. }
  196. in := make(chan []map[string]interface{})
  197. errCh := make(chan error)
  198. var result []interface{}
  199. go func() {
  200. err := <-errCh
  201. t.Fatal(err)
  202. return
  203. }()
  204. // send data
  205. sc := NewSyncCache(ctx, in, errCh, stats, tt.sconf, 100)
  206. for i := 0; i < tt.stopPt; i++ {
  207. in <- tt.dataIn[i]
  208. time.Sleep(1 * time.Millisecond)
  209. }
  210. cancel()
  211. // send the second half data
  212. ctx, cancel = context.WithValue(context.Background(), context.LoggerKey, contextLogger).WithMeta(fmt.Sprintf("rule%d", i), fmt.Sprintf("op%d", i), tempStore).WithCancel()
  213. sc = NewSyncCache(ctx, in, errCh, stats, tt.sconf, 100)
  214. for i := tt.stopPt; i < len(tt.dataIn); i++ {
  215. in <- tt.dataIn[i]
  216. time.Sleep(1 * time.Millisecond)
  217. }
  218. loop:
  219. for range tt.dataIn {
  220. sc.Ack <- true
  221. select {
  222. case r := <-sc.Out:
  223. result = append(result, r)
  224. case <-time.After(1 * time.Second):
  225. t.Log(fmt.Sprintf("test %d no data", i))
  226. break loop
  227. }
  228. }
  229. cancel()
  230. if tt.dataOut == nil {
  231. tt.dataOut = tt.dataIn
  232. }
  233. if len(tt.dataOut) != len(result) {
  234. t.Errorf("test %d data mismatch\nexpect\t%v\nbut got\t%v", i, tt.dataOut, result)
  235. continue
  236. }
  237. for i, v := range result {
  238. if !reflect.DeepEqual(tt.dataOut[i], v) {
  239. t.Errorf("test %d data mismatch\nexpect\t%v\nbut got\t%v", i, tt.dataOut, result)
  240. break
  241. }
  242. }
  243. }
  244. }
  245. func deleteCachedb() {
  246. loc, err := conf.GetDataLoc()
  247. if err != nil {
  248. fmt.Println(err)
  249. }
  250. err = os.RemoveAll(filepath.Join(loc, "cache.db"))
  251. if err != nil {
  252. fmt.Println(err)
  253. }
  254. }