diff --git a/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/CollectionRuleOptionsTests.cs b/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/CollectionRuleOptionsTests.cs index 222731d31bb..c66ff038820 100644 --- a/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/CollectionRuleOptionsTests.cs +++ b/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/CollectionRuleOptionsTests.cs @@ -7,9 +7,7 @@ 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; using System; using System.Collections.Generic; @@ -720,48 +718,14 @@ public Task CollectionRuleOptions_ExecuteAction_PropertyValidation() }); } - private async Task Validate( + private Task Validate( Action setup, Action> validate) { - IHost host = new HostBuilder() - .ConfigureAppConfiguration(builder => - { - RootOptions options = new(); - setup(options); - - IDictionary 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>()); - } - finally - { - if (host is IAsyncDisposable asyncDisposable) - { - await asyncDisposable.DisposeAsync(); - } - else - { - host.Dispose(); - } - } + return TestHostHelper.CreateCollectionRulesHost( + _outputHelper, + setup, + host => validate(host.Services.GetRequiredService>())); } private Task ValidateSuccess( diff --git a/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/TestHostHelper.cs b/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/TestHostHelper.cs new file mode 100644 index 00000000000..e8d3056471e --- /dev/null +++ b/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/TestHostHelper.cs @@ -0,0 +1,62 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Diagnostics.Tools.Monitor; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using System; +using System.Collections.Generic; +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 setup, + Action callback) + { + IHost host = new HostBuilder() + .ConfigureAppConfiguration(builder => + { + RootOptions options = new(); + setup(options); + + IDictionary 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(); + } + } + } + } +}