pulumi/sdk/dotnet/Pulumi.Tests/PulumiTest.cs
Pat Gavlin bd18384038
Await outstanding async work in .NET. (#6993)
The Pulumi .NET SDK does not currently await all outstanding asynchronous
work associated with a Pulumi program. Because all relevant asynchronous
work is created via the Pulumi SDK, we can track this asynchronous work
and ensure that it has all completed prior to returning from
`Deployment.RunAsync`.

The implementation here is simpler than that in #6983, and re-uses the
existing support for tracking outstanding RPCs. If this proves to
negatively impact performance (which is a very real possibility for
programs that create many `Output` instances), we can simplify this
using a semaphore and a counter (essentially Go's `sync.WaitGroup`).

This fixes the .NET portion of #3991.
2021-05-12 13:23:47 -07:00

44 lines
1.3 KiB
C#

// Copyright 2016-2019, Pulumi Corporation
using System;
using System.Threading.Tasks;
using Moq;
namespace Pulumi.Tests
{
public abstract class PulumiTest
{
private static Task Run(Action action, bool dryRun)
=> Run(() =>
{
action();
return Task.CompletedTask;
}, dryRun);
private static async Task Run(Func<Task> func, bool dryRun)
{
var runner = new Mock<IRunner>(MockBehavior.Strict);
runner.Setup(r => r.RegisterTask(It.IsAny<string>(), It.IsAny<Task>()));
var mock = new Mock<IDeploymentInternal>(MockBehavior.Strict);
mock.Setup(d => d.IsDryRun).Returns(dryRun);
mock.Setup(d => d.Runner).Returns(runner.Object);
Deployment.Instance = new DeploymentInstance(mock.Object);
await func().ConfigureAwait(false);
}
protected static Task RunInPreview(Action action)
=> Run(action, dryRun: true);
protected static Task RunInNormal(Action action)
=> Run(action, dryRun: false);
protected static Task RunInPreview(Func<Task> func)
=> Run(func, dryRun: true);
protected static Task RunInNormal(Func<Task> func)
=> Run(func, dryRun: false);
}
}