Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(tests): do not osExit and return error #2452

Merged
merged 3 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 41 additions & 21 deletions tests/utils/gossamer_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,7 @@ func startGossamer(t *testing.T, node *Node, websocket bool) error {
func RunGossamer(t *testing.T, idx int, basepath, genesis, config string, websocket, babeLead bool) (*Node, error) {
node, err := InitGossamer(idx, basepath, genesis, config)
if err != nil {
Logger.Criticalf("could not initialise gossamer: %s", err)
os.Exit(1)
return nil, fmt.Errorf("could not initialise gossamer: %w", err)
}

if idx == 0 || babeLead {
Expand All @@ -243,8 +242,7 @@ func RunGossamer(t *testing.T, idx int, basepath, genesis, config string, websoc

err = startGossamer(t, node, websocket)
if err != nil {
Logger.Criticalf("could not start gossamer: %s", err)
os.Exit(1)
return nil, fmt.Errorf("could not start gossamer: %w", err)
}

return node, nil
Expand Down Expand Up @@ -314,61 +312,83 @@ func StartNodes(t *testing.T, nodes []*Node) error {
}

// InitializeAndStartNodes will spin up `num` gossamer nodes
func InitializeAndStartNodes(t *testing.T, num int, genesis, config string) ([]*Node, error) {
var nodes []*Node

func InitializeAndStartNodes(t *testing.T, num int, genesis, config string) (
nodes []*Node, err error) {
var wg sync.WaitGroup
var nodeMu sync.Mutex
var nodesMutex, errMutex sync.Mutex
wg.Add(num)

for i := 0; i < num; i++ {
go func(i int) {
defer wg.Done()
name := strconv.Itoa(i)
if i < len(KeyList) {
name = KeyList[i]
}
node, err := RunGossamer(t, i, TestDir(t, name), genesis, config, false, false)
if err != nil {
Logger.Errorf("failed to run Gossamer for node index %d", i)
node, runErr := RunGossamer(t, i, TestDir(t, name), genesis, config, false, false)
if runErr != nil {
errMutex.Lock()
if err == nil {
err = fmt.Errorf("failed to run Gossamer for node index %d: %w", i, runErr)
}
errMutex.Unlock()
return
}

nodeMu.Lock()
nodesMutex.Lock()
nodes = append(nodes, node)
nodeMu.Unlock()
wg.Done()
nodesMutex.Unlock()
}(i)
}

wg.Wait()

if err != nil {
_ = StopNodes(t, nodes)
return nil, err
}

return nodes, nil
}

// InitializeAndStartNodesWebsocket will spin up `num` gossamer nodes running with Websocket rpc enabled
func InitializeAndStartNodesWebsocket(t *testing.T, num int, genesis, config string) ([]*Node, error) {
var nodes []*Node

func InitializeAndStartNodesWebsocket(t *testing.T, num int, genesis, config string) (
nodes []*Node, err error) {
var nodesMutex, errMutex sync.Mutex
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use an errChan or a defined []error of len(num) to get the errors?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, although there is little point to do this considering the future-to-be changes: this parallel logic is changed to be sequential since it takes a short time to init+start, so only the first error encountered is returned.

var wg sync.WaitGroup

wg.Add(num)

for i := 0; i < num; i++ {
go func(i int) {
defer wg.Done()
name := strconv.Itoa(i)
if i < len(KeyList) {
name = KeyList[i]
}
node, err := RunGossamer(t, i, TestDir(t, name), genesis, config, true, false)
if err != nil {
Logger.Errorf("failed to run Gossamer for node index %d", i)
node, runErr := RunGossamer(t, i, TestDir(t, name), genesis, config, true, false)
if runErr != nil {
errMutex.Lock()
if err == nil {
err = fmt.Errorf("failed to run Gossamer for node index %d: %w", i, runErr)
}
errMutex.Unlock()
return
}

nodesMutex.Lock()
nodes = append(nodes, node)
wg.Done()
nodesMutex.Unlock()
}(i)
}

wg.Wait()

if err != nil {
_ = StopNodes(t, nodes)
return nil, err
}

return nodes, nil
}

Expand Down
2 changes: 1 addition & 1 deletion tests/utils/request_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func PostRPCWithRetry(ctx context.Context, endpoint, method, params string,

postRPCCtx, postRPCCancel := context.WithTimeout(ctx, requestWait)

data, err := PostRPC(postRPCCtx, endpoint, method, params)
data, err = PostRPC(postRPCCtx, endpoint, method, params)

if err == nil {
postRPCCancel()
Expand Down