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.

119 lines
2.1 KiB

  1. package filer
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  5. "math/rand"
  6. "testing"
  7. )
  8. func TestReadResolvedChunks(t *testing.T) {
  9. chunks := []*filer_pb.FileChunk{
  10. {
  11. FileId: "a",
  12. Offset: 0,
  13. Size: 100,
  14. Mtime: 1,
  15. },
  16. {
  17. FileId: "b",
  18. Offset: 50,
  19. Size: 100,
  20. Mtime: 2,
  21. },
  22. {
  23. FileId: "c",
  24. Offset: 200,
  25. Size: 50,
  26. Mtime: 3,
  27. },
  28. {
  29. FileId: "d",
  30. Offset: 250,
  31. Size: 50,
  32. Mtime: 4,
  33. },
  34. {
  35. FileId: "e",
  36. Offset: 175,
  37. Size: 100,
  38. Mtime: 5,
  39. },
  40. }
  41. visibles := readResolvedChunks(chunks)
  42. for _, visible := range visibles {
  43. fmt.Printf("[%d,%d) %s %d\n", visible.start, visible.stop, visible.fileId, visible.modifiedTime)
  44. }
  45. }
  46. func TestRandomizedReadResolvedChunks(t *testing.T) {
  47. var limit int64 = 1024*1024
  48. array := make([]int64, limit)
  49. var chunks []*filer_pb.FileChunk
  50. for ts := int64(0); ts < 1024; ts++ {
  51. x := rand.Int63n(limit)
  52. y := rand.Int63n(limit)
  53. size := x - y
  54. if size < 0 {
  55. size = -size
  56. }
  57. if size > 1024 {
  58. size = 1024
  59. }
  60. start := x
  61. if start > y {
  62. start = y
  63. }
  64. chunks = append(chunks, randomWrite(array, start, size, ts))
  65. }
  66. visibles := readResolvedChunks(chunks)
  67. for _, visible := range visibles {
  68. for i := visible.start; i<visible.stop;i++{
  69. if array[i] != visible.modifiedTime {
  70. t.Errorf("position %d expected ts %d actual ts %d", i, array[i], visible.modifiedTime)
  71. }
  72. }
  73. }
  74. // fmt.Printf("visibles %d", len(visibles))
  75. }
  76. func randomWrite(array []int64, start int64, size int64, ts int64) *filer_pb.FileChunk {
  77. for i := start; i < start+size; i++ {
  78. array[i] = ts
  79. }
  80. // fmt.Printf("write [%d,%d) %d\n", start, start+size, ts)
  81. return &filer_pb.FileChunk{
  82. FileId: "",
  83. Offset: start,
  84. Size: uint64(size),
  85. Mtime: ts,
  86. }
  87. }
  88. func TestSequentialReadResolvedChunks(t *testing.T) {
  89. var chunkSize int64 = 1024*1024*2
  90. var chunks []*filer_pb.FileChunk
  91. for ts := int64(0); ts < 13; ts++ {
  92. chunks = append(chunks, &filer_pb.FileChunk{
  93. FileId: "",
  94. Offset: chunkSize*ts,
  95. Size: uint64(chunkSize),
  96. Mtime: 1,
  97. })
  98. }
  99. visibles := readResolvedChunks(chunks)
  100. fmt.Printf("visibles %d", len(visibles))
  101. }