lock_ring_test.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package lock_manager
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/seaweedfs/seaweedfs/weed/pb"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestAddServer(t *testing.T) {
  9. r := NewLockRing(100 * time.Millisecond)
  10. // Add servers
  11. r.AddServer("localhost:8080")
  12. r.AddServer("localhost:8081")
  13. r.AddServer("localhost:8082")
  14. r.AddServer("localhost:8083")
  15. r.AddServer("localhost:8084")
  16. // Verify all servers are present
  17. servers := r.GetSnapshot()
  18. assert.Equal(t, 5, len(servers))
  19. assert.Contains(t, servers, pb.ServerAddress("localhost:8080"))
  20. assert.Contains(t, servers, pb.ServerAddress("localhost:8081"))
  21. assert.Contains(t, servers, pb.ServerAddress("localhost:8082"))
  22. assert.Contains(t, servers, pb.ServerAddress("localhost:8083"))
  23. assert.Contains(t, servers, pb.ServerAddress("localhost:8084"))
  24. // Remove servers
  25. r.RemoveServer("localhost:8084")
  26. r.RemoveServer("localhost:8082")
  27. r.RemoveServer("localhost:8080")
  28. // Wait for all cleanup operations to complete
  29. r.WaitForCleanup()
  30. // Verify only 2 servers remain (localhost:8081 and localhost:8083)
  31. servers = r.GetSnapshot()
  32. assert.Equal(t, 2, len(servers))
  33. assert.Contains(t, servers, pb.ServerAddress("localhost:8081"))
  34. assert.Contains(t, servers, pb.ServerAddress("localhost:8083"))
  35. // Verify cleanup has happened - wait for snapshot interval and check snapshots are compacted
  36. time.Sleep(110 * time.Millisecond)
  37. r.WaitForCleanup()
  38. // Verify snapshot history is cleaned up properly (should have at most 2 snapshots after compaction)
  39. snapshotCount := r.GetSnapshotCount()
  40. assert.LessOrEqual(t, snapshotCount, 2, "Snapshot history should be compacted")
  41. }
  42. func TestLockRing(t *testing.T) {
  43. r := NewLockRing(100 * time.Millisecond)
  44. // Test initial snapshot
  45. r.SetSnapshot([]pb.ServerAddress{"localhost:8080", "localhost:8081"})
  46. assert.Equal(t, 1, r.GetSnapshotCount())
  47. servers := r.GetSnapshot()
  48. assert.Equal(t, 2, len(servers))
  49. assert.Contains(t, servers, pb.ServerAddress("localhost:8080"))
  50. assert.Contains(t, servers, pb.ServerAddress("localhost:8081"))
  51. // Add another server
  52. r.SetSnapshot([]pb.ServerAddress{"localhost:8080", "localhost:8081", "localhost:8082"})
  53. assert.Equal(t, 2, r.GetSnapshotCount())
  54. servers = r.GetSnapshot()
  55. assert.Equal(t, 3, len(servers))
  56. assert.Contains(t, servers, pb.ServerAddress("localhost:8082"))
  57. // Wait for cleanup interval and add another server
  58. time.Sleep(110 * time.Millisecond)
  59. r.WaitForCleanup()
  60. r.SetSnapshot([]pb.ServerAddress{"localhost:8080", "localhost:8081", "localhost:8082", "localhost:8083"})
  61. assert.LessOrEqual(t, r.GetSnapshotCount(), 3)
  62. servers = r.GetSnapshot()
  63. assert.Equal(t, 4, len(servers))
  64. assert.Contains(t, servers, pb.ServerAddress("localhost:8083"))
  65. // Wait for cleanup and verify compaction
  66. time.Sleep(110 * time.Millisecond)
  67. r.WaitForCleanup()
  68. assert.LessOrEqual(t, r.GetSnapshotCount(), 2, "Snapshots should be compacted")
  69. // Add final server
  70. r.SetSnapshot([]pb.ServerAddress{"localhost:8080", "localhost:8081", "localhost:8082", "localhost:8083", "localhost:8084"})
  71. servers = r.GetSnapshot()
  72. assert.Equal(t, 5, len(servers))
  73. assert.Contains(t, servers, pb.ServerAddress("localhost:8084"))
  74. assert.LessOrEqual(t, r.GetSnapshotCount(), 3)
  75. }