Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion public/Get-DbaWaitResource.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ function Get-DbaWaitResource {
try {
$server = Connect-DbaInstance -SqlInstance $SqlInstance -SqlCredential $SqlCredential
} catch {
Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
# No -Continue here: this block has no enclosing loop, so the continue would escape the command
# and eat an iteration of whatever loop the caller runs in (#10638).
Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance
return
}

$null = $WaitResource -match '^(?<Type>[A-Z]*): (?<dbid>[0-9]*):*'
Expand Down
27 changes: 26 additions & 1 deletion tests/Get-DbaWaitResource.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,29 @@ Describe $CommandName -Tag IntegrationTests {
$resultskey.ObjectData.col2 | Should -Be "bilbo"
}
}
}

Context "When the instance cannot be reached" {
BeforeAll {
# Lower the connection timeout so the three failing connection attempts stay fast.
$oldConnectionTimeout = Get-DbatoolsConfigValue -FullName sql.connection.timeout
$null = Set-DbatoolsConfig -FullName sql.connection.timeout -Value 2
}

AfterAll {
$null = Set-DbatoolsConfig -FullName sql.connection.timeout -Value $oldConnectionTimeout
}

It "Warns without eating an iteration of the caller's loop" {
# The connection catch used to run Stop-Function -Continue in the process block, where no loop
# encloses it - the continue escaped the command and consumed an iteration of this very loop, so
# the counter stayed at zero (#10638).
$loopCount = 0
foreach ($i in 1..3) {
$null = Get-DbaWaitResource -SqlInstance dbatoolsci-nohost -WaitResource "PAGE: 1:1:1" -WarningAction SilentlyContinue
$loopCount++
}
$loopCount | Should -Be 3
($WarnVar -join " ") | Should -BeLike "*Failure*"
}
}
}