From 7420d0d373219c00455375803ace77cf90390bdc Mon Sep 17 00:00:00 2001 From: Justin Anderson Date: Wed, 25 Aug 2021 12:33:33 -0700 Subject: [PATCH 1/2] Refactor host building into helper method. --- .../CollectionRuleOptionsTests.cs | 45 ++------------ .../TestHostHelper.cs | 59 +++++++++++++++++++ 2 files changed, 64 insertions(+), 40 deletions(-) create mode 100644 src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/TestHostHelper.cs diff --git a/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/CollectionRuleOptionsTests.cs b/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/CollectionRuleOptionsTests.cs index 222731d31bb..dc7203d392f 100644 --- a/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/CollectionRuleOptionsTests.cs +++ b/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/CollectionRuleOptionsTests.cs @@ -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; @@ -720,48 +719,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..2c2df442b36 --- /dev/null +++ b/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/TestHostHelper.cs @@ -0,0 +1,59 @@ +using Microsoft.Diagnostics.Tools.Monitor; +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 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(); + } + } + } + } +} From 61781b174a06b471e1dffdce3aafd188d573dc26 Mon Sep 17 00:00:00 2001 From: Justin Anderson Date: Wed, 25 Aug 2021 15:33:32 -0700 Subject: [PATCH 2/2] Add license header. --- .../CollectionRuleOptionsTests.cs | 1 - .../TestHostHelper.cs | 7 +++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/CollectionRuleOptionsTests.cs b/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/CollectionRuleOptionsTests.cs index dc7203d392f..c66ff038820 100644 --- a/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/CollectionRuleOptionsTests.cs +++ b/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/CollectionRuleOptionsTests.cs @@ -8,7 +8,6 @@ using Microsoft.Diagnostics.Tools.Monitor.CollectionRules.Options.Actions; using Microsoft.Diagnostics.Tools.Monitor.CollectionRules.Options.Triggers; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Options; using System; using System.Collections.Generic; diff --git a/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/TestHostHelper.cs b/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/TestHostHelper.cs index 2c2df442b36..e8d3056471e 100644 --- a/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/TestHostHelper.cs +++ b/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/TestHostHelper.cs @@ -1,9 +1,12 @@ -using Microsoft.Diagnostics.Tools.Monitor; +// 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.Text; using System.Threading.Tasks; using Xunit.Abstractions;