volume_read_test.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package storage
  2. import (
  3. "testing"
  4. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  5. "github.com/seaweedfs/seaweedfs/weed/storage/super_block"
  6. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestReadNeedMetaWithWritesAndUpdates(t *testing.T) {
  10. dir := t.TempDir()
  11. v, err := NewVolume(dir, dir, "", 1, NeedleMapInMemory, &super_block.ReplicaPlacement{}, &needle.TTL{}, 0, 0, 0)
  12. if err != nil {
  13. t.Fatalf("volume creation: %v", err)
  14. }
  15. type WriteInfo struct {
  16. offset int64
  17. size int32
  18. }
  19. writeInfos := make([]WriteInfo, 30)
  20. mockLastUpdateTime := uint64(1000000000000)
  21. // initialize 20 needles then update first 10 needles
  22. for i := 1; i <= 30; i++ {
  23. n := newRandomNeedle(uint64(i % 20))
  24. n.Flags = 0x08
  25. n.LastModified = mockLastUpdateTime
  26. mockLastUpdateTime += 2000
  27. offset, _, _, err := v.writeNeedle2(n, true, false)
  28. if err != nil {
  29. t.Fatalf("write needle %d: %v", i, err)
  30. }
  31. writeInfos[i-1] = WriteInfo{offset: int64(offset), size: int32(n.Size)}
  32. }
  33. expectedLastUpdateTime := uint64(1000000000000)
  34. for i := 0; i < 30; i++ {
  35. testNeedle := new(needle.Needle)
  36. testNeedle.Id = types.Uint64ToNeedleId(uint64(i + 1%20))
  37. testNeedle.Flags = 0x08
  38. v.readNeedleMetaAt(testNeedle, writeInfos[i].offset, writeInfos[i].size)
  39. actualLastModifiedTime := testNeedle.LastModified
  40. assert.Equal(t, expectedLastUpdateTime, actualLastModifiedTime, "The two words should be the same.")
  41. expectedLastUpdateTime += 2000
  42. }
  43. }
  44. func TestReadNeedMetaWithDeletesThenWrites(t *testing.T) {
  45. dir := t.TempDir()
  46. v, err := NewVolume(dir, dir, "", 1, NeedleMapInMemory, &super_block.ReplicaPlacement{}, &needle.TTL{}, 0, 0, 0)
  47. if err != nil {
  48. t.Fatalf("volume creation: %v", err)
  49. }
  50. type WriteInfo struct {
  51. offset int64
  52. size int32
  53. }
  54. writeInfos := make([]WriteInfo, 10)
  55. mockLastUpdateTime := uint64(1000000000000)
  56. for i := 1; i <= 10; i++ {
  57. n := newRandomNeedle(uint64(i % 5))
  58. n.Flags = 0x08
  59. n.LastModified = mockLastUpdateTime
  60. mockLastUpdateTime += 2000
  61. offset, _, _, err := v.writeNeedle2(n, true, false)
  62. if err != nil {
  63. t.Fatalf("write needle %d: %v", i, err)
  64. }
  65. if i < 5 {
  66. size, err := v.deleteNeedle2(n)
  67. if err != nil {
  68. t.Fatalf("delete needle %d: %v", i, err)
  69. }
  70. writeInfos[i-1] = WriteInfo{offset: int64(offset), size: int32(size)}
  71. } else {
  72. writeInfos[i-1] = WriteInfo{offset: int64(offset), size: int32(n.Size)}
  73. }
  74. }
  75. expectedLastUpdateTime := uint64(1000000000000)
  76. for i := 0; i < 10; i++ {
  77. testNeedle := new(needle.Needle)
  78. testNeedle.Id = types.Uint64ToNeedleId(uint64(i + 1%5))
  79. testNeedle.Flags = 0x08
  80. v.readNeedleMetaAt(testNeedle, writeInfos[i].offset, writeInfos[i].size)
  81. actualLastModifiedTime := testNeedle.LastModified
  82. assert.Equal(t, expectedLastUpdateTime, actualLastModifiedTime, "The two words should be the same.")
  83. expectedLastUpdateTime += 2000
  84. }
  85. }