diff --git a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 index 4d635133b..4e07f0390 100644 --- a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 +++ b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 @@ -280,4 +280,31 @@ Describe 'PowerShell adapter resource tests' { $env:TestClassResourceResultCount = $null } + + It 'Verify that there are no cache rebuilds for several sequential executions' { + + # remove cache file + $cacheFilePath = if ($IsWindows) { + # PS 6+ on Windows + Join-Path $env:LocalAppData "dsc\PSAdapterCache.json" + } else { + # either WinPS or PS 6+ on Linux/Mac + if ($PSVersionTable.PSVersion.Major -le 5) { + Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json" + } else { + Join-Path $env:HOME ".dsc" "PSAdapterCache.json" + } + } + Remove-Item -Force -Path $cacheFilePath -ErrorAction Ignore + + # first execution should build the cache + dsc -l trace resource list -a Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt + "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Constructing Get-DscResource cache' + + # next executions following shortly after should Not rebuild the cache + 1..3 | %{ + dsc -l trace resource list -a Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt + "$TestDrive/tracing.txt" | Should -Not -FileContentMatchExactly 'Constructing Get-DscResource cache' + } + } } diff --git a/powershell-adapter/Tests/win_powershellgroup.tests.ps1 b/powershell-adapter/Tests/win_powershellgroup.tests.ps1 index d1aee97db..d5e72204d 100644 --- a/powershell-adapter/Tests/win_powershellgroup.tests.ps1 +++ b/powershell-adapter/Tests/win_powershellgroup.tests.ps1 @@ -70,4 +70,21 @@ Describe 'WindowsPowerShell adapter resource tests' { $res = $r | ConvertFrom-Json $res.results[0].result.actualState.result[0].properties.DestinationPath | Should -Be "$testFile" } + + It 'Verify that there are no cache rebuilds for several sequential executions' -Skip:(!$IsWindows) { + + # remove cache file + $cacheFilePath = Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json" + Remove-Item -Force -Path $cacheFilePath -ErrorAction Ignore + + # first execution should build the cache + dsc -l trace resource list -a Microsoft.Windows/WindowsPowerShell 2> $TestDrive/tracing.txt + "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Constructing Get-DscResource cache' + + # next executions following shortly after should Not rebuild the cache + 1..3 | %{ + dsc -l trace resource list -a Microsoft.Windows/WindowsPowerShell 2> $TestDrive/tracing.txt + "$TestDrive/tracing.txt" | Should -Not -FileContentMatchExactly 'Constructing Get-DscResource cache' + } + } } diff --git a/powershell-adapter/psDscAdapter/psDscAdapter.psm1 b/powershell-adapter/psDscAdapter/psDscAdapter.psm1 index b1fb56a50..610503876 100644 --- a/powershell-adapter/psDscAdapter/psDscAdapter.psm1 +++ b/powershell-adapter/psDscAdapter/psDscAdapter.psm1 @@ -264,12 +264,19 @@ function Invoke-DscCacheRefresh { "Checking cache for stale entries" | Write-DscTrace foreach ($cacheEntry in $dscResourceCacheEntries) { - #"Checking cache entry '$($cacheEntry.Type) $($cacheEntry.LastWriteTimes)'" | Write-DscTrace -Operation Trace $cacheEntry.LastWriteTimes.PSObject.Properties | ForEach-Object { if (Test-Path $_.Name) { - if (-not ((Get-Item $_.Name).LastWriteTime.Equals([DateTime]$_.Value))) + $file_LastWriteTime = (Get-Item $_.Name).LastWriteTime + # Truncate DateTime to seconds + $file_LastWriteTime = $file_LastWriteTime.AddTicks( - ($file_LastWriteTime.Ticks % [TimeSpan]::TicksPerSecond)); + + $cache_LastWriteTime = [DateTime]$_.Value + # Truncate DateTime to seconds + $cache_LastWriteTime = $cache_LastWriteTime.AddTicks( - ($cache_LastWriteTime.Ticks % [TimeSpan]::TicksPerSecond)); + + if (-not ($file_LastWriteTime.Equals($cache_LastWriteTime))) { "Detected stale cache entry '$($_.Name)'" | Write-DscTrace $refreshCache = $true diff --git a/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 b/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 index c842a450c..2194ca238 100644 --- a/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 +++ b/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 @@ -86,12 +86,19 @@ function Invoke-DscCacheRefresh { "Checking cache for stale entries" | Write-DscTrace foreach ($cacheEntry in $dscResourceCacheEntries) { - #"Checking cache entry '$($cacheEntry.Type) $($cacheEntry.LastWriteTimes)'" | Write-DscTrace -Operation Trace $cacheEntry.LastWriteTimes.PSObject.Properties | ForEach-Object { if (Test-Path $_.Name) { - if (-not ((Get-Item $_.Name).LastWriteTime.Equals([DateTime]$_.Value))) + $file_LastWriteTime = (Get-Item $_.Name).LastWriteTimeUtc + # Truncate DateTime to seconds + $file_LastWriteTime = $file_LastWriteTime.AddTicks( - ($file_LastWriteTime.Ticks % [TimeSpan]::TicksPerSecond)); + + $cache_LastWriteTime = [DateTime]$_.Value + # Truncate DateTime to seconds + $cache_LastWriteTime = $cache_LastWriteTime.AddTicks( - ($cache_LastWriteTime.Ticks % [TimeSpan]::TicksPerSecond)); + + if (-not ($file_LastWriteTime.Equals($cache_LastWriteTime))) { "Detected stale cache entry '$($_.Name)'" | Write-DscTrace $refreshCache = $true