Skip to content

Commit 2deb7dd

Browse files
authored
chore(end-to-end): do not os.Exit and return an error instead (#2452)
- `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`
1 parent 9efde47 commit 2deb7dd

File tree

2 files changed

+42
-22
lines changed

2 files changed

+42
-22
lines changed

tests/utils/gossamer_utils.go

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,7 @@ func startGossamer(t *testing.T, node *Node, websocket bool) error {
233233
func RunGossamer(t *testing.T, idx int, basepath, genesis, config string, websocket, babeLead bool) (*Node, error) {
234234
node, err := InitGossamer(idx, basepath, genesis, config)
235235
if err != nil {
236-
Logger.Criticalf("could not initialise gossamer: %s", err)
237-
os.Exit(1)
236+
return nil, fmt.Errorf("could not initialise gossamer: %w", err)
238237
}
239238

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

244243
err = startGossamer(t, node, websocket)
245244
if err != nil {
246-
Logger.Criticalf("could not start gossamer: %s", err)
247-
os.Exit(1)
245+
return nil, fmt.Errorf("could not start gossamer: %w", err)
248246
}
249247

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

316314
// InitializeAndStartNodes will spin up `num` gossamer nodes
317-
func InitializeAndStartNodes(t *testing.T, num int, genesis, config string) ([]*Node, error) {
318-
var nodes []*Node
319-
315+
func InitializeAndStartNodes(t *testing.T, num int, genesis, config string) (
316+
nodes []*Node, err error) {
320317
var wg sync.WaitGroup
321-
var nodeMu sync.Mutex
318+
var nodesMutex, errMutex sync.Mutex
322319
wg.Add(num)
323320

324321
for i := 0; i < num; i++ {
325322
go func(i int) {
323+
defer wg.Done()
326324
name := strconv.Itoa(i)
327325
if i < len(KeyList) {
328326
name = KeyList[i]
329327
}
330-
node, err := RunGossamer(t, i, TestDir(t, name), genesis, config, false, false)
331-
if err != nil {
332-
Logger.Errorf("failed to run Gossamer for node index %d", i)
328+
node, runErr := RunGossamer(t, i, TestDir(t, name), genesis, config, false, false)
329+
if runErr != nil {
330+
errMutex.Lock()
331+
if err == nil {
332+
err = fmt.Errorf("failed to run Gossamer for node index %d: %w", i, runErr)
333+
}
334+
errMutex.Unlock()
335+
return
333336
}
334337

335-
nodeMu.Lock()
338+
nodesMutex.Lock()
336339
nodes = append(nodes, node)
337-
nodeMu.Unlock()
338-
wg.Done()
340+
nodesMutex.Unlock()
339341
}(i)
340342
}
341343

342344
wg.Wait()
343345

346+
if err != nil {
347+
_ = StopNodes(t, nodes)
348+
return nil, err
349+
}
350+
344351
return nodes, nil
345352
}
346353

347354
// InitializeAndStartNodesWebsocket will spin up `num` gossamer nodes running with Websocket rpc enabled
348-
func InitializeAndStartNodesWebsocket(t *testing.T, num int, genesis, config string) ([]*Node, error) {
349-
var nodes []*Node
350-
355+
func InitializeAndStartNodesWebsocket(t *testing.T, num int, genesis, config string) (
356+
nodes []*Node, err error) {
357+
var nodesMutex, errMutex sync.Mutex
351358
var wg sync.WaitGroup
359+
352360
wg.Add(num)
353361

354362
for i := 0; i < num; i++ {
355363
go func(i int) {
364+
defer wg.Done()
356365
name := strconv.Itoa(i)
357366
if i < len(KeyList) {
358367
name = KeyList[i]
359368
}
360-
node, err := RunGossamer(t, i, TestDir(t, name), genesis, config, true, false)
361-
if err != nil {
362-
Logger.Errorf("failed to run Gossamer for node index %d", i)
369+
node, runErr := RunGossamer(t, i, TestDir(t, name), genesis, config, true, false)
370+
if runErr != nil {
371+
errMutex.Lock()
372+
if err == nil {
373+
err = fmt.Errorf("failed to run Gossamer for node index %d: %w", i, runErr)
374+
}
375+
errMutex.Unlock()
376+
return
363377
}
364378

379+
nodesMutex.Lock()
365380
nodes = append(nodes, node)
366-
wg.Done()
381+
nodesMutex.Unlock()
367382
}(i)
368383
}
369384

370385
wg.Wait()
371386

387+
if err != nil {
388+
_ = StopNodes(t, nodes)
389+
return nil, err
390+
}
391+
372392
return nodes, nil
373393
}
374394

tests/utils/request_utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func PostRPCWithRetry(ctx context.Context, endpoint, method, params string,
6666

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

69-
data, err := PostRPC(postRPCCtx, endpoint, method, params)
69+
data, err = PostRPC(postRPCCtx, endpoint, method, params)
7070

7171
if err == nil {
7272
postRPCCancel()

0 commit comments

Comments
 (0)