Browse Source

etcdserver: add StopNotify

Xiang Li 11 years ago
parent
commit
b5d480f17a
2 changed files with 28 additions and 0 deletions
  1. 4 0
      etcdserver/server.go
  2. 24 0
      etcdserver/server_test.go

+ 4 - 0
etcdserver/server.go

@@ -391,6 +391,10 @@ func (s *EtcdServer) Stop() {
 	<-s.done
 	<-s.done
 }
 }
 
 
+// StopNotify returns a channel that receives a empty struct
+// when the server is stopped.
+func (s *EtcdServer) StopNotify() <-chan struct{} { return s.done }
+
 // Do interprets r and performs an operation on s.store according to r.Method
 // Do interprets r and performs an operation on s.store according to r.Method
 // and other fields. If r.Method is "POST", "PUT", "DELETE", or a "GET" with
 // and other fields. If r.Method is "POST", "PUT", "DELETE", or a "GET" with
 // Quorum == true, r will be sent through consensus before performing its
 // Quorum == true, r will be sent through consensus before performing its

+ 24 - 0
etcdserver/server_test.go

@@ -1135,6 +1135,30 @@ func TestPublishRetry(t *testing.T) {
 	}
 	}
 }
 }
 
 
+func TestStopNotify(t *testing.T) {
+	s := &EtcdServer{
+		stop: make(chan struct{}),
+		done: make(chan struct{}),
+	}
+	go func() {
+		<-s.stop
+		close(s.done)
+	}()
+
+	notifier := s.StopNotify()
+	select {
+	case <-notifier:
+		t.Fatalf("received unexpected stop notification")
+	default:
+	}
+	s.Stop()
+	select {
+	case <-notifier:
+	default:
+		t.Fatalf("cannot receive stop notification")
+	}
+}
+
 func TestGetOtherPeerURLs(t *testing.T) {
 func TestGetOtherPeerURLs(t *testing.T) {
 	tests := []struct {
 	tests := []struct {
 		membs []*Member
 		membs []*Member