quick_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 confchange
  15. import (
  16. "math/rand"
  17. "reflect"
  18. "testing"
  19. "testing/quick"
  20. pb "go.etcd.io/etcd/raft/raftpb"
  21. "go.etcd.io/etcd/raft/tracker"
  22. )
  23. // TestConfChangeQuick uses quickcheck to verify that simple and joint config
  24. // changes arrive at the same result.
  25. func TestConfChangeQuick(t *testing.T) {
  26. cfg := &quick.Config{
  27. MaxCount: 1000,
  28. }
  29. // Log the first couple of runs to give some indication of things working
  30. // as intended.
  31. const infoCount = 5
  32. runWithJoint := func(c *Changer, ccs []pb.ConfChange) error {
  33. cfg, prs, err := c.EnterJoint(ccs...)
  34. if err != nil {
  35. return err
  36. }
  37. c.Tracker.Config = cfg
  38. c.Tracker.Progress = prs
  39. cfg, prs, err = c.LeaveJoint()
  40. if err != nil {
  41. return err
  42. }
  43. c.Tracker.Config = cfg
  44. c.Tracker.Progress = prs
  45. return nil
  46. }
  47. runWithSimple := func(c *Changer, ccs []pb.ConfChange) error {
  48. for _, cc := range ccs {
  49. cfg, prs, err := c.Simple(cc)
  50. if err != nil {
  51. return err
  52. }
  53. c.Tracker.Config, c.Tracker.Progress = cfg, prs
  54. }
  55. return nil
  56. }
  57. type testFunc func(*Changer, []pb.ConfChange) error
  58. wrapper := func(invoke testFunc) func(setup initialChanges, ccs confChanges) (*Changer, error) {
  59. return func(setup initialChanges, ccs confChanges) (*Changer, error) {
  60. tr := tracker.MakeProgressTracker(10)
  61. c := &Changer{
  62. Tracker: tr,
  63. LastIndex: 10,
  64. }
  65. if err := runWithSimple(c, setup); err != nil {
  66. return nil, err
  67. }
  68. err := invoke(c, ccs)
  69. return c, err
  70. }
  71. }
  72. var n int
  73. f1 := func(setup initialChanges, ccs confChanges) *Changer {
  74. c, err := wrapper(runWithSimple)(setup, ccs)
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. if n < infoCount {
  79. t.Log("initial setup:", Describe(setup...))
  80. t.Log("changes:", Describe(ccs...))
  81. t.Log(c.Tracker.Config)
  82. t.Log(c.Tracker.Progress)
  83. }
  84. n++
  85. return c
  86. }
  87. f2 := func(setup initialChanges, ccs confChanges) *Changer {
  88. c, err := wrapper(runWithJoint)(setup, ccs)
  89. if err != nil {
  90. t.Fatal(err)
  91. }
  92. return c
  93. }
  94. err := quick.CheckEqual(f1, f2, cfg)
  95. if err == nil {
  96. return
  97. }
  98. cErr, ok := err.(*quick.CheckEqualError)
  99. if !ok {
  100. t.Fatal(err)
  101. }
  102. t.Error("setup:", Describe(cErr.In[0].([]pb.ConfChange)...))
  103. t.Error("ccs:", Describe(cErr.In[1].([]pb.ConfChange)...))
  104. t.Errorf("out1: %+v\nout2: %+v", cErr.Out1, cErr.Out2)
  105. }
  106. type confChangeTyp pb.ConfChangeType
  107. func (confChangeTyp) Generate(rand *rand.Rand, _ int) reflect.Value {
  108. return reflect.ValueOf(confChangeTyp(rand.Intn(4)))
  109. }
  110. type confChanges []pb.ConfChange
  111. func genCC(num func() int, id func() uint64, typ func() pb.ConfChangeType) []pb.ConfChange {
  112. var ccs []pb.ConfChange
  113. n := num()
  114. for i := 0; i < n; i++ {
  115. ccs = append(ccs, pb.ConfChange{Type: typ(), NodeID: id()})
  116. }
  117. return ccs
  118. }
  119. func (confChanges) Generate(rand *rand.Rand, _ int) reflect.Value {
  120. num := func() int {
  121. return 1 + rand.Intn(9)
  122. }
  123. id := func() uint64 {
  124. // Note that num() >= 1, so we're never returning 1 from this method,
  125. // meaning that we'll never touch NodeID one, which is special to avoid
  126. // voterless configs altogether in this test.
  127. return 1 + uint64(num())
  128. }
  129. typ := func() pb.ConfChangeType {
  130. return pb.ConfChangeType(rand.Intn(len(pb.ConfChangeType_name)))
  131. }
  132. return reflect.ValueOf(genCC(num, id, typ))
  133. }
  134. type initialChanges []pb.ConfChange
  135. func (initialChanges) Generate(rand *rand.Rand, _ int) reflect.Value {
  136. num := func() int {
  137. return 1 + rand.Intn(5)
  138. }
  139. id := func() uint64 { return uint64(num()) }
  140. typ := func() pb.ConfChangeType {
  141. return pb.ConfChangeAddNode
  142. }
  143. // NodeID one is special - it's in the initial config and will be a voter
  144. // always (this is to avoid uninteresting edge cases where the simple conf
  145. // changes can't easily make progress).
  146. ccs := append([]pb.ConfChange{{Type: pb.ConfChangeAddNode, NodeID: 1}}, genCC(num, id, typ)...)
  147. return reflect.ValueOf(ccs)
  148. }