From 02b24c58fd1d325c127af8e696886bebbd2c6412 Mon Sep 17 00:00:00 2001 From: Anthony Romano Date: Wed, 10 Feb 2016 10:21:34 -0800 Subject: [PATCH] contrib/raftexample: fix tests os.Exit() on raft stop breaks out of the test fixture; instead, monitor the error channel and exit on close --- contrib/raftexample/httpapi.go | 9 +++++++++ contrib/raftexample/raft.go | 2 -- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/contrib/raftexample/httpapi.go b/contrib/raftexample/httpapi.go index 9c1af96bb..dee2a4666 100644 --- a/contrib/raftexample/httpapi.go +++ b/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{ diff --git a/contrib/raftexample/raft.go b/contrib/raftexample/raft.go index 1ac56824a..493e31ec6 100644 --- a/contrib/raftexample/raft.go +++ b/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() {