fix(n+1): 批量查询替代循环单查
- IsAdminBootstrapRequired: userRepo.GetByID 循环 → GetByIDs 批量 - AssignRoles: roleRepo.GetByID 循环 → GetByIDs 批量 - 在 userRepositoryInterface 补充 GetByIDs 方法签名
This commit is contained in:
@@ -125,6 +125,75 @@ function Sync-AdminBootstrapExpectation {
|
||||
Write-Host "playwright e2e admin bootstrap expected: $requiresBootstrap"
|
||||
}
|
||||
|
||||
function Get-PositiveIntegerFromEnv {
|
||||
param(
|
||||
[Parameter(Mandatory = $true)][string]$Name,
|
||||
[int]$DefaultValue = 3
|
||||
)
|
||||
|
||||
$rawValue = [Environment]::GetEnvironmentVariable($Name)
|
||||
if ([string]::IsNullOrWhiteSpace($rawValue)) {
|
||||
return $DefaultValue
|
||||
}
|
||||
|
||||
$parsedValue = 0
|
||||
if ([int]::TryParse($rawValue, [ref]$parsedValue) -and $parsedValue -gt 0) {
|
||||
return $parsedValue
|
||||
}
|
||||
|
||||
return $DefaultValue
|
||||
}
|
||||
|
||||
function Get-PlaywrightScenarioNames {
|
||||
$env:E2E_LIST_SCENARIOS = '1'
|
||||
try {
|
||||
$output = & node ./scripts/run-playwright-cdp-e2e.mjs
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
throw "failed to list Playwright CDP scenarios with exit code $LASTEXITCODE"
|
||||
}
|
||||
|
||||
return @($output | Where-Object { -not [string]::IsNullOrWhiteSpace($_) })
|
||||
} finally {
|
||||
Remove-Item Env:E2E_LIST_SCENARIOS -ErrorAction SilentlyContinue
|
||||
}
|
||||
}
|
||||
|
||||
function Invoke-IsolatedPlaywrightScenario {
|
||||
param(
|
||||
[Parameter(Mandatory = $true)][string]$ScenarioName,
|
||||
[Parameter(Mandatory = $true)][string]$BackendBaseUrl,
|
||||
[int]$BrowserPort = 0,
|
||||
[int]$ScenarioAttempts = 3
|
||||
)
|
||||
|
||||
$lastError = $null
|
||||
for ($attempt = 1; $attempt -le $ScenarioAttempts; $attempt++) {
|
||||
try {
|
||||
Sync-AdminBootstrapExpectation -BackendBaseUrl $BackendBaseUrl
|
||||
$env:E2E_SCENARIOS = $ScenarioName
|
||||
& (Join-Path $PSScriptRoot 'run-cdp-smoke.ps1') `
|
||||
-Port $BrowserPort `
|
||||
-Command @('node', './scripts/run-playwright-cdp-e2e.mjs')
|
||||
$lastError = $null
|
||||
break
|
||||
} catch {
|
||||
$lastError = $_
|
||||
if ($attempt -ge $ScenarioAttempts) {
|
||||
throw
|
||||
}
|
||||
$retryReason = if ($_.Exception -and $_.Exception.Message) { $_.Exception.Message } else { $_ | Out-String }
|
||||
Write-Host "playwright-cdp scenario retry [$ScenarioName]: restarting browser and rerunning attempt $($attempt + 1) :: $retryReason"
|
||||
Start-Sleep -Seconds 1
|
||||
} finally {
|
||||
Remove-Item Env:E2E_SCENARIOS -ErrorAction SilentlyContinue
|
||||
}
|
||||
}
|
||||
|
||||
if ($lastError) {
|
||||
throw $lastError
|
||||
}
|
||||
}
|
||||
|
||||
function Start-ManagedProcess {
|
||||
param(
|
||||
[Parameter(Mandatory = $true)][string]$Name,
|
||||
@@ -309,36 +378,53 @@ try {
|
||||
|
||||
Push-Location $frontendRoot
|
||||
try {
|
||||
$lastError = $null
|
||||
$suiteAttempts = 3
|
||||
if ($env:E2E_SUITE_ATTEMPTS) {
|
||||
$parsedSuiteAttempts = 0
|
||||
if ([int]::TryParse($env:E2E_SUITE_ATTEMPTS, [ref]$parsedSuiteAttempts) -and $parsedSuiteAttempts -gt 0) {
|
||||
$suiteAttempts = $parsedSuiteAttempts
|
||||
}
|
||||
$scenarioIsolationEnabled = $true
|
||||
if ($env:E2E_SCENARIO_ISOLATION -eq '0') {
|
||||
$scenarioIsolationEnabled = $false
|
||||
}
|
||||
|
||||
for ($attempt = 1; $attempt -le $suiteAttempts; $attempt++) {
|
||||
try {
|
||||
Sync-AdminBootstrapExpectation -BackendBaseUrl $backendBaseUrl
|
||||
& (Join-Path $PSScriptRoot 'run-cdp-smoke.ps1') `
|
||||
-Port $BrowserPort `
|
||||
-Command @('node', './scripts/run-playwright-cdp-e2e.mjs')
|
||||
$lastError = $null
|
||||
break
|
||||
} catch {
|
||||
$lastError = $_
|
||||
if ($attempt -ge $suiteAttempts) {
|
||||
throw
|
||||
$suiteAttempts = Get-PositiveIntegerFromEnv -Name 'E2E_SUITE_ATTEMPTS' -DefaultValue 3
|
||||
$scenarioAttempts = Get-PositiveIntegerFromEnv -Name 'E2E_SCENARIO_ATTEMPTS' -DefaultValue $suiteAttempts
|
||||
|
||||
if ($scenarioIsolationEnabled) {
|
||||
Sync-AdminBootstrapExpectation -BackendBaseUrl $backendBaseUrl
|
||||
$scenarioNames = Get-PlaywrightScenarioNames
|
||||
if ($scenarioNames.Count -eq 0) {
|
||||
throw 'no Playwright CDP scenarios were selected for execution'
|
||||
}
|
||||
|
||||
Write-Host "playwright-cdp isolated scenarios: $($scenarioNames -join ', ')"
|
||||
foreach ($scenarioName in $scenarioNames) {
|
||||
Invoke-IsolatedPlaywrightScenario `
|
||||
-ScenarioName $scenarioName `
|
||||
-BackendBaseUrl $backendBaseUrl `
|
||||
-BrowserPort $BrowserPort `
|
||||
-ScenarioAttempts $scenarioAttempts
|
||||
}
|
||||
} else {
|
||||
$lastError = $null
|
||||
for ($attempt = 1; $attempt -le $suiteAttempts; $attempt++) {
|
||||
try {
|
||||
Sync-AdminBootstrapExpectation -BackendBaseUrl $backendBaseUrl
|
||||
& (Join-Path $PSScriptRoot 'run-cdp-smoke.ps1') `
|
||||
-Port $BrowserPort `
|
||||
-Command @('node', './scripts/run-playwright-cdp-e2e.mjs')
|
||||
$lastError = $null
|
||||
break
|
||||
} catch {
|
||||
$lastError = $_
|
||||
if ($attempt -ge $suiteAttempts) {
|
||||
throw
|
||||
}
|
||||
$retryReason = if ($_.Exception -and $_.Exception.Message) { $_.Exception.Message } else { $_ | Out-String }
|
||||
Write-Host "playwright-cdp suite retry: restarting browser and rerunning attempt $($attempt + 1) :: $retryReason"
|
||||
Start-Sleep -Seconds 1
|
||||
}
|
||||
$retryReason = if ($_.Exception -and $_.Exception.Message) { $_.Exception.Message } else { $_ | Out-String }
|
||||
Write-Host "playwright-cdp suite retry: restarting browser and rerunning attempt $($attempt + 1) :: $retryReason"
|
||||
Start-Sleep -Seconds 1
|
||||
}
|
||||
}
|
||||
|
||||
if ($lastError) {
|
||||
throw $lastError
|
||||
if ($lastError) {
|
||||
throw $lastError
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
Pop-Location
|
||||
@@ -351,6 +437,8 @@ try {
|
||||
Remove-Item Env:E2E_EXTERNAL_WEB_SERVER -ErrorAction SilentlyContinue
|
||||
Remove-Item Env:E2E_BASE_URL -ErrorAction SilentlyContinue
|
||||
Remove-Item Env:E2E_API_BASE_URL -ErrorAction SilentlyContinue
|
||||
Remove-Item Env:E2E_LIST_SCENARIOS -ErrorAction SilentlyContinue
|
||||
Remove-Item Env:E2E_SCENARIOS -ErrorAction SilentlyContinue
|
||||
Remove-Item Env:E2E_SMTP_CAPTURE_FILE -ErrorAction SilentlyContinue
|
||||
}
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user