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.

51 lines
1.5 KiB

2 years ago
  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. counter := 0
  10. r := NewLockRing(100*time.Millisecond, func(snapshot []pb.ServerAddress) {
  11. counter++
  12. if counter == 1 {
  13. assert.Equal(t, 1, len(snapshot))
  14. } else if counter == 2 {
  15. assert.Equal(t, 2, len(snapshot))
  16. }
  17. })
  18. r.AddServer("localhost:8080")
  19. assert.Equal(t, 1, len(r.snapshots))
  20. r.AddServer("localhost:8081")
  21. r.AddServer("localhost:8082")
  22. r.AddServer("localhost:8083")
  23. r.AddServer("localhost:8084")
  24. r.RemoveServer("localhost:8084")
  25. r.RemoveServer("localhost:8082")
  26. r.RemoveServer("localhost:8080")
  27. assert.Equal(t, 8, len(r.snapshots))
  28. time.Sleep(110 * time.Millisecond)
  29. assert.Equal(t, 2, len(r.snapshots))
  30. }
  31. func TestLockRing(t *testing.T) {
  32. r := NewLockRing(100*time.Millisecond, nil)
  33. r.SetSnapshot([]pb.ServerAddress{"localhost:8080", "localhost:8081"})
  34. assert.Equal(t, 1, len(r.snapshots))
  35. r.SetSnapshot([]pb.ServerAddress{"localhost:8080", "localhost:8081", "localhost:8082"})
  36. assert.Equal(t, 2, len(r.snapshots))
  37. time.Sleep(110 * time.Millisecond)
  38. r.SetSnapshot([]pb.ServerAddress{"localhost:8080", "localhost:8081", "localhost:8082", "localhost:8083"})
  39. assert.Equal(t, 3, len(r.snapshots))
  40. time.Sleep(110 * time.Millisecond)
  41. assert.Equal(t, 2, len(r.snapshots))
  42. r.SetSnapshot([]pb.ServerAddress{"localhost:8080", "localhost:8081", "localhost:8082", "localhost:8083", "localhost:8084"})
  43. assert.Equal(t, 3, len(r.snapshots))
  44. }