Skip to content

Is there any way to use RunspacePools such that Runspace state is reset between uses? #7131

@alx9r

Description

@alx9r

I'm experimenting with parallelizing unit tests. I'd like to be able to run each test in a clean runspace so that variables set during one test don't affect other tests.

Steps to reproduce

$runspacePool = [runspacefactory]::CreateRunspacePool() 
$runspacePool.Open()

$scriptblock = { $a; $a = 1 }

'first'
$powershell1 = [powershell]::Create().AddScript($scriptblock)
$powershell1.RunspacePool = $runspacePool
$powershell1.Invoke()

'second'
$powershell2 = [powershell]::Create().AddScript($scriptblock)
$powershell2.RunspacePool = $runspacePool
$powershell2.Invoke()

Expected behavior

I expected the PowerShell/RunspacePool objects to ensure that the Runspace used on each call to .Invoke() was reset prior to use.

first
second

Actual behavior

The variable set by the first PowerShell object is available to the second PowerShell object.

first
second
1

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.1.0-preview.688
PSEdition                      Core
GitCommitId                    v6.1.0-preview.688
OS                             Microsoft Windows 6.3.9600
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions