Browse Source

discovery: add tests for sortableNodes

Xiang Li 11 years ago
parent
commit
00d1daaf1e
1 changed files with 18 additions and 0 deletions
  1. 18 0
      discovery/discovery_test.go

+ 18 - 0
discovery/discovery_test.go

@@ -2,6 +2,8 @@ package discovery
 
 import (
 	"errors"
+	"math/rand"
+	"sort"
 
 	"reflect"
 	"testing"
@@ -243,6 +245,22 @@ func TestNodesToPeers(t *testing.T) {
 	}
 }
 
+func TestSortableNodes(t *testing.T) {
+	ns := make(client.Nodes, 0)
+	for i := 0; i < 10000; i++ {
+		ns = append(ns, &client.Node{CreatedIndex: uint64(rand.Int31())})
+	}
+	sns := SortableNodes{ns}
+	sort.Sort(sns)
+	cis := make([]int, 0)
+	for _, n := range sns.Nodes {
+		cis = append(cis, int(n.CreatedIndex))
+	}
+	if sort.IntsAreSorted(cis) != true {
+		t.Errorf("isSorted = %v, want %v", sort.IntsAreSorted(cis), true)
+	}
+}
+
 type clientWithResp struct {
 	rs []*client.Response
 	w  client.Watcher