log_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. if !reflect.DeepEqual(ents, tt.wents) {
  124. t.Errorf("#%d: unstableEnts = %+v, want %+v", i, ents, tt.wents)
  125. }
  126. if g := raftLog.unstable; g != tt.wunstable {
  127. t.Errorf("#%d: unstable = %d, want %d", i, g, tt.wunstable)
  128. }
  129. }
  130. }
  131. //TestCompaction ensures that the number of log entreis is correct after compactions.
  132. func TestCompaction(t *testing.T) {
  133. tests := []struct {
  134. app int
  135. compact []int64
  136. wleft []int
  137. wallow bool
  138. }{
  139. // out of upper bound
  140. {1000, []int64{1001}, []int{-1}, false},
  141. {1000, []int64{300, 500, 800, 900}, []int{701, 501, 201, 101}, true},
  142. // out of lower bound
  143. {1000, []int64{300, 299}, []int{701, -1}, false},
  144. }
  145. for i, tt := range tests {
  146. func() {
  147. defer func() {
  148. if r := recover(); r != nil {
  149. if tt.wallow == true {
  150. t.Errorf("%d: allow = %v, want %v", i, false, true)
  151. }
  152. }
  153. }()
  154. raftLog := newLog()
  155. for i := 0; i < tt.app; i++ {
  156. raftLog.append(int64(i), Entry{})
  157. }
  158. for j := 0; j < len(tt.compact); j++ {
  159. raftLog.compact(tt.compact[j])
  160. if len(raftLog.ents) != tt.wleft[j] {
  161. t.Errorf("#%d.%d len = %d, want %d", i, j, len(raftLog.ents), tt.wleft[j])
  162. }
  163. }
  164. }()
  165. }
  166. }
  167. func TestLogRestore(t *testing.T) {
  168. var i int64
  169. raftLog := newLog()
  170. for i = 0; i < 100; i++ {
  171. raftLog.append(i, Entry{Term: i + 1})
  172. }
  173. index := int64(1000)
  174. term := int64(1000)
  175. raftLog.restore(index, term)
  176. // only has the guard entry
  177. if len(raftLog.ents) != 1 {
  178. t.Errorf("len = %d, want 0", len(raftLog.ents))
  179. }
  180. if raftLog.offset != index {
  181. t.Errorf("offset = %d, want %d", raftLog.offset, index)
  182. }
  183. if raftLog.applied != index {
  184. t.Errorf("applied = %d, want %d", raftLog.applied, index)
  185. }
  186. if raftLog.committed != index {
  187. t.Errorf("comitted = %d, want %d", raftLog.committed, index)
  188. }
  189. if raftLog.unstable != index+1 {
  190. t.Errorf("unstable = %d, want %d", raftLog.unstable, index+1)
  191. }
  192. if raftLog.term(index) != term {
  193. t.Errorf("term = %d, want %d", raftLog.term(index), term)
  194. }
  195. }
  196. func TestIsOutOfBounds(t *testing.T) {
  197. offset := int64(100)
  198. num := int64(100)
  199. l := &raftLog{offset: offset, ents: make([]Entry, num)}
  200. tests := []struct {
  201. index int64
  202. w bool
  203. }{
  204. {offset - 1, true},
  205. {offset, false},
  206. {offset + num/2, false},
  207. {offset + num - 1, false},
  208. {offset + num, true},
  209. }
  210. for i, tt := range tests {
  211. g := l.isOutOfBounds(tt.index)
  212. if g != tt.w {
  213. t.Errorf("#%d: isOutOfBounds = %v, want %v", i, g, tt.w)
  214. }
  215. }
  216. }
  217. func TestAt(t *testing.T) {
  218. var i int64
  219. offset := int64(100)
  220. num := int64(100)
  221. l := &raftLog{offset: offset}
  222. for i = 0; i < num; i++ {
  223. l.ents = append(l.ents, Entry{Term: i})
  224. }
  225. tests := []struct {
  226. index int64
  227. w *Entry
  228. }{
  229. {offset - 1, nil},
  230. {offset, &Entry{Term: 0}},
  231. {offset + num/2, &Entry{Term: num / 2}},
  232. {offset + num - 1, &Entry{Term: num - 1}},
  233. {offset + num, nil},
  234. }
  235. for i, tt := range tests {
  236. g := l.at(tt.index)
  237. if !reflect.DeepEqual(g, tt.w) {
  238. t.Errorf("#%d: at = %v, want %v", i, g, tt.w)
  239. }
  240. }
  241. }
  242. func TestSlice(t *testing.T) {
  243. var i int64
  244. offset := int64(100)
  245. num := int64(100)
  246. l := &raftLog{offset: offset}
  247. for i = 0; i < num; i++ {
  248. l.ents = append(l.ents, Entry{Term: i})
  249. }
  250. tests := []struct {
  251. from int64
  252. to int64
  253. w []Entry
  254. }{
  255. {offset - 1, offset + 1, nil},
  256. {offset, offset + 1, []Entry{{Term: 0}}},
  257. {offset + num/2, offset + num/2 + 1, []Entry{{Term: num / 2}}},
  258. {offset + num - 1, offset + num, []Entry{{Term: num - 1}}},
  259. {offset + num, offset + num + 1, nil},
  260. {offset + num/2, offset + num/2, nil},
  261. {offset + num/2, offset + num/2 - 1, nil},
  262. }
  263. for i, tt := range tests {
  264. g := l.slice(tt.from, tt.to)
  265. if !reflect.DeepEqual(g, tt.w) {
  266. t.Errorf("#%d: from %d to %d = %v, want %v", i, tt.from, tt.to, g, tt.w)
  267. }
  268. }
  269. }