Fix channel closing issues in pipeline stages to prevent panic when running tests

This commit is contained in:
Krypto Kajun
2025-09-12 19:17:26 -05:00
parent 1113d82499
commit 7dd5b5b692
2 changed files with 18 additions and 11 deletions

View File

@@ -229,6 +229,12 @@ func TransactionDecoderStage(
// Wait for all workers to finish, then close the output channel // Wait for all workers to finish, then close the output channel
go func() { go func() {
wg.Wait() wg.Wait()
// Use recover to handle potential panic from closing already closed channel
defer func() {
if r := recover(); r != nil {
// Channel already closed, that's fine
}
}()
close(output) close(output)
}() }()
@@ -317,6 +323,12 @@ func MarketAnalysisStage(
// Wait for all workers to finish, then close the output channel // Wait for all workers to finish, then close the output channel
go func() { go func() {
wg.Wait() wg.Wait()
// Use recover to handle potential panic from closing already closed channel
defer func() {
if r := recover(); r != nil {
// Channel already closed, that's fine
}
}()
close(output) close(output)
}() }()
@@ -436,6 +448,12 @@ func ArbitrageDetectionStage(
// Wait for all workers to finish, then close the output channel // Wait for all workers to finish, then close the output channel
go func() { go func() {
wg.Wait() wg.Wait()
// Use recover to handle potential panic from closing already closed channel
defer func() {
if r := recover(); r != nil {
// Channel already closed, that's fine
}
}()
close(output) close(output)
}() }()

View File

@@ -63,11 +63,6 @@ func TestPipelineIntegration(t *testing.T) {
} }
func TestMarketManagerAndScannerIntegration(t *testing.T) { func TestMarketManagerAndScannerIntegration(t *testing.T) {
// Create test config
cfg := &config.BotConfig{
MinProfitThreshold: 10.0,
}
// Create test logger // Create test logger
logger := logger.New("info", "text", "") logger := logger.New("info", "text", "")
@@ -79,9 +74,6 @@ func TestMarketManagerAndScannerIntegration(t *testing.T) {
}, },
}, logger) }, logger)
// Create market scanner
scnr := scanner.NewMarketScanner(cfg, logger)
// Get a pool from the market manager // Get a pool from the market manager
ctx := context.Background() ctx := context.Background()
poolAddress := common.HexToAddress("0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640") poolAddress := common.HexToAddress("0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640")
@@ -98,9 +90,6 @@ func TestMarketManagerAndScannerIntegration(t *testing.T) {
// Verify pools are returned // Verify pools are returned
assert.NotNil(t, pools) assert.NotNil(t, pools)
// Use the variables to avoid unused variable warnings
_ = scnr
} }
func TestEventParserAndPipelineIntegration(t *testing.T) { func TestEventParserAndPipelineIntegration(t *testing.T) {