node_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. package raft
  2. import (
  3. "reflect"
  4. "runtime"
  5. "testing"
  6. "time"
  7. "github.com/coreos/etcd/raft/raftpb"
  8. "github.com/coreos/etcd/third_party/code.google.com/p/go.net/context"
  9. )
  10. // TestNodeStep ensures that node.Step sends msgProp to propc chan
  11. // and other kinds of messages to recvc chan.
  12. func TestNodeStep(t *testing.T) {
  13. for i := range mtmap {
  14. n := &Node{
  15. propc: make(chan raftpb.Message, 1),
  16. recvc: make(chan raftpb.Message, 1),
  17. }
  18. n.Step(context.TODO(), raftpb.Message{Type: int64(i)})
  19. // Proposal goes to proc chan. Others go to recvc chan.
  20. if int64(i) == msgProp {
  21. select {
  22. case <-n.propc:
  23. default:
  24. t.Errorf("%d: cannot receive %s on propc chan", i, mtmap[i])
  25. }
  26. } else {
  27. select {
  28. case <-n.recvc:
  29. default:
  30. t.Errorf("%d: cannot receive %s on recvc chan", i, mtmap[i])
  31. }
  32. }
  33. }
  34. }
  35. // Cancel and Stop should unblock Step()
  36. func TestNodeStepUnblock(t *testing.T) {
  37. // a node without buffer to block step
  38. n := &Node{
  39. propc: make(chan raftpb.Message),
  40. done: make(chan struct{}),
  41. }
  42. ctx, cancel := context.WithCancel(context.Background())
  43. stopFunc := func() { close(n.done) }
  44. tests := []struct {
  45. unblock func()
  46. werr error
  47. }{
  48. {stopFunc, ErrStopped},
  49. {cancel, context.Canceled},
  50. }
  51. for i, tt := range tests {
  52. errc := make(chan error, 1)
  53. go func() {
  54. err := n.Step(ctx, raftpb.Message{Type: msgProp})
  55. errc <- err
  56. }()
  57. tt.unblock()
  58. select {
  59. case err := <-errc:
  60. if err != tt.werr {
  61. t.Errorf("#%d: err = %v, want %v", err, tt.werr)
  62. }
  63. //clean up side-effect
  64. if ctx.Err() != nil {
  65. ctx = context.TODO()
  66. }
  67. select {
  68. case <-n.done:
  69. n.done = make(chan struct{})
  70. default:
  71. }
  72. case <-time.After(time.Millisecond * 100):
  73. t.Errorf("#%d: failed to unblock step", i)
  74. }
  75. }
  76. }
  77. // TestBlockProposal ensures that node will block proposal when it does not
  78. // know who is the current leader; node will accept proposal when it knows
  79. // who is the current leader.
  80. func TestBlockProposal(t *testing.T) {
  81. n := newNode()
  82. r := newRaft(1, []int64{1}, 10, 1)
  83. go n.run(r)
  84. defer n.Stop()
  85. errc := make(chan error, 1)
  86. go func() {
  87. errc <- n.Propose(context.TODO(), []byte("somedata"))
  88. }()
  89. forceGosched()
  90. select {
  91. case err := <-errc:
  92. t.Errorf("err = %v, want blocking", err)
  93. default:
  94. }
  95. n.Campaign(context.TODO())
  96. forceGosched()
  97. select {
  98. case err := <-errc:
  99. if err != nil {
  100. t.Errorf("err = %v, want %v", err, nil)
  101. }
  102. default:
  103. t.Errorf("blocking proposal, want unblocking")
  104. }
  105. }
  106. func TestReadyContainUpdates(t *testing.T) {
  107. tests := []struct {
  108. rd Ready
  109. wcontain bool
  110. }{
  111. {Ready{}, false},
  112. {Ready{SoftState: &SoftState{Lead: 1}}, true},
  113. {Ready{HardState: raftpb.HardState{Vote: 1}}, true},
  114. {Ready{Entries: make([]raftpb.Entry, 1, 1)}, true},
  115. {Ready{CommittedEntries: make([]raftpb.Entry, 1, 1)}, true},
  116. {Ready{Messages: make([]raftpb.Message, 1, 1)}, true},
  117. {Ready{Snapshot: raftpb.Snapshot{Index: 1}}, true},
  118. }
  119. for i, tt := range tests {
  120. if g := tt.rd.containsUpdates(); g != tt.wcontain {
  121. t.Errorf("#%d: containUpdates = %v, want %v", i, g, tt.wcontain)
  122. }
  123. }
  124. }
  125. func TestNode(t *testing.T) {
  126. ctx, cancel := context.WithCancel(context.Background())
  127. defer cancel()
  128. wants := []Ready{
  129. {
  130. SoftState: &SoftState{Lead: 1, RaftState: StateLeader},
  131. HardState: raftpb.HardState{Term: 1, Commit: 1},
  132. Entries: []raftpb.Entry{{}, {Term: 1, Index: 1}},
  133. CommittedEntries: []raftpb.Entry{{Term: 1, Index: 1}},
  134. },
  135. {
  136. HardState: raftpb.HardState{Term: 1, Commit: 2},
  137. Entries: []raftpb.Entry{{Term: 1, Index: 2, Data: []byte("foo")}},
  138. CommittedEntries: []raftpb.Entry{{Term: 1, Index: 2, Data: []byte("foo")}},
  139. },
  140. }
  141. n := Start(1, []int64{1}, 0, 0)
  142. n.Campaign(ctx)
  143. if g := <-n.Ready(); !reflect.DeepEqual(g, wants[0]) {
  144. t.Errorf("#%d: g = %+v,\n w %+v", 1, g, wants[0])
  145. }
  146. n.Propose(ctx, []byte("foo"))
  147. if g := <-n.Ready(); !reflect.DeepEqual(g, wants[1]) {
  148. t.Errorf("#%d: g = %+v,\n w %+v", 2, g, wants[1])
  149. }
  150. select {
  151. case rd := <-n.Ready():
  152. t.Errorf("unexpected Ready: %+v", rd)
  153. default:
  154. }
  155. }
  156. func TestNodeRestart(t *testing.T) {
  157. entries := []raftpb.Entry{
  158. {},
  159. {Term: 1, Index: 1},
  160. {Term: 1, Index: 2, Data: []byte("foo")},
  161. }
  162. st := raftpb.HardState{Term: 1, Commit: 1}
  163. want := Ready{
  164. HardState: emptyState,
  165. // commit upto index commit index in st
  166. CommittedEntries: entries[1 : st.Commit+1],
  167. }
  168. n := Restart(1, []int64{1}, 0, 0, nil, st, entries)
  169. if g := <-n.Ready(); !reflect.DeepEqual(g, want) {
  170. t.Errorf("g = %+v,\n w %+v", g, want)
  171. }
  172. select {
  173. case rd := <-n.Ready():
  174. t.Errorf("unexpected Ready: %+v", rd)
  175. default:
  176. }
  177. }
  178. // TestCompacts ensures Node.Compact creates a correct raft snapshot and compacts
  179. // the raft log (call raft.compact)
  180. func TestCompact(t *testing.T) {
  181. ctx := context.Background()
  182. n := newNode()
  183. r := newRaft(1, []int64{1}, 0, 0)
  184. go n.run(r)
  185. n.Campaign(ctx)
  186. n.Propose(ctx, []byte("foo"))
  187. w := raftpb.Snapshot{
  188. Term: 1,
  189. Index: 2, // one nop + one proposal
  190. Data: []byte("a snapshot"),
  191. Nodes: []int64{1},
  192. }
  193. forceGosched()
  194. select {
  195. case <-n.Ready():
  196. default:
  197. t.Fatalf("unexpected proposal failure: unable to commit entry")
  198. }
  199. n.Compact(w.Data)
  200. forceGosched()
  201. select {
  202. case rd := <-n.Ready():
  203. if !reflect.DeepEqual(rd.Snapshot, w) {
  204. t.Errorf("snap = %+v, want %+v", rd.Snapshot, w)
  205. }
  206. default:
  207. t.Fatalf("unexpected compact failure: unable to create a snapshot")
  208. }
  209. forceGosched()
  210. // TODO: this test the run updates the snapi correctly... should be tested
  211. // separately with other kinds of updates
  212. select {
  213. case <-n.Ready():
  214. t.Fatalf("unexpected more ready")
  215. default:
  216. }
  217. n.Stop()
  218. if r.raftLog.offset != w.Index {
  219. t.Errorf("log.offset = %d, want %d", r.raftLog.offset, w.Index)
  220. }
  221. }
  222. func TestIsStateEqual(t *testing.T) {
  223. tests := []struct {
  224. st raftpb.HardState
  225. we bool
  226. }{
  227. {emptyState, true},
  228. {raftpb.HardState{Vote: 1}, false},
  229. {raftpb.HardState{Commit: 1}, false},
  230. {raftpb.HardState{Term: 1}, false},
  231. }
  232. for i, tt := range tests {
  233. if isHardStateEqual(tt.st, emptyState) != tt.we {
  234. t.Errorf("#%d, equal = %v, want %v", i, isHardStateEqual(tt.st, emptyState), tt.we)
  235. }
  236. }
  237. }
  238. // WARNING: This is a hack.
  239. // Remove this when we are able to block/check the status of the go-routines.
  240. func forceGosched() {
  241. // possibility enough to sched upto 10 go routines.
  242. for i := 0; i < 10000; i++ {
  243. runtime.Gosched()
  244. }
  245. }