-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add tests for Profiler method cache.
- Loading branch information
1 parent
c8e2339
commit 4e1d1d9
Showing
1 changed file
with
101 additions
and
0 deletions.
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
tests/Agent/UnitTests/Core.UnitTest/ProfilerAgentMethodCallCacheTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright 2020 New Relic, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System.Reflection; | ||
using System; | ||
using NUnit.Framework; | ||
using NewRelic.Testing.Assertions; | ||
|
||
namespace NewRelic.Agent.Core | ||
{ | ||
[TestFixture] | ||
public class ProfilerAgentMethodCallCacheTests | ||
{ | ||
[Test] | ||
public void GetMethodCacheFuncShouldReturnAFuncAsAnObject() | ||
{ | ||
var methodReference = ProfilerAgentMethodCallCache.GetMethodCacheFunc(); | ||
Assert.IsAssignableFrom(typeof(Func<string, string, string, Type[], MethodInfo>), methodReference); | ||
} | ||
|
||
[Test] | ||
public void ShouldGetMethodWithNoParameters() | ||
{ | ||
var cacheGetter = (Func<string, string, string, Type[], MethodInfo>)ProfilerAgentMethodCallCache.GetMethodCacheFunc(); | ||
|
||
var typeName = typeof(TestingClass).AssemblyQualifiedName; | ||
var methodName = nameof(TestingClass.MethodWithNoParams); | ||
var cacheKey = string.Concat(typeName, "|", methodName); | ||
|
||
var methodInfoFromCache = cacheGetter.Invoke(cacheKey, typeName, methodName, null); | ||
|
||
NrAssert.Multiple( | ||
() => Assert.IsNotNull(methodInfoFromCache), | ||
() => Assert.AreEqual(methodName, methodInfoFromCache.Invoke(null, null)) | ||
); | ||
} | ||
|
||
[Test] | ||
public void ShouldGetMethodWithParameters() | ||
{ | ||
var cacheGetter = (Func<string, string, string, Type[], MethodInfo>)ProfilerAgentMethodCallCache.GetMethodCacheFunc(); | ||
|
||
var typeName = typeof(TestingClass).AssemblyQualifiedName; | ||
var methodName = nameof(TestingClass.MethodWithParams); | ||
var cacheKey = string.Concat(typeName, "|", methodName); | ||
|
||
var methodInfoFromCache = cacheGetter.Invoke(cacheKey, typeName, methodName, new Type[] { typeof(string) }); | ||
|
||
var expectedMethodResult = $"{methodName} + param_value"; | ||
|
||
NrAssert.Multiple( | ||
() => Assert.IsNotNull(methodInfoFromCache), | ||
() => Assert.AreEqual(expectedMethodResult, methodInfoFromCache.Invoke(null, new object[] { "param_value" })) | ||
); | ||
} | ||
|
||
[Test] | ||
public void ShouldGetWrongMethodIfCacheKeyNotUniqueEnough() | ||
{ | ||
var cacheGetter = (Func<string, string, string, Type[], MethodInfo>)ProfilerAgentMethodCallCache.GetMethodCacheFunc(); | ||
|
||
var typeName = typeof(TestingClass).AssemblyQualifiedName; | ||
var methodName = nameof(TestingClass.MethodWithOverload); | ||
var cacheKey = string.Concat(typeName, "|", methodName); | ||
|
||
var overloadWith1Param = cacheGetter.Invoke(cacheKey, typeName, methodName, new Type[] { typeof(string) }); | ||
var overloadWith2Params = cacheGetter.Invoke(cacheKey, typeName, methodName, new Type[] { typeof(string), typeof(string) }); | ||
|
||
NrAssert.Multiple( | ||
() => Assert.IsNotNull(overloadWith1Param), | ||
() => Assert.IsNotNull(overloadWith2Params), | ||
() => Assert.AreEqual(overloadWith2Params, overloadWith1Param), | ||
// The call to the 2 parameter overload should fail because the 1 parameter overload was returned from the cache | ||
() => Assert.Throws(typeof(TargetParameterCountException), () => overloadWith2Params.Invoke(null, new object[] { "param_1", "param_2" })) | ||
); | ||
} | ||
|
||
public class TestingClass | ||
{ | ||
public static string MethodWithNoParams() | ||
{ | ||
return nameof(MethodWithNoParams); | ||
} | ||
|
||
public static string MethodWithParams(string param) | ||
{ | ||
return $"{nameof(MethodWithParams)} + {param}"; | ||
} | ||
|
||
public static string MethodWithOverload(string param) | ||
{ | ||
return $"{nameof(MethodWithOverload)} + {param}"; | ||
} | ||
|
||
public static string MethodWithOverload(string param1, string param2) | ||
{ | ||
return $"{nameof(MethodWithOverload)} + {param1} + {param2}"; | ||
} | ||
} | ||
} | ||
} |