You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

clean_pinned_test.go 7.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package event
  2. import (
  3. "testing"
  4. . "github.com/smartystreets/goconvey/convey"
  5. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  6. )
  7. func newTreeTest(nodeBlocksMap []bitmap) combinatorialTree {
  8. tree := combinatorialTree{
  9. blocksMaps: make(map[int]bitmap),
  10. nodeIDToLocalNodeID: make(map[cdssdk.NodeID]int),
  11. }
  12. tree.nodes = make([]combinatorialTreeNode, (1 << len(nodeBlocksMap)))
  13. for id, mp := range nodeBlocksMap {
  14. tree.nodeIDToLocalNodeID[cdssdk.NodeID(id)] = len(tree.localNodeIDToNodeID)
  15. tree.blocksMaps[len(tree.localNodeIDToNodeID)] = mp
  16. tree.localNodeIDToNodeID = append(tree.localNodeIDToNodeID, cdssdk.NodeID(id))
  17. }
  18. tree.nodes[0].localNodeID = -1
  19. index := 1
  20. tree.initNode(0, &tree.nodes[0], &index)
  21. return tree
  22. }
  23. func Test_iterCombBits(t *testing.T) {
  24. testcases := []struct {
  25. title string
  26. width int
  27. count int
  28. expectedValues []int
  29. }{
  30. {
  31. title: "1 of 4",
  32. width: 4,
  33. count: 1,
  34. expectedValues: []int{16, 8, 4, 2},
  35. },
  36. {
  37. title: "2 of 4",
  38. width: 4,
  39. count: 2,
  40. expectedValues: []int{24, 20, 18, 12, 10, 6},
  41. },
  42. {
  43. title: "3 of 4",
  44. width: 4,
  45. count: 3,
  46. expectedValues: []int{28, 26, 22, 14},
  47. },
  48. {
  49. title: "4 of 4",
  50. width: 4,
  51. count: 4,
  52. expectedValues: []int{30},
  53. },
  54. }
  55. for _, test := range testcases {
  56. Convey(test.title, t, func() {
  57. var ret []int
  58. var t combinatorialTree
  59. t.iterCombBits(test.width, test.count, 0, func(i int) {
  60. ret = append(ret, i)
  61. })
  62. So(ret, ShouldResemble, test.expectedValues)
  63. })
  64. }
  65. }
  66. func Test_newCombinatorialTree(t *testing.T) {
  67. testcases := []struct {
  68. title string
  69. nodeBlocks []bitmap
  70. expectedTreeNodeLocalIDs []int
  71. expectedTreeNodeBitmaps []int
  72. }{
  73. {
  74. title: "1个节点",
  75. nodeBlocks: []bitmap{1},
  76. expectedTreeNodeLocalIDs: []int{-1, 0},
  77. expectedTreeNodeBitmaps: []int{0, 1},
  78. },
  79. {
  80. title: "2个节点",
  81. nodeBlocks: []bitmap{1, 0},
  82. expectedTreeNodeLocalIDs: []int{-1, 0, 1, 1},
  83. expectedTreeNodeBitmaps: []int{0, 1, 1, 0},
  84. },
  85. {
  86. title: "4个节点",
  87. nodeBlocks: []bitmap{1, 2, 4, 8},
  88. expectedTreeNodeLocalIDs: []int{-1, 0, 1, 2, 3, 3, 2, 3, 3, 1, 2, 3, 3, 2, 3, 3},
  89. expectedTreeNodeBitmaps: []int{0, 1, 3, 7, 15, 11, 5, 13, 9, 2, 6, 14, 10, 4, 12, 8},
  90. },
  91. }
  92. for _, test := range testcases {
  93. Convey(test.title, t, func() {
  94. t := newTreeTest(test.nodeBlocks)
  95. var localIDs []int
  96. var bitmaps []int
  97. for _, n := range t.nodes {
  98. localIDs = append(localIDs, n.localNodeID)
  99. bitmaps = append(bitmaps, int(n.blocksBitmap))
  100. }
  101. So(localIDs, ShouldResemble, test.expectedTreeNodeLocalIDs)
  102. So(bitmaps, ShouldResemble, test.expectedTreeNodeBitmaps)
  103. })
  104. }
  105. }
  106. func Test_UpdateBitmap(t *testing.T) {
  107. testcases := []struct {
  108. title string
  109. nodeBlocks []bitmap
  110. updatedNodeID cdssdk.NodeID
  111. updatedBitmap bitmap
  112. k int
  113. expectedTreeNodeBitmaps []int
  114. }{
  115. {
  116. title: "4个节点,更新但值不变",
  117. nodeBlocks: []bitmap{1, 2, 4, 8},
  118. updatedNodeID: cdssdk.NodeID(0),
  119. updatedBitmap: bitmap(1),
  120. k: 4,
  121. expectedTreeNodeBitmaps: []int{0, 1, 3, 7, 15, 11, 5, 13, 9, 2, 6, 14, 10, 4, 12, 8},
  122. },
  123. {
  124. title: "4个节点,更新0",
  125. nodeBlocks: []bitmap{1, 2, 4, 8},
  126. updatedNodeID: cdssdk.NodeID(0),
  127. updatedBitmap: bitmap(2),
  128. k: 4,
  129. expectedTreeNodeBitmaps: []int{0, 2, 2, 6, 14, 10, 6, 14, 10, 2, 6, 14, 10, 4, 12, 8},
  130. },
  131. {
  132. title: "4个节点,更新1",
  133. nodeBlocks: []bitmap{1, 2, 4, 8},
  134. updatedNodeID: cdssdk.NodeID(1),
  135. updatedBitmap: bitmap(1),
  136. k: 4,
  137. expectedTreeNodeBitmaps: []int{0, 1, 1, 5, 13, 9, 5, 13, 9, 1, 5, 13, 9, 4, 12, 8},
  138. },
  139. {
  140. title: "4个节点,更新2",
  141. nodeBlocks: []bitmap{1, 2, 4, 8},
  142. updatedNodeID: cdssdk.NodeID(2),
  143. updatedBitmap: bitmap(1),
  144. k: 4,
  145. expectedTreeNodeBitmaps: []int{0, 1, 3, 3, 11, 11, 1, 9, 9, 2, 3, 11, 10, 1, 9, 8},
  146. },
  147. {
  148. title: "4个节点,更新3",
  149. nodeBlocks: []bitmap{1, 2, 4, 8},
  150. updatedNodeID: cdssdk.NodeID(3),
  151. updatedBitmap: bitmap(1),
  152. k: 4,
  153. expectedTreeNodeBitmaps: []int{0, 1, 3, 7, 7, 3, 5, 5, 1, 2, 6, 7, 3, 4, 5, 1},
  154. },
  155. {
  156. title: "4个节点,k<4,更新0,0之前没有k个块,现在拥有",
  157. nodeBlocks: []bitmap{1, 2, 4, 8},
  158. updatedNodeID: cdssdk.NodeID(0),
  159. updatedBitmap: bitmap(3),
  160. k: 2,
  161. expectedTreeNodeBitmaps: []int{0, 3, 3, 7, 15, 11, 5, 13, 9, 2, 6, 14, 10, 4, 12, 8},
  162. },
  163. {
  164. title: "4个节点,k<4,更新0,0之前有k个块,现在没有",
  165. nodeBlocks: []bitmap{3, 4, 0, 0},
  166. updatedNodeID: cdssdk.NodeID(0),
  167. updatedBitmap: bitmap(0),
  168. k: 2,
  169. expectedTreeNodeBitmaps: []int{0, 0, 4, 4, 4, 4, 0, 0, 0, 4, 4, 4, 4, 0, 0, 0},
  170. },
  171. }
  172. for _, test := range testcases {
  173. Convey(test.title, t, func() {
  174. t := newTreeTest(test.nodeBlocks)
  175. t.UpdateBitmap(test.updatedNodeID, test.updatedBitmap, test.k)
  176. var bitmaps []int
  177. for _, n := range t.nodes {
  178. bitmaps = append(bitmaps, int(n.blocksBitmap))
  179. }
  180. So(bitmaps, ShouldResemble, test.expectedTreeNodeBitmaps)
  181. })
  182. }
  183. }
  184. func Test_FindKBlocksMaxDepth(t *testing.T) {
  185. testcases := []struct {
  186. title string
  187. nodeBlocks []bitmap
  188. k int
  189. expected int
  190. }{
  191. {
  192. title: "每个节点各有一个块",
  193. nodeBlocks: []bitmap{1, 2, 4, 8},
  194. k: 2,
  195. expected: 2,
  196. },
  197. {
  198. title: "所有节点加起来块数不足",
  199. nodeBlocks: []bitmap{1, 1, 1, 1},
  200. k: 2,
  201. expected: 4,
  202. },
  203. {
  204. title: "不同节点有相同块",
  205. nodeBlocks: []bitmap{1, 1, 2, 4},
  206. k: 2,
  207. expected: 3,
  208. },
  209. {
  210. title: "一个节点就拥有所有块",
  211. nodeBlocks: []bitmap{3, 6, 12, 24},
  212. k: 2,
  213. expected: 1,
  214. },
  215. {
  216. title: "只有一块,且只在某一个节点1",
  217. nodeBlocks: []bitmap{1, 0},
  218. k: 1,
  219. expected: 2,
  220. },
  221. {
  222. title: "只有一块,且只在某一个节点2",
  223. nodeBlocks: []bitmap{0, 1},
  224. k: 1,
  225. expected: 2,
  226. },
  227. }
  228. for _, test := range testcases {
  229. Convey(test.title, t, func() {
  230. t := newTreeTest(test.nodeBlocks)
  231. ret := t.FindKBlocksMaxDepth(test.k)
  232. So(ret, ShouldResemble, test.expected)
  233. })
  234. }
  235. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。