raftexample_test.go 4.0 KB

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