storage_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // Copyright 2015 CoreOS, Inc.
  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 raft
  15. import (
  16. "reflect"
  17. "testing"
  18. pb "github.com/coreos/etcd/raft/raftpb"
  19. )
  20. // TODO(xiangli): Test panic cases
  21. func TestStorageTerm(t *testing.T) {
  22. ents := []pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 5}}
  23. tests := []struct {
  24. i uint64
  25. werr error
  26. wterm uint64
  27. }{
  28. {2, ErrCompacted, 0},
  29. {3, nil, 3},
  30. {4, nil, 4},
  31. {5, nil, 5},
  32. }
  33. for i, tt := range tests {
  34. s := &MemoryStorage{ents: ents}
  35. term, err := s.Term(tt.i)
  36. if err != tt.werr {
  37. t.Errorf("#%d: err = %v, want %v", i, err, tt.werr)
  38. }
  39. if term != tt.wterm {
  40. t.Errorf("#%d: term = %d, want %d", i, term, tt.wterm)
  41. }
  42. }
  43. }
  44. func TestStorageEntries(t *testing.T) {
  45. ents := []pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 5}}
  46. tests := []struct {
  47. lo, hi uint64
  48. werr error
  49. wentries []pb.Entry
  50. }{
  51. {2, 6, ErrCompacted, nil},
  52. {3, 4, ErrCompacted, nil},
  53. {4, 5, nil, []pb.Entry{{Index: 4, Term: 4}}},
  54. {4, 6, nil, []pb.Entry{{Index: 4, Term: 4}, {Index: 5, Term: 5}}},
  55. }
  56. for i, tt := range tests {
  57. s := &MemoryStorage{ents: ents}
  58. entries, err := s.Entries(tt.lo, tt.hi)
  59. if err != tt.werr {
  60. t.Errorf("#%d: err = %v, want %v", i, err, tt.werr)
  61. }
  62. if !reflect.DeepEqual(entries, tt.wentries) {
  63. t.Errorf("#%d: entries = %v, want %v", i, entries, tt.wentries)
  64. }
  65. }
  66. }
  67. func TestStorageLastIndex(t *testing.T) {
  68. ents := []pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 5}}
  69. s := &MemoryStorage{ents: ents}
  70. last, err := s.LastIndex()
  71. if err != nil {
  72. t.Errorf("err = %v, want nil", err)
  73. }
  74. if last != 5 {
  75. t.Errorf("term = %d, want %d", last, 5)
  76. }
  77. s.Append([]pb.Entry{{Index: 6, Term: 5}})
  78. last, err = s.LastIndex()
  79. if err != nil {
  80. t.Errorf("err = %v, want nil", err)
  81. }
  82. if last != 6 {
  83. t.Errorf("last = %d, want %d", last, 5)
  84. }
  85. }
  86. func TestStorageFirstIndex(t *testing.T) {
  87. ents := []pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 5}}
  88. s := &MemoryStorage{ents: ents}
  89. first, err := s.FirstIndex()
  90. if err != nil {
  91. t.Errorf("err = %v, want nil", err)
  92. }
  93. if first != 4 {
  94. t.Errorf("first = %d, want %d", first, 4)
  95. }
  96. s.Compact(4)
  97. first, err = s.FirstIndex()
  98. if err != nil {
  99. t.Errorf("err = %v, want nil", err)
  100. }
  101. if first != 5 {
  102. t.Errorf("first = %d, want %d", first, 5)
  103. }
  104. }
  105. func TestStorageCompact(t *testing.T) {
  106. ents := []pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 5}}
  107. tests := []struct {
  108. i uint64
  109. werr error
  110. windex uint64
  111. wterm uint64
  112. wlen int
  113. }{
  114. {2, ErrCompacted, 3, 3, 3},
  115. {3, ErrCompacted, 3, 3, 3},
  116. {4, nil, 4, 4, 2},
  117. {5, nil, 5, 5, 1},
  118. }
  119. for i, tt := range tests {
  120. s := &MemoryStorage{ents: ents}
  121. err := s.Compact(tt.i)
  122. if err != tt.werr {
  123. t.Errorf("#%d: err = %v, want %v", i, err, tt.werr)
  124. }
  125. if s.ents[0].Index != tt.windex {
  126. t.Errorf("#%d: index = %d, want %d", i, s.ents[0].Index, tt.windex)
  127. }
  128. if s.ents[0].Term != tt.wterm {
  129. t.Errorf("#%d: term = %d, want %d", i, s.ents[0].Term, tt.wterm)
  130. }
  131. if len(s.ents) != tt.wlen {
  132. t.Errorf("#%d: len = %d, want %d", i, len(s.ents), tt.wlen)
  133. }
  134. }
  135. }
  136. func TestStorageCreateSnapshot(t *testing.T) {
  137. ents := []pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 5}}
  138. cs := &pb.ConfState{Nodes: []uint64{1, 2, 3}}
  139. data := []byte("data")
  140. tests := []struct {
  141. i uint64
  142. werr error
  143. wsnap pb.Snapshot
  144. }{
  145. {4, nil, pb.Snapshot{Data: data, Metadata: pb.SnapshotMetadata{Index: 4, Term: 4, ConfState: *cs}}},
  146. {5, nil, pb.Snapshot{Data: data, Metadata: pb.SnapshotMetadata{Index: 5, Term: 5, ConfState: *cs}}},
  147. }
  148. for i, tt := range tests {
  149. s := &MemoryStorage{ents: ents}
  150. snap, err := s.CreateSnapshot(tt.i, cs, data)
  151. if err != tt.werr {
  152. t.Errorf("#%d: err = %v, want %v", i, err, tt.werr)
  153. }
  154. if !reflect.DeepEqual(snap, tt.wsnap) {
  155. t.Errorf("#%d: snap = %+v, want %+v", i, snap, tt.wsnap)
  156. }
  157. }
  158. }
  159. func TestStorageAppend(t *testing.T) {
  160. ents := []pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 5}}
  161. tests := []struct {
  162. entries []pb.Entry
  163. werr error
  164. wentries []pb.Entry
  165. }{
  166. {
  167. []pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 5}},
  168. nil,
  169. []pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 5}},
  170. },
  171. {
  172. []pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 6}, {Index: 5, Term: 6}},
  173. nil,
  174. []pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 6}, {Index: 5, Term: 6}},
  175. },
  176. {
  177. []pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 5}, {Index: 6, Term: 5}},
  178. nil,
  179. []pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 5}, {Index: 6, Term: 5}},
  180. },
  181. // truncate incoming entries, truncate the existing entries and append
  182. {
  183. []pb.Entry{{Index: 2, Term: 3}, {Index: 3, Term: 3}, {Index: 4, Term: 5}},
  184. nil,
  185. []pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 5}},
  186. },
  187. // tunncate the existing entries and append
  188. {
  189. []pb.Entry{{Index: 4, Term: 5}},
  190. nil,
  191. []pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 5}},
  192. },
  193. // direct append
  194. {
  195. []pb.Entry{{Index: 6, Term: 5}},
  196. nil,
  197. []pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 5}, {Index: 6, Term: 5}},
  198. },
  199. }
  200. for i, tt := range tests {
  201. s := &MemoryStorage{ents: ents}
  202. err := s.Append(tt.entries)
  203. if err != tt.werr {
  204. t.Errorf("#%d: err = %v, want %v", i, err, tt.werr)
  205. }
  206. if !reflect.DeepEqual(s.ents, tt.wentries) {
  207. t.Errorf("#%d: entries = %v, want %v", i, s.ents, tt.wentries)
  208. }
  209. }
  210. }