Skip to content

Commit

Permalink
initial commit for GetClusters function
Browse files Browse the repository at this point in the history
This function is async.
  • Loading branch information
jsanda committed Mar 19, 2020
1 parent 4c77184 commit e847f26
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
29 changes: 29 additions & 0 deletions reaper/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"net/url"
"sync"
)

type Client struct {
Expand Down Expand Up @@ -65,6 +66,34 @@ func (c *Client) GetCluster(ctx context.Context, name string) (*Cluster, error)
return cluster, nil
}

func (c *Client) GetClusters(ctx context.Context) <-chan GetClusterResult {
results := make(chan GetClusterResult)

clusterNames, err := c.GetClusterNames(ctx)
if err != nil {
close(results)
return results
}

var wg sync.WaitGroup

go func() {
defer close(results)
for _, clusterName := range clusterNames {
wg.Add(1)
go func(name string) {
defer wg.Done()
cluster, err := c.GetCluster(ctx, name)
result := GetClusterResult{Cluster: cluster, Error: err}
results <- result
}(clusterName)
}
wg.Wait()
}()

return results
}

func (c *Client) AddCluster(ctx context.Context, cluster string, seed string) error {
rel := &url.URL{Path: fmt.Sprintf("/cluster/%s", cluster)}
u := c.BaseURL.ResolveReference(rel)
Expand Down
39 changes: 37 additions & 2 deletions reaper/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,16 @@ func TestClient(t *testing.T) {
}
// TODO add ready check for reaper

testenv.AddCluster("cluster-1", "cluster-1-node-0")
testenv.AddCluster("cluster-2", "cluster-2-node-0")
if err = testenv.AddCluster("cluster-1", "cluster-1-node-0"); err != nil {
t.Fatalf("failed to add cluster-1: %s", err)
}
if err = testenv.AddCluster("cluster-2", "cluster-2-node-0"); err != nil {
t.Fatalf("failed to add cluster-2: %s", err)
}

t.Run("GetClusterNames", run(client, testGetClusterNames))
t.Run("GetCluster", run(client, testGetCluster))
t.Run("GetClusters", run(client, testGetClusters))
t.Run("AddDeleteCluster", run(client, testAddDeleteCluster))
}

Expand Down Expand Up @@ -104,6 +109,36 @@ func testGetCluster(t *testing.T, client *Client) {
}
}

func testGetClusters(t *testing.T, client *Client) {
results := make([]GetClusterResult, 0)

for result := range client.GetClusters(context.TODO()) {
results = append(results, result)
}

// Verify that we got the expected number of results
assert.Equal(t, 2, len(results))

// Verify that there were no errors
for _, result := range results {
assert.Nil(t, result.Error)
}

assertGetClusterResultsContains(t, results, "cluster-1")
assertGetClusterResultsContains(t, results, "cluster-2")
}

func assertGetClusterResultsContains(t *testing.T, results []GetClusterResult, clusterName string) {
var cluster *Cluster
for _, result := range results {
if result.Cluster.Name == clusterName {
cluster = result.Cluster
break
}
}
assert.NotNil(t, cluster, "failed to find %s", clusterName)
}

func testAddDeleteCluster(t *testing.T, client *Client) {
cluster := "cluster-3"
seed := "cluster-3-node-0"
Expand Down
5 changes: 5 additions & 0 deletions reaper/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ type EndpointState struct {
Load float64
}

type GetClusterResult struct {
Cluster *Cluster
Error error
}

// All the following types are used internally by the client and not part of the public API

type clusterStatus struct {
Expand Down

0 comments on commit e847f26

Please sign in to comment.