node_test.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. "reflect"
  16. "testing"
  17. "time"
  18. "github.com/coreos/etcd/Godeps/_workspace/src/code.google.com/p/go.net/context"
  19. "github.com/coreos/etcd/pkg/testutil"
  20. "github.com/coreos/etcd/raft/raftpb"
  21. )
  22. // TestNodeStep ensures that node.Step sends msgProp to propc chan
  23. // and other kinds of messages to recvc chan.
  24. func TestNodeStep(t *testing.T) {
  25. for i, msgn := range raftpb.MessageType_name {
  26. n := &node{
  27. propc: make(chan raftpb.Message, 1),
  28. recvc: make(chan raftpb.Message, 1),
  29. }
  30. msgt := raftpb.MessageType(i)
  31. n.Step(context.TODO(), raftpb.Message{Type: msgt})
  32. // Proposal goes to proc chan. Others go to recvc chan.
  33. if msgt == raftpb.MsgProp {
  34. select {
  35. case <-n.propc:
  36. default:
  37. t.Errorf("%d: cannot receive %s on propc chan", msgt, msgn)
  38. }
  39. } else {
  40. if msgt == raftpb.MsgBeat || msgt == raftpb.MsgHup {
  41. select {
  42. case <-n.recvc:
  43. t.Errorf("%d: step should ignore %s", msgt, msgn)
  44. default:
  45. }
  46. } else {
  47. select {
  48. case <-n.recvc:
  49. default:
  50. t.Errorf("%d: cannot receive %s on recvc chan", msgt, msgn)
  51. }
  52. }
  53. }
  54. }
  55. }
  56. // Cancel and Stop should unblock Step()
  57. func TestNodeStepUnblock(t *testing.T) {
  58. // a node without buffer to block step
  59. n := &node{
  60. propc: make(chan raftpb.Message),
  61. done: make(chan struct{}),
  62. }
  63. ctx, cancel := context.WithCancel(context.Background())
  64. stopFunc := func() { close(n.done) }
  65. tests := []struct {
  66. unblock func()
  67. werr error
  68. }{
  69. {stopFunc, ErrStopped},
  70. {cancel, context.Canceled},
  71. }
  72. for i, tt := range tests {
  73. errc := make(chan error, 1)
  74. go func() {
  75. err := n.Step(ctx, raftpb.Message{Type: raftpb.MsgProp})
  76. errc <- err
  77. }()
  78. tt.unblock()
  79. select {
  80. case err := <-errc:
  81. if err != tt.werr {
  82. t.Errorf("#%d: err = %v, want %v", i, err, tt.werr)
  83. }
  84. //clean up side-effect
  85. if ctx.Err() != nil {
  86. ctx = context.TODO()
  87. }
  88. select {
  89. case <-n.done:
  90. n.done = make(chan struct{})
  91. default:
  92. }
  93. case <-time.After(time.Millisecond * 100):
  94. t.Errorf("#%d: failed to unblock step", i)
  95. }
  96. }
  97. }
  98. // TestNodePropose ensures that node.Propose sends the given proposal to the underlying raft.
  99. func TestNodePropose(t *testing.T) {
  100. msgs := []raftpb.Message{}
  101. appendStep := func(r *raft, m raftpb.Message) {
  102. msgs = append(msgs, m)
  103. }
  104. n := newNode()
  105. r := newRaft(1, []uint64{1}, 10, 1)
  106. go n.run(r)
  107. n.Campaign(context.TODO())
  108. for {
  109. rd := <-n.Ready()
  110. // change the step function to appendStep until this raft becomes leader
  111. if rd.SoftState.Lead == r.id {
  112. r.step = appendStep
  113. n.Advance()
  114. break
  115. }
  116. n.Advance()
  117. }
  118. n.Propose(context.TODO(), []byte("somedata"))
  119. n.Stop()
  120. if len(msgs) != 1 {
  121. t.Fatalf("len(msgs) = %d, want %d", len(msgs), 1)
  122. }
  123. if msgs[0].Type != raftpb.MsgProp {
  124. t.Errorf("msg type = %d, want %d", msgs[0].Type, raftpb.MsgProp)
  125. }
  126. if !reflect.DeepEqual(msgs[0].Entries[0].Data, []byte("somedata")) {
  127. t.Errorf("data = %v, want %v", msgs[0].Entries[0].Data, []byte("somedata"))
  128. }
  129. }
  130. // TestBlockProposal ensures that node will block proposal when it does not
  131. // know who is the current leader; node will accept proposal when it knows
  132. // who is the current leader.
  133. func TestBlockProposal(t *testing.T) {
  134. n := newNode()
  135. r := newRaft(1, []uint64{1}, 10, 1)
  136. go n.run(r)
  137. defer n.Stop()
  138. errc := make(chan error, 1)
  139. go func() {
  140. errc <- n.Propose(context.TODO(), []byte("somedata"))
  141. }()
  142. testutil.ForceGosched()
  143. select {
  144. case err := <-errc:
  145. t.Errorf("err = %v, want blocking", err)
  146. default:
  147. }
  148. n.Campaign(context.TODO())
  149. testutil.ForceGosched()
  150. select {
  151. case err := <-errc:
  152. if err != nil {
  153. t.Errorf("err = %v, want %v", err, nil)
  154. }
  155. default:
  156. t.Errorf("blocking proposal, want unblocking")
  157. }
  158. }
  159. // TestNodeTick ensures that node.Tick() will increase the
  160. // elapsed of the underly raft state machine.
  161. func TestNodeTick(t *testing.T) {
  162. n := newNode()
  163. r := newRaft(1, []uint64{1}, 10, 1)
  164. go n.run(r)
  165. elapsed := r.elapsed
  166. n.Tick()
  167. n.Stop()
  168. if r.elapsed != elapsed+1 {
  169. t.Errorf("elapsed = %d, want %d", r.elapsed, elapsed+1)
  170. }
  171. }
  172. func TestReadyContainUpdates(t *testing.T) {
  173. tests := []struct {
  174. rd Ready
  175. wcontain bool
  176. }{
  177. {Ready{}, false},
  178. {Ready{SoftState: &SoftState{Lead: 1}}, true},
  179. {Ready{HardState: raftpb.HardState{Vote: 1}}, true},
  180. {Ready{Entries: make([]raftpb.Entry, 1, 1)}, true},
  181. {Ready{CommittedEntries: make([]raftpb.Entry, 1, 1)}, true},
  182. {Ready{Messages: make([]raftpb.Message, 1, 1)}, true},
  183. {Ready{Snapshot: raftpb.Snapshot{Index: 1}}, true},
  184. }
  185. for i, tt := range tests {
  186. if g := tt.rd.containsUpdates(); g != tt.wcontain {
  187. t.Errorf("#%d: containUpdates = %v, want %v", i, g, tt.wcontain)
  188. }
  189. }
  190. }
  191. // TestNodeStart ensures that a node can be started correctly. The node should
  192. // start with correct configuration change entries, and can accept and commit
  193. // proposals.
  194. func TestNodeStart(t *testing.T) {
  195. ctx, cancel := context.WithCancel(context.Background())
  196. defer cancel()
  197. cc := raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 1}
  198. ccdata, err := cc.Marshal()
  199. if err != nil {
  200. t.Fatalf("unexpected marshal error: %v", err)
  201. }
  202. wants := []Ready{
  203. {
  204. SoftState: &SoftState{Lead: 1, Nodes: []uint64{1}, RaftState: StateLeader},
  205. HardState: raftpb.HardState{Term: 1, Commit: 2},
  206. Entries: []raftpb.Entry{
  207. {},
  208. {Type: raftpb.EntryConfChange, Term: 1, Index: 1, Data: ccdata},
  209. {Term: 1, Index: 2},
  210. },
  211. CommittedEntries: []raftpb.Entry{
  212. {Type: raftpb.EntryConfChange, Term: 1, Index: 1, Data: ccdata},
  213. {Term: 1, Index: 2},
  214. },
  215. },
  216. {
  217. HardState: raftpb.HardState{Term: 1, Commit: 3},
  218. Entries: []raftpb.Entry{{Term: 1, Index: 3, Data: []byte("foo")}},
  219. CommittedEntries: []raftpb.Entry{{Term: 1, Index: 3, Data: []byte("foo")}},
  220. },
  221. }
  222. n := StartNode(1, []Peer{{ID: 1}}, 10, 1)
  223. n.ApplyConfChange(cc)
  224. n.Campaign(ctx)
  225. if g := <-n.Ready(); !reflect.DeepEqual(g, wants[0]) {
  226. t.Errorf("#%d: g = %+v,\n w %+v", 1, g, wants[0])
  227. } else {
  228. n.Advance()
  229. }
  230. n.Propose(ctx, []byte("foo"))
  231. if g := <-n.Ready(); !reflect.DeepEqual(g, wants[1]) {
  232. t.Errorf("#%d: g = %+v,\n w %+v", 2, g, wants[1])
  233. } else {
  234. n.Advance()
  235. }
  236. select {
  237. case rd := <-n.Ready():
  238. t.Errorf("unexpected Ready: %+v", rd)
  239. default:
  240. }
  241. }
  242. func TestNodeRestart(t *testing.T) {
  243. entries := []raftpb.Entry{
  244. {},
  245. {Term: 1, Index: 1},
  246. {Term: 1, Index: 2, Data: []byte("foo")},
  247. }
  248. st := raftpb.HardState{Term: 1, Commit: 1}
  249. want := Ready{
  250. HardState: emptyState,
  251. // commit upto index commit index in st
  252. CommittedEntries: entries[1 : st.Commit+1],
  253. }
  254. n := RestartNode(1, 10, 1, nil, st, entries)
  255. if g := <-n.Ready(); !reflect.DeepEqual(g, want) {
  256. t.Errorf("g = %+v,\n w %+v", g, want)
  257. } else {
  258. n.Advance()
  259. }
  260. select {
  261. case rd := <-n.Ready():
  262. t.Errorf("unexpected Ready: %+v", rd)
  263. default:
  264. }
  265. }
  266. // TestCompacts ensures Node.Compact creates a correct raft snapshot and compacts
  267. // the raft log (call raft.compact)
  268. func TestNodeCompact(t *testing.T) {
  269. ctx := context.Background()
  270. n := newNode()
  271. r := newRaft(1, []uint64{1}, 10, 1)
  272. go n.run(r)
  273. n.Campaign(ctx)
  274. n.Propose(ctx, []byte("foo"))
  275. w := raftpb.Snapshot{
  276. Term: 1,
  277. Index: 2, // one nop + one proposal
  278. Data: []byte("a snapshot"),
  279. Nodes: []uint64{1},
  280. }
  281. testutil.ForceGosched()
  282. select {
  283. case <-n.Ready():
  284. n.Advance()
  285. default:
  286. t.Fatalf("unexpected proposal failure: unable to commit entry")
  287. }
  288. n.Compact(w.Index, w.Nodes, w.Data)
  289. testutil.ForceGosched()
  290. select {
  291. case rd := <-n.Ready():
  292. if !reflect.DeepEqual(rd.Snapshot, w) {
  293. t.Errorf("snap = %+v, want %+v", rd.Snapshot, w)
  294. }
  295. n.Advance()
  296. default:
  297. t.Fatalf("unexpected compact failure: unable to create a snapshot")
  298. }
  299. testutil.ForceGosched()
  300. // TODO: this test the run updates the snapi correctly... should be tested
  301. // separately with other kinds of updates
  302. select {
  303. case <-n.Ready():
  304. t.Fatalf("unexpected more ready")
  305. default:
  306. }
  307. n.Stop()
  308. if r.raftLog.offset != w.Index {
  309. t.Errorf("log.offset = %d, want %d", r.raftLog.offset, w.Index)
  310. }
  311. }
  312. func TestNodeAdvance(t *testing.T) {
  313. ctx, cancel := context.WithCancel(context.Background())
  314. defer cancel()
  315. n := StartNode(1, []Peer{{ID: 1}}, 10, 1)
  316. n.ApplyConfChange(raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 1})
  317. n.Campaign(ctx)
  318. <-n.Ready()
  319. n.Propose(ctx, []byte("foo"))
  320. select {
  321. case rd := <-n.Ready():
  322. t.Fatalf("unexpected Ready before Advance: %+v", rd)
  323. default:
  324. }
  325. n.Advance()
  326. select {
  327. case <-n.Ready():
  328. default:
  329. t.Errorf("expect Ready after Advance, but there is no Ready available")
  330. }
  331. }
  332. func TestSoftStateEqual(t *testing.T) {
  333. tests := []struct {
  334. st *SoftState
  335. we bool
  336. }{
  337. {&SoftState{}, true},
  338. {&SoftState{Lead: 1}, false},
  339. {&SoftState{RaftState: StateLeader}, false},
  340. {&SoftState{Nodes: []uint64{1, 2}}, false},
  341. }
  342. for i, tt := range tests {
  343. if g := tt.st.equal(&SoftState{}); g != tt.we {
  344. t.Errorf("#%d, equal = %v, want %v", i, g, tt.we)
  345. }
  346. }
  347. }
  348. func TestIsHardStateEqual(t *testing.T) {
  349. tests := []struct {
  350. st raftpb.HardState
  351. we bool
  352. }{
  353. {emptyState, true},
  354. {raftpb.HardState{Vote: 1}, false},
  355. {raftpb.HardState{Commit: 1}, false},
  356. {raftpb.HardState{Term: 1}, false},
  357. }
  358. for i, tt := range tests {
  359. if isHardStateEqual(tt.st, emptyState) != tt.we {
  360. t.Errorf("#%d, equal = %v, want %v", i, isHardStateEqual(tt.st, emptyState), tt.we)
  361. }
  362. }
  363. }