node_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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{State: raftpb.State{Vote: 1}}, true},
  113. {Ready{Entries: make([]raftpb.Entry, 1, 1)}, true},
  114. {Ready{CommittedEntries: make([]raftpb.Entry, 1, 1)}, true},
  115. {Ready{Messages: make([]raftpb.Message, 1, 1)}, true},
  116. }
  117. for i, tt := range tests {
  118. if tt.rd.containsUpdates() != tt.wcontain {
  119. t.Errorf("#%d: containUpdates = %v, want %v", i, tt.rd.containsUpdates(), tt.wcontain)
  120. }
  121. }
  122. }
  123. func TestNode(t *testing.T) {
  124. ctx, cancel := context.WithCancel(context.Background())
  125. defer cancel()
  126. wants := []Ready{
  127. {
  128. State: raftpb.State{Term: 1, Commit: 1},
  129. Entries: []raftpb.Entry{{}, {Term: 1, Index: 1}},
  130. CommittedEntries: []raftpb.Entry{{Term: 1, Index: 1}},
  131. },
  132. {
  133. State: raftpb.State{Term: 1, Commit: 2},
  134. Entries: []raftpb.Entry{{Term: 1, Index: 2, Data: []byte("foo")}},
  135. CommittedEntries: []raftpb.Entry{{Term: 1, Index: 2, Data: []byte("foo")}},
  136. },
  137. }
  138. n := Start(1, []int64{1}, 0, 0)
  139. n.Campaign(ctx)
  140. if g := <-n.Ready(); !reflect.DeepEqual(g, wants[0]) {
  141. t.Errorf("#%d: g = %+v,\n w %+v", 1, g, wants[0])
  142. }
  143. n.Propose(ctx, []byte("foo"))
  144. if g := <-n.Ready(); !reflect.DeepEqual(g, wants[1]) {
  145. t.Errorf("#%d: g = %+v,\n w %+v", 2, g, wants[1])
  146. }
  147. select {
  148. case rd := <-n.Ready():
  149. t.Errorf("unexpected Ready: %+v", rd)
  150. default:
  151. }
  152. }
  153. func TestNodeRestart(t *testing.T) {
  154. entries := []raftpb.Entry{
  155. {},
  156. {Term: 1, Index: 1},
  157. {Term: 1, Index: 2, Data: []byte("foo")},
  158. }
  159. st := raftpb.State{Term: 1, Commit: 1}
  160. want := Ready{
  161. State: emptyState,
  162. // commit upto index commit index in st
  163. CommittedEntries: entries[1 : st.Commit+1],
  164. }
  165. n := Restart(1, []int64{1}, 0, 0, st, entries)
  166. if g := <-n.Ready(); !reflect.DeepEqual(g, want) {
  167. t.Errorf("g = %+v,\n w %+v", g, want)
  168. }
  169. select {
  170. case rd := <-n.Ready():
  171. t.Errorf("unexpected Ready: %+v", rd)
  172. default:
  173. }
  174. }
  175. func TestIsStateEqual(t *testing.T) {
  176. tests := []struct {
  177. st raftpb.State
  178. we bool
  179. }{
  180. {emptyState, true},
  181. {raftpb.State{Vote: 1}, false},
  182. {raftpb.State{Commit: 1}, false},
  183. {raftpb.State{Term: 1}, false},
  184. }
  185. for i, tt := range tests {
  186. if isStateEqual(tt.st, emptyState) != tt.we {
  187. t.Errorf("#%d, equal = %v, want %v", i, isStateEqual(tt.st, emptyState), tt.we)
  188. }
  189. }
  190. }
  191. // WARNING: This is a hack.
  192. // Remove this when we are able to block/check the status of the go-routines.
  193. func forceGosched() {
  194. // possibility enough to sched upto 10 go routines.
  195. for i := 0; i < 10000; i++ {
  196. runtime.Gosched()
  197. }
  198. }