ps/Modules/Alkami.PowerShell.Common/Public/Invoke-CommandWithRetry.tests.ps1

77 lines
2.9 KiB
PowerShell
Raw Permalink Normal View History

2023-05-30 22:51:22 -07:00
#. $PSScriptRoot\..\..\Load-PesterModules.ps1
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.tests\.', '.'
$functionPath = Join-Path -Path $here -ChildPath $sut
Write-Host "Overriding SUT: $functionPath"
Import-Module $functionPath -Force
$moduleForMock = ""
Describe "Invoke-CommandWithRetry" {
Context "Test linear error-outs" {
Mock -ModuleName $moduleForMock -CommandName Get-Random -MockWith { return 0 }
Mock -ModuleName $moduleForMock -CommandName Write-Error
Mock -ModuleName $moduleForMock -CommandName Get-LogLeadName -MockWith { "UUT" }
$global:WarningStatements = @()
$global:WarningStatementCount = 0
Mock -ModuleName $moduleForMock -CommandName Write-Warning -MockWith {param($message) if ($message -match "Attempt #") {
$messageSplit = (($message -split '\[')[2] -split ']')[0]
if (![string]::IsNullOrWhiteSpace($messageSplit)) {
$global:WarningStatements += "$messageSplit"
}
$global:WarningStatementCount++
}}
$scriptDelayMilliseconds = 200
$maxRetries = 3;
$array = (1..($maxRetries - 1))|%{"$scriptDelayMilliseconds"}
Invoke-CommandWithRetry -Milliseconds $scriptDelayMilliseconds -ScriptBlock { throw 'test' } -MaxRetries $maxRetries
It "should have the right elements in the array" {
$global:WarningStatements | Should -Be $array
}
It "should have done `$maxRetries count" {
$global:WarningStatementCount | Should -Be $maxRetries
}
}
Context "Exponential error-outs" {
Mock -ModuleName $moduleForMock -CommandName Get-Random -MockWith { return 0 }
Mock -ModuleName $moduleForMock -CommandName Write-Error
Mock -ModuleName $moduleForMock -CommandName Get-LogLeadName -MockWith { "UUT" }
$global:WarningStatements = @()
$global:WarningStatementCount = 0
Mock -ModuleName $moduleForMock -CommandName Write-Warning -MockWith {param($message) if ($message -match "Attempt #") {
$messageSplit = (($message -split '\[')[2] -split ']')[0]
if (![string]::IsNullOrWhiteSpace($messageSplit)) {
$global:WarningStatements += "$messageSplit"
}
$global:WarningStatementCount++
}}
$scriptDelayMilliseconds = 200
$maxRetries = 3;
$array = (1..($maxRetries - 1))|%{"$($scriptDelayMilliseconds * (1 -shl ($_ - 1)))"}
Invoke-CommandWithRetry -Milliseconds $scriptDelayMilliseconds -ScriptBlock { throw 'test' } -MaxRetries $maxRetries -Exponential
It "should have the right elements in the array" {
$global:WarningStatements | Should -Be $array
}
It "should have done `$maxRetries count" {
$global:WarningStatementCount | Should -Be $maxRetries
}
}
}