🚀 feat: make random ports default for BDD tests to prevent conflicts
Some checks failed
CI/CD Pipeline / Build Docker Cache (push) Successful in 8s
CI/CD Pipeline / CI Pipeline (push) Failing after 4m1s

This commit is contained in:
2026-04-10 14:36:43 +02:00
parent 7b0135c537
commit 778d8822dc
2 changed files with 69 additions and 2 deletions

View File

@@ -84,6 +84,72 @@ GODOG_TAGS="@jwt && ~@todo" go test ./features/...
DLC_DATABASE_HOST=localhost GODOG_TAGS="@wip" go test ./features/jwt/... DLC_DATABASE_HOST=localhost GODOG_TAGS="@wip" go test ./features/jwt/...
``` ```
### Random Port Selection (Default Behavior)
By default, BDD tests use **random ports** (10000-19999) to prevent port conflicts during parallel execution. This ensures tests can run reliably in CI/CD pipelines and when executed multiple times.
**Benefits:**
- ✅ No port conflicts in parallel test execution
- ✅ Safe for repeated test runs
- ✅ Better for CI/CD environments
**Disable random ports (not recommended):**
```bash
FIXED_TEST_PORT=true go test ./features/...
```
**Force specific port (debugging only):**
```bash
# Create a test config file with fixed port
echo "server:
port: 9191" > test-config.yaml
FEATURE=debug FIXED_TEST_PORT=true go test ./features/...
```
### Test Validation Process
To ensure test suite stability, follow this validation process:
**Validation Command:**
```bash
# Clean cache and run all tests 20 times
echo "🧪 Validating test suite stability..."
for i in {1..20}; do
echo "Run $i/20..."
go clean -testcache
if ! go test ./... > /dev/null 2>&1; then
echo "❌ Test run $i failed"
go test ./... -v
exit 1
fi
done
echo "✅ All 20 test runs passed successfully!"
```
**Failure Handling:**
- If any test fails during validation, mark it as `@wip` and investigate
- Use `@flaky` tag for intermittently failing tests
- Document the issue in the test scenario comments
**Success Criteria:**
- ✅ 100% pass rate across 20 consecutive runs
- ✅ No undefined/pending steps
- ✅ No race conditions or port conflicts
- ✅ Consistent execution time
**CI/CD Integration:**
```yaml
- name: Validate Test Suite
run: |
echo "🧪 Running 20 validation runs..."
for i in {1..20}; do
echo "Run $i/20"
go clean -testcache
go test ./... || exit 1
done
echo "✅ Test suite validated successfully"
```
### Stop On Failure Control ### Stop On Failure Control
You can control whether tests stop on first failure using the `GODOG_STOP_ON_FAILURE` environment variable. You can control whether tests stop on first failure using the `GODOG_STOP_ON_FAILURE` environment variable.

View File

@@ -36,8 +36,9 @@ func NewServer() *Server {
feature := os.Getenv("FEATURE") feature := os.Getenv("FEATURE")
port := 9191 // Default port port := 9191 // Default port
// Check for random port mode (better for parallel testing) // Use random port by default for better parallel testing
if os.Getenv("RANDOM_TEST_PORT") == "true" { // Can be disabled with FIXED_TEST_PORT=true if needed
if os.Getenv("FIXED_TEST_PORT") != "true" {
// Generate a random port in the test range (10000-19999) // Generate a random port in the test range (10000-19999)
port = 10000 + rand.Intn(9999) port = 10000 + rand.Intn(9999)
log.Debug().Int("port", port).Msg("Using random test port") log.Debug().Int("port", port).Msg("Using random test port")