-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
await background tasks in inline deployments (#420)
Adds an await for inline deployments @Frassle As dicussed over slack, new pr since I used the wrong base branch.
- Loading branch information
Showing
7 changed files
with
120 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
component: sdk | ||
kind: bug-fixes | ||
body: Await background tasks during inline deployment | ||
time: 2024-12-04T15:32:00.3901679+01:00 | ||
custom: | ||
PR: "420" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Threading.Tasks; | ||
using Pulumi.Testing; | ||
using Xunit; | ||
|
||
namespace Pulumi.Tests; | ||
|
||
public class InlineDeploymentTests | ||
{ | ||
[Fact] | ||
public async Task InlineDeploymentAwaitsTasks() | ||
{ | ||
var mocks = new AwaitingMocks(); | ||
var monitor = new MockMonitor(mocks); | ||
var res = TryInline<Component>(monitor, async () => | ||
{ | ||
var stack = new Stack(); | ||
await Task.Delay(1); | ||
return new Component("test", null, new ComponentResourceOptions | ||
{ | ||
Parent = stack | ||
}); | ||
}); | ||
|
||
await Task.WhenAny(res, Task.Delay(TimeSpan.FromSeconds(1))); | ||
Assert.False(res.IsCompleted); | ||
mocks.Complete(); | ||
await res; | ||
Assert.Equal(4, monitor.Resources.Count); | ||
} | ||
|
||
internal static async Task<T> TryInline<T>(IMonitor monitor, Func<Task<T>> runAsync) | ||
{ | ||
var engine = new MockEngine(); | ||
|
||
var deploymentBuilder = new MockDeploymentBuilder(engine, monitor); | ||
|
||
var inlineDeploymentSettings = new InlineDeploymentSettings(null, "1", "1", new Dictionary<string, string>(), | ||
new List<string>(), "organization", "project", "stack", 0, false); | ||
|
||
return await Deployment.RunInlineAsyncWithResult(deploymentBuilder, inlineDeploymentSettings, runAsync) | ||
.ConfigureAwait(false); | ||
} | ||
|
||
internal class Component : ComponentResource | ||
{ | ||
public Component(string name, ResourceArgs? args, ComponentResourceOptions? options = null, bool remote = false) | ||
: base("test:res:a", name, args, options, remote) | ||
{ | ||
new ComponentResource("test:res:b", "testB", new ComponentResourceOptions | ||
{ | ||
Parent = this | ||
}); | ||
new ComponentResource("test:res:b", "testC", new ComponentResourceOptions | ||
{ | ||
Parent = this | ||
}); | ||
} | ||
} | ||
|
||
internal class AwaitingMocks : IMocks | ||
{ | ||
private readonly TaskCompletionSource _taskCompletionSource = new(); | ||
|
||
private Task Task => _taskCompletionSource.Task; | ||
|
||
public void Complete() | ||
{ | ||
_taskCompletionSource.TrySetResult(); | ||
} | ||
|
||
public async Task<(string? id, object state)> NewResourceAsync(MockResourceArgs args) | ||
{ | ||
if (args.Type?.Equals("test:res:b") ?? false) | ||
{ | ||
await Task; | ||
} | ||
|
||
string? id = args.Name; | ||
return (id, ImmutableDictionary.Create<string, string>()); | ||
} | ||
|
||
public Task<object> CallAsync(MockCallArgs args) | ||
{ | ||
return Task.FromResult(new object()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters