File size: 3,037 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | # Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe 'PipelineStopToken tests' -Tags 'CI' {
BeforeAll {
Function Invoke-WithStop {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[ScriptBlock]
$ScriptBlock,
[Parameter(ValueFromRemainingArguments)]
[object[]]
$ArgumentList
)
$ps = [PowerShell]::Create()
$null = $ps.AddScript("'start'`n" + $ScriptBlock.ToString())
foreach ($arg in $ArgumentList) {
$null = $ps.AddArgument($arg)
}
$inPipe = [System.Management.Automation.PSDataCollection[object]]::new()
$inPipe.Complete()
$outPipe = [System.Management.Automation.PSDataCollection[object]]::new()
# Use an event to make sure Stop is called once the pipeline has started
# and not before.
$eventId = [Guid]::NewGuid().ToString()
Register-ObjectEvent -InputObject $outPipe -EventName DataAdded -SourceIdentifier $eventId
try {
$task = $ps.BeginInvoke($inPipe, $outPipe)
Wait-Event -SourceIdentifier $eventId | Remove-Event
}
finally {
Remove-Event -SourceIdentifier $eventId -ErrorAction SilentlyContinue
Unregister-Event -SourceIdentifier $eventId
}
$ps.Stop()
$ps.Streams.Error | Write-Error
{ $ps.EndInvoke($task) } | Should -Throw -ErrorId PipelineStoppedException
}
}
It 'Signal advanced function to stop' {
$start = Get-Date
Invoke-WithStop -ScriptBlock {
Function Test-FunctionWithStop {
[CmdletBinding()]
param ([Parameter()][int]$Timeout)
[System.Threading.Tasks.Task]::Delay($Timeout * 1000, $PSCmdlet.PipelineStopToken).GetAwaiter().GetResult()
}
Test-FunctionWithStop -Timeout 10
}
$end = (Get-Date) - $start
$end.TotalSeconds | Should -BeLessThan 10
}
It 'Signals compiled cmdlet to stop' {
$binaryAssembly = Add-Type @'
using System;
using System.Management.Automation;
using System.Threading.Tasks;
namespace PipelineStoppedToken.Tests;
[Cmdlet(VerbsDiagnostic.Test, "CmdletWithStop")]
public sealed class TestCmdletWithStop : Cmdlet
{
[Parameter]
public int Timeout { get; set; }
protected override void EndProcessing()
{
Task.Delay(Timeout * 1000, PipelineStopToken).GetAwaiter().GetResult();
}
}
'@ -PassThru | ForEach-Object Assembly | Select-Object -First 1
$start = Get-Date
Invoke-WithStop -ScriptBlock {
Import-Module -Assembly $args[0]
Test-CmdletWithStop -Timeout 10
} -ArgumentList $binaryAssembly
$end = (Get-Date) - $start
$end.TotalSeconds | Should -BeLessThan 10
}
}
|