diff --git a/.github/workflows/run_unit_tests.yml b/.github/workflows/run_unit_tests.yml index b05a618df..ed209ae83 100644 --- a/.github/workflows/run_unit_tests.yml +++ b/.github/workflows/run_unit_tests.yml @@ -44,7 +44,7 @@ jobs: run: dotnet restore - name: Build & Run Unit Tests - run: dotnet test --verbosity normal --no-restore --settings tests\UnitTests.runsettings --results-directory ${{ env.test_results_path }} + run: dotnet test --verbosity minimal --no-restore --settings tests\UnitTests.runsettings --results-directory ${{ env.test_results_path }} - name: Upload coverage reports to Codecov.io uses: codecov/codecov-action@eaaf4bedf32dbdc6b720b63067d99c4d77d6047d # v3.1.4 diff --git a/tests/Agent/UnitTests/CompositeTests/CrossAgentTests/DistributedTracing/TraceContextCrossAgentTests.cs b/tests/Agent/UnitTests/CompositeTests/CrossAgentTests/DistributedTracing/TraceContextCrossAgentTests.cs index bd28f5c79..a3809eef6 100644 --- a/tests/Agent/UnitTests/CompositeTests/CrossAgentTests/DistributedTracing/TraceContextCrossAgentTests.cs +++ b/tests/Agent/UnitTests/CompositeTests/CrossAgentTests/DistributedTracing/TraceContextCrossAgentTests.cs @@ -60,12 +60,7 @@ private static List GetTraceContextTestData() { var testCaseData = new List(); - string location; -#if NETFRAMEWORK - location = Assembly.GetExecutingAssembly().CodeBase; -#else - location = Assembly.GetExecutingAssembly().Location; -#endif + string location = Assembly.GetExecutingAssembly().GetLocation(); var dllPath = Path.GetDirectoryName(new Uri(location).LocalPath); var jsonPath = Path.Combine(dllPath, "CrossAgentTests", "DistributedTracing", "trace_context.json"); var jsonString = File.ReadAllText(jsonPath); diff --git a/tests/Agent/UnitTests/CompositeTests/CrossAgentTests/SecurityPolicies/SecurityPoliciesCrossAgentTests.cs b/tests/Agent/UnitTests/CompositeTests/CrossAgentTests/SecurityPolicies/SecurityPoliciesCrossAgentTests.cs index a39476083..0a8a9baa7 100644 --- a/tests/Agent/UnitTests/CompositeTests/CrossAgentTests/SecurityPolicies/SecurityPoliciesCrossAgentTests.cs +++ b/tests/Agent/UnitTests/CompositeTests/CrossAgentTests/SecurityPolicies/SecurityPoliciesCrossAgentTests.cs @@ -13,6 +13,7 @@ using NewRelic.Agent.Core.DataTransport; using NewRelic.Agent.Core.Labels; using NewRelic.Agent.Core.Utilities; +using NewRelic.Agent.TestUtilities; using NewRelic.SystemInterfaces; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -80,12 +81,7 @@ private static List GetSecurityPoliciesTestData() { var testCaseDatas = new List(); - string location; -#if NETFRAMEWORK - location = Assembly.GetExecutingAssembly().CodeBase; -#else - location = Assembly.GetExecutingAssembly().Location; -#endif + string location = Assembly.GetExecutingAssembly().GetLocation(); var dllPath = Path.GetDirectoryName(new Uri(location).LocalPath); var jsonPath = Path.Combine(dllPath, "CrossAgentTests", "SecurityPolicies", "security_policies.json"); var jsonString = File.ReadAllText(jsonPath); diff --git a/tests/Agent/UnitTests/CompositeTests/CrossAgentTests/SqlObfuscation/SqlObfuscationCrossAgentTests.cs b/tests/Agent/UnitTests/CompositeTests/CrossAgentTests/SqlObfuscation/SqlObfuscationCrossAgentTests.cs index 2bf3c2427..555719295 100644 --- a/tests/Agent/UnitTests/CompositeTests/CrossAgentTests/SqlObfuscation/SqlObfuscationCrossAgentTests.cs +++ b/tests/Agent/UnitTests/CompositeTests/CrossAgentTests/SqlObfuscation/SqlObfuscationCrossAgentTests.cs @@ -9,6 +9,7 @@ using NewRelic.Agent.Api; using NewRelic.Agent.Core.Database; using NewRelic.Agent.Extensions.Providers.Wrapper; +using NewRelic.Agent.TestUtilities; using Newtonsoft.Json; using NUnit.Framework; @@ -85,12 +86,7 @@ private static List GetSqlObfuscationTestDatas() { var testCaseDatas = new List(); - string location; -#if NETFRAMEWORK - location = Assembly.GetExecutingAssembly().CodeBase; -#else - location = Assembly.GetExecutingAssembly().Location; -#endif + string location = Assembly.GetExecutingAssembly().GetLocation(); var dllPath = Path.GetDirectoryName(new Uri(location).LocalPath); var jsonPath = Path.Combine(dllPath, "CrossAgentTests", "SqlObfuscation", "sql_obfuscation.json"); var jsonString = File.ReadAllText(jsonPath); diff --git a/tests/Agent/UnitTests/CompositeTests/CrossAgentTests/Utilization/UtilizationCrossAgentTests.cs b/tests/Agent/UnitTests/CompositeTests/CrossAgentTests/Utilization/UtilizationCrossAgentTests.cs index fb59607b0..e1f65b6ec 100644 --- a/tests/Agent/UnitTests/CompositeTests/CrossAgentTests/Utilization/UtilizationCrossAgentTests.cs +++ b/tests/Agent/UnitTests/CompositeTests/CrossAgentTests/Utilization/UtilizationCrossAgentTests.cs @@ -3,6 +3,7 @@ using NewRelic.Agent.Api; using NewRelic.Agent.Core.Utilization; +using NewRelic.Agent.TestUtilities; using Newtonsoft.Json; using NUnit.Framework; using System; @@ -208,12 +209,7 @@ private static List GetUtilizationTestData() { var testCaseDatas = new List(); - string location; -#if NETFRAMEWORK - location = Assembly.GetExecutingAssembly().CodeBase; -#else - location = Assembly.GetExecutingAssembly().Location; -#endif + string location = Assembly.GetExecutingAssembly().GetLocation(); var dllPath = Path.GetDirectoryName(new Uri(location).LocalPath); var jsonPath = Path.Combine(dllPath, "CrossAgentTests", "Utilization", "utilization_json.json"); var jsonString = File.ReadAllText(jsonPath); diff --git a/tests/Agent/UnitTests/CompositeTests/Samplers/GcSamplerNetCoreTests.cs b/tests/Agent/UnitTests/CompositeTests/Samplers/GcSamplerNetCoreTests.cs index 9666576ef..f37ca50c0 100644 --- a/tests/Agent/UnitTests/CompositeTests/Samplers/GcSamplerNetCoreTests.cs +++ b/tests/Agent/UnitTests/CompositeTests/Samplers/GcSamplerNetCoreTests.cs @@ -75,75 +75,75 @@ public void SamplerStartsWithoutException() sampler.Start(); } - //[Test] - //public void SamplerDisposesEventListenerOnException() - //{ - // var samplerWasStopped = false; - // var listenerWasDisposed = false; + [Test] + public void SamplerDisposesEventListenerOnException() + { + var samplerWasStopped = false; + var listenerWasDisposed = false; - // var mockListener = Mock.Create>>(); + var mockListener = Mock.Create>>(); - // Mock.Arrange(() => mockListener.Dispose()) - // .DoInstead(() => { listenerWasDisposed = true; }); + Mock.Arrange(() => mockListener.Dispose()) + .DoInstead(() => { listenerWasDisposed = true; }); - // //This is our mechanism for shutting down the sampler. If a config change is used, it starts/stops 3x which makes - // //it difficult to determine current state. Instead, throw an exception in the eventListener's sample method. - // Mock.Arrange(() => mockListener.Sample()) - // .DoInstead(() => throw new Exception()); + //This is our mechanism for shutting down the sampler. If a config change is used, it starts/stops 3x which makes + //it difficult to determine current state. Instead, throw an exception in the eventListener's sample method. + Mock.Arrange(() => mockListener.Sample()) + .DoInstead(() => throw new Exception()); - // Func>> mockListenerFactory = () => - // { - // return mockListener; - // }; + Func>> mockListenerFactory = () => + { + return mockListener; + }; - // var mockScheduler = Mock.Create(); + var mockScheduler = Mock.Create(); - // //Prevents the scheduler from actually running - // Mock.Arrange(() => _mockScheduler.ExecuteEvery(Arg.IsAny(), Arg.IsAny(), Arg.IsAny())) - // .DoNothing(); + //Prevents the scheduler from actually running + Mock.Arrange(() => _mockScheduler.ExecuteEvery(Arg.IsAny(), Arg.IsAny(), Arg.IsAny())) + .DoNothing(); - // //Tracks the stop executing for the scheduler which indicates that the sampler - // //has requested it to stop; - // Mock.Arrange(() => mockScheduler.StopExecuting(Arg.IsAny(), Arg.IsAny())) - // .DoInstead((a, t) => { samplerWasStopped = true; }); + //Tracks the stop executing for the scheduler which indicates that the sampler + //has requested it to stop; + Mock.Arrange(() => mockScheduler.StopExecuting(Arg.IsAny(), Arg.IsAny())) + .DoInstead((a, t) => { samplerWasStopped = true; }); - // var sampler = new GCSamplerNetCore(mockScheduler, mockListenerFactory, _mockTransformer, _fxSamplerValidForFrameworkOverride); + var sampler = new GCSamplerNetCore(mockScheduler, mockListenerFactory, _mockTransformer, _fxSamplerValidForFrameworkOverride); - // //Act - // sampler.Start(); + //Act + sampler.Start(); - // //Cause error which will shut down the sampler - // sampler.Sample(); + //Cause error which will shut down the sampler + sampler.Sample(); - // //Assert - // Assert.IsTrue(samplerWasStopped); - // Assert.IsTrue(listenerWasDisposed); - //} + //Assert + Assert.IsTrue(samplerWasStopped); + Assert.IsTrue(listenerWasDisposed); + } - //[Test] - //public void SamplerDisposesEventListenerWhenDisposed() - //{ - // var listenerWasDisposed = false; + [Test] + public void SamplerDisposesEventListenerWhenDisposed() + { + var listenerWasDisposed = false; - // var mockListener = Mock.Create>>(); + var mockListener = Mock.Create>>(); - // Mock.Arrange(() => mockListener.Dispose()) - // .DoInstead(() => { listenerWasDisposed = true; }); + Mock.Arrange(() => mockListener.Dispose()) + .DoInstead(() => { listenerWasDisposed = true; }); - // Func>> mockListenerFactory = () => - // { - // return mockListener; - // }; + Func>> mockListenerFactory = () => + { + return mockListener; + }; - // var sampler = new GCSamplerNetCore(_mockScheduler, mockListenerFactory, _mockTransformer, _fxSamplerValidForFrameworkOverride); + var sampler = new GCSamplerNetCore(_mockScheduler, mockListenerFactory, _mockTransformer, _fxSamplerValidForFrameworkOverride); - // //Act - // sampler.Start(); - // sampler.Dispose(); + //Act + sampler.Start(); + sampler.Dispose(); - // //Assert - // Assert.IsTrue(listenerWasDisposed); - //} + //Assert + Assert.IsTrue(listenerWasDisposed); + } [Test] public void SamplerStartsEventListenerWhenStarted() diff --git a/tests/Agent/UnitTests/Core.UnitTest/CrossAgentTests/DataTransport/CollectorHostNameTests.cs b/tests/Agent/UnitTests/Core.UnitTest/CrossAgentTests/DataTransport/CollectorHostNameTests.cs index d2a9203d1..173ad151a 100644 --- a/tests/Agent/UnitTests/Core.UnitTest/CrossAgentTests/DataTransport/CollectorHostNameTests.cs +++ b/tests/Agent/UnitTests/Core.UnitTest/CrossAgentTests/DataTransport/CollectorHostNameTests.cs @@ -13,6 +13,7 @@ using NewRelic.Agent.Core.Configuration; using NewRelic.Agent.Core.DataTransport; using System.Reflection; +using NewRelic.Agent.TestUtilities; namespace NewRelic.Agent.Core.CrossAgentTests.DataTransport { @@ -107,11 +108,7 @@ public void RunCrossAgentCollectorHostnameTests(string configFileKey, string env private static List GetCollectorHostnameTestData() { var testDatas = new List(); -#if NETFRAMEWORK - var location = Assembly.GetExecutingAssembly().CodeBase; -#else - var location = Assembly.GetExecutingAssembly().Location; -#endif + string location = Assembly.GetExecutingAssembly().GetLocation(); var dllPath = Path.GetDirectoryName(new Uri(location).LocalPath); var jsonPath = Path.Combine(dllPath, "CrossAgentTests", "DataTransport", "collector_hostname.json"); var jsonString = File.ReadAllText(jsonPath); diff --git a/tests/Agent/UnitTests/Core.UnitTest/CrossAgentTests/ServerSentEvent/ServerSentEventTests.cs b/tests/Agent/UnitTests/Core.UnitTest/CrossAgentTests/ServerSentEvent/ServerSentEventTests.cs index fb721c772..da2992008 100644 --- a/tests/Agent/UnitTests/Core.UnitTest/CrossAgentTests/ServerSentEvent/ServerSentEventTests.cs +++ b/tests/Agent/UnitTests/Core.UnitTest/CrossAgentTests/ServerSentEvent/ServerSentEventTests.cs @@ -25,6 +25,7 @@ using System.IO; using System.Linq; using System.Reflection; +using NewRelic.Agent.TestUtilities; using Telerik.JustMock; namespace NewRelic.Agent.Core.CrossAgentTests @@ -203,11 +204,7 @@ public static IEnumerable TestCases { get { -#if NETFRAMEWORK - var location = Assembly.GetExecutingAssembly().CodeBase; -#else - var location = Assembly.GetExecutingAssembly().Location; -#endif + string location = Assembly.GetExecutingAssembly().GetLocation(); var dllPath = Path.GetDirectoryName(new Uri(location).LocalPath); var jsonPath = Path.Combine(dllPath, "CrossAgentTests", "ServerSentEvent", "data_collection_server_configuration.json"); var jsonString = File.ReadAllText(jsonPath); diff --git a/tests/Agent/UnitTests/NewRelic.Agent.TestUtilities/AssemblyExtensions.cs b/tests/Agent/UnitTests/NewRelic.Agent.TestUtilities/AssemblyExtensions.cs new file mode 100644 index 000000000..fddf71ab4 --- /dev/null +++ b/tests/Agent/UnitTests/NewRelic.Agent.TestUtilities/AssemblyExtensions.cs @@ -0,0 +1,20 @@ +// Copyright 2020 New Relic, Inc. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +using System.Reflection; + +namespace NewRelic.Agent.TestUtilities +{ + public static class AssemblyExtensions + { + public static string GetLocation(this Assembly assembly) + { +#if NETFRAMEWORK + return assembly.CodeBase; +#else + return assembly.Location; +#endif + + } + } +}