raftexample_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2015 The etcd Authors
  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 main
  15. import (
  16. "fmt"
  17. "os"
  18. "testing"
  19. "github.com/coreos/etcd/raft/raftpb"
  20. )
  21. type cluster struct {
  22. peers []string
  23. commitC []<-chan *string
  24. errorC []<-chan error
  25. proposeC []chan string
  26. confChangeC []chan raftpb.ConfChange
  27. }
  28. // newCluster creates a cluster of n nodes
  29. func newCluster(n int) *cluster {
  30. peers := make([]string, n)
  31. for i := range peers {
  32. peers[i] = fmt.Sprintf("http://127.0.0.1:%d", 10000+i)
  33. }
  34. clus := &cluster{
  35. peers: peers,
  36. commitC: make([]<-chan *string, len(peers)),
  37. errorC: make([]<-chan error, len(peers)),
  38. proposeC: make([]chan string, len(peers)),
  39. confChangeC: make([]chan raftpb.ConfChange, len(peers)),
  40. }
  41. for i := range clus.peers {
  42. os.RemoveAll(fmt.Sprintf("raftexample-%d", i+1))
  43. clus.proposeC[i] = make(chan string, 1)
  44. clus.confChangeC[i] = make(chan raftpb.ConfChange, 1)
  45. clus.commitC[i], clus.errorC[i] = newRaftNode(i+1, clus.peers, false, clus.proposeC[i], clus.confChangeC[i])
  46. }
  47. return clus
  48. }
  49. // sinkReplay reads all commits in each node's local log.
  50. func (clus *cluster) sinkReplay() {
  51. for i := range clus.peers {
  52. for s := range clus.commitC[i] {
  53. if s == nil {
  54. break
  55. }
  56. }
  57. }
  58. }
  59. // Close closes all cluster nodes and returns an error if any failed.
  60. func (clus *cluster) Close() (err error) {
  61. for i := range clus.peers {
  62. close(clus.proposeC[i])
  63. for range clus.commitC[i] {
  64. // drain pending commits
  65. }
  66. // wait for channel to close
  67. if erri, _ := <-clus.errorC[i]; erri != nil {
  68. err = erri
  69. }
  70. // clean intermediates
  71. os.RemoveAll(fmt.Sprintf("raftexample-%d", i+1))
  72. }
  73. return err
  74. }
  75. func (clus *cluster) closeNoErrors(t *testing.T) {
  76. if err := clus.Close(); err != nil {
  77. t.Fatal(err)
  78. }
  79. }
  80. // TestProposeOnCommit starts three nodes and feeds commits back into the proposal
  81. // channel. The intent is to ensure blocking on a proposal won't block raft progress.
  82. func TestProposeOnCommit(t *testing.T) {
  83. clus := newCluster(3)
  84. defer clus.closeNoErrors(t)
  85. clus.sinkReplay()
  86. donec := make(chan struct{})
  87. for i := range clus.peers {
  88. // feedback for "n" committed entries, then update donec
  89. go func(pC chan<- string, cC <-chan *string, eC <-chan error) {
  90. for n := 0; n < 100; n++ {
  91. s, ok := <-cC
  92. if !ok {
  93. pC = nil
  94. }
  95. select {
  96. case pC <- *s:
  97. continue
  98. case err, _ := <-eC:
  99. t.Fatalf("eC message (%v)", err)
  100. }
  101. }
  102. donec <- struct{}{}
  103. for range cC {
  104. // acknowledge the commits from other nodes so
  105. // raft continues to make progress
  106. }
  107. }(clus.proposeC[i], clus.commitC[i], clus.errorC[i])
  108. // one message feedback per node
  109. go func(i int) { clus.proposeC[i] <- "foo" }(i)
  110. }
  111. for range clus.peers {
  112. <-donec
  113. }
  114. }
  115. // TestCloseProposerBeforeReplay tests closing the producer before raft starts.
  116. func TestCloseProposerBeforeReplay(t *testing.T) {
  117. clus := newCluster(1)
  118. // close before replay so raft never starts
  119. defer clus.closeNoErrors(t)
  120. }
  121. // TestCloseProposerInflight tests closing the producer while
  122. // committed messages are being published to the client.
  123. func TestCloseProposerInflight(t *testing.T) {
  124. clus := newCluster(1)
  125. defer clus.closeNoErrors(t)
  126. clus.sinkReplay()
  127. // some inflight ops
  128. go func() {
  129. clus.proposeC[0] <- "foo"
  130. clus.proposeC[0] <- "bar"
  131. }()
  132. // wait for one message
  133. if c, ok := <-clus.commitC[0]; *c != "foo" || !ok {
  134. t.Fatalf("Commit failed")
  135. }
  136. }