From 20b4e73d7da33887fc5e4d9561c909c52f23ebe4 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Wed, 19 Sep 2018 16:04:39 +0500 Subject: [PATCH 01/14] Convert tabs to spaces in tests --- .../ConvertTo-Xml.Tests.ps1 | 22 +- .../XMLCommand.Tests.ps1 | 230 +++++++++--------- 2 files changed, 126 insertions(+), 126 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Xml.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Xml.Tests.ps1 index 848167703ab..d061a400f58 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Xml.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Xml.Tests.ps1 @@ -59,22 +59,22 @@ Describe "ConvertTo-Xml DRT Unit Tests" -Tags "CI" { } It "StopProcessing should work" { - $ps = [PowerShell]::Create() - $ps.AddCommand("Get-Process") - $ps.AddCommand("ConvertTo-Xml") - $ps.AddParameter("Depth", 2) - $ps.BeginInvoke() - $ps.Stop() - $ps.InvocationStateInfo.State | Should -BeExactly "Stopped" + $ps = [PowerShell]::Create() + $ps.AddCommand("Get-Process") + $ps.AddCommand("ConvertTo-Xml") + $ps.AddParameter("Depth", 2) + $ps.BeginInvoke() + $ps.Stop() + $ps.InvocationStateInfo.State | Should -BeExactly "Stopped" } # these tests just cover aspects that aren't normally exercised being used as a cmdlet - It "Can read back switch and parameter values using api" { + It "Can read back switch and parameter values using api" { Add-Type -AssemblyName "${pshome}/Microsoft.PowerShell.Commands.Utility.dll" - $cmd = [Microsoft.PowerShell.Commands.ConvertToXmlCommand]::new() - $cmd.NoTypeInformation = $true - $cmd.NoTypeInformation | Should -BeTrue + $cmd = [Microsoft.PowerShell.Commands.ConvertToXmlCommand]::new() + $cmd.NoTypeInformation = $true + $cmd.NoTypeInformation | Should -BeTrue } It "Serialize primitive type" { diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 index 2882d88f888..7af1f999e16 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 @@ -2,11 +2,11 @@ # Licensed under the MIT License. Describe "XmlCommand DRT basic functionality Tests" -Tags "CI" { - BeforeAll { - if(-not ('IsHiddenTestType' -as "type")) - { - Add-Type -TypeDefinition @" - public class IsHiddenTestType + BeforeAll { + if(-not ('IsHiddenTestType' -as "type")) + { + Add-Type -TypeDefinition @" + public class IsHiddenTestType { public IsHiddenTestType() { @@ -24,35 +24,35 @@ Describe "XmlCommand DRT basic functionality Tests" -Tags "CI" { public string Property2; } "@ - } + } } - BeforeEach { - $testfile = Join-Path -Path $TestDrive -ChildPath "clixml-directive.xml" - } + BeforeEach { + $testfile = Join-Path -Path $TestDrive -ChildPath "clixml-directive.xml" + } AfterEach { - remove-item $testfile -Force -ErrorAction SilentlyContinue + remove-item $testfile -Force -ErrorAction SilentlyContinue } - It "Import with CliXml directive should work" { + It "Import with CliXml directive should work" { Get-Command export* -Type Cmdlet | Select-Object -First 3 | Export-Clixml -Path $testfile - $results = Import-Clixml $testfile - $results.Count | Should -BeExactly 3 + $results = Import-Clixml $testfile + $results.Count | Should -BeExactly 3 $results[0].PSTypeNames[0] | Should -Be "Deserialized.System.Management.Automation.CmdletInfo" } - It "Import with Rehydration should work" { - $property1 = 256 - $property2 = "abcdef" - $isHiddenTestType = [IsHiddenTestType]::New($property1,$property2) - $isHiddenTestType | Export-Clixml $testfile - $results = Import-Clixml $testfile - $results.Property1 | Should -Be $property1 - $results.Property2 | Should -Be $property2 + It "Import with Rehydration should work" { + $property1 = 256 + $property2 = "abcdef" + $isHiddenTestType = [IsHiddenTestType]::New($property1,$property2) + $isHiddenTestType | Export-Clixml $testfile + $results = Import-Clixml $testfile + $results.Property1 | Should -Be $property1 + $results.Property2 | Should -Be $property2 } - It "Export-Clixml StopProcessing should succeed" { + It "Export-Clixml StopProcessing should succeed" { $ps = [PowerShell]::Create() $null = $ps.AddScript("1..10") $null = $ps.AddCommand("foreach-object") @@ -64,99 +64,99 @@ Describe "XmlCommand DRT basic functionality Tests" -Tags "CI" { $null = $ps.Stop() $ps.InvocationStateInfo.State | Should -Be "Stopped" $ps.Dispose() - } - - It "Import-Clixml StopProcessing should succeed" { - 1,2,3 | Export-Clixml -Path $testfile - $ps = [PowerShell]::Create() - $ps.AddCommand("Get-Process") - $ps.AddCommand("Import-CliXml") - $ps.AddParameter("Path", $testfile) - $ps.BeginInvoke() - $ps.Stop() - $ps.InvocationStateInfo.State | Should -Be "Stopped" - } - - It "Export-Clixml using -Depth should work" { - class Three - { - [int] $num = 3; - } - - class Two - { - [Three] $three = [Three]::New(); - [int] $value = 2; - } - - class One - { - [Two] $two = [Two]::New(); - [int] $value = 1; - } - - $one = [One]::New() - $one | Export-Clixml -Depth 2 -Path $testfile - $deserialized_one = Import-Clixml -Path $testfile - $deserialized_one.Value | Should -Be 1 - $deserialized_one.two.Value | Should -Be 2 - $deserialized_one.two.Three | Should -Not -BeNullOrEmpty - $deserialized_one.two.three.num | Should -BeNullOrEmpty - } - - It "Import-Clixml should work with XML serialization from pwsh.exe" { - # need to create separate process so that current powershell doesn't interpret clixml output - Start-Process -FilePath $pshome\pwsh -RedirectStandardOutput $testfile -Args "-noprofile -nologo -outputformat xml -command get-command import-clixml" -Wait - $out = Import-Clixml -Path $testfile - $out.Name | Should -Be "Import-CliXml" - $out.CommandType.ToString() | Should -Be "Cmdlet" - $out.Source | Should -Be "Microsoft.PowerShell.Utility" - } - - It "Import-Clixml -IncludeTotalCount always returns unknown total count" { - # this cmdlets supports paging, but not this switch - [PSCustomObject]@{foo=1;bar=@{hello="world"}} | Export-Clixml -Path $testfile - $out = Import-Clixml -Path $testfile -IncludeTotalCount - $out[0].ToString() | Should -BeExactly "Unknown total count" - } - - It "Import-Clixml -First and -Skip work together for simple types" { - "one","two","three","four" | Export-Clixml -Path $testfile - $out = Import-Clixml -Path $testfile -First 2 -Skip 1 - $out.Count | Should -Be 2 - $out[0] | Should -BeExactly "two" - $out[1] | Should -BeExactly "three" - } - - It "Import-Clixml -First and -Skip work together for collections" { - @{a=1;b=2;c=3;d=4} | Export-Clixml -Path $testfile - # order not guaranteed, even with [ordered] so we have to be smart here and compare against the full result - $out1 = Import-Clixml -Path $testfile # this results in a hashtable - $out2 = Import-Clixml -Path $testfile -First 2 -Skip 1 # this results in a dictionary entry - $out2.Count | Should -Be 2 + } + + It "Import-Clixml StopProcessing should succeed" { + 1,2,3 | Export-Clixml -Path $testfile + $ps = [PowerShell]::Create() + $ps.AddCommand("Get-Process") + $ps.AddCommand("Import-CliXml") + $ps.AddParameter("Path", $testfile) + $ps.BeginInvoke() + $ps.Stop() + $ps.InvocationStateInfo.State | Should -Be "Stopped" + } + + It "Export-Clixml using -Depth should work" { + class Three + { + [int] $num = 3; + } + + class Two + { + [Three] $three = [Three]::New(); + [int] $value = 2; + } + + class One + { + [Two] $two = [Two]::New(); + [int] $value = 1; + } + + $one = [One]::New() + $one | Export-Clixml -Depth 2 -Path $testfile + $deserialized_one = Import-Clixml -Path $testfile + $deserialized_one.Value | Should -Be 1 + $deserialized_one.two.Value | Should -Be 2 + $deserialized_one.two.Three | Should -Not -BeNullOrEmpty + $deserialized_one.two.three.num | Should -BeNullOrEmpty + } + + It "Import-Clixml should work with XML serialization from pwsh.exe" { + # need to create separate process so that current powershell doesn't interpret clixml output + Start-Process -FilePath $pshome\pwsh -RedirectStandardOutput $testfile -Args "-noprofile -nologo -outputformat xml -command get-command import-clixml" -Wait + $out = Import-Clixml -Path $testfile + $out.Name | Should -Be "Import-CliXml" + $out.CommandType.ToString() | Should -Be "Cmdlet" + $out.Source | Should -Be "Microsoft.PowerShell.Utility" + } + + It "Import-Clixml -IncludeTotalCount always returns unknown total count" { + # this cmdlets supports paging, but not this switch + [PSCustomObject]@{foo=1;bar=@{hello="world"}} | Export-Clixml -Path $testfile + $out = Import-Clixml -Path $testfile -IncludeTotalCount + $out[0].ToString() | Should -BeExactly "Unknown total count" + } + + It "Import-Clixml -First and -Skip work together for simple types" { + "one","two","three","four" | Export-Clixml -Path $testfile + $out = Import-Clixml -Path $testfile -First 2 -Skip 1 + $out.Count | Should -Be 2 + $out[0] | Should -BeExactly "two" + $out[1] | Should -BeExactly "three" + } + + It "Import-Clixml -First and -Skip work together for collections" { + @{a=1;b=2;c=3;d=4} | Export-Clixml -Path $testfile + # order not guaranteed, even with [ordered] so we have to be smart here and compare against the full result + $out1 = Import-Clixml -Path $testfile # this results in a hashtable + $out2 = Import-Clixml -Path $testfile -First 2 -Skip 1 # this results in a dictionary entry + $out2.Count | Should -Be 2 ($out2.Name) -join ":" | Should -Be (@($out1.Keys)[1, 2] -join ":") ($out2.Value) -join ":" | Should -Be (@($out1.Values)[1, 2] -join ":") - } - - # these tests just cover aspects that aren't normally exercised being used as a cmdlet - It "Can read back switch and parameter values using api" { - Add-Type -AssemblyName "${pshome}/Microsoft.PowerShell.Commands.Utility.dll" - - $cmd = [Microsoft.PowerShell.Commands.ExportClixmlCommand]::new() - $cmd.LiteralPath = "foo" - $cmd.LiteralPath | Should -BeExactly "foo" - $cmd.NoClobber = $true - $cmd.NoClobber | Should -BeTrue - - $cmd = [Microsoft.PowerShell.Commands.ImportClixmlCommand]::new() - $cmd.LiteralPath = "bar" - $cmd.LiteralPath | Should -BeExactly "bar" - - $cmd = [Microsoft.PowerShell.Commands.SelectXmlCommand]::new() - $cmd.LiteralPath = "foo" - $cmd.LiteralPath | Should -BeExactly "foo" - $xml = [xml]"" - $cmd.Xml = $xml - $cmd.Xml | Should -Be $xml - } + } + + # these tests just cover aspects that aren't normally exercised being used as a cmdlet + It "Can read back switch and parameter values using api" { + Add-Type -AssemblyName "${pshome}/Microsoft.PowerShell.Commands.Utility.dll" + + $cmd = [Microsoft.PowerShell.Commands.ExportClixmlCommand]::new() + $cmd.LiteralPath = "foo" + $cmd.LiteralPath | Should -BeExactly "foo" + $cmd.NoClobber = $true + $cmd.NoClobber | Should -BeTrue + + $cmd = [Microsoft.PowerShell.Commands.ImportClixmlCommand]::new() + $cmd.LiteralPath = "bar" + $cmd.LiteralPath | Should -BeExactly "bar" + + $cmd = [Microsoft.PowerShell.Commands.SelectXmlCommand]::new() + $cmd.LiteralPath = "foo" + $cmd.LiteralPath | Should -BeExactly "foo" + $xml = [xml]"" + $cmd.Xml = $xml + $cmd.Xml | Should -Be $xml + } } From 0d8b026862637de31479ac6866f8131db5776fac Mon Sep 17 00:00:00 2001 From: iSazonov Date: Wed, 19 Sep 2018 16:15:34 +0500 Subject: [PATCH 02/14] Use test hook in XML commands --- .../commands/utility/XmlCommands.cs | 14 ++++++++++++++ .../ConvertTo-Xml.Tests.ps1 | 9 ++++----- .../XMLCommand.Tests.ps1 | 16 +++++++++------- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs index 1fa3f9befa5..07f07fa0aaf 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs @@ -123,6 +123,8 @@ protected override BeginProcessing() { CreateFileStream(); + + WriteVerbose("Export-Clixml started"); } /// @@ -325,6 +327,16 @@ public void Dispose() private ImportXmlHelper _helper; + /// + /// BeginProcessing override. + /// + protected override + void + BeginProcessing() + { + WriteVerbose("Import-Clixml started"); + } + /// /// ProcessRecord overload. /// @@ -416,6 +428,8 @@ protected override void BeginProcessing() WriteObject(string.Format(CultureInfo.InvariantCulture, "", Encoding.UTF8.WebName)); WriteObject(""); } + + WriteVerbose("ConvertTo-Xml started"); } /// diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Xml.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Xml.Tests.ps1 index d061a400f58..4c038e286d4 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Xml.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Xml.Tests.ps1 @@ -60,11 +60,10 @@ Describe "ConvertTo-Xml DRT Unit Tests" -Tags "CI" { It "StopProcessing should work" { $ps = [PowerShell]::Create() - $ps.AddCommand("Get-Process") - $ps.AddCommand("ConvertTo-Xml") - $ps.AddParameter("Depth", 2) - $ps.BeginInvoke() - $ps.Stop() + $null = $ps.AddScript({ Get-Process | ConvertTo-Xml -Depth 2 -Verbose }) + $null = $ps.BeginInvoke() + Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 } -IntervalInMilliseconds 50 + $null = $ps.Stop() $ps.InvocationStateInfo.State | Should -BeExactly "Stopped" } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 index 7af1f999e16..b5a5c8e113c 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 @@ -59,21 +59,23 @@ Describe "XmlCommand DRT basic functionality Tests" -Tags "CI" { $null = $ps.AddParameter("Process", { $_; start-sleep 1 }) $null = $ps.AddCommand("Export-CliXml") $null = $ps.AddParameter("Path", $testfile) + $null = $ps.AddParameter("Verbose") $null = $ps.BeginInvoke() - Start-Sleep 1 + Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 } -IntervalInMilliseconds 50 $null = $ps.Stop() $ps.InvocationStateInfo.State | Should -Be "Stopped" $ps.Dispose() } It "Import-Clixml StopProcessing should succeed" { - 1,2,3 | Export-Clixml -Path $testfile + 1..50000 | Export-Clixml -Path $testfile $ps = [PowerShell]::Create() - $ps.AddCommand("Get-Process") - $ps.AddCommand("Import-CliXml") - $ps.AddParameter("Path", $testfile) - $ps.BeginInvoke() - $ps.Stop() + $null = $ps.AddCommand("Import-CliXml") + $null = $ps.AddParameter("Path", $testfile) + $null = $ps.AddParameter("Verbose") + $null = $ps.BeginInvoke() + Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 } -IntervalInMilliseconds 20 + $null = $ps.Stop() $ps.InvocationStateInfo.State | Should -Be "Stopped" } From 2e5fb8d5adc543ee86612ddc6d231efe4b5a6de9 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Thu, 20 Sep 2018 12:37:18 +0500 Subject: [PATCH 03/14] Use test hook --- .../utility/WebCmdlet/ConvertToJsonCommand.cs | 6 +++ .../commands/utility/XmlCommands.cs | 32 +++++++------ .../engine/Utils.cs | 3 ++ .../ConvertTo-Json.Tests.ps1 | 35 +++++++------- .../ConvertTo-Xml.Tests.ps1 | 18 +++++--- .../XMLCommand.Tests.ps1 | 46 ++++++++++--------- 6 files changed, 83 insertions(+), 57 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs index 7848a61b227..9e7400f2430 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs @@ -109,6 +109,12 @@ protected override void BeginProcessing() /// protected override void ProcessRecord() { + if (InternalTestHooks.ActivateSleepForStoppingTest) + { + WriteVerbose("ConvertTo-Json started"); + System.Threading.Thread.Sleep(50); + } + if (InputObject != null) { _inputObjects.Add(InputObject); diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs index 07f07fa0aaf..6c7dfd9060e 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs @@ -123,8 +123,6 @@ protected override BeginProcessing() { CreateFileStream(); - - WriteVerbose("Export-Clixml started"); } /// @@ -133,6 +131,12 @@ protected override void ProcessRecord() { + if (InternalTestHooks.ActivateSleepForStoppingTest) + { + WriteVerbose("Import-Clixml started"); + System.Threading.Thread.Sleep(50); + } + if (_serializer != null) { _serializer.Serialize(InputObject); @@ -327,16 +331,6 @@ public void Dispose() private ImportXmlHelper _helper; - /// - /// BeginProcessing override. - /// - protected override - void - BeginProcessing() - { - WriteVerbose("Import-Clixml started"); - } - /// /// ProcessRecord overload. /// @@ -346,6 +340,12 @@ protected override void ProcessRecord() { foreach (string path in Path) { + if (InternalTestHooks.ActivateSleepForStoppingTest) + { + WriteVerbose("Import-Clixml started"); + System.Threading.Thread.Sleep(50); + } + _helper = new ImportXmlHelper(path, this, _isLiteralPath); _helper.Import(); } @@ -428,8 +428,6 @@ protected override void BeginProcessing() WriteObject(string.Format(CultureInfo.InvariantCulture, "", Encoding.UTF8.WebName)); WriteObject(""); } - - WriteVerbose("ConvertTo-Xml started"); } /// @@ -437,6 +435,12 @@ protected override void BeginProcessing() /// protected override void ProcessRecord() { + if (InternalTestHooks.ActivateSleepForStoppingTest) + { + WriteVerbose("ConvertTo-Xml started"); + System.Threading.Thread.Sleep(50); + } + if (As.Equals("Stream", StringComparison.OrdinalIgnoreCase)) { CreateMemoryStream(); diff --git a/src/System.Management.Automation/engine/Utils.cs b/src/System.Management.Automation/engine/Utils.cs index 5210755f0bd..fa551452f61 100644 --- a/src/System.Management.Automation/engine/Utils.cs +++ b/src/System.Management.Automation/engine/Utils.cs @@ -1395,6 +1395,9 @@ public static class InternalTestHooks internal static bool ShowMarkdownOutputBypass; + // Slow down a cmdlet to predictably check the cmdlet stopping (that 'PowerShell.Stop()' or 'Ctrl-C' work). + internal static bool ActivateSleepForStoppingTest; + /// This member is used for internal test purposes. public static void SetTestHook(string property, object value) { diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 index 198cccf5964..47b1f588aaa 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 @@ -22,22 +22,25 @@ Describe 'ConvertTo-Json' -tags "CI" { } It "StopProcessing should succeed" { - $ps = [PowerShell]::Create() - $null = $ps.AddScript({ - $obj = [PSCustomObject]@{P1 = ''; P2 = ''; P3 = ''; P4 = ''; P5 = ''; P6 = ''} - $obj.P1 = $obj.P2 = $obj.P3 = $obj.P4 = $obj.P5 = $obj.P6 = $obj - 1..100 | Foreach-Object { $obj } | ConvertTo-Json -Depth 10 -Verbose - # the conversion is expected to take some time, this throw is in case it doesn't - throw "Should not have thrown exception" - }) - $null = $ps.BeginInvoke() - # wait for verbose message from ConvertTo-Json to ensure cmdlet is processing - Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 } - $null = $ps.BeginStop($null, $null) - # wait a bit to ensure state has changed, not using synchronous Stop() to avoid blocking Pester - Start-Sleep -Milliseconds 100 - $ps.InvocationStateInfo.State | Should -BeExactly "Stopped" - $ps.Dispose() + try { + $ps = [PowerShell]::Create() + $null = $ps.AddScript({ + $obj = [PSCustomObject]@{P1 = ''; P2 = ''; P3 = ''; P4 = ''; P5 = ''; P6 = ''} + $obj.P1 = $obj.P2 = $obj.P3 = $obj.P4 = $obj.P5 = $obj.P6 = $obj + 1..100 | Foreach-Object { $obj } | ConvertTo-Json -Depth 10 -Verbose + # the conversion is expected to take some time, this throw is in case it doesn't + throw "Should not have thrown exception" + }) + [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ActivateSleepForStoppingTest', $true) + $null = $ps.BeginInvoke() + # wait for verbose message from ConvertTo-Json to ensure cmdlet is processing + Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 } -IntervalInMilliseconds 50 + $null = $ps.Stop() + $ps.InvocationStateInfo.State | Should -BeExactly "Stopped" + } finally { + $ps.Dispose() + [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ActivateSleepForStoppingTest', $false) + } } It "The result string is packed in an array symbols when AsArray parameter is used." { diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Xml.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Xml.Tests.ps1 index 4c038e286d4..c4e8b54c053 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Xml.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Xml.Tests.ps1 @@ -59,12 +59,18 @@ Describe "ConvertTo-Xml DRT Unit Tests" -Tags "CI" { } It "StopProcessing should work" { - $ps = [PowerShell]::Create() - $null = $ps.AddScript({ Get-Process | ConvertTo-Xml -Depth 2 -Verbose }) - $null = $ps.BeginInvoke() - Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 } -IntervalInMilliseconds 50 - $null = $ps.Stop() - $ps.InvocationStateInfo.State | Should -BeExactly "Stopped" + try { + $ps = [PowerShell]::Create() + $null = $ps.AddScript({ Get-Process | ConvertTo-Xml -Depth 2 -Verbose }) + [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ActivateSleepForStoppingTest', $true) + $null = $ps.BeginInvoke() + Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 } -IntervalInMilliseconds 50 + $null = $ps.Stop() + $ps.InvocationStateInfo.State | Should -BeExactly "Stopped" + } finally { + $ps.Dispose() + [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ActivateSleepForStoppingTest', $false) + } } # these tests just cover aspects that aren't normally exercised being used as a cmdlet diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 index b5a5c8e113c..7bdb6c0f1c1 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 @@ -53,30 +53,34 @@ Describe "XmlCommand DRT basic functionality Tests" -Tags "CI" { } It "Export-Clixml StopProcessing should succeed" { - $ps = [PowerShell]::Create() - $null = $ps.AddScript("1..10") - $null = $ps.AddCommand("foreach-object") - $null = $ps.AddParameter("Process", { $_; start-sleep 1 }) - $null = $ps.AddCommand("Export-CliXml") - $null = $ps.AddParameter("Path", $testfile) - $null = $ps.AddParameter("Verbose") - $null = $ps.BeginInvoke() - Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 } -IntervalInMilliseconds 50 - $null = $ps.Stop() - $ps.InvocationStateInfo.State | Should -Be "Stopped" - $ps.Dispose() + try { + $ps = [PowerShell]::Create() + $null = $ps.AddScript("1..10000 | Export-CliXml -Path $testfile -Verbose") + [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ActivateSleepForStoppingTest', $true) + $null = $ps.BeginInvoke() + Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 } -IntervalInMilliseconds 50 + $null = $ps.Stop() + $ps.InvocationStateInfo.State | Should -Be "Stopped" + } finally { + $ps.Dispose() + [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ActivateSleepForStoppingTest', $false) + } } It "Import-Clixml StopProcessing should succeed" { - 1..50000 | Export-Clixml -Path $testfile - $ps = [PowerShell]::Create() - $null = $ps.AddCommand("Import-CliXml") - $null = $ps.AddParameter("Path", $testfile) - $null = $ps.AddParameter("Verbose") - $null = $ps.BeginInvoke() - Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 } -IntervalInMilliseconds 20 - $null = $ps.Stop() - $ps.InvocationStateInfo.State | Should -Be "Stopped" + try { + 1..10000 | Export-Clixml -Path $testfile + $ps = [PowerShell]::Create() + $null = $ps.AddScript("Import-CliXml -Path $testfile -Verbose") + [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ActivateSleepForStoppingTest', $true) + $null = $ps.BeginInvoke() + Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 } -IntervalInMilliseconds 20 + $null = $ps.Stop() + $ps.InvocationStateInfo.State | Should -Be "Stopped" + } finally { + $ps.Dispose() + [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ActivateSleepForStoppingTest', $false) + } } It "Export-Clixml using -Depth should work" { From b855bc932d23dd375169cc9a754c6da9429dbe8f Mon Sep 17 00:00:00 2001 From: iSazonov Date: Thu, 20 Sep 2018 13:14:21 +0500 Subject: [PATCH 04/14] Suppress unneeded StyleCop message --- src/System.Management.Automation/engine/Utils.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/System.Management.Automation/engine/Utils.cs b/src/System.Management.Automation/engine/Utils.cs index fa551452f61..7a47049ff63 100644 --- a/src/System.Management.Automation/engine/Utils.cs +++ b/src/System.Management.Automation/engine/Utils.cs @@ -1366,6 +1366,7 @@ namespace System.Management.Automation.Internal { /// This class is used for internal test purposes. [SuppressMessage("Microsoft.MSInternal", "CA903:InternalNamespaceShouldNotContainPublicTypes", Justification = "Needed Internal use only")] + [SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1401:FieldsMustBePrivate", Justification = "Reviewed.")] public static class InternalTestHooks { internal static bool BypassGroupPolicyCaching; From 680f1a0e61d6e1ec2a122db608aa9a6c57ca4074 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Thu, 20 Sep 2018 15:00:40 +0500 Subject: [PATCH 05/14] Use async BeginStop() --- .../Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 | 3 ++- .../Microsoft.PowerShell.Utility/ConvertTo-Xml.Tests.ps1 | 3 ++- .../Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 | 6 ++++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 index 47b1f588aaa..6879b7b506c 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 @@ -35,7 +35,8 @@ Describe 'ConvertTo-Json' -tags "CI" { $null = $ps.BeginInvoke() # wait for verbose message from ConvertTo-Json to ensure cmdlet is processing Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 } -IntervalInMilliseconds 50 - $null = $ps.Stop() + $null = $ps.BeginStop($null, $null) + Wait-UntilTrue { $ps.InvocationStateInfo.State -eq "Stopped" } -IntervalInMilliseconds 50 $ps.InvocationStateInfo.State | Should -BeExactly "Stopped" } finally { $ps.Dispose() diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Xml.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Xml.Tests.ps1 index c4e8b54c053..d75b230c644 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Xml.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Xml.Tests.ps1 @@ -65,7 +65,8 @@ Describe "ConvertTo-Xml DRT Unit Tests" -Tags "CI" { [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ActivateSleepForStoppingTest', $true) $null = $ps.BeginInvoke() Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 } -IntervalInMilliseconds 50 - $null = $ps.Stop() + $null = $ps.BeginStop($null, $null) + Wait-UntilTrue { $ps.InvocationStateInfo.State -eq "Stopped" } -IntervalInMilliseconds 50 $ps.InvocationStateInfo.State | Should -BeExactly "Stopped" } finally { $ps.Dispose() diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 index 7bdb6c0f1c1..a08dfc258e0 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 @@ -59,7 +59,8 @@ Describe "XmlCommand DRT basic functionality Tests" -Tags "CI" { [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ActivateSleepForStoppingTest', $true) $null = $ps.BeginInvoke() Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 } -IntervalInMilliseconds 50 - $null = $ps.Stop() + $null = $ps.BeginStop($null, $null) + Wait-UntilTrue { $ps.InvocationStateInfo.State -eq "Stopped" } -IntervalInMilliseconds 50 $ps.InvocationStateInfo.State | Should -Be "Stopped" } finally { $ps.Dispose() @@ -75,7 +76,8 @@ Describe "XmlCommand DRT basic functionality Tests" -Tags "CI" { [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ActivateSleepForStoppingTest', $true) $null = $ps.BeginInvoke() Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 } -IntervalInMilliseconds 20 - $null = $ps.Stop() + $null = $ps.BeginStop($null, $null) + Wait-UntilTrue { $ps.InvocationStateInfo.State -eq "Stopped" } -IntervalInMilliseconds 50 $ps.InvocationStateInfo.State | Should -Be "Stopped" } finally { $ps.Dispose() From fc0291ffd00cd74b1dfc4cad67647ce987af1be7 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Thu, 20 Sep 2018 18:21:18 +0500 Subject: [PATCH 06/14] Create Test-Stopping() function --- .../ConvertTo-Json.Tests.ps1 | 24 +++----------- .../ConvertTo-Xml.Tests.ps1 | 14 +------- .../XMLCommand.Tests.ps1 | 32 +++---------------- .../Modules/HelpersCommon/HelpersCommon.psd1 | 1 + .../Modules/HelpersCommon/HelpersCommon.psm1 | 26 +++++++++++++++ 5 files changed, 38 insertions(+), 59 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 index 6879b7b506c..f643fb901ce 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 @@ -22,26 +22,12 @@ Describe 'ConvertTo-Json' -tags "CI" { } It "StopProcessing should succeed" { - try { - $ps = [PowerShell]::Create() - $null = $ps.AddScript({ - $obj = [PSCustomObject]@{P1 = ''; P2 = ''; P3 = ''; P4 = ''; P5 = ''; P6 = ''} - $obj.P1 = $obj.P2 = $obj.P3 = $obj.P4 = $obj.P5 = $obj.P6 = $obj - 1..100 | Foreach-Object { $obj } | ConvertTo-Json -Depth 10 -Verbose - # the conversion is expected to take some time, this throw is in case it doesn't - throw "Should not have thrown exception" - }) - [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ActivateSleepForStoppingTest', $true) - $null = $ps.BeginInvoke() - # wait for verbose message from ConvertTo-Json to ensure cmdlet is processing - Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 } -IntervalInMilliseconds 50 - $null = $ps.BeginStop($null, $null) - Wait-UntilTrue { $ps.InvocationStateInfo.State -eq "Stopped" } -IntervalInMilliseconds 50 - $ps.InvocationStateInfo.State | Should -BeExactly "Stopped" - } finally { - $ps.Dispose() - [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ActivateSleepForStoppingTest', $false) + $sb = { + $obj = [PSCustomObject]@{P1 = ''; P2 = ''; P3 = ''; P4 = ''; P5 = ''; P6 = ''} + $obj.P1 = $obj.P2 = $obj.P3 = $obj.P4 = $obj.P5 = $obj.P6 = $obj + 1..100 | Foreach-Object { $obj } | ConvertTo-Json -Depth 10 -Verbose } + Test-Stopping $sb -IntervalInMilliseconds 50 } It "The result string is packed in an array symbols when AsArray parameter is used." { diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Xml.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Xml.Tests.ps1 index d75b230c644..df94b523323 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Xml.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Xml.Tests.ps1 @@ -59,19 +59,7 @@ Describe "ConvertTo-Xml DRT Unit Tests" -Tags "CI" { } It "StopProcessing should work" { - try { - $ps = [PowerShell]::Create() - $null = $ps.AddScript({ Get-Process | ConvertTo-Xml -Depth 2 -Verbose }) - [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ActivateSleepForStoppingTest', $true) - $null = $ps.BeginInvoke() - Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 } -IntervalInMilliseconds 50 - $null = $ps.BeginStop($null, $null) - Wait-UntilTrue { $ps.InvocationStateInfo.State -eq "Stopped" } -IntervalInMilliseconds 50 - $ps.InvocationStateInfo.State | Should -BeExactly "Stopped" - } finally { - $ps.Dispose() - [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ActivateSleepForStoppingTest', $false) - } + Test-Stopping { Get-Process | ConvertTo-Xml -Depth 2 -Verbose } -IntervalInMilliseconds 50 } # these tests just cover aspects that aren't normally exercised being used as a cmdlet diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 index a08dfc258e0..7a3b03277b5 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 @@ -53,36 +53,14 @@ Describe "XmlCommand DRT basic functionality Tests" -Tags "CI" { } It "Export-Clixml StopProcessing should succeed" { - try { - $ps = [PowerShell]::Create() - $null = $ps.AddScript("1..10000 | Export-CliXml -Path $testfile -Verbose") - [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ActivateSleepForStoppingTest', $true) - $null = $ps.BeginInvoke() - Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 } -IntervalInMilliseconds 50 - $null = $ps.BeginStop($null, $null) - Wait-UntilTrue { $ps.InvocationStateInfo.State -eq "Stopped" } -IntervalInMilliseconds 50 - $ps.InvocationStateInfo.State | Should -Be "Stopped" - } finally { - $ps.Dispose() - [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ActivateSleepForStoppingTest', $false) - } + $sb = [scriptblock]::Create("1..10000 | Export-CliXml -Path $testfile -Verbose") + Test-Stopping $sb -IntervalInMilliseconds 50 } It "Import-Clixml StopProcessing should succeed" { - try { - 1..10000 | Export-Clixml -Path $testfile - $ps = [PowerShell]::Create() - $null = $ps.AddScript("Import-CliXml -Path $testfile -Verbose") - [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ActivateSleepForStoppingTest', $true) - $null = $ps.BeginInvoke() - Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 } -IntervalInMilliseconds 20 - $null = $ps.BeginStop($null, $null) - Wait-UntilTrue { $ps.InvocationStateInfo.State -eq "Stopped" } -IntervalInMilliseconds 50 - $ps.InvocationStateInfo.State | Should -Be "Stopped" - } finally { - $ps.Dispose() - [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ActivateSleepForStoppingTest', $false) - } + 1..10000 | Export-Clixml -Path $testfile + $sb = [scriptblock]::Create("Import-CliXml -Path $testfile -Verbose") + Test-Stopping $sb -IntervalInMilliseconds 20 } It "Export-Clixml using -Depth should work" { diff --git a/test/tools/Modules/HelpersCommon/HelpersCommon.psd1 b/test/tools/Modules/HelpersCommon/HelpersCommon.psd1 index 0dea69c59dc..7c65fc5d457 100644 --- a/test/tools/Modules/HelpersCommon/HelpersCommon.psd1 +++ b/test/tools/Modules/HelpersCommon/HelpersCommon.psd1 @@ -30,6 +30,7 @@ FunctionsToExport = @( 'Test-IsVstsLinux' 'Test-IsVstsWindows' 'Test-TesthookIsSet' + 'Test-Stopping' 'Wait-FileToBePresent' 'Wait-UntilTrue' ) diff --git a/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 b/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 index f3ca73a1fed..d997492b749 100644 --- a/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 +++ b/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 @@ -1,5 +1,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. + function Wait-UntilTrue { [CmdletBinding()] @@ -312,6 +313,31 @@ function Start-NativeExecution } finally { $script:ErrorActionPreference = $backupEAP } +# Test if the scriptblock (cmdlet) can be stopped +function Test-Stopping +{ + [CmdletBinding()] + param ( + [ScriptBlock]$sb, + [int]$TimeoutInMilliseconds = 10000, + [int]$IntervalInMilliseconds = 1000 + ) + + try { + $ps = [PowerShell]::Create() + $null = $ps.AddScript($sb) + Enable-Testhook 'ActivateSleepForStoppingTest' + $null = $ps.BeginInvoke() + Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 } -TimeoutInMilliseconds $TimeoutInMilliseconds -IntervalInMilliseconds $IntervalInMilliseconds + $null = $ps.BeginStop($null, $null) + Wait-UntilTrue { $ps.InvocationStateInfo.State -eq "Stopped" } -TimeoutInMilliseconds $TimeoutInMilliseconds -IntervalInMilliseconds $IntervalInMilliseconds + + $ps.InvocationStateInfo.State | Should -Be "Stopped" + + } finally { + $ps.Dispose() + Disable-Testhook 'ActivateSleepForStoppingTest' + } } # Creates a new random hex string for use with things like test certificate passwords From e3e077896b429155964cc7f5caf8947996fc4a2a Mon Sep 17 00:00:00 2001 From: iSazonov Date: Thu, 20 Sep 2018 19:47:08 +0500 Subject: [PATCH 07/14] Fix typo and slow down Import-CliXml --- .../commands/utility/XmlCommands.cs | 2 +- .../Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs index 6c7dfd9060e..ac5342224c9 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs @@ -133,7 +133,7 @@ protected override { if (InternalTestHooks.ActivateSleepForStoppingTest) { - WriteVerbose("Import-Clixml started"); + WriteVerbose("Export-Clixml started"); System.Threading.Thread.Sleep(50); } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 index 7a3b03277b5..5f31f964b24 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 @@ -59,7 +59,7 @@ Describe "XmlCommand DRT basic functionality Tests" -Tags "CI" { It "Import-Clixml StopProcessing should succeed" { 1..10000 | Export-Clixml -Path $testfile - $sb = [scriptblock]::Create("Import-CliXml -Path $testfile -Verbose") + $sb = [scriptblock]::Create("Import-CliXml -Path $testfile,$testfile,$testfile,$testfile,$testfile -Verbose") Test-Stopping $sb -IntervalInMilliseconds 20 } From a81629c34e939c5d86bcce6a3c88f0c99069f350 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Tue, 25 Sep 2018 09:36:22 +0500 Subject: [PATCH 08/14] Add comment --- .../commands/utility/WebCmdlet/ConvertToJsonCommand.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs index 9e7400f2430..fbf1406c6d4 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs @@ -111,6 +111,10 @@ protected override void ProcessRecord() { if (InternalTestHooks.ActivateSleepForStoppingTest) { + // We'll wait the verbose stream marker in tests and then call Stop(). + // The cmdlet is very fast and likely to end before the test sees the marker + // so we have to slow down it by Sleep(50). + // We can not sleep for a long time because Stop() doesn't work on sleeping runspace. WriteVerbose("ConvertTo-Json started"); System.Threading.Thread.Sleep(50); } From 3e51c92413ded9c66e105052379fee4b4e97d3d7 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Wed, 26 Sep 2018 10:08:05 +0500 Subject: [PATCH 09/14] Convert test hook to int type --- .../utility/WebCmdlet/ConvertToJsonCommand.cs | 4 ++-- .../commands/utility/XmlCommands.cs | 12 ++++++------ src/System.Management.Automation/engine/Utils.cs | 2 +- test/tools/Modules/HelpersCommon/HelpersCommon.psm1 | 7 ++++--- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs index fbf1406c6d4..9cf6062e95d 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs @@ -109,14 +109,14 @@ protected override void BeginProcessing() /// protected override void ProcessRecord() { - if (InternalTestHooks.ActivateSleepForStoppingTest) + if (InternalTestHooks.ActivateSleepForStoppingTest != 0) { // We'll wait the verbose stream marker in tests and then call Stop(). // The cmdlet is very fast and likely to end before the test sees the marker // so we have to slow down it by Sleep(50). // We can not sleep for a long time because Stop() doesn't work on sleeping runspace. WriteVerbose("ConvertTo-Json started"); - System.Threading.Thread.Sleep(50); + System.Threading.Thread.Sleep(InternalTestHooks.ActivateSleepForStoppingTest); } if (InputObject != null) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs index ac5342224c9..8f467b024c7 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs @@ -131,10 +131,10 @@ protected override void ProcessRecord() { - if (InternalTestHooks.ActivateSleepForStoppingTest) + if (InternalTestHooks.ActivateSleepForStoppingTest != 0) { WriteVerbose("Export-Clixml started"); - System.Threading.Thread.Sleep(50); + System.Threading.Thread.Sleep(InternalTestHooks.ActivateSleepForStoppingTest); } if (_serializer != null) @@ -340,10 +340,10 @@ protected override void ProcessRecord() { foreach (string path in Path) { - if (InternalTestHooks.ActivateSleepForStoppingTest) + if (InternalTestHooks.ActivateSleepForStoppingTest != 0) { WriteVerbose("Import-Clixml started"); - System.Threading.Thread.Sleep(50); + System.Threading.Thread.Sleep(InternalTestHooks.ActivateSleepForStoppingTest); } _helper = new ImportXmlHelper(path, this, _isLiteralPath); @@ -435,10 +435,10 @@ protected override void BeginProcessing() /// protected override void ProcessRecord() { - if (InternalTestHooks.ActivateSleepForStoppingTest) + if (InternalTestHooks.ActivateSleepForStoppingTest != 0) { WriteVerbose("ConvertTo-Xml started"); - System.Threading.Thread.Sleep(50); + System.Threading.Thread.Sleep(InternalTestHooks.ActivateSleepForStoppingTest); } if (As.Equals("Stream", StringComparison.OrdinalIgnoreCase)) diff --git a/src/System.Management.Automation/engine/Utils.cs b/src/System.Management.Automation/engine/Utils.cs index 7a47049ff63..9f0a44680c1 100644 --- a/src/System.Management.Automation/engine/Utils.cs +++ b/src/System.Management.Automation/engine/Utils.cs @@ -1397,7 +1397,7 @@ public static class InternalTestHooks internal static bool ShowMarkdownOutputBypass; // Slow down a cmdlet to predictably check the cmdlet stopping (that 'PowerShell.Stop()' or 'Ctrl-C' work). - internal static bool ActivateSleepForStoppingTest; + internal static int ActivateSleepForStoppingTest; /// This member is used for internal test purposes. public static void SetTestHook(string property, object value) diff --git a/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 b/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 index d997492b749..1dd3f61720f 100644 --- a/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 +++ b/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 @@ -313,6 +313,7 @@ function Start-NativeExecution } finally { $script:ErrorActionPreference = $backupEAP } + # Test if the scriptblock (cmdlet) can be stopped function Test-Stopping { @@ -320,13 +321,13 @@ function Test-Stopping param ( [ScriptBlock]$sb, [int]$TimeoutInMilliseconds = 10000, - [int]$IntervalInMilliseconds = 1000 + [int]$IntervalInMilliseconds = 100 ) try { $ps = [PowerShell]::Create() $null = $ps.AddScript($sb) - Enable-Testhook 'ActivateSleepForStoppingTest' + ${Script:TesthookType}::SetTestHook('ActivateSleepForStoppingTest', 50) $null = $ps.BeginInvoke() Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 } -TimeoutInMilliseconds $TimeoutInMilliseconds -IntervalInMilliseconds $IntervalInMilliseconds $null = $ps.BeginStop($null, $null) @@ -336,7 +337,7 @@ function Test-Stopping } finally { $ps.Dispose() - Disable-Testhook 'ActivateSleepForStoppingTest' + ${Script:TesthookType}::SetTestHook('ActivateSleepForStoppingTest', 0) } } From 80c742a73d7e976ec59e61340700d8db510d9a46 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Wed, 26 Sep 2018 10:11:44 +0500 Subject: [PATCH 10/14] Add SlowDownCmdletInMilliseconds parameter in Test-Stopping --- test/tools/Modules/HelpersCommon/HelpersCommon.psm1 | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 b/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 index 1dd3f61720f..e28882b74c2 100644 --- a/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 +++ b/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 @@ -321,13 +321,14 @@ function Test-Stopping param ( [ScriptBlock]$sb, [int]$TimeoutInMilliseconds = 10000, - [int]$IntervalInMilliseconds = 100 + [int]$IntervalInMilliseconds = 100, + [int]$SlowDownCmdletInMilliseconds = 50 ) try { $ps = [PowerShell]::Create() $null = $ps.AddScript($sb) - ${Script:TesthookType}::SetTestHook('ActivateSleepForStoppingTest', 50) + ${Script:TesthookType}::SetTestHook('ActivateSleepForStoppingTest', $SlowDownCmdletInMilliseconds) $null = $ps.BeginInvoke() Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 } -TimeoutInMilliseconds $TimeoutInMilliseconds -IntervalInMilliseconds $IntervalInMilliseconds $null = $ps.BeginStop($null, $null) From c9ee507f8f30b07d231a2c77c6d06452787ff8fb Mon Sep 17 00:00:00 2001 From: iSazonov Date: Mon, 1 Oct 2018 09:00:07 +0500 Subject: [PATCH 11/14] Fix typo --- test/tools/Modules/HelpersCommon/HelpersCommon.psm1 | 1 + 1 file changed, 1 insertion(+) diff --git a/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 b/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 index e28882b74c2..16d1e0591b4 100644 --- a/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 +++ b/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 @@ -313,6 +313,7 @@ function Start-NativeExecution } finally { $script:ErrorActionPreference = $backupEAP } +} # Test if the scriptblock (cmdlet) can be stopped function Test-Stopping From 057edb58d7d660044c74132adc2a408813372307 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Tue, 2 Oct 2018 09:11:16 +0500 Subject: [PATCH 12/14] Address feedback --- .../Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 index 5f31f964b24..11f5932064d 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 @@ -53,7 +53,7 @@ Describe "XmlCommand DRT basic functionality Tests" -Tags "CI" { } It "Export-Clixml StopProcessing should succeed" { - $sb = [scriptblock]::Create("1..10000 | Export-CliXml -Path $testfile -Verbose") + $sb = { 1..10000 | Export-CliXml -Path $testfile -Verbose } Test-Stopping $sb -IntervalInMilliseconds 50 } From 7507fd7cf03e57e48664a44414ba8d6e4dfcbce5 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Wed, 3 Oct 2018 08:05:04 +0500 Subject: [PATCH 13/14] Address feedback 2 --- .../Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 index 11f5932064d..e6b5d1345ab 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 @@ -59,7 +59,7 @@ Describe "XmlCommand DRT basic functionality Tests" -Tags "CI" { It "Import-Clixml StopProcessing should succeed" { 1..10000 | Export-Clixml -Path $testfile - $sb = [scriptblock]::Create("Import-CliXml -Path $testfile,$testfile,$testfile,$testfile,$testfile -Verbose") + $sb = { Import-CliXml -Path $testfile,$testfile,$testfile,$testfile,$testfile -Verbose } Test-Stopping $sb -IntervalInMilliseconds 20 } From 3503a059e961cc65d8f7babb42651c59513be39a Mon Sep 17 00:00:00 2001 From: iSazonov Date: Wed, 3 Oct 2018 19:31:19 +0500 Subject: [PATCH 14/14] Enlarge test file --- .../Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 index e6b5d1345ab..d9b1e3ae86d 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 @@ -53,12 +53,12 @@ Describe "XmlCommand DRT basic functionality Tests" -Tags "CI" { } It "Export-Clixml StopProcessing should succeed" { - $sb = { 1..10000 | Export-CliXml -Path $testfile -Verbose } + $sb = { 1..20000 | Export-CliXml -Path $testfile -Verbose } Test-Stopping $sb -IntervalInMilliseconds 50 } It "Import-Clixml StopProcessing should succeed" { - 1..10000 | Export-Clixml -Path $testfile + 1..20000 | Export-Clixml -Path $testfile $sb = { Import-CliXml -Path $testfile,$testfile,$testfile,$testfile,$testfile -Verbose } Test-Stopping $sb -IntervalInMilliseconds 20 }