lock_ring_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package lock_manager
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/pb"
  4. "github.com/stretchr/testify/assert"
  5. "testing"
  6. "time"
  7. )
  8. func TestAddServer(t *testing.T) {
  9. r := NewLockRing(100 * time.Millisecond)
  10. r.AddServer("localhost:8080")
  11. assert.Equal(t, 1, len(r.snapshots))
  12. r.AddServer("localhost:8081")
  13. r.AddServer("localhost:8082")
  14. r.AddServer("localhost:8083")
  15. r.AddServer("localhost:8084")
  16. r.RemoveServer("localhost:8084")
  17. r.RemoveServer("localhost:8082")
  18. r.RemoveServer("localhost:8080")
  19. assert.Equal(t, 8, len(r.snapshots))
  20. time.Sleep(110 * time.Millisecond)
  21. assert.Equal(t, 2, len(r.snapshots))
  22. }
  23. func TestLockRing(t *testing.T) {
  24. r := NewLockRing(100 * time.Millisecond)
  25. r.SetSnapshot([]pb.ServerAddress{"localhost:8080", "localhost:8081"})
  26. assert.Equal(t, 1, len(r.snapshots))
  27. r.SetSnapshot([]pb.ServerAddress{"localhost:8080", "localhost:8081", "localhost:8082"})
  28. assert.Equal(t, 2, len(r.snapshots))
  29. time.Sleep(110 * time.Millisecond)
  30. r.SetSnapshot([]pb.ServerAddress{"localhost:8080", "localhost:8081", "localhost:8082", "localhost:8083"})
  31. assert.Equal(t, 3, len(r.snapshots))
  32. time.Sleep(110 * time.Millisecond)
  33. assert.Equal(t, 2, len(r.snapshots))
  34. r.SetSnapshot([]pb.ServerAddress{"localhost:8080", "localhost:8081", "localhost:8082", "localhost:8083", "localhost:8084"})
  35. assert.Equal(t, 3, len(r.snapshots))
  36. }