Skip to content

Commit da92401

Browse files
authored
Fix async test bug in StateIsPersistent (#108)
1 parent 45d2ba4 commit da92401

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

sdk/Pulumi.Tests/Provider/ProviderTests.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,12 @@ public async Task StateIsPersistent()
5959

6060
var cts = new System.Threading.CancellationTokenSource();
6161

62-
// Hook stdout so we can see what port Serve chooses
63-
var stringWriter = new System.IO.StringWriter();
64-
System.Console.SetOut(stringWriter);
65-
66-
var server = Pulumi.Experimental.Provider.Provider.Serve(args, "1.0", _ => new TestConfigureProvider(), cts.Token);
62+
// Custom stdout so we can see what port Serve chooses
63+
var stdout = new System.IO.StringWriter();
64+
var server = Pulumi.Experimental.Provider.Provider.Serve(args, "1.0", _ => new TestConfigureProvider(), cts.Token, stdout);
6765

6866
// Grab the port from stdout and create a connection to it
69-
var port = int.Parse(stringWriter.ToString().Trim());
70-
67+
var port = int.Parse(stdout.ToString().Trim());
7168

7269
// Inititialize the engine channel once for this address
7370
var channel = GrpcChannel.ForAddress(new Uri($"http://localhost:{port}"), new GrpcChannelOptions

sdk/Pulumi/Provider/Provider.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,12 @@ public virtual Task Delete(DeleteRequest request, CancellationToken ct)
325325
throw new NotImplementedException();
326326
}
327327

328-
public static async Task Serve(string[] args, string? version, Func<IHost, Provider> factory, System.Threading.CancellationToken cancellationToken)
328+
public static Task Serve(string[] args, string? version, Func<IHost, Provider> factory, System.Threading.CancellationToken cancellationToken)
329+
{
330+
return Serve(args, version, factory, cancellationToken, System.Console.Out);
331+
}
332+
333+
public static async Task Serve(string[] args, string? version, Func<IHost, Provider> factory, System.Threading.CancellationToken cancellationToken, System.IO.TextWriter stdout)
329334
{
330335
// maxRpcMessageSize raises the gRPC Max message size from `4194304` (4mb) to `419430400` (400mb)
331336
var maxRpcMessageSize = 400 * 1024 * 1024;
@@ -412,7 +417,7 @@ public static async Task Serve(string[] args, string? version, Func<IHost, Provi
412417
// Explicitly write just the number and "\n". WriteLine would write "\r\n" on Windows, and while
413418
// the engine has now been fixed to handle that (see https://github.com/pulumi/pulumi/pull/11915)
414419
// we work around this here so that old engines can use dotnet providers as well.
415-
System.Console.Write(port.ToString() + "\n");
420+
stdout.Write(port.ToString() + "\n");
416421

417422
await host.WaitForShutdownAsync(cancellationToken);
418423

@@ -513,7 +518,7 @@ private CancellationTokenSource GetToken(ServerCallContext context)
513518
}
514519

515520
// Helper to deal with the fact that at the GRPC layer any Struct property might be null. For those we just want to return empty dictionaries at this level.
516-
// This keeps the PropertyValue.Marshal clean in terms of not handling nulls.
521+
// This keeps the PropertyValue.Marshal clean in terms of not handling nulls.
517522
private ImmutableDictionary<string, PropertyValue> Marshal(Struct? properties)
518523
{
519524
if (properties == null)

0 commit comments

Comments
 (0)