log_unstable_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package raft
  14. import (
  15. "testing"
  16. pb "github.com/coreos/etcd/raft/raftpb"
  17. )
  18. func TestUnstableMaybeFirstIndex(t *testing.T) {
  19. tests := []struct {
  20. entries []pb.Entry
  21. offset uint64
  22. snap *pb.Snapshot
  23. wok bool
  24. windex uint64
  25. }{
  26. // no snapshot
  27. {
  28. []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
  29. false, 0,
  30. },
  31. {
  32. []pb.Entry{}, 0, nil,
  33. false, 0,
  34. },
  35. // has snapshot
  36. {
  37. []pb.Entry{{Index: 5, Term: 1}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
  38. true, 5,
  39. },
  40. {
  41. []pb.Entry{}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
  42. true, 5,
  43. },
  44. }
  45. for i, tt := range tests {
  46. u := unstable{
  47. entries: tt.entries,
  48. offset: tt.offset,
  49. snapshot: tt.snap,
  50. }
  51. index, ok := u.maybeFirstIndex()
  52. if ok != tt.wok {
  53. t.Errorf("#%d: ok = %t, want %t", i, ok, tt.wok)
  54. }
  55. if index != tt.windex {
  56. t.Errorf("#%d: index = %d, want %d", i, index, tt.windex)
  57. }
  58. }
  59. }
  60. func TestMaybeLastIndex(t *testing.T) {
  61. tests := []struct {
  62. entries []pb.Entry
  63. offset uint64
  64. snap *pb.Snapshot
  65. wok bool
  66. windex uint64
  67. }{
  68. // last in entries
  69. {
  70. []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
  71. true, 5,
  72. },
  73. {
  74. []pb.Entry{{Index: 5, Term: 1}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
  75. true, 5,
  76. },
  77. // last in snapshot
  78. {
  79. []pb.Entry{}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
  80. true, 4,
  81. },
  82. // empty unstable
  83. {
  84. []pb.Entry{}, 0, nil,
  85. false, 0,
  86. },
  87. }
  88. for i, tt := range tests {
  89. u := unstable{
  90. entries: tt.entries,
  91. offset: tt.offset,
  92. snapshot: tt.snap,
  93. }
  94. index, ok := u.maybeLastIndex()
  95. if ok != tt.wok {
  96. t.Errorf("#%d: ok = %t, want %t", i, ok, tt.wok)
  97. }
  98. if index != tt.windex {
  99. t.Errorf("#%d: index = %d, want %d", i, index, tt.windex)
  100. }
  101. }
  102. }
  103. func TestUnstableMaybeTerm(t *testing.T) {
  104. tests := []struct {
  105. entries []pb.Entry
  106. offset uint64
  107. snap *pb.Snapshot
  108. index uint64
  109. wok bool
  110. wterm uint64
  111. }{
  112. // term from entries
  113. {
  114. []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
  115. 5,
  116. true, 1,
  117. },
  118. {
  119. []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
  120. 6,
  121. false, 0,
  122. },
  123. {
  124. []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
  125. 4,
  126. false, 0,
  127. },
  128. {
  129. []pb.Entry{{Index: 5, Term: 1}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
  130. 5,
  131. true, 1,
  132. },
  133. {
  134. []pb.Entry{{Index: 5, Term: 1}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
  135. 6,
  136. false, 0,
  137. },
  138. // term from snapshot
  139. {
  140. []pb.Entry{{Index: 5, Term: 1}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
  141. 4,
  142. true, 1,
  143. },
  144. {
  145. []pb.Entry{}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
  146. 5,
  147. false, 0,
  148. },
  149. {
  150. []pb.Entry{}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
  151. 4,
  152. true, 1,
  153. },
  154. {
  155. []pb.Entry{}, 0, nil,
  156. 5,
  157. false, 0,
  158. },
  159. }
  160. for i, tt := range tests {
  161. u := unstable{
  162. entries: tt.entries,
  163. offset: tt.offset,
  164. snapshot: tt.snap,
  165. }
  166. term, ok := u.maybeTerm(tt.index)
  167. if ok != tt.wok {
  168. t.Errorf("#%d: ok = %t, want %t", i, ok, tt.wok)
  169. }
  170. if term != tt.wterm {
  171. t.Errorf("#%d: term = %d, want %d", i, term, tt.wterm)
  172. }
  173. }
  174. }