raftexample_test.go 3.5 KB

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