quick_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 quorum
  15. import (
  16. "math"
  17. "math/rand"
  18. "reflect"
  19. "testing"
  20. "testing/quick"
  21. )
  22. // TestQuick uses quickcheck to heuristically assert that the main
  23. // implementation of (MajorityConfig).CommittedIndex agrees with a "dumb"
  24. // alternative version.
  25. func TestQuick(t *testing.T) {
  26. cfg := &quick.Config{
  27. MaxCount: 50000,
  28. }
  29. t.Run("majority_commit", func(t *testing.T) {
  30. fn1 := func(c memberMap, l idxMap) uint64 {
  31. return uint64(MajorityConfig(c).CommittedIndex(mapAckIndexer(l)))
  32. }
  33. fn2 := func(c memberMap, l idxMap) uint64 {
  34. return uint64(alternativeMajorityCommittedIndex(MajorityConfig(c), mapAckIndexer(l)))
  35. }
  36. if err := quick.CheckEqual(fn1, fn2, cfg); err != nil {
  37. t.Fatal(err)
  38. }
  39. })
  40. }
  41. // smallRandIdxMap returns a reasonably sized map of ids to commit indexes.
  42. func smallRandIdxMap(rand *rand.Rand, size int) map[uint64]Index {
  43. // Hard-code a reasonably small size here (quick will hard-code 50, which
  44. // is not useful here).
  45. size = 10
  46. n := rand.Intn(size)
  47. ids := rand.Perm(2 * n)[:n]
  48. idxs := make([]int, len(ids))
  49. for i := range idxs {
  50. idxs[i] = rand.Intn(n)
  51. }
  52. m := map[uint64]Index{}
  53. for i := range ids {
  54. m[uint64(ids[i])] = Index(idxs[i])
  55. }
  56. return m
  57. }
  58. type idxMap map[uint64]Index
  59. func (idxMap) Generate(rand *rand.Rand, size int) reflect.Value {
  60. m := smallRandIdxMap(rand, size)
  61. return reflect.ValueOf(m)
  62. }
  63. type memberMap map[uint64]struct{}
  64. func (memberMap) Generate(rand *rand.Rand, size int) reflect.Value {
  65. m := smallRandIdxMap(rand, size)
  66. mm := map[uint64]struct{}{}
  67. for id := range m {
  68. mm[id] = struct{}{}
  69. }
  70. return reflect.ValueOf(mm)
  71. }
  72. // This is an alternative implementation of (MajorityConfig).CommittedIndex(l).
  73. func alternativeMajorityCommittedIndex(c MajorityConfig, l AckedIndexer) Index {
  74. if len(c) == 0 {
  75. return math.MaxUint64
  76. }
  77. idToIdx := map[uint64]Index{}
  78. for id := range c {
  79. if idx, ok := l.AckedIndex(id); ok {
  80. idToIdx[id] = idx
  81. }
  82. }
  83. // Build a map from index to voters who have acked that or any higher index.
  84. idxToVotes := map[Index]int{}
  85. for _, idx := range idToIdx {
  86. idxToVotes[idx] = 0
  87. }
  88. for _, idx := range idToIdx {
  89. for idy := range idxToVotes {
  90. if idy > idx {
  91. continue
  92. }
  93. idxToVotes[idy]++
  94. }
  95. }
  96. // Find the maximum index that has achieved quorum.
  97. q := len(c)/2 + 1
  98. var maxQuorumIdx Index
  99. for idx, n := range idxToVotes {
  100. if n >= q && idx > maxQuorumIdx {
  101. maxQuorumIdx = idx
  102. }
  103. }
  104. return maxQuorumIdx
  105. }