🚀 feat: implement random port selection for BDD tests to prevent conflicts
Some checks failed
CI/CD Pipeline / Build Docker Cache (push) Successful in 9s
CI/CD Pipeline / CI Pipeline (push) Failing after 4m47s

This commit is contained in:
2026-04-10 14:29:04 +02:00
parent 9467fd942c
commit 7b0135c537
3 changed files with 57 additions and 3 deletions

View File

@@ -1,6 +1,10 @@
package bdd
import (
"fmt"
"strings"
"time"
"dance-lessons-coach/pkg/bdd/steps"
"dance-lessons-coach/pkg/bdd/testserver"
@@ -12,9 +16,16 @@ var sharedServer *testserver.Server
func InitializeTestSuite(ctx *godog.TestSuiteContext) {
ctx.BeforeSuite(func() {
// Small delay to ensure any previous server instances are fully cleaned up
time.Sleep(50 * time.Millisecond)
sharedServer = testserver.NewServer()
if err := sharedServer.Start(); err != nil {
panic(err)
// Improved error message for port conflicts
if strings.Contains(err.Error(), "address already in use") {
panic(fmt.Sprintf("Port conflict: %v. Try running 'lsof -i :9191' and 'kill -9 <PID>' to free the port", err))
}
panic(fmt.Sprintf("Failed to start test server: %v", err))
}
})
@@ -28,7 +39,12 @@ func InitializeTestSuite(ctx *godog.TestSuiteContext) {
if err := sharedServer.CloseDatabase(); err != nil {
log.Warn().Err(err).Msg("Failed to close database connection")
}
sharedServer.Stop()
// Shutdown HTTP server gracefully
if err := sharedServer.Stop(); err != nil {
log.Warn().Err(err).Msg("Failed to shutdown HTTP server")
}
// Small delay to ensure port is fully released
time.Sleep(100 * time.Millisecond)
}
// Cleanup any test config files
steps.CleanupAllTestConfigFiles()