Skip to content

Commit

Permalink
* Add WaitOnFirstCheck optional parameter to SetAndCheckTenantSetting
Browse files Browse the repository at this point in the history
* Add debug statements
  • Loading branch information
schrolla committed Dec 2, 2024
1 parent 8c80b4a commit 3090a03
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions Testing/Functional/Products/FunctionalTestUtils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -338,15 +338,17 @@ function LoadTestResults() {
function SetAndCheckTenantSetting {
<#
.SYNOPSIS
Function executes one script block until the second registers it as successful or timeout occurs.
Function executes one script block until the second block registers it as successful or timeout occurs.
.PARAMETER SetBlock
Script block used to set the tenant setting value
.PARAMETER CheckBlock
A hashtable of key/value pairs used as a splat for the Update-MgBetaDirectorySetting commandlet.
.PARAMETER Retries
Number of times to retry the set block before failing (0 - 10)
.PARAMETER WaitInterval
Number of seconds to wait before each check (0 - 3600)
Number of seconds to wait before each check, except the first which always checks immediately (0 - 3600)
.PARAMETER WaitOnFirstCheck
Delay first check by WaitInterval if true, otherwise check immediately (Default: True)
.NOTES
If the check block does not return true after the last retry, function will throw an error.
#>
Expand All @@ -367,27 +369,35 @@ param (
[Parameter(Mandatory = $false)]
[ValidateRange(0,3600)]
[int]
$WaitInterval = 10
$WaitInterval = 10,
[Parameter(Mandatory = $false)]
[switch]
$WaitOnFirstCheck = $False
)
$SetAttempts = 0

try {
$SetFunc = [ScriptBlock]::Create($SetBlock)
$CheckFunc = [ScriptBlock]::Create($CheckBlock)
do {

Write-Debug("Running set block: $($SetFunc.Ast.EndBlock)...")
Invoke-Command -ScriptBlock $SetFunc
Start-Sleep $WaitInterval

# Sleep if not first check or option to always wait is set
if($SetAttempts -ne 0 -or $WaitOnFirstCheck) {
Write-Debug("Sleeping for $WaitInterval seconds...")
Start-Sleep $WaitInterval
}
Write-Debug("Running check block: $($CheckFunc.Ast.EndBlock)...")
$CheckSucceeded = Invoke-Command -ScriptBlock $CheckFunc
Write-Host("Tenant value changed?: $CheckSucceeded")
Write-Verbose("(Attempt $SetAttempts) Check block result = $CheckSucceeded")
++$SetAttempts
} while(-not $CheckSucceeded -and $SetAttempts -lt $Retries)

if(-not $CheckSucceeded) {
throw "Unable to set value after $SetAttempts attempts."
throw "Unable to set value after $SetAttempts attempts."
}
} catch {
throw "Error executing script blocks., $_.StackTrace"
throw "Error executing script block: $_.StackTrace"
}

}
}

0 comments on commit 3090a03

Please sign in to comment.