log_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. package raft
  2. import (
  3. "reflect"
  4. "testing"
  5. )
  6. // TestAppend ensures:
  7. // 1. If an existing entry conflicts with a new one (same index
  8. // but different terms), delete the existing entry and all that
  9. // follow it
  10. // 2.Append any new entries not already in the log
  11. func TestAppend(t *testing.T) {
  12. previousEnts := []Entry{{Term: 1}, {Term: 2}}
  13. previousUnstable := int64(3)
  14. tests := []struct {
  15. after int64
  16. ents []Entry
  17. windex int64
  18. wents []Entry
  19. wunstable int64
  20. }{
  21. {
  22. 2,
  23. []Entry{},
  24. 2,
  25. []Entry{{Term: 1}, {Term: 2}},
  26. 3,
  27. },
  28. {
  29. 2,
  30. []Entry{{Term: 2}},
  31. 3,
  32. []Entry{{Term: 1}, {Term: 2}, {Term: 2}},
  33. 3,
  34. },
  35. // conflicts with index 1
  36. {
  37. 0,
  38. []Entry{{Term: 2}},
  39. 1,
  40. []Entry{{Term: 2}},
  41. 1,
  42. },
  43. // conflicts with index 2
  44. {
  45. 1,
  46. []Entry{{Term: 3}, {Term: 3}},
  47. 3,
  48. []Entry{{Term: 1}, {Term: 3}, {Term: 3}},
  49. 2,
  50. },
  51. }
  52. for i, tt := range tests {
  53. raftLog := newLog()
  54. raftLog.ents = append(raftLog.ents, previousEnts...)
  55. raftLog.unstable = previousUnstable
  56. index := raftLog.append(tt.after, tt.ents...)
  57. if index != tt.windex {
  58. t.Errorf("#%d: lastIndex = %d, want %d", i, index, tt.windex)
  59. }
  60. if g := raftLog.entries(1); !reflect.DeepEqual(g, tt.wents) {
  61. t.Errorf("#%d: logEnts = %+v, want %+v", i, g, tt.wents)
  62. }
  63. if g := raftLog.unstable; g != tt.wunstable {
  64. t.Errorf("#%d: unstable = %d, want %d", i, g, tt.wunstable)
  65. }
  66. }
  67. }
  68. // TestCompactionSideEffects ensures that all the log related funcationality works correctly after
  69. // a compaction.
  70. func TestCompactionSideEffects(t *testing.T) {
  71. var i int64
  72. lastIndex := int64(1000)
  73. raftLog := newLog()
  74. for i = 0; i < lastIndex; i++ {
  75. raftLog.append(int64(i), Entry{Term: int64(i + 1), Index: int64(i + 1)})
  76. }
  77. raftLog.compact(500)
  78. if raftLog.lastIndex() != lastIndex {
  79. t.Errorf("lastIndex = %d, want %d", raftLog.lastIndex(), lastIndex)
  80. }
  81. for i := raftLog.offset; i <= raftLog.lastIndex(); i++ {
  82. if raftLog.term(i) != i {
  83. t.Errorf("term(%d) = %d, want %d", i, raftLog.term(i), i)
  84. }
  85. }
  86. for i := raftLog.offset; i <= raftLog.lastIndex(); i++ {
  87. if !raftLog.matchTerm(i, i) {
  88. t.Errorf("matchTerm(%d) = false, want true", i)
  89. }
  90. }
  91. unstableEnts := raftLog.unstableEnts()
  92. if g := len(unstableEnts); g != 500 {
  93. t.Errorf("len(unstableEntries) = %d, want = %d", g, 500)
  94. }
  95. if unstableEnts[0].Index != 501 {
  96. t.Errorf("Index = %d, want = %d", unstableEnts[0].Index, 501)
  97. }
  98. prev := raftLog.lastIndex()
  99. raftLog.append(raftLog.lastIndex(), Entry{Term: raftLog.lastIndex() + 1})
  100. if raftLog.lastIndex() != prev+1 {
  101. t.Errorf("lastIndex = %d, want = %d", raftLog.lastIndex(), prev+1)
  102. }
  103. ents := raftLog.entries(raftLog.lastIndex())
  104. if len(ents) != 1 {
  105. t.Errorf("len(entries) = %d, want = %d", len(ents), 1)
  106. }
  107. }
  108. func TestUnstableEnts(t *testing.T) {
  109. previousEnts := []Entry{{Term: 1, Index: 1}, {Term: 2, Index: 2}}
  110. tests := []struct {
  111. unstable int64
  112. wents []Entry
  113. wunstable int64
  114. }{
  115. {3, nil, 3},
  116. {1, previousEnts, 3},
  117. }
  118. for i, tt := range tests {
  119. raftLog := newLog()
  120. raftLog.ents = append(raftLog.ents, previousEnts...)
  121. raftLog.unstable = tt.unstable
  122. ents := raftLog.unstableEnts()
  123. raftLog.resetUnstable()
  124. if !reflect.DeepEqual(ents, tt.wents) {
  125. t.Errorf("#%d: unstableEnts = %+v, want %+v", i, ents, tt.wents)
  126. }
  127. if g := raftLog.unstable; g != tt.wunstable {
  128. t.Errorf("#%d: unstable = %d, want %d", i, g, tt.wunstable)
  129. }
  130. }
  131. }
  132. //TestCompaction ensures that the number of log entreis is correct after compactions.
  133. func TestCompaction(t *testing.T) {
  134. tests := []struct {
  135. app int
  136. compact []int64
  137. wleft []int
  138. wallow bool
  139. }{
  140. // out of upper bound
  141. {1000, []int64{1001}, []int{-1}, false},
  142. {1000, []int64{300, 500, 800, 900}, []int{701, 501, 201, 101}, true},
  143. // out of lower bound
  144. {1000, []int64{300, 299}, []int{701, -1}, false},
  145. }
  146. for i, tt := range tests {
  147. func() {
  148. defer func() {
  149. if r := recover(); r != nil {
  150. if tt.wallow == true {
  151. t.Errorf("%d: allow = %v, want %v", i, false, true)
  152. }
  153. }
  154. }()
  155. raftLog := newLog()
  156. for i := 0; i < tt.app; i++ {
  157. raftLog.append(int64(i), Entry{})
  158. }
  159. for j := 0; j < len(tt.compact); j++ {
  160. raftLog.compact(tt.compact[j])
  161. if len(raftLog.ents) != tt.wleft[j] {
  162. t.Errorf("#%d.%d len = %d, want %d", i, j, len(raftLog.ents), tt.wleft[j])
  163. }
  164. }
  165. }()
  166. }
  167. }
  168. func TestLogRestore(t *testing.T) {
  169. var i int64
  170. raftLog := newLog()
  171. for i = 0; i < 100; i++ {
  172. raftLog.append(i, Entry{Term: i + 1})
  173. }
  174. index := int64(1000)
  175. term := int64(1000)
  176. raftLog.restore(Snapshot{Index: index, Term: term})
  177. // only has the guard entry
  178. if len(raftLog.ents) != 1 {
  179. t.Errorf("len = %d, want 0", len(raftLog.ents))
  180. }
  181. if raftLog.offset != index {
  182. t.Errorf("offset = %d, want %d", raftLog.offset, index)
  183. }
  184. if raftLog.applied != index {
  185. t.Errorf("applied = %d, want %d", raftLog.applied, index)
  186. }
  187. if raftLog.committed != index {
  188. t.Errorf("comitted = %d, want %d", raftLog.committed, index)
  189. }
  190. if raftLog.unstable != index+1 {
  191. t.Errorf("unstable = %d, want %d", raftLog.unstable, index+1)
  192. }
  193. if raftLog.term(index) != term {
  194. t.Errorf("term = %d, want %d", raftLog.term(index), term)
  195. }
  196. }
  197. func TestIsOutOfBounds(t *testing.T) {
  198. offset := int64(100)
  199. num := int64(100)
  200. l := &raftLog{offset: offset, ents: make([]Entry, num)}
  201. tests := []struct {
  202. index int64
  203. w bool
  204. }{
  205. {offset - 1, true},
  206. {offset, false},
  207. {offset + num/2, false},
  208. {offset + num - 1, false},
  209. {offset + num, true},
  210. }
  211. for i, tt := range tests {
  212. g := l.isOutOfBounds(tt.index)
  213. if g != tt.w {
  214. t.Errorf("#%d: isOutOfBounds = %v, want %v", i, g, tt.w)
  215. }
  216. }
  217. }
  218. func TestAt(t *testing.T) {
  219. var i int64
  220. offset := int64(100)
  221. num := int64(100)
  222. l := &raftLog{offset: offset}
  223. for i = 0; i < num; i++ {
  224. l.ents = append(l.ents, Entry{Term: i})
  225. }
  226. tests := []struct {
  227. index int64
  228. w *Entry
  229. }{
  230. {offset - 1, nil},
  231. {offset, &Entry{Term: 0}},
  232. {offset + num/2, &Entry{Term: num / 2}},
  233. {offset + num - 1, &Entry{Term: num - 1}},
  234. {offset + num, nil},
  235. }
  236. for i, tt := range tests {
  237. g := l.at(tt.index)
  238. if !reflect.DeepEqual(g, tt.w) {
  239. t.Errorf("#%d: at = %v, want %v", i, g, tt.w)
  240. }
  241. }
  242. }
  243. func TestSlice(t *testing.T) {
  244. var i int64
  245. offset := int64(100)
  246. num := int64(100)
  247. l := &raftLog{offset: offset}
  248. for i = 0; i < num; i++ {
  249. l.ents = append(l.ents, Entry{Term: i})
  250. }
  251. tests := []struct {
  252. from int64
  253. to int64
  254. w []Entry
  255. }{
  256. {offset - 1, offset + 1, nil},
  257. {offset, offset + 1, []Entry{{Term: 0}}},
  258. {offset + num/2, offset + num/2 + 1, []Entry{{Term: num / 2}}},
  259. {offset + num - 1, offset + num, []Entry{{Term: num - 1}}},
  260. {offset + num, offset + num + 1, nil},
  261. {offset + num/2, offset + num/2, nil},
  262. {offset + num/2, offset + num/2 - 1, nil},
  263. }
  264. for i, tt := range tests {
  265. g := l.slice(tt.from, tt.to)
  266. if !reflect.DeepEqual(g, tt.w) {
  267. t.Errorf("#%d: from %d to %d = %v, want %v", i, tt.from, tt.to, g, tt.w)
  268. }
  269. }
  270. }