node_test.go 3.2 KB

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