Skip to content

Commit

Permalink
Preliminary updates to multi-target Core.UnitTest project. Still abou…
Browse files Browse the repository at this point in the history
…t 100 failing tests on NET 7.
  • Loading branch information
tippmar-nr committed Jun 16, 2023
1 parent beb32bf commit 96393c5
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public void GetWebConfigAppSetting_NonWebApp_ReturnsDefaultSettings()
Assert.IsNull(valueWithProvenance.Value);
}

#if NETFRAMEWORK
[Test]
public void GetWebConfigAppSetting_WebApp_ReturnsSettingsForApp()
{
Expand All @@ -41,7 +42,6 @@ public void GetWebConfigAppSetting_WebApp_ReturnsSettingsForApp()
Assert.AreEqual("bar", valueWithProvenance.Value);
}
}

[Test]
public void GetWebConfigAppSetting_WebApp_ReturnsDefaultSettingsIfSettingNotAvailable()
{
Expand Down Expand Up @@ -73,6 +73,7 @@ public void GetWebConfigAppSetting_WebApp_ReturnsDefaultSettingsIfExceptionOccur
Assert.IsNull(valueWithProvenance.Value);
}
}
#endif

[Test]
public void GetConfigSetting_NonWebApp_ReturnsConfigurationManagerSetting()
Expand All @@ -82,6 +83,7 @@ public void GetConfigSetting_NonWebApp_ReturnsConfigurationManagerSetting()
Assert.IsNull(valueWithProvenance.Value);
}

#if NETFRAMEWORK
[Test]
public void GetConfigSetting_WebApp_ReturnsWebAppSetting()
{
Expand All @@ -99,6 +101,7 @@ public void GetConfigSetting_WebApp_ReturnsWebAppSetting()
Assert.AreEqual("bar", valueWithProvenance.Value);
}
}
#endif

[Test]
public void GetAgentConfigFileName_ThrowsExceptionWhenNoneFound()
Expand All @@ -111,6 +114,7 @@ public void GetAgentConfigFileName_ThrowsExceptionWhenNoneFound()
}
}

#if NETFRAMEWORK
[Test]
public void GetAgentConfigFileName_ReturnsConfigFileFromAppConfig()
{
Expand All @@ -130,7 +134,6 @@ public void GetAgentConfigFileName_ReturnsConfigFileFromAppConfig()
Assert.AreEqual(expectedFileName, agentConfigFileName);
}
}

[Test]
public void TryGetAgentConfigFileFromAppConfig_ReturnsNullWhenFileDoesNotExist()
{
Expand Down Expand Up @@ -168,15 +171,17 @@ public void TryGetAgentConfigFileFromAppConfig_ReturnsNullOnException()
StringAssert.Contains("Could not find newrelic.config", actualException.Message);
}
}
#endif

[Test]
public void TryGetAgentConfigFileFromAppRoot_ReturnsNullIfNoAppPath()
{
using (var staticMocks = new ConfigurationLoaderStaticMocks())
{
ReplaceNewRelicHomeWithNullIfNecessary(staticMocks);
#if NETFRAMEWORK
staticMocks.UseAppDomainAppVirtualPathFunc(() => "testVirtualPath");

#endif
var actualException = Assert.Catch<Exception>(() => ConfigurationLoader.GetAgentConfigFileName(), "Expected an exception to be thrown");
StringAssert.Contains("Could not find newrelic.config", actualException.Message);
}
Expand All @@ -187,8 +192,10 @@ public void TryGetAgentConfigFileFromAppRoot_ReturnsNullIfFileDoesNotExist()
{
using (var staticMocks = new ConfigurationLoaderStaticMocks())
{
#if NETFRAMEWORK
staticMocks.UseAppDomainAppVirtualPathFunc(() => "testVirtualPath");
staticMocks.UseAppDomainAppPathFunc(() => "testPath");
#endif
staticMocks.UseFileExistsFunc(_ => false);

var actualException = Assert.Catch<Exception>(() => ConfigurationLoader.GetAgentConfigFileName(), "Expected an exception to be thrown");
Expand All @@ -201,8 +208,10 @@ public void TryGetAgentConfigFileFromAppRoot_ReturnsNullIfExceptionOccurs()
{
using (var staticMocks = new ConfigurationLoaderStaticMocks())
{
#if NETFRAMEWORK
staticMocks.UseAppDomainAppVirtualPathFunc(() => "testVirtualPath");
staticMocks.UseAppDomainAppPathFunc(() => "testPath");
#endif
staticMocks.UseFileExistsFunc(_ => throw new Exception("Exception from FileExists call"));

var actualException = Assert.Catch<Exception>(() => ConfigurationLoader.GetAgentConfigFileName(), "Expected an exception to be thrown");
Expand All @@ -215,8 +224,10 @@ public void TryGetAgentConfigFileFromAppRoot_ReturnsFileNameIfExists()
{
using (var staticMocks = new ConfigurationLoaderStaticMocks())
{
#if NETFRAMEWORK
staticMocks.UseAppDomainAppVirtualPathFunc(() => "testVirtualPath");
staticMocks.UseAppDomainAppPathFunc(() => "testPath");
#endif
staticMocks.UseFileExistsFunc(f => f.Contains("testPath"));


Expand Down Expand Up @@ -571,25 +582,30 @@ private static void ReplaceNewRelicHomeWithNullIfNecessary(ConfigurationLoaderSt

private class ConfigurationLoaderStaticMocks : IDisposable
{
#if NETFRAMEWORK
private readonly Func<string> _originalGetAppDomainAppId;
private readonly Func<string> _originalGetAppDomainAppVirtualPath;
private readonly Func<string> _originalGetAppDomainAppPath;
private readonly Func<string, System.Configuration.Configuration> _originalOpenWebConfiguration;
#endif
private readonly Func<string, bool> _originalFileExists;
private readonly Func<string, string> _originalPathGetDirectoryName;
private readonly Func<string> _originalGetNewRelicHome;

public ConfigurationLoaderStaticMocks()
{
#if NETFRAMEWORK
_originalGetAppDomainAppId = ConfigurationLoader.GetAppDomainAppId;
_originalGetAppDomainAppVirtualPath = ConfigurationLoader.GetAppDomainAppVirtualPath;
_originalGetAppDomainAppPath = ConfigurationLoader.GetAppDomainAppPath;
_originalOpenWebConfiguration = ConfigurationLoader.OpenWebConfiguration;
#endif
_originalFileExists = ConfigurationLoader.FileExists;
_originalPathGetDirectoryName = ConfigurationLoader.PathGetDirectoryName;
_originalGetNewRelicHome = ConfigurationLoader.GetNewRelicHome;
}

#if NETFRAMEWORK
public void UseAppDomainAppIdFunc(Func<string> appDomainAppIdFunc)
{
ConfigurationLoader.GetAppDomainAppId = appDomainAppIdFunc;
Expand All @@ -609,6 +625,7 @@ public void UseOpenWebConfigurationFunc(Func<string, System.Configuration.Config
{
ConfigurationLoader.OpenWebConfiguration = openWebConfigurationFunc;
}
#endif

public void UseFileExistsFunc(Func<string, bool> fileExistsFunc)
{
Expand All @@ -627,10 +644,12 @@ public void UseGetNewRelicHome(Func<string> getNewRelicHome)

public void Dispose()
{
#if NETFRAMEWORK
ConfigurationLoader.GetAppDomainAppId = _originalGetAppDomainAppId;
ConfigurationLoader.GetAppDomainAppVirtualPath = _originalGetAppDomainAppVirtualPath;
ConfigurationLoader.GetAppDomainAppPath = _originalGetAppDomainAppPath;
ConfigurationLoader.OpenWebConfiguration = _originalOpenWebConfiguration;
#endif
ConfigurationLoader.FileExists = _originalFileExists;
ConfigurationLoader.PathGetDirectoryName = _originalPathGetDirectoryName;
ConfigurationLoader.GetNewRelicHome = _originalGetNewRelicHome;
Expand Down
10 changes: 8 additions & 2 deletions tests/Agent/UnitTests/Core.UnitTest/Core.UnitTest.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">
<PropertyGroup>
<TargetFrameworks>net462</TargetFrameworks>
<TargetFrameworks>net462;net7.0</TargetFrameworks>
<RootNamespace>NewRelic.Agent.Core</RootNamespace>
<AssemblyName>NewRelic.Agent.Core.UnitTest</AssemblyName>
<DebugType Condition="'$(TargetFramework)' != '' AND '$(TargetFramework)' != 'netcoreapp1.0'">Full</DebugType>
<RunSettingsFilePath>$(SolutionDir)test.runsettings</RunSettingsFilePath>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)'=='net7.0'">
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PrivateAssets>all</PrivateAssets>
Expand All @@ -21,7 +24,10 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net70'">
<PackageReference Include="System.Configuration.ConfigurationManager" Version="7.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net462'">
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Data" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@ public void RunCrossAgentCollectorHostnameTests(string configFileKey, string env
private static List<TestCaseData> GetCollectorHostnameTestData()
{
var testDatas = new List<TestCaseData>();
var dllPath = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath);
#if NETFRAMEWORK
var location = Assembly.GetExecutingAssembly().CodeBase;
#else
var location = Assembly.GetExecutingAssembly().Location;
#endif
var dllPath = Path.GetDirectoryName(new Uri(location).LocalPath);
var jsonPath = Path.Combine(dllPath, "CrossAgentTests", "DataTransport", "collector_hostname.json");
var jsonString = File.ReadAllText(jsonPath);
var objectArray = JArray.Parse(jsonString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,12 @@ public static IEnumerable<TestCase[]> TestCases
{
get
{
var dllPath = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath);
#if NETFRAMEWORK
var location = Assembly.GetExecutingAssembly().CodeBase;
#else
var location = Assembly.GetExecutingAssembly().Location;
#endif
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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
using Newtonsoft.Json;
using NUnit.Framework;
using NewRelic.Agent.Core.WireModels;
using Google.Protobuf.WellKnownTypes;
using NUnit.Framework.Interfaces;
using static Grpc.Core.Metadata;
using System.Diagnostics;
using System.Web.Configuration;

namespace NewRelic.Agent.Core.Utilities
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
using NUnit.Framework;
using NewRelic.Agent.Core.DataTransport;
using System.Threading.Tasks;

#if NETFRAMEWORK
using GrpcChannel = Grpc.Core.Channel;
#else
using Grpc.Net.Client;
#endif

namespace NewRelic.Agent.Core.GrpcWrapper.Tests
{
Expand Down Expand Up @@ -75,7 +80,7 @@ public FakeGrpcWrapper(bool connectShouldSucceed, Exception ex)
_ex = ex;
}

protected override AsyncDuplexStreamingCall<FakeGrpcRequest, FakeGrpcResponse> CreateStreamsImpl(Channel channel, Metadata headers, int connectTimeoutMs, CancellationToken cancellationToken)
protected override AsyncDuplexStreamingCall<FakeGrpcRequest, FakeGrpcResponse> CreateStreamsImpl(GrpcChannel channel, Metadata headers, int connectTimeoutMs, CancellationToken cancellationToken)
{
if (_ex != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ public void BeforeWrappedMethod_DoesNotSetNullOnFirstThrownException()
Mock.Assert(_wrapperMap);
}

#if NETFRAMEWORK //TODO: update this test to use something other than `System.Web.HttpApplication`
[Test]
public void BeforeWrappedMethod_SetsNoOpWhenThrowsExceptionTooManyTimes()
{
Expand Down Expand Up @@ -191,7 +192,7 @@ public void BeforeWrappedMethod_SetsNoOpWhenThrowsExceptionTooManyTimes()
Assert.DoesNotThrow(() => wrapperService.BeforeWrappedMethod(type, methodName, argumentSignature, invocationTarget, arguments, tracerFactoryName, metricName, EmptyTracerArgs, 0));
Mock.Assert(_noOpWrapper);
}

#endif
[Test]
public void AfterWrappedMethod_DoesNotSetNullOnFirstThrownException()
{
Expand All @@ -212,6 +213,7 @@ public void AfterWrappedMethod_DoesNotSetNullOnFirstThrownException()
Mock.Assert(_wrapperMap);
}

#if NETFRAMEWORK //TODO: update this test to use something other than `System.Web.HttpApplication`
[Test]
public void AfterWrappedMethod_SetsNoOpWhenThrowsExceptionTooManyTimes()
{
Expand Down Expand Up @@ -244,6 +246,7 @@ public void AfterWrappedMethod_SetsNoOpWhenThrowsExceptionTooManyTimes()
var afterWrappedMethod2 = wrapperService.BeforeWrappedMethod(type, methodName, argumentSignature, invocationTarget, arguments, tracerFactoryName, metricName, EmptyTracerArgs, 0);
Assert.DoesNotThrow(() => afterWrappedMethod2(null, null));
}
#endif

[Test]
public void BeforeWrappedMethod_ReturnsNoOp_IfTheCurrentSegmentIsLeaf()
Expand Down

0 comments on commit 96393c5

Please sign in to comment.