sync_cache_test.go 6.4 KB

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