Browse Source

contrib/raftexample: fix tests

os.Exit() on raft stop breaks out of the test fixture; instead,
monitor the error channel and exit on close
Anthony Romano 10 years ago
parent
commit
02b24c58fd
2 changed files with 9 additions and 2 deletions
  1. 9 0
      contrib/raftexample/httpapi.go
  2. 0 2
      contrib/raftexample/raft.go

+ 9 - 0
contrib/raftexample/httpapi.go

@@ -18,6 +18,7 @@ import (
 	"io/ioutil"
 	"log"
 	"net/http"
+	"os"
 	"strconv"
 
 	"github.com/coreos/etcd/raft/raftpb"
@@ -104,6 +105,14 @@ func (h *httpKVAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 func serveHttpKVAPI(port int, proposeC chan<- string, confChangeC chan<- raftpb.ConfChange,
 	commitC <-chan *string, errorC <-chan error) {
 
+	// exit when raft goes down
+	go func() {
+		if err, ok := <-errorC; ok {
+			log.Fatal(err)
+		}
+		os.Exit(0)
+	}()
+
 	srv := http.Server{
 		Addr: ":" + strconv.Itoa(port),
 		Handler: &httpKVAPI{

+ 0 - 2
contrib/raftexample/raft.go

@@ -236,8 +236,6 @@ func (rc *raftNode) stop() {
 	close(rc.commitC)
 	close(rc.errorC)
 	rc.node.Stop()
-
-	os.Exit(0)
 }
 
 func (rc *raftNode) stopHTTP() {