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.

113 lines
3.1 KiB

  1. package topology
  2. import (
  3. "testing"
  4. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  5. )
  6. func TestVolumesBinaryState(t *testing.T) {
  7. vids := []needle.VolumeId{
  8. needle.VolumeId(1),
  9. needle.VolumeId(2),
  10. needle.VolumeId(3),
  11. needle.VolumeId(4),
  12. needle.VolumeId(5),
  13. }
  14. dns := []*DataNode{
  15. &DataNode{
  16. Ip: "127.0.0.1",
  17. Port: 8081,
  18. },
  19. &DataNode{
  20. Ip: "127.0.0.1",
  21. Port: 8082,
  22. },
  23. &DataNode{
  24. Ip: "127.0.0.1",
  25. Port: 8083,
  26. },
  27. }
  28. state_exist := NewVolumesBinaryState(readOnlyState, 3, ExistCopies())
  29. state_exist.Add(vids[0], dns[0])
  30. state_exist.Add(vids[0], dns[1])
  31. state_exist.Add(vids[1], dns[2])
  32. state_exist.Add(vids[2], dns[1])
  33. state_exist.Add(vids[4], dns[1])
  34. state_exist.Add(vids[4], dns[2])
  35. state_no := NewVolumesBinaryState(readOnlyState, 3, NoCopies())
  36. state_no.Add(vids[0], dns[0])
  37. state_no.Add(vids[0], dns[1])
  38. state_no.Add(vids[3], dns[1])
  39. tests := []struct {
  40. name string
  41. state *volumesBinaryState
  42. expectResult []bool
  43. update func()
  44. expectResultAfterUpdate []bool
  45. }{
  46. {
  47. name: "mark true when exist copies",
  48. state: state_exist,
  49. expectResult: []bool{true, true, true, false, true},
  50. update: func() {
  51. state_exist.Remove(vids[0], dns[2])
  52. state_exist.Remove(vids[1], dns[2])
  53. state_exist.Remove(vids[3], dns[2])
  54. state_exist.Remove(vids[4], dns[1])
  55. state_exist.Remove(vids[4], dns[2])
  56. },
  57. expectResultAfterUpdate: []bool{true, false, true, false, false},
  58. },
  59. {
  60. name: "mark true when inexist copies",
  61. state: state_no,
  62. expectResult: []bool{false, true, true, false, true},
  63. update: func() {
  64. state_no.Remove(vids[0], dns[2])
  65. state_no.Remove(vids[1], dns[2])
  66. state_no.Add(vids[2], dns[1])
  67. state_no.Remove(vids[3], dns[1])
  68. state_no.Remove(vids[4], dns[2])
  69. },
  70. expectResultAfterUpdate: []bool{false, true, false, true, true},
  71. },
  72. }
  73. for _, test := range tests {
  74. t.Run(test.name, func(t *testing.T) {
  75. var result []bool
  76. for index, _ := range vids {
  77. result = append(result, test.state.IsTrue(vids[index]))
  78. }
  79. if len(result) != len(test.expectResult) {
  80. t.Fatalf("len(result) != len(expectResult), got %d, expected %d\n",
  81. len(result), len(test.expectResult))
  82. }
  83. for index, val := range result {
  84. if val != test.expectResult[index] {
  85. t.Fatalf("result not matched, index %d, got %v, expect %v\n",
  86. index, val, test.expectResult[index])
  87. }
  88. }
  89. test.update()
  90. var updateResult []bool
  91. for index, _ := range vids {
  92. updateResult = append(updateResult, test.state.IsTrue(vids[index]))
  93. }
  94. if len(updateResult) != len(test.expectResultAfterUpdate) {
  95. t.Fatalf("len(updateResult) != len(expectResultAfterUpdate), got %d, expected %d\n",
  96. len(updateResult), len(test.expectResultAfterUpdate))
  97. }
  98. for index, val := range updateResult {
  99. if val != test.expectResultAfterUpdate[index] {
  100. t.Fatalf("update result not matched, index %d, got %v, expect %v\n",
  101. index, val, test.expectResultAfterUpdate[index])
  102. }
  103. }
  104. })
  105. }
  106. }