2 Commits

Author SHA1 Message Date
63d27cc35e 🐛 fix(bdd): revert PR #26 schema isolation, add cache flush + sequential test execution
All checks were successful
CI/CD Pipeline / Build Docker Cache (push) Successful in 12s
CI/CD Pipeline / CI Pipeline (push) Successful in 8m44s
CI/CD Pipeline / Trigger Docker Push (push) Has been skipped
PR #26 added BDD_SCHEMA_ISOLATION=true to CI but this creates per-scenario schemas
WITHOUT running migrations on them, causing 500 errors on user registration. This
PR reverts that and instead relies on:

1. The existing CleanupDatabase hook (truncates all tables AfterScenario)
2. Sequential test package execution (-p 1) to avoid contention between feature
   packages sharing the same Postgres DB

Plus defensive additions for future-proofing:
- pkg/server/server.go: GetCacheService() exposed for test cleanup
- pkg/bdd/testserver/server.go: cacheService field + FlushCache() method
- pkg/bdd/testserver/state_tracer.go: TraceStateCacheOperation
- pkg/bdd/suite.go: AfterScenario hook calls FlushCache()
- scripts/run-bdd-tests.sh: -p 1 added (sequential package execution)

Validation:
- AuthBDD alone: 5/5 PASS (was 0/5 with broken schema isolation)
- Full features/ via run-bdd-tests.sh: ALL PASS (auth, config, greet, health, jwt)

Out of scope (follow-up T12):
- Proper parallel BDD with schema migrations per scenario + dedicated connection
  pools. Required for scaling tests but architecturally significant. Tracked.

Co-Authored-By: Mistral Vibe (devstral-2 / mistral-medium-3.5) - cache flush diagnosis
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> - root cause + revert
2026-05-03 16:28:21 +02:00
11fefe3bd9 🐛 fix(bdd): exclude @v2 scenarios from default BDD test runs (#27)
All checks were successful
CI/CD Pipeline / Build Docker Cache (push) Successful in 12s
CI/CD Pipeline / CI Pipeline (push) Successful in 7m36s
CI/CD Pipeline / Trigger Docker Push (push) Successful in 12s
Tag 3 untagged v2 scenarios + extend DEFAULT_TAGS to exclude @v2. Companion to PR #26 (BDD_SCHEMA_ISOLATION). Together should produce green CI on default daily runs.

Co-authored-by: Gabriel Radureau <arcodange@gmail.com>
Co-committed-by: Gabriel Radureau <arcodange@gmail.com>
2026-05-03 13:59:25 +02:00
7 changed files with 54 additions and 7 deletions

View File

@@ -219,10 +219,10 @@ jobs:
export DLC_DATABASE_PASSWORD=postgres
export DLC_DATABASE_NAME=dance_lessons_coach_bdd_test
export DLC_DATABASE_SSL_MODE=disable
# Enable per-scenario schema isolation (ADR-0025) to prevent flaky AuthBDD failures.
# Without this, scenarios share the public schema and pollute each other's state.
# Observed flakiness: same code passes in #605, fails in #606 on TestAuthBDD/*.
export BDD_SCHEMA_ISOLATION=true
# NOTE: BDD_SCHEMA_ISOLATION was tried (PR #26) but creates empty per-scenario schemas
# without table migrations, causing 500 errors on registration. Reverted in PR #28.
# The default mode (CleanupDatabase truncates between scenarios) works fine when tests
# run sequentially (Go BDD doesn't use t.Parallel by default).
./scripts/run-bdd-tests.sh
# Generate BDD coverage report

View File

@@ -21,16 +21,19 @@ Feature: Greet Service
When I send a POST request to v2 greet with name "John"
Then the response should be "{\"message\":\"Hello my friend John!\"}"
@v2 @api
Scenario: v2 default greeting with empty name
Given the server is running with v2 enabled
When I send a POST request to v2 greet with name ""
Then the response should be "{\"message\":\"Hello my friend!\"}"
@v2 @api
Scenario: v2 greeting with missing name field
Given the server is running with v2 enabled
When I send a POST request to v2 greet with invalid JSON "{}"
Then the response should be "{\"message\":\"Hello my friend!\"}"
@v2 @api
Scenario: v2 greeting with name that is too long
Given the server is running with v2 enabled
When I send a POST request to v2 greet with name "ThisNameIsWayTooLongAndShouldFailValidationBecauseItExceedsTheMaximumAllowedLengthOf100Characters!!!!"

View File

@@ -115,6 +115,15 @@ func InitializeTestSuite(ctx *godog.TestSuiteContext) {
testserver.TraceStateJWTSecretOperation(feature, scenarioKey, "RESET", "ok")
}
// Flush cache after every scenario to prevent cache pollution
if flushErr := sharedServer.FlushCache(); flushErr != nil {
if isCleanupLoggingEnabled() {
log.Warn().Err(flushErr).Msg("CLEANUP: Failed to flush cache after scenario")
}
} else {
testserver.TraceStateCacheOperation(feature, scenarioKey, "FLUSH", "ok")
}
// Clean database after every scenario (only if schema isolation is disabled)
if !isSchemaIsolationEnabled() {
if cleanupErr := sharedServer.CleanupDatabase(); cleanupErr != nil {

View File

@@ -15,6 +15,7 @@ import (
"sync"
"time"
"dance-lessons-coach/pkg/cache"
"dance-lessons-coach/pkg/config"
"dance-lessons-coach/pkg/server"
"dance-lessons-coach/pkg/user"
@@ -48,6 +49,7 @@ type Server struct {
baseURL string
db *sql.DB
authService user.AuthService // Reference to auth service for cleanup
cacheService cache.Service // Reference to cache service for cleanup
schemaMutex sync.Mutex // Protects schema operations
currentSchema string // Current schema being used
originalSearchPath string // Original search_path to restore
@@ -153,6 +155,9 @@ func (s *Server) Start() error {
// Store auth service for cleanup
s.authService = realServer.GetAuthService()
// Store cache service for cleanup
s.cacheService = realServer.GetCacheService()
// Initialize database connection for cleanup
if err := s.initDBConnection(); err != nil {
return fmt.Errorf("failed to initialize database connection: %w", err)
@@ -409,6 +414,23 @@ func (s *Server) ResetJWTSecrets() error {
return nil
}
// FlushCache clears all cached data to prevent cache pollution between scenarios
// This prevents cached responses from affecting subsequent test scenarios
func (s *Server) FlushCache() error {
if s.cacheService == nil {
if isCleanupLoggingEnabled() {
log.Info().Msg("CLEANUP: No cache service available, skipping cache flush")
}
return nil
}
s.cacheService.Flush()
if isCleanupLoggingEnabled() {
log.Info().Msg("CLEANUP: Cache flushed successfully")
}
return nil
}
// CleanupDatabase deletes all test data from all tables
// This uses raw SQL to avoid dependency on repositories and handles foreign keys properly
// Uses SET CONSTRAINTS ALL DEFERRED to temporarily disable foreign key checks
@@ -555,7 +577,7 @@ func (s *Server) SetupScenarioSchema(feature, scenario string) error {
return fmt.Errorf("failed to create schema %s: %w", schemaName, err)
}
// Set search path to use the new schema
// Set search path to use the new schema (testserver's own connection)
searchPathSQL := fmt.Sprintf("SET search_path = %s, %s", schemaName, s.originalSearchPath)
if _, err := s.db.Exec(searchPathSQL); err != nil {
return fmt.Errorf("failed to set search_path: %w", err)

View File

@@ -31,6 +31,11 @@ func TraceStateJWTSecretOperation(feature, scenario, operation, details string)
writeTraceLine(feature, scenario, "JWT_"+operation, details)
}
// TraceStateCacheOperation logs a cache operation
func TraceStateCacheOperation(feature, scenario, operation, details string) {
writeTraceLine(feature, scenario, "CACHE_"+operation, details)
}
// TraceStateSchemaIsolation logs a schema isolation operation
func TraceStateSchemaIsolation(feature, scenario, operation, details string) {
writeTraceLine(feature, scenario, "SCHEMA_"+operation, details)

View File

@@ -118,6 +118,12 @@ func (s *Server) GetAuthService() user.AuthService {
return s.userService
}
// GetCacheService returns the cache service for test cleanup
// This allows test suites to flush cache between tests
func (s *Server) GetCacheService() cache.Service {
return s.cacheService
}
// initializeUserServices initializes the user repository and unified user service
func initializeUserServices(cfg *config.Config) (user.UserRepository, user.UserService, error) {
// Create user repository using PostgreSQL

View File

@@ -133,7 +133,7 @@ run_tests_with_tags() {
set +e
# Default tag filter: exclude flaky, todo, and skip scenarios
DEFAULT_TAGS="~@flaky && ~@todo && ~@skip"
DEFAULT_TAGS="~@flaky && ~@todo && ~@skip && ~@v2"
if [ -n "$tags" ]; then
# Use godog directly for tag filtering with exclusion
@@ -144,7 +144,9 @@ run_tests_with_tags() {
# Note: -tags flag in go test is for Go build tags, NOT Godog feature tags
# We use GODOG_TAGS env var which is read by the test framework
echo "🚀 Running: GODOG_TAGS=\"${DEFAULT_TAGS}\" go test ./features/..."
GODOG_TAGS="$DEFAULT_TAGS" go test ./features/... -v -cover -coverpkg=./... -coverprofile=coverage.out 2>&1 | tee /tmp/bdd_test_output.txt && test_output=$(cat /tmp/bdd_test_output.txt) && rm -f /tmp/bdd_test_output.txt || test_output=$(cat /tmp/bdd_test_output.txt 2>/dev/null || echo "")
# -p 1 forces sequential package execution to avoid DB-state contention between feature packages
# (different packages would otherwise spawn concurrent test servers sharing the same Postgres DB).
GODOG_TAGS="$DEFAULT_TAGS" go test ./features/... -v -p 1 -cover -coverpkg=./... -coverprofile=coverage.out 2>&1 | tee /tmp/bdd_test_output.txt && test_output=$(cat /tmp/bdd_test_output.txt) && rm -f /tmp/bdd_test_output.txt || test_output=$(cat /tmp/bdd_test_output.txt 2>/dev/null || echo "")
test_exit_code=${PIPESTATUS[0]}
fi