confstate_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2019 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package raftpb
  15. import (
  16. "testing"
  17. )
  18. func TestConfState_Equivalent(t *testing.T) {
  19. type testCase struct {
  20. cs, cs2 ConfState
  21. ok bool
  22. }
  23. testCases := []testCase{
  24. // Reordered voters and learners.
  25. {ConfState{
  26. Voters: []uint64{1, 2, 3},
  27. Learners: []uint64{5, 4, 6},
  28. VotersOutgoing: []uint64{9, 8, 7},
  29. LearnersNext: []uint64{10, 20, 15},
  30. }, ConfState{
  31. Voters: []uint64{1, 2, 3},
  32. Learners: []uint64{4, 5, 6},
  33. VotersOutgoing: []uint64{7, 9, 8},
  34. LearnersNext: []uint64{20, 10, 15},
  35. }, true},
  36. // Not sensitive to nil vs empty slice.
  37. {ConfState{Voters: []uint64{}}, ConfState{Voters: []uint64(nil)}, true},
  38. // Non-equivalent voters.
  39. {ConfState{Voters: []uint64{1, 2, 3, 4}}, ConfState{Voters: []uint64{2, 1, 3}}, false},
  40. {ConfState{Voters: []uint64{1, 4, 3}}, ConfState{Voters: []uint64{2, 1, 3}}, false},
  41. // Non-equivalent learners.
  42. {ConfState{Voters: []uint64{1, 2, 3, 4}}, ConfState{Voters: []uint64{2, 1, 3}}, false},
  43. // Sensitive to AutoLeave flag.
  44. {ConfState{AutoLeave: true}, ConfState{}, false},
  45. }
  46. for _, tc := range testCases {
  47. t.Run("", func(t *testing.T) {
  48. if err := tc.cs.Equivalent(tc.cs2); (err == nil) != tc.ok {
  49. t.Fatalf("wanted error: %t, got:\n%s", tc.ok, err)
  50. }
  51. })
  52. }
  53. }