storage_test.go 6.9 KB

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