filechunk_manifest_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package filer
  2. import (
  3. "bytes"
  4. "math"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  8. )
  9. func TestDoMaybeManifestize(t *testing.T) {
  10. var manifestTests = []struct {
  11. inputs []*filer_pb.FileChunk
  12. expected []*filer_pb.FileChunk
  13. }{
  14. {
  15. inputs: []*filer_pb.FileChunk{
  16. {FileId: "1", IsChunkManifest: false},
  17. {FileId: "2", IsChunkManifest: false},
  18. {FileId: "3", IsChunkManifest: false},
  19. {FileId: "4", IsChunkManifest: false},
  20. },
  21. expected: []*filer_pb.FileChunk{
  22. {FileId: "12", IsChunkManifest: true},
  23. {FileId: "34", IsChunkManifest: true},
  24. },
  25. },
  26. {
  27. inputs: []*filer_pb.FileChunk{
  28. {FileId: "1", IsChunkManifest: true},
  29. {FileId: "2", IsChunkManifest: false},
  30. {FileId: "3", IsChunkManifest: false},
  31. {FileId: "4", IsChunkManifest: false},
  32. },
  33. expected: []*filer_pb.FileChunk{
  34. {FileId: "1", IsChunkManifest: true},
  35. {FileId: "23", IsChunkManifest: true},
  36. {FileId: "4", IsChunkManifest: false},
  37. },
  38. },
  39. {
  40. inputs: []*filer_pb.FileChunk{
  41. {FileId: "1", IsChunkManifest: false},
  42. {FileId: "2", IsChunkManifest: true},
  43. {FileId: "3", IsChunkManifest: false},
  44. {FileId: "4", IsChunkManifest: false},
  45. },
  46. expected: []*filer_pb.FileChunk{
  47. {FileId: "2", IsChunkManifest: true},
  48. {FileId: "13", IsChunkManifest: true},
  49. {FileId: "4", IsChunkManifest: false},
  50. },
  51. },
  52. {
  53. inputs: []*filer_pb.FileChunk{
  54. {FileId: "1", IsChunkManifest: true},
  55. {FileId: "2", IsChunkManifest: true},
  56. {FileId: "3", IsChunkManifest: false},
  57. {FileId: "4", IsChunkManifest: false},
  58. },
  59. expected: []*filer_pb.FileChunk{
  60. {FileId: "1", IsChunkManifest: true},
  61. {FileId: "2", IsChunkManifest: true},
  62. {FileId: "34", IsChunkManifest: true},
  63. },
  64. },
  65. }
  66. for i, mtest := range manifestTests {
  67. println("test", i)
  68. actual, _ := doMaybeManifestize(nil, mtest.inputs, 2, mockMerge)
  69. assertEqualChunks(t, mtest.expected, actual)
  70. }
  71. }
  72. func assertEqualChunks(t *testing.T, expected, actual []*filer_pb.FileChunk) {
  73. assert.Equal(t, len(expected), len(actual))
  74. for i := 0; i < len(actual); i++ {
  75. assertEqualChunk(t, actual[i], expected[i])
  76. }
  77. }
  78. func assertEqualChunk(t *testing.T, expected, actual *filer_pb.FileChunk) {
  79. assert.Equal(t, expected.FileId, actual.FileId)
  80. assert.Equal(t, expected.IsChunkManifest, actual.IsChunkManifest)
  81. }
  82. func mockMerge(saveFunc SaveDataAsChunkFunctionType, dataChunks []*filer_pb.FileChunk) (manifestChunk *filer_pb.FileChunk, err error) {
  83. var buf bytes.Buffer
  84. minOffset, maxOffset := int64(math.MaxInt64), int64(math.MinInt64)
  85. for k := 0; k < len(dataChunks); k++ {
  86. chunk := dataChunks[k]
  87. buf.WriteString(chunk.FileId)
  88. if minOffset > int64(chunk.Offset) {
  89. minOffset = chunk.Offset
  90. }
  91. if maxOffset < int64(chunk.Size)+chunk.Offset {
  92. maxOffset = int64(chunk.Size) + chunk.Offset
  93. }
  94. }
  95. manifestChunk = &filer_pb.FileChunk{
  96. FileId: buf.String(),
  97. }
  98. manifestChunk.IsChunkManifest = true
  99. manifestChunk.Offset = minOffset
  100. manifestChunk.Size = uint64(maxOffset - minOffset)
  101. return
  102. }