Browse Source

discovery: address comments

Xiang Li 11 years ago
parent
commit
fdfaf07c46
2 changed files with 22 additions and 11 deletions
  1. 7 9
      discovery/discovery.go
  2. 15 2
      discovery/discovery_test.go

+ 7 - 9
discovery/discovery.go

@@ -61,10 +61,8 @@ func (d *discovery) createSelf() error {
 
 	// ensure self appears on the server we connected to
 	w := d.c.Watch(d.selfKey(), resp.Node.CreatedIndex)
-	if _, err = w.Next(); err != nil {
-		return err
-	}
-	return nil
+	_, err = w.Next()
+	return err
 }
 
 func (d *discovery) checkCluster() (client.Nodes, int, error) {
@@ -94,7 +92,7 @@ func (d *discovery) checkCluster() (client.Nodes, int, error) {
 		}
 	}
 
-	snodes := SortableNodes{nodes}
+	snodes := sortableNodes{nodes}
 	sort.Sort(snodes)
 
 	// find self position
@@ -144,10 +142,10 @@ func nodesToPeers(ns client.Nodes) (*etcdhttp.Peers, error) {
 	return &peers, nil
 }
 
-type SortableNodes struct{ client.Nodes }
+type sortableNodes struct{ client.Nodes }
 
-func (ns SortableNodes) Len() int { return len(ns.Nodes) }
-func (ns SortableNodes) Less(i, j int) bool {
+func (ns sortableNodes) Len() int { return len(ns.Nodes) }
+func (ns sortableNodes) Less(i, j int) bool {
 	return ns.Nodes[i].CreatedIndex < ns.Nodes[j].CreatedIndex
 }
-func (ns SortableNodes) Swap(i, j int) { ns.Nodes[i], ns.Nodes[j] = ns.Nodes[j], ns.Nodes[i] }
+func (ns sortableNodes) Swap(i, j int) { ns.Nodes[i], ns.Nodes[j] = ns.Nodes[j], ns.Nodes[i] }

+ 15 - 2
discovery/discovery_test.go

@@ -245,11 +245,17 @@ func TestNodesToPeers(t *testing.T) {
 }
 
 func TestSortableNodes(t *testing.T) {
-	ns := make(client.Nodes, 0)
+	ns := client.Nodes{
+		{CreatedIndex: 5},
+		{CreatedIndex: 1},
+		{CreatedIndex: 3},
+		{CreatedIndex: 4},
+	}
+	// add some randomness
 	for i := 0; i < 10000; i++ {
 		ns = append(ns, &client.Node{CreatedIndex: uint64(rand.Int31())})
 	}
-	sns := SortableNodes{ns}
+	sns := sortableNodes{ns}
 	sort.Sort(sns)
 	cis := make([]int, 0)
 	for _, n := range sns.Nodes {
@@ -258,6 +264,13 @@ func TestSortableNodes(t *testing.T) {
 	if sort.IntsAreSorted(cis) != true {
 		t.Errorf("isSorted = %v, want %v", sort.IntsAreSorted(cis), true)
 	}
+	cis = make([]int, 0)
+	for _, n := range ns {
+		cis = append(cis, int(n.CreatedIndex))
+	}
+	if sort.IntsAreSorted(cis) != true {
+		t.Errorf("isSorted = %v, want %v", sort.IntsAreSorted(cis), true)
+	}
 }
 
 type clientWithResp struct {