Skip to content
This repository has been archived by the owner on Jul 1, 2023. It is now read-only.

Commit

Permalink
Refactor *TestContext.Status to use typed values.
Browse files Browse the repository at this point in the history
As @a-palchikov suggested here:

  https://github.com/gravitational/robotest/pull/233/files/67c8aeb172d284a8c0439f489c48ae32c4ff1581#r435196137

Using a strongly typed value channel and errgroup makes this code easier
to understand and considerably more robust.
  • Loading branch information
wadells committed Jun 4, 2020
1 parent 67c8aeb commit 0809da1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ require (
go.opencensus.io v0.0.0-20181129005706-8b019f31bc1c // indirect
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
golang.org/x/oauth2 v0.0.0-20181128211412-28207608b838
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f // indirect
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f
google.golang.org/api v0.0.0-20181129220737-af4fc4062c26 // indirect
google.golang.org/appengine v1.3.0 // indirect
google.golang.org/genproto v0.0.0-20181202183823-bd91e49a0898 // indirect
Expand Down
33 changes: 16 additions & 17 deletions infra/gravity/cluster_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/cenkalti/backoff"
"golang.org/x/sync/errgroup"

"github.com/gravitational/robotest/lib/constants"
sshutils "github.com/gravitational/robotest/lib/ssh"
Expand Down Expand Up @@ -71,34 +72,32 @@ func (c *TestContext) WaitForStatus(nodes []Gravity, expected statusValidator) e

}

// Status queries `gravity status` on each node in nodes.
func (c *TestContext) Status(nodes []Gravity) ([]GravityStatus, error) {
// Status queries `gravity status` once from each node in nodes.
func (c *TestContext) Status(nodes []Gravity) (statuses []GravityStatus, err error) {
ctx, cancel := context.WithTimeout(c.ctx, c.timeouts.NodeStatus)
defer cancel()

errC := make(chan error, len(nodes))
valueC := make(chan interface{}, len(nodes))

valueC := make(chan GravityStatus, len(nodes))
g, ctx := errgroup.WithContext(ctx)
for _, node := range nodes {
go func(n Gravity) {
status, err := n.Status(ctx)
errC <- err
node := node
g.Go(func() error {
status, err := node.Status(ctx)
if err != nil {
return trace.Wrap(err)
}
if status != nil {
valueC <- *status
}
}(node)
return nil
})
}

values, err := utils.Collect(ctx, cancel, errC, valueC)
err = g.Wait()
if err != nil {
return nil, trace.Wrap(err)
}
var statuses []GravityStatus
for _, v := range values {
status, ok := v.(GravityStatus)
if !ok {
return nil, trace.BadParameter("expected %T, got %T", status, v)
}
close(valueC)
for status := range valueC {
statuses = append(statuses, status)
}
return statuses, nil
Expand Down

0 comments on commit 0809da1

Please sign in to comment.