Skip to content

Commit 7fdaf82

Browse files
committed
improved WaitForJobCompletion logic
1 parent 33d1e6d commit 7fdaf82

File tree

1 file changed

+15
-32
lines changed

1 file changed

+15
-32
lines changed

src/ServiceManagement/RecoveryServices/Commands.RecoveryServices.Test/ScenarioTests/RecoveryServicesTests.ps1

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -281,43 +281,26 @@ function Test-NetworkUnMapping
281281
Assert-True { $networkMappings.Count -eq 0 }
282282
}
283283

284+
<#
285+
.SYNOPSIS
286+
Wait for job completion
287+
Usage:
288+
WaitForJobCompletion -JobId $job.ID
289+
WaitForJobCompletion -JobId $job.ID -NumOfSecondsToWait 10
290+
#>
284291
function WaitForJobCompletion
285292
{
286-
param([string] $JobId)
293+
param([string] $JobId, [Int] $NumOfSecondsToWait = 30)
294+
$endStateDescription = @('Succeeded','Failed','Cancelled','Suspended')
287295

296+
$timeElapse = 0;
297+
$interval = 5;
288298
do
289299
{
290-
Start-Sleep 5
300+
Start-Sleep $interval
301+
$timeElapse = $timeElapse + $interval
291302
$job = Get-AzureSiteRecoveryJob -Id $JobId;
292-
} while( -not (($job.State -eq "Succeeded") -or ($job.State -eq "Failed") -or ($job.State -eq "Cancelled") -or ($job.State -eq "Suspended")))
293-
}
303+
} while((-not ($endStateDescription -ccontains $job.State)) -and ($timeElapse -lt $NumOfSecondsToWait))
294304

295-
<#
296-
.SYNOPSIS
297-
Wait for job completion
298-
#>
299-
<#function WaitForJobCompletion
300-
{
301-
param([string] $JobId, [Int] $numOfSeconds = 150, [String] $StateDescription)
302-
303-
$timeElapse = 0
304-
$interval = 3
305-
$endStateDescription = @('Completed','Failed')
306-
while($timeElapse -lt $numOfSeconds)
307-
{
308-
Wait-Seconds $interval
309-
$timeElapse = $timeElapse + $interval
310-
$job = Get-AzureSiteRecoveryJob -Id $JobId
311-
if($job.StateDescription -eq StateDescription)
312-
{
313-
break
314-
}
315-
elseif($endStateDescription -ccontains $job.StateDescription.ToLower())
316-
{
317-
Write-Output ("The Job with ID $($job.ID) reached $($job.ccontains) ccontains already.")
318-
return
319-
}
320-
}
321-
Assert-AreEqual $StateDescription $job.StateDescription "Job did not reach $StateDescription StateDescription within $numOfSeconds seconds."
305+
Assert-True { $endStateDescription -ccontains $job.State } "Job did not reached desired state within $NumOfSecondsToWait seconds."
322306
}
323-
#>

0 commit comments

Comments
 (0)