Skip to content

Commit

Permalink
resource filtering is async all the way down
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Oct 29, 2024
1 parent 4b8397c commit 1ab93c1
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 34 deletions.
6 changes: 3 additions & 3 deletions src/CommandLineTests/Resources/ResourceCommandContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal Task<IHost> buildHost()
return Host.CreateDefaultBuilder().ConfigureServices(CopyResources).StartAsync();
}

internal IList<IStatefulResource> applyTheResourceFiltering()
internal Task<IList<IStatefulResource>> applyTheResourceFiltering()
{
theInput.HostBuilder = Host.CreateDefaultBuilder().ConfigureServices(CopyResources);
var command = new ResourcesCommand();
Expand Down Expand Up @@ -106,9 +106,9 @@ public IStatefulResource Add(string name, string type = "Resource")
return resource;
}

public IReadOnlyList<IStatefulResource> FindResources()
ValueTask<IReadOnlyList<IStatefulResource>> IStatefulResourceSource.FindResources()
{
return _resources;
return new ValueTask<IReadOnlyList<IStatefulResource>>(_resources);
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/CommandLineTests/Resources/resource_filtering.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace CommandLineTests.Resources
public class resource_filtering : ResourceCommandContext
{
[Fact]
public void uses_resource_source()
public async Task uses_resource_source()
{
var blue = AddResource("blue", "color");
var red = AddResource("red", "color");
Expand All @@ -22,7 +22,7 @@ public void uses_resource_source()
col.Add("white", "color");
});

var resources = applyTheResourceFiltering();
var resources = await applyTheResourceFiltering();

var colors = resources.Select(x => x.Name).OrderBy(x => x)
.ToList();
Expand All @@ -31,15 +31,15 @@ public void uses_resource_source()
}

[Fact]
public void no_filtering()
public async Task no_filtering()
{
var blue = AddResource("blue", "color");
var red = AddResource("red", "color");

var tx = AddResource("tx", "state");
var ar = AddResource("ar", "state");

var resources = applyTheResourceFiltering();
var resources = await applyTheResourceFiltering();

resources.Count.ShouldBe(4);

Expand All @@ -50,7 +50,7 @@ public void no_filtering()
}

[Fact]
public void filter_by_name()
public async Task filter_by_name()
{
var blue = AddResource("blue", "color");
var red = AddResource("red", "color");
Expand All @@ -60,13 +60,13 @@ public void filter_by_name()

theInput.NameFlag = "tx";

var resources = applyTheResourceFiltering();
var resources = await applyTheResourceFiltering();
resources.Single()
.ShouldBe(tx);
}

[Fact]
public void filter_by_type()
public async Task filter_by_type()
{
var blue = AddResource("blue", "color");
var red = AddResource("red", "color");
Expand All @@ -77,7 +77,7 @@ public void filter_by_type()
var mo = AddResource("mo", "state");

theInput.TypeFlag = "color";
var resources = applyTheResourceFiltering();
var resources = await applyTheResourceFiltering();

resources.Count.ShouldBe(3);
resources.ShouldContain(blue);
Expand Down
4 changes: 2 additions & 2 deletions src/CommandLineTests/Resources/resource_ordering.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace CommandLineTests.Resources;
public class resource_ordering : ResourceCommandContext
{
[Fact]
public void respect_ordering_by_dependencies()
public async Task respect_ordering_by_dependencies()
{
var one = AddResourceWithDependencies("one", "system", "blue", "red");
var two = AddResourceWithDependencies("two", "system", "blue", "red", "one");
Expand All @@ -16,7 +16,7 @@ public void respect_ordering_by_dependencies()
var tx = AddResource("tx", "state");
var ar = AddResource("ar", "state");

var resources = applyTheResourceFiltering();
var resources = await applyTheResourceFiltering();

resources.Count.ShouldBe(6);

Expand Down
32 changes: 20 additions & 12 deletions src/JasperFx/Environment/EnvironmentChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static async Task<EnvironmentCheckResults> ExecuteAllEnvironmentChecks(IS
{
var results = new EnvironmentCheckResults();

var checks = services.discoverChecks().ToArray();
var checks = await services.DiscoverChecks();
if (!checks.Any())
{
AnsiConsole.WriteLine("No environment checks.");
Expand All @@ -25,10 +25,10 @@ await AnsiConsole.Progress().StartAsync(async c =>
{
var task = c.AddTask("[bold]Running Environment Checks[/]", new ProgressTaskSettings
{
MaxValue = checks.Length
MaxValue = checks.Count
});

for (var i = 0; i < checks.Length; i++)
for (var i = 0; i < checks.Count; i++)
{
var check = checks[i];

Expand Down Expand Up @@ -61,19 +61,27 @@ await AnsiConsole.Progress().StartAsync(async c =>
return results;
}

private static IEnumerable<IEnvironmentCheck> discoverChecks(this IServiceProvider services)
// TODO -- get a unit test on this
public static async Task<IList<IEnvironmentCheck>> DiscoverChecks(this IServiceProvider services)
{
foreach (var check in services.GetServices<IEnvironmentCheck>()) yield return check;
var list = new List<IEnvironmentCheck>();
list.AddRange(services.GetServices<IEnvironmentCheck>());

foreach (var factory in services.GetServices<IEnvironmentCheckFactory>())
foreach (var check in factory.Build())
yield return check;

foreach (var resource in services.GetServices<IStatefulResource>())
yield return new ResourceEnvironmentCheck(resource);
{
list.AddRange(factory.Build());
}

list.AddRange(services.GetServices<IStatefulResource>().Select(x => new ResourceEnvironmentCheck(x)));

foreach (var source in services.GetServices<IStatefulResourceSource>())
foreach (var resource in source.FindResources())
yield return new ResourceEnvironmentCheck(resource);
{
foreach (var resource in await source.FindResources())
{
list.Add(new ResourceEnvironmentCheck(resource));
}
}

return list;
}
}
2 changes: 1 addition & 1 deletion src/JasperFx/Resources/IStatefulResourceSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace JasperFx.Resources;
/// </summary>
public interface IStatefulResourceSource
{
IReadOnlyList<IStatefulResource> FindResources();
ValueTask<IReadOnlyList<IStatefulResource>> FindResources();
}

#endregion
6 changes: 3 additions & 3 deletions src/JasperFx/Resources/ResourceHostExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static IHostBuilder UseResourceSetupOnStartupInDevelopment(this IHostBuil
public static async Task ResetResourceState(this IHost host, CancellationToken cancellation = default,
string resourceType = null, string resourceName = null)
{
var resources = ResourcesCommand.FindResources(host.Services, resourceType, resourceName);
var resources = await ResourcesCommand.FindResources(host.Services, resourceType, resourceName);
foreach (var resource in resources)
{
await resource.Setup(cancellation);
Expand All @@ -105,7 +105,7 @@ public static async Task ResetResourceState(this IHost host, CancellationToken c
public static async Task SetupResources(this IHost host, CancellationToken cancellation = default,
string resourceType = null, string resourceName = null)
{
var resources = ResourcesCommand.FindResources(host.Services, resourceType, resourceName);
var resources = await ResourcesCommand.FindResources(host.Services, resourceType, resourceName);
foreach (var resource in resources) await resource.Setup(cancellation);
}

Expand All @@ -119,7 +119,7 @@ public static async Task SetupResources(this IHost host, CancellationToken cance
public static async Task TeardownResources(this IHost host, CancellationToken cancellation = default,
string resourceType = null, string resourceName = null)
{
var resources = ResourcesCommand.FindResources(host.Services, resourceType, resourceName);
var resources = await ResourcesCommand.FindResources(host.Services, resourceType, resourceName);
foreach (var resource in resources) await resource.Teardown(cancellation);
}
}
2 changes: 1 addition & 1 deletion src/JasperFx/Resources/ResourceSetupHostService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task StartAsync(CancellationToken cancellationToken)
{
try
{
resources.AddRange(source.FindResources());
resources.AddRange(await source.FindResources());
}
catch (Exception e)
{
Expand Down
8 changes: 4 additions & 4 deletions src/JasperFx/Resources/ResourcesCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public override async Task<bool> Execute(ResourceInput input)

var cancellation = input.TokenSource.Token;
using var host = input.BuildHost();
var resources = FindResources(input, host);
var resources = await FindResources(input, host);

if (!resources.Any())
{
Expand Down Expand Up @@ -86,7 +86,7 @@ private bool listAll(IList<IStatefulResource> statefulResources)
return true;
}

public IList<IStatefulResource> FindResources(ResourceInput input, IHost host)
public Task<IList<IStatefulResource>> FindResources(ResourceInput input, IHost host)
{
return FindResources(host.Services, input.TypeFlag, input.NameFlag);
}
Expand Down Expand Up @@ -159,13 +159,13 @@ await AnsiConsole.Progress().StartAsync(async c =>
return !exceptions.Any();
}

internal static IList<IStatefulResource> FindResources(IServiceProvider services, string typeName,
internal static async Task<IList<IStatefulResource>> FindResources(IServiceProvider services, string typeName,
string resourceName)
{
var list = services.GetServices<IStatefulResource>().ToList();
foreach (var source in services.GetServices<IStatefulResourceSource>())
{
var sources = source.FindResources();
var sources = await source.FindResources();
list.AddRange(sources);
}

Expand Down

0 comments on commit 1ab93c1

Please sign in to comment.