Skip to content

Add 5 'InvokeAsync' overloads and 'StopAsync' to the 'PowerShell' type - #8056

Merged
Dongbo Wang (daxian-dbw) merged 38 commits into
PowerShell:masterfrom
KirkMunro:powershell-invokeasync-methods
Mar 6, 2019
Merged

Dongbo Wang (daxian-dbw) merged 38 commits into
PowerShell:masterfrom
KirkMunro:powershell-invokeasync-methods

Conversation

@KirkMunro

@KirkMunro Kirk Munro (KirkMunro) commented Oct 17, 2018 •

Copy link
Copy Markdown
Contributor

PR Summary

It is much easier to deal with asynchronous invocation of PowerShell using async and await, but the S.M.A.PowerShell class only exposes BeginInvoke and EndInvoke methods for async support. This PR adds 5 InvokeAsync overloads (one for each BeginInvoke method) to S.M.A.PowerShell to facilitate async invocation of PowerShell commands from .NET.

PR Checklist

@KirkMunro

Copy link
Copy Markdown
Contributor Author

Note that I have not added unit tests yet for this PR. I looked to see where BeginInvoke and EndInvoke were tested, and they are included as part of a Pester test; however, PowerShell does not support async/await, so Pester does not appear to be an appropriate test framework for these changes. Please identify how these new methods should be automatically tested. I haven't added C# unit tests to this repo yet, so I'm not sure how I should proceed. Thank you.

@iSazonov Ilya (iSazonov) added the Review - Committee The PR/Issue needs a review from the PowerShell Committee label Oct 17, 2018
@iSazonov

Copy link
Copy Markdown
Collaborator

Kirk Munro (@KirkMunro) Have you any gists with demo? It would help to document the enhancement.

@KirkMunro

Copy link
Copy Markdown
Contributor Author

I don't have examples showing how complicated this can be using BeginInvoke/EndInvoke, because I just went straight to defining these locally in projects where I have used async PowerShell invocation since they are just so much simpler, so I don't have a before/after example to show for comparison. Also, the code is so short, I'll just share it here instead of in a gist. Here's a snippet showing what use of this looks like when you want async invocation of PowerShell from a .NET assembly with these additional methods in place:

using (var ps = PowerShell.Create())
{
    ps.AddCommand("Connect-AzureRmAccount")
      .AddParameter("ServicePrincipal")
      .AddParameter("TenantId", tenantName)
      .AddParameter("Subscription", subscriptionId)
      .AddParameter("Credential", credential);

    var results = await ps.InvokeAsync();
    // Process results
}

@adityapatwardhan

Copy link
Copy Markdown
Member

@SteveL-MSFT

Copy link
Copy Markdown
Member

@PowerShell/powershell-committee agree that we should expose a task model for async PowerShell, leaving implementation details to PR review

@SteveL-MSFT Steve Lee (SteveL-MSFT) added Committee-Reviewed PS-Committee has reviewed this and made a decision and removed Review - Committee The PR/Issue needs a review from the PowerShell Committee labels Oct 24, 2018
@daxian-dbw

Copy link
Copy Markdown
Member

Bruce Payette (@BrucePay) Can you please review the signature of the proposed task-based async APIs?

In committee's discussion on this, there are proposals that we might want to re-implement the async invocation in the task-based model instead of wrapping the Begin/End Invoke. But as long as the API signatures are reviewed and approved, I guess we can go with wrapping the existing async APIs for now, and replace the implementation of those task-based APIs at later time, if we want to.

@KirkMunro

Copy link
Copy Markdown
Contributor Author

Regarding c# unit tests, I didn't get to them before going on vacation this week, so I'll come back to this PR next week and catch up.

@iSazonov

Copy link
Copy Markdown
Collaborator

@powershell-maintainers The test/csharp folder is targeted as Linux-only tests. Suggestion is to put new tests in test/xUnit/System.Management.Automation. Sample https://github.com/iSazonov/PowerShell/tree/add-unicode3/test/xUnit/System.Management.Automation

@KirkMunro

Copy link
Copy Markdown
Contributor Author

Ilya (@iSazonov) I'll move the tests over. Thanks for letting me know the correct path for those tests.

@KirkMunro

Copy link
Copy Markdown
Contributor Author

Wait. Ilya (@iSazonov), that path doesn't exist in master, so this is a new process then? How are tests under that path executed locally for verification?

@iSazonov

Ilya (iSazonov) commented Nov 5, 2018 •

Copy link
Copy Markdown
Collaborator

Kirk Munro (@KirkMunro) We haven't such tests at all. And my question is for MSFT team where we should place these tests. I point my new unmerged code which have engine API xUnit tests too. I look how CoreFX places API tests and do the same.

@KirkMunro

Copy link
Copy Markdown
Contributor Author

Ilya (@iSazonov) Ok, thanks. I'll wait until the other maintainers discuss with you what should be done with these then, and follow the process accordingly.

@stale

stale Bot commented Dec 15, 2018

Copy link
Copy Markdown

This PR has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed if no further activity occurs within 10 days.
Thank you for your contributions.
Community members are welcome to grab these works.

@stale stale Bot added the Stale label Dec 15, 2018
@vexx32

Copy link
Copy Markdown
Collaborator

Kirk Munro (@KirkMunro) Looks like Ilya (@iSazonov) has created a place for the xUnit tests in #8356 😄

@stale stale Bot removed the Stale label Dec 16, 2018
@iSazonov

Copy link
Copy Markdown
Collaborator

Yes, binary tests was moved to new folder. Now all tests run parallel (excluding macOS).

Comment thread test/xUnit/csharp/test_PowerShell.cs Resolved
Comment thread test/xUnit/csharp/test_PowerShell.cs Resolved
Comment thread test/xUnit/csharp/test_PowerShell.cs Resolved
Comment thread test/xUnit/csharp/test_PowerShell.cs Resolved
Comment thread test/xUnit/csharp/test_PowerShell.cs Resolved
Comment thread test/xUnit/csharp/test_PowerShell.cs Resolved
@daxian-dbw Dongbo Wang (daxian-dbw) added the CL-Engine Indicates that a PR should be marked as an engine change in the Change Log label Mar 2, 2019
@daxian-dbw Dongbo Wang (daxian-dbw) changed the title WIP: Add 5 InvokeAsync overloads to S.M.A.PowerShell Add 5 'InvokeAsync' overloads and 'StopAsync' to the 'PowerShell' type Mar 2, 2019

@daxian-dbw Dongbo Wang (daxian-dbw) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You xUnit tests were failing in CIs. Please fix them. I'm also fine that you remove the xUnit tests for this PR.

@KirkMunro

Copy link
Copy Markdown
Contributor Author

You xUnit tests were failing in CIs. Please fix them. I'm also fine that you remove the xUnit tests for this PR.

I removed them since they were just duplicates of the Pester tests, and I'm not set up at the moment to easily test C# Unit tests (hence the issues with the CI not working).

@KirkMunro

Copy link
Copy Markdown
Contributor Author

Pending any further feedback, I think this PR is good to go with enough tests to cover the new Async APIs well enough.

@daxian-dbw Dongbo Wang (daxian-dbw) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@daxian-dbw
Dongbo Wang (daxian-dbw) merged commit 45aba2a into PowerShell:master Mar 6, 2019
@KirkMunro
Kirk Munro (KirkMunro) deleted the powershell-invokeasync-methods branch March 6, 2019 18:10
@iSazonov

Copy link
Copy Markdown
Collaborator

Kirk Munro (@KirkMunro) Thanks for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-Engine Indicates that a PR should be marked as an engine change in the Change Log Committee-Reviewed PS-Committee has reviewed this and made a decision

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants