From 975290d138b3044c891e1144b4be6976d6d81c2a Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Tue, 15 Mar 2022 09:49:56 +0000 Subject: [PATCH 1/3] chore(tests): do not `os.Exit` and return error - `RunGossamer`: do not os.Exit and return an error - `InitializeAndStartNodes` return error and teardown nodes on error - `InitializeAndStartNodesWebsocket` return error and teardown nodes on error - Fix thread safety on nodes slice in `InitializeAndStartNodesWebsocket` --- tests/utils/gossamer_utils.go | 59 ++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 19 deletions(-) diff --git a/tests/utils/gossamer_utils.go b/tests/utils/gossamer_utils.go index 73f1d408be..df74841441 100644 --- a/tests/utils/gossamer_utils.go +++ b/tests/utils/gossamer_utils.go @@ -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 { @@ -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 @@ -314,11 +312,10 @@ 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++ { @@ -327,28 +324,39 @@ func InitializeAndStartNodes(t *testing.T, num int, genesis, config string) ([]* 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() + nodesMutex.Unlock() wg.Done() }(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 var wg sync.WaitGroup + wg.Add(num) for i := 0; i < num; i++ { @@ -357,18 +365,31 @@ func InitializeAndStartNodesWebsocket(t *testing.T, num int, genesis, config str 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) + nodesMutex.Unlock() + wg.Done() }(i) } wg.Wait() + if err != nil { + _ = StopNodes(t, nodes) + return nil, err + } + return nodes, nil } From 3beef6fcbc8f2db426723f698730c71e1b82960c Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Thu, 31 Mar 2022 12:04:56 +0000 Subject: [PATCH 2/3] (OOS): fix error message --- tests/utils/request_utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/utils/request_utils.go b/tests/utils/request_utils.go index eff8b8c52a..4e07bbab80 100644 --- a/tests/utils/request_utils.go +++ b/tests/utils/request_utils.go @@ -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() From 642f5ba3f8b4f10e9c787acb560037b00f2de6a7 Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Thu, 31 Mar 2022 14:24:24 +0000 Subject: [PATCH 3/3] Apply Eclesio's fix suggestions --- tests/utils/gossamer_utils.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/utils/gossamer_utils.go b/tests/utils/gossamer_utils.go index df74841441..35ef8d1f8c 100644 --- a/tests/utils/gossamer_utils.go +++ b/tests/utils/gossamer_utils.go @@ -320,6 +320,7 @@ func InitializeAndStartNodes(t *testing.T, num int, genesis, config string) ( for i := 0; i < num; i++ { go func(i int) { + defer wg.Done() name := strconv.Itoa(i) if i < len(KeyList) { name = KeyList[i] @@ -337,7 +338,6 @@ func InitializeAndStartNodes(t *testing.T, num int, genesis, config string) ( nodesMutex.Lock() nodes = append(nodes, node) nodesMutex.Unlock() - wg.Done() }(i) } @@ -361,6 +361,7 @@ func InitializeAndStartNodesWebsocket(t *testing.T, num int, genesis, config str for i := 0; i < num; i++ { go func(i int) { + defer wg.Done() name := strconv.Itoa(i) if i < len(KeyList) { name = KeyList[i] @@ -378,8 +379,6 @@ func InitializeAndStartNodesWebsocket(t *testing.T, num int, genesis, config str nodesMutex.Lock() nodes = append(nodes, node) nodesMutex.Unlock() - - wg.Done() }(i) }