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.

rs_test.go 5.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package ec
  2. import (
  3. "bytes"
  4. "io"
  5. "sync"
  6. "testing"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. func Test_EncodeReconstruct(t *testing.T) {
  10. Convey("编码后使用校验块重建数据", t, func() {
  11. rs, err := NewStreamRs(2, 3, 5)
  12. So(err, ShouldBeNil)
  13. outputs := rs.EncodeAll([]io.Reader{
  14. bytes.NewReader([]byte{1, 2, 3, 4, 5}),
  15. bytes.NewReader([]byte{6, 7, 8, 9, 10}),
  16. })
  17. var outputData = [][]byte{
  18. make([]byte, 5),
  19. make([]byte, 5),
  20. make([]byte, 5),
  21. }
  22. { // 编码所有块
  23. errs := make([]error, 3)
  24. wg := sync.WaitGroup{}
  25. for i := range outputs {
  26. idx := i
  27. wg.Add(1)
  28. go func() {
  29. defer wg.Done()
  30. _, err := io.ReadFull(outputs[idx], outputData[idx])
  31. errs[idx] = err
  32. }()
  33. }
  34. wg.Wait()
  35. for _, e := range errs {
  36. if e != io.EOF {
  37. So(e, ShouldBeNil)
  38. }
  39. }
  40. So(outputData[0], ShouldResemble, []byte{1, 2, 3, 4, 5})
  41. So(outputData[1], ShouldResemble, []byte{6, 7, 8, 9, 10})
  42. }
  43. { // 重建所有数据块
  44. recOutputs := rs.ReconstructData([]io.Reader{
  45. bytes.NewBuffer(outputData[1]),
  46. bytes.NewBuffer(outputData[2]),
  47. }, []int{1, 2})
  48. recOutputData := [][]byte{
  49. make([]byte, 5),
  50. make([]byte, 5),
  51. }
  52. errs := make([]error, 2)
  53. wg := sync.WaitGroup{}
  54. for i := range recOutputs {
  55. idx := i
  56. wg.Add(1)
  57. go func() {
  58. defer wg.Done()
  59. _, err := io.ReadFull(recOutputs[idx], recOutputData[idx])
  60. errs[idx] = err
  61. }()
  62. }
  63. wg.Wait()
  64. for _, e := range errs {
  65. if e != io.EOF {
  66. So(e, ShouldBeNil)
  67. }
  68. }
  69. So(recOutputData[0], ShouldResemble, []byte{1, 2, 3, 4, 5})
  70. So(recOutputData[1], ShouldResemble, []byte{6, 7, 8, 9, 10})
  71. }
  72. { // 重建指定的数据块
  73. recOutputs := rs.ReconstructSome([]io.Reader{
  74. bytes.NewBuffer(outputData[1]),
  75. bytes.NewBuffer(outputData[2]),
  76. }, []int{1, 2}, []int{0, 1})
  77. recOutputData := [][]byte{
  78. make([]byte, 5),
  79. make([]byte, 5),
  80. }
  81. errs := make([]error, 2)
  82. wg := sync.WaitGroup{}
  83. for i := range recOutputs {
  84. idx := i
  85. wg.Add(1)
  86. go func() {
  87. defer wg.Done()
  88. _, err := io.ReadFull(recOutputs[idx], recOutputData[idx])
  89. errs[idx] = err
  90. }()
  91. }
  92. wg.Wait()
  93. for _, e := range errs {
  94. if e != io.EOF {
  95. So(e, ShouldBeNil)
  96. }
  97. }
  98. So(recOutputData[0], ShouldResemble, []byte{1, 2, 3, 4, 5})
  99. So(recOutputData[1], ShouldResemble, []byte{6, 7, 8, 9, 10})
  100. }
  101. { // 重建指定的数据块
  102. recOutputs := rs.ReconstructSome([]io.Reader{
  103. bytes.NewBuffer(outputData[1]),
  104. bytes.NewBuffer(outputData[2]),
  105. }, []int{1, 2}, []int{0})
  106. recOutputData := [][]byte{
  107. make([]byte, 5),
  108. }
  109. errs := make([]error, 2)
  110. wg := sync.WaitGroup{}
  111. for i := range recOutputs {
  112. idx := i
  113. wg.Add(1)
  114. go func() {
  115. defer wg.Done()
  116. _, err := io.ReadFull(recOutputs[idx], recOutputData[idx])
  117. errs[idx] = err
  118. }()
  119. }
  120. wg.Wait()
  121. for _, e := range errs {
  122. if e != io.EOF {
  123. So(e, ShouldBeNil)
  124. }
  125. }
  126. So(recOutputData[0], ShouldResemble, []byte{1, 2, 3, 4, 5})
  127. }
  128. { // 重建指定的数据块
  129. recOutputs := rs.ReconstructSome([]io.Reader{
  130. bytes.NewBuffer(outputData[1]),
  131. bytes.NewBuffer(outputData[2]),
  132. }, []int{1, 2}, []int{1})
  133. recOutputData := [][]byte{
  134. make([]byte, 5),
  135. }
  136. errs := make([]error, 2)
  137. wg := sync.WaitGroup{}
  138. for i := range recOutputs {
  139. idx := i
  140. wg.Add(1)
  141. go func() {
  142. defer wg.Done()
  143. _, err := io.ReadFull(recOutputs[idx], recOutputData[idx])
  144. errs[idx] = err
  145. }()
  146. }
  147. wg.Wait()
  148. for _, e := range errs {
  149. if e != io.EOF {
  150. So(e, ShouldBeNil)
  151. }
  152. }
  153. So(recOutputData[0], ShouldResemble, []byte{6, 7, 8, 9, 10})
  154. }
  155. { // 单独产生校验块
  156. encOutputs := rs.Encode([]io.Reader{
  157. bytes.NewBuffer(outputData[0]),
  158. bytes.NewBuffer(outputData[1]),
  159. })
  160. encOutputData := [][]byte{
  161. make([]byte, 5),
  162. }
  163. errs := make([]error, 2)
  164. wg := sync.WaitGroup{}
  165. for i := range encOutputs {
  166. idx := i
  167. wg.Add(1)
  168. go func() {
  169. defer wg.Done()
  170. _, err := io.ReadFull(encOutputs[idx], encOutputData[idx])
  171. errs[idx] = err
  172. }()
  173. }
  174. wg.Wait()
  175. for _, e := range errs {
  176. if e != io.EOF {
  177. So(e, ShouldBeNil)
  178. }
  179. }
  180. So(encOutputData[0], ShouldResemble, outputData[2])
  181. }
  182. { // 使用ReconstructAny单独重建校验块
  183. encOutputs := rs.ReconstructAny([]io.Reader{
  184. bytes.NewBuffer(outputData[0]),
  185. bytes.NewBuffer(outputData[1]),
  186. }, []int{0, 1}, []int{2})
  187. encOutputData := [][]byte{
  188. make([]byte, 5),
  189. }
  190. errs := make([]error, 2)
  191. wg := sync.WaitGroup{}
  192. for i := range encOutputs {
  193. idx := i
  194. wg.Add(1)
  195. go func() {
  196. defer wg.Done()
  197. _, err := io.ReadFull(encOutputs[idx], encOutputData[idx])
  198. errs[idx] = err
  199. }()
  200. }
  201. wg.Wait()
  202. for _, e := range errs {
  203. if e != io.EOF {
  204. So(e, ShouldBeNil)
  205. }
  206. }
  207. So(encOutputData[0], ShouldResemble, outputData[2])
  208. }
  209. })
  210. }

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