node_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package rafttest
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  6. "github.com/coreos/etcd/raft"
  7. )
  8. func TestBasicProgress(t *testing.T) {
  9. peers := []raft.Peer{{1, nil}, {2, nil}, {3, nil}, {4, nil}, {5, nil}}
  10. nt := newRaftNetwork(1, 2, 3, 4, 5)
  11. nodes := make([]*node, 0)
  12. for i := 1; i <= 5; i++ {
  13. n := startNode(uint64(i), peers, nt.nodeNetwork(uint64(i)))
  14. nodes = append(nodes, n)
  15. }
  16. time.Sleep(10 * time.Millisecond)
  17. for i := 0; i < 10000; i++ {
  18. nodes[0].Propose(context.TODO(), []byte("somedata"))
  19. }
  20. time.Sleep(500 * time.Millisecond)
  21. for _, n := range nodes {
  22. n.stop()
  23. if n.state.Commit != 10006 {
  24. t.Errorf("commit = %d, want = 10006", n.state.Commit)
  25. }
  26. }
  27. }
  28. func TestRestart(t *testing.T) {
  29. peers := []raft.Peer{{1, nil}, {2, nil}, {3, nil}, {4, nil}, {5, nil}}
  30. nt := newRaftNetwork(1, 2, 3, 4, 5)
  31. nodes := make([]*node, 0)
  32. for i := 1; i <= 5; i++ {
  33. n := startNode(uint64(i), peers, nt.nodeNetwork(uint64(i)))
  34. nodes = append(nodes, n)
  35. }
  36. time.Sleep(50 * time.Millisecond)
  37. for i := 0; i < 300; i++ {
  38. nodes[0].Propose(context.TODO(), []byte("somedata"))
  39. }
  40. nodes[1].stop()
  41. for i := 0; i < 300; i++ {
  42. nodes[0].Propose(context.TODO(), []byte("somedata"))
  43. }
  44. nodes[2].stop()
  45. for i := 0; i < 300; i++ {
  46. nodes[0].Propose(context.TODO(), []byte("somedata"))
  47. }
  48. nodes[2].restart()
  49. for i := 0; i < 300; i++ {
  50. nodes[0].Propose(context.TODO(), []byte("somedata"))
  51. }
  52. nodes[1].restart()
  53. // give some time for nodes to catch up with the raft leader
  54. time.Sleep(500 * time.Millisecond)
  55. for _, n := range nodes {
  56. n.stop()
  57. if n.state.Commit != 1206 {
  58. t.Errorf("commit = %d, want = 1206", n.state.Commit)
  59. }
  60. }
  61. }
  62. func TestPause(t *testing.T) {
  63. peers := []raft.Peer{{1, nil}, {2, nil}, {3, nil}, {4, nil}, {5, nil}}
  64. nt := newRaftNetwork(1, 2, 3, 4, 5)
  65. nodes := make([]*node, 0)
  66. for i := 1; i <= 5; i++ {
  67. n := startNode(uint64(i), peers, nt.nodeNetwork(uint64(i)))
  68. nodes = append(nodes, n)
  69. }
  70. time.Sleep(50 * time.Millisecond)
  71. for i := 0; i < 300; i++ {
  72. nodes[0].Propose(context.TODO(), []byte("somedata"))
  73. }
  74. nodes[1].pause()
  75. for i := 0; i < 300; i++ {
  76. nodes[0].Propose(context.TODO(), []byte("somedata"))
  77. }
  78. nodes[2].pause()
  79. for i := 0; i < 300; i++ {
  80. nodes[0].Propose(context.TODO(), []byte("somedata"))
  81. }
  82. nodes[2].resume()
  83. for i := 0; i < 300; i++ {
  84. nodes[0].Propose(context.TODO(), []byte("somedata"))
  85. }
  86. nodes[1].resume()
  87. // give some time for nodes to catch up with the raft leader
  88. time.Sleep(300 * time.Millisecond)
  89. for _, n := range nodes {
  90. n.stop()
  91. if n.state.Commit != 1206 {
  92. t.Errorf("commit = %d, want = 1206", n.state.Commit)
  93. }
  94. }
  95. }