Browse Source

etcdserver: add Peers.Addrs

Brian Waldon 11 years ago
parent
commit
99e9f561ee
2 changed files with 22 additions and 0 deletions
  1. 13 0
      etcdserver/etcdhttp/peers.go
  2. 9 0
      etcdserver/etcdhttp/peers_test.go

+ 13 - 0
etcdserver/etcdhttp/peers.go

@@ -85,6 +85,19 @@ func (ps Peers) Endpoints() []string {
 	return endpoints
 }
 
+// Addrs returns a list of all peer addresses. The returned list is sorted
+// in ascending lexicographical order.
+func (ps Peers) Addrs() []string {
+	addrs := make([]string, 0)
+	for _, paddrs := range ps {
+		for _, paddr := range paddrs {
+			addrs = append(addrs, paddr)
+		}
+	}
+	sort.Strings(addrs)
+	return addrs
+}
+
 func Sender(t *http.Transport, p Peers) func(msgs []raftpb.Message) {
 	c := &http.Client{Transport: t}
 

+ 9 - 0
etcdserver/etcdhttp/peers_test.go

@@ -16,24 +16,28 @@ func TestPeers(t *testing.T) {
 		in      string
 		wids    []int64
 		wep     []string
+		waddrs  []string
 		wstring string
 	}{
 		{
 			"1=1.1.1.1",
 			[]int64{1},
 			[]string{"http://1.1.1.1"},
+			[]string{"1.1.1.1"},
 			"1=1.1.1.1",
 		},
 		{
 			"2=2.2.2.2",
 			[]int64{2},
 			[]string{"http://2.2.2.2"},
+			[]string{"2.2.2.2"},
 			"2=2.2.2.2",
 		},
 		{
 			"1=1.1.1.1&1=1.1.1.2&2=2.2.2.2",
 			[]int64{1, 2},
 			[]string{"http://1.1.1.1", "http://1.1.1.2", "http://2.2.2.2"},
+			[]string{"1.1.1.1", "1.1.1.2", "2.2.2.2"},
 			"1=1.1.1.1&1=1.1.1.2&2=2.2.2.2",
 		},
 		{
@@ -41,6 +45,7 @@ func TestPeers(t *testing.T) {
 			[]int64{1, 2, 3, 4},
 			[]string{"http://1.1.1.1", "http://1.1.1.2", "http://2.2.2.2",
 				"http://3.3.3.3", "http://4.4.4.4"},
+			[]string{"1.1.1.1", "1.1.1.2", "2.2.2.2", "3.3.3.3", "4.4.4.4"},
 			"1=1.1.1.1&1=1.1.1.2&2=2.2.2.2&3=3.3.3.3&4=4.4.4.4",
 		},
 	}
@@ -59,6 +64,10 @@ func TestPeers(t *testing.T) {
 		if !reflect.DeepEqual(ep, tt.wep) {
 			t.Errorf("#%d: Endpoints=%#v, want %#v", i, ep, tt.wep)
 		}
+		addrs := p.Addrs()
+		if !reflect.DeepEqual(addrs, tt.waddrs) {
+			t.Errorf("#%d: addrs=%#v, want %#v", i, ep, tt.waddrs)
+		}
 		s := p.String()
 		if s != tt.wstring {
 			t.Errorf("#%d: string=%q, want %q", i, s, tt.wstring)