Skip to content

Commit cb5ca81

Browse files
committed
fix error logging
1 parent 976e94f commit cb5ca81

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

petri/cosmos/tests/e2e/utils.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,31 +68,50 @@ func CreateChainsConcurrently(
6868
chainOptions types.ChainOptions,
6969
) {
7070
var wg sync.WaitGroup
71-
chainErrors := make(chan error, endIndex-startIndex)
71+
var mu sync.Mutex
72+
var errors []error
7273

7374
for i := startIndex; i < endIndex; i++ {
7475
wg.Add(1)
7576
go func(index int) {
7677
defer wg.Done()
78+
79+
// Create a copy of the config to avoid race conditions
7780
config := chainConfig
7881
config.ChainId = fmt.Sprintf(chainIDFmtStr, index+1)
79-
config.Name = fmt.Sprintf("chain-%d", index)
82+
config.Name = fmt.Sprintf("chain-%d", index+1)
8083

8184
c, err := cosmoschain.CreateChain(ctx, logger, p, config, chainOptions)
8285
if err != nil {
83-
t.Logf("Chain creation error: %v", err)
84-
chainErrors <- fmt.Errorf("failed to create chain %d: %w", index, err)
86+
t.Logf("Chain creation error for chain %d: %v", index+1, err)
87+
mu.Lock()
88+
errors = append(errors, fmt.Errorf("failed to create chain %d: %w", index+1, err))
89+
mu.Unlock()
8590
return
8691
}
92+
8793
if err := c.Init(ctx, chainOptions); err != nil {
88-
t.Logf("Chain creation error: %v", err)
89-
chainErrors <- fmt.Errorf("failed to init chain %d: %w", index, err)
94+
t.Logf("Chain initialization error for chain %d: %v", index+1, err)
95+
mu.Lock()
96+
errors = append(errors, fmt.Errorf("failed to init chain %d: %w", index+1, err))
97+
mu.Unlock()
9098
return
9199
}
100+
101+
// Safely write to the chains slice
102+
mu.Lock()
92103
chains[index] = c
104+
mu.Unlock()
93105
}(i)
94106
}
107+
95108
wg.Wait()
96-
t.Log(chainErrors)
97-
require.Empty(t, chainErrors)
109+
110+
// Check for any errors that occurred during chain creation
111+
if len(errors) > 0 {
112+
for _, err := range errors {
113+
t.Errorf("Chain creation failed: %v", err)
114+
}
115+
require.Empty(t, errors, "Chain creation failed with %d errors", len(errors))
116+
}
98117
}

0 commit comments

Comments
 (0)