Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor host building into helper method #769

Merged
merged 2 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Microsoft.Diagnostics.Tools.Monitor.CollectionRules.Options;
using Microsoft.Diagnostics.Tools.Monitor.CollectionRules.Options.Actions;
using Microsoft.Diagnostics.Tools.Monitor.CollectionRules.Options.Triggers;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -720,48 +719,14 @@ public Task CollectionRuleOptions_ExecuteAction_PropertyValidation()
});
}

private async Task Validate(
private Task Validate(
Action<RootOptions> setup,
Action<IOptionsMonitor<CollectionRuleOptions>> validate)
{
IHost host = new HostBuilder()
.ConfigureAppConfiguration(builder =>
{
RootOptions options = new();
setup(options);

IDictionary<string, string> configurationValues = options.ToConfigurationValues();
_outputHelper.WriteLine("Begin Configuration:");
foreach ((string key, string value) in configurationValues)
{
_outputHelper.WriteLine("{0} = {1}", key, value);
}
_outputHelper.WriteLine("End Configuration");

builder.AddInMemoryCollection(configurationValues);
})
.ConfigureServices(services =>
{
services.ConfigureCollectionRules();
services.ConfigureEgress();
})
.Build();

try
{
validate(host.Services.GetRequiredService<IOptionsMonitor<CollectionRuleOptions>>());
}
finally
{
if (host is IAsyncDisposable asyncDisposable)
{
await asyncDisposable.DisposeAsync();
}
else
{
host.Dispose();
}
}
return TestHostHelper.CreateCollectionRulesHost(
_outputHelper,
setup,
host => validate(host.Services.GetRequiredService<IOptionsMonitor<CollectionRuleOptions>>()));
}

private Task ValidateSuccess(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Microsoft.Diagnostics.Tools.Monitor;
jander-msft marked this conversation as resolved.
Show resolved Hide resolved
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Xunit.Abstractions;

namespace Microsoft.Diagnostics.Monitoring.Tool.UnitTests
{
internal static class TestHostHelper
{
public static async Task CreateCollectionRulesHost(
ITestOutputHelper outputHelper,
Action<RootOptions> setup,
Action<IHost> callback)
{
IHost host = new HostBuilder()
.ConfigureAppConfiguration(builder =>
jander-msft marked this conversation as resolved.
Show resolved Hide resolved
{
RootOptions options = new();
setup(options);

IDictionary<string, string> configurationValues = options.ToConfigurationValues();
outputHelper.WriteLine("Begin Configuration:");
foreach ((string key, string value) in configurationValues)
{
outputHelper.WriteLine("{0} = {1}", key, value);
}
outputHelper.WriteLine("End Configuration");

builder.AddInMemoryCollection(configurationValues);
})
.ConfigureServices(services =>
{
services.ConfigureCollectionRules();
services.ConfigureEgress();
})
.Build();

try
{
callback(host);
}
finally
{
if (host is IAsyncDisposable asyncDisposable)
{
await asyncDisposable.DisposeAsync();
}
else
{
host.Dispose();
}
}
}
}
}