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

112 lines
3.5 KiB

7 years ago
  1. package filer2
  2. import (
  3. "testing"
  4. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  5. "log"
  6. )
  7. func TestIntervalMerging(t *testing.T) {
  8. testcases := []struct {
  9. Chunks []*filer_pb.FileChunk
  10. Expected []*visibleInterval
  11. }{
  12. // case 0: normal
  13. {
  14. Chunks: []*filer_pb.FileChunk{
  15. {Offset: 0, Size: 100, FileId: "abc", Mtime: 123},
  16. {Offset: 100, Size: 100, FileId: "asdf", Mtime: 134},
  17. {Offset: 200, Size: 100, FileId: "fsad", Mtime: 353},
  18. },
  19. Expected: []*visibleInterval{
  20. {start: 0, stop: 100, fileId: "abc"},
  21. {start: 100, stop: 200, fileId: "asdf"},
  22. {start: 200, stop: 300, fileId: "fsad"},
  23. },
  24. },
  25. // case 1: updates overwrite full chunks
  26. {
  27. Chunks: []*filer_pb.FileChunk{
  28. {Offset: 0, Size: 100, FileId: "abc", Mtime: 123},
  29. {Offset: 0, Size: 200, FileId: "asdf", Mtime: 134},
  30. },
  31. Expected: []*visibleInterval{
  32. {start: 0, stop: 200, fileId: "asdf"},
  33. },
  34. },
  35. // case 2: updates overwrite part of previous chunks
  36. {
  37. Chunks: []*filer_pb.FileChunk{
  38. {Offset: 0, Size: 100, FileId: "abc", Mtime: 123},
  39. {Offset: 0, Size: 50, FileId: "asdf", Mtime: 134},
  40. },
  41. Expected: []*visibleInterval{
  42. {start: 0, stop: 50, fileId: "asdf"},
  43. {start: 50, stop: 100, fileId: "abc"},
  44. },
  45. },
  46. // case 3: updates overwrite full chunks
  47. {
  48. Chunks: []*filer_pb.FileChunk{
  49. {Offset: 0, Size: 100, FileId: "abc", Mtime: 123},
  50. {Offset: 0, Size: 200, FileId: "asdf", Mtime: 134},
  51. {Offset: 50, Size: 250, FileId: "xxxx", Mtime: 154},
  52. },
  53. Expected: []*visibleInterval{
  54. {start: 0, stop: 50, fileId: "asdf"},
  55. {start: 50, stop: 300, fileId: "xxxx"},
  56. },
  57. },
  58. // case 4: updates far away from prev chunks
  59. {
  60. Chunks: []*filer_pb.FileChunk{
  61. {Offset: 0, Size: 100, FileId: "abc", Mtime: 123},
  62. {Offset: 0, Size: 200, FileId: "asdf", Mtime: 134},
  63. {Offset: 250, Size: 250, FileId: "xxxx", Mtime: 154},
  64. },
  65. Expected: []*visibleInterval{
  66. {start: 0, stop: 200, fileId: "asdf"},
  67. {start: 250, stop: 500, fileId: "xxxx"},
  68. },
  69. },
  70. // case 5: updates overwrite full chunks
  71. {
  72. Chunks: []*filer_pb.FileChunk{
  73. {Offset: 0, Size: 100, FileId: "abc", Mtime: 123},
  74. {Offset: 0, Size: 200, FileId: "asdf", Mtime: 184},
  75. {Offset: 70, Size: 150, FileId: "abc", Mtime: 143},
  76. {Offset: 80, Size: 100, FileId: "xxxx", Mtime: 134},
  77. },
  78. Expected: []*visibleInterval{
  79. {start: 0, stop: 200, fileId: "asdf"},
  80. {start: 200, stop: 220, fileId: "abc"},
  81. },
  82. },
  83. }
  84. for i, testcase := range testcases {
  85. log.Printf("++++++++++ merged test case %d ++++++++++++++++++++", i)
  86. intervals := nonOverlappingVisibleIntervals(testcase.Chunks)
  87. for x, interval := range intervals {
  88. log.Printf("test case %d, interval %d, start=%d, stop=%d, fileId=%s",
  89. i, x, interval.start, interval.stop, interval.fileId)
  90. if interval.start != testcase.Expected[x].start {
  91. t.Fatalf("failed on test case %d, interval %d, start %d, expect %d",
  92. i, x, interval.start, testcase.Expected[x].start)
  93. }
  94. if interval.stop != testcase.Expected[x].stop {
  95. t.Fatalf("failed on test case %d, interval %d, stop %d, expect %d",
  96. i, x, interval.stop, testcase.Expected[x].stop)
  97. }
  98. if interval.fileId != testcase.Expected[x].fileId {
  99. t.Fatalf("failed on test case %d, interval %d, chunkId %s, expect %s",
  100. i, x, interval.fileId, testcase.Expected[x].fileId)
  101. }
  102. }
  103. if len(intervals) != len(testcase.Expected) {
  104. t.Fatalf("failed to compact test case %d, len %d expected %d", i, len(intervals), len(testcase.Expected))
  105. }
  106. }
  107. }