Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Test failing on v3.18.0.2 but was not failing in v.3.17.0.2
Dinesh
#1 Posted : Monday, September 3, 2018 4:03:39 PM(UTC)
Rank: Member

Groups: Registered
Joined: 9/3/2018(UTC)
Posts: 11
Location: United Kingdom

Hi Support,

I have just upgraded to your latest version and lots of my test are failing.

It only fails when the item in the generic list is an interface e.g. List<IEvent>, please note this works correctly in version 3.17.0.2

Error
Code line: return Expression.Lambda<Func<object, object>>(boxedResult, parameter).Compile();

Exception:
OneTimeSetUp: System.Exception : Error registering message type List`1 with TradeMessageSerialiser.
----> System.Exception : Error creating WeakGetter for property Item on type System.Collections.Generic.List`1[Test.Events.IEvent].
----> System.InvalidProgramException : Common Language Runtime detected an invalid program.

Visual Studio 2017 Professional 15.8.2
Windows 10 Professional 64 bit

I can't upgrade until this issue is resolved.
Remco
#2 Posted : Tuesday, September 4, 2018 1:05:08 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,974

Thanks: 929 times
Was thanked: 1256 time(s) in 1169 post(s)
Hi, thanks for sharing this issue.

I can't think of anything in the v3.18 release that would have affected this kind of functionality. Are you sure this wasn't introduced due to another change in your toolset? (i.e. a VS upgrade).

Does setting NCrunch in compatibility mode make any difference?

Could you share the full code needed to reproduce this issue?

Can you confirm that this problem appears only when running the test under NCrunch and not with any other runner?
Dinesh
#3 Posted : Tuesday, September 4, 2018 7:29:56 AM(UTC)
Rank: Member

Groups: Registered
Joined: 9/3/2018(UTC)
Posts: 11
Location: United Kingdom

100% it is the nCrunch v3.18 release, I downgraded to v3.17 and it works, I then upgraded back v3.18 and it failed, then downgraded one more time to v3.17 and it works.
Remco
#4 Posted : Tuesday, September 4, 2018 7:33:29 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,974

Thanks: 929 times
Was thanked: 1256 time(s) in 1169 post(s)
Dinesh;12615 wrote:
100% it is the nCrunch v3.18 release, I downgraded to v3.17 and it works, I then upgraded back v3.18 and it failed, then downgraded one more time to v3.17 and it works.


Does setting NCrunch in compatibility mode make any difference?

Could you share the full code needed to reproduce this issue?

Can you confirm that this problem appears only when running the test under NCrunch and not with any other runner?
Dinesh
#5 Posted : Tuesday, September 4, 2018 3:06:03 PM(UTC)
Rank: Member

Groups: Registered
Joined: 9/3/2018(UTC)
Posts: 11
Location: United Kingdom

Quote:

private class TestMessage2
{
public DateTime DateTimeData { get; set; }
public string StringData { get; set; }
public int IntData { get; set; }
}

[Test]
public void TestNotWorking()
{
RegisterMessageType<List<TestMessage2>>();
}

private void RegisterMessageType<T>()
{
var messageType = typeof(T);

try
{
Build(messageType);
return;
}
catch (Exception ex)
{
throw new Exception($"Error registering message type {messageType.Name} with type.", ex);
}
}
private void Build(Type classType)
{
if (classType == null) throw new ArgumentNullException(nameof(classType));

// It is important that the schema stores the properties in a predictable order:
// a) as xml comparison tools used in unit tests are sensitive to field order.
// b) it makes log inspection etc much easier.
// For that reason they are sorted alphabetically regardless of definition order on the classes.
var propertyInfos = classType.GetProperties(BindingFlags.Instance | BindingFlags.Public);

foreach (var p in propertyInfos)
{
var weakGetter = CreateWeakGetter(p);
}

return;
}

/// <summary>
/// Create a weakly typed getter, i.e. one that takes an instance of type object as the argument and returns the boxed
/// result as an object.
/// </summary>
/// <param name="propertyInfo">PropertyInfo of the property to create a getter for.</param>
/// <returns>A compiled weakly typed property getter delegate.</returns>
private Func<object, object> CreateWeakGetter(PropertyInfo propertyInfo)
{
try
{
// We want to handle the class instance as an object so that we can work with a consistent delegate type across
// all mapping.
var parameter = Expression.Parameter(typeof(object));

// Convert the class instance argument from object into the declaring type.
// ReSharper disable once AssignNullToNotNullAttribute
var converted = Expression.Convert(parameter, propertyInfo.DeclaringType);

// Invoke the getter on the typed instance.
var property = Expression.Property(converted, propertyInfo);

// Box the result (convert to object).
var boxedResult = Expression.Convert(property, typeof(object));

// Build the delegate.
return Expression.Lambda<Func<object, object>>(boxedResult, parameter).Compile();
}
catch (Exception ex)
{
throw new Exception($"Error creating WeakGetter for property {propertyInfo.Name} on type {propertyInfo.ReflectedType}. ", ex);
}
}
Remco
#6 Posted : Tuesday, September 4, 2018 11:29:50 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,974

Thanks: 929 times
Was thanked: 1256 time(s) in 1169 post(s)
Thanks for sharing this code! The code does produce an exception for me, but I don't think it's the one giving you trouble. I receive the following:

System.Exception: Error registering message type List`1 with type. ---> System.Exception: Error creating WeakGetter for property Item on type System.Collections.Generic.List`1[UnitTestProject8.UnitTest1+TestMessage2]. ---> System.ArgumentException: Incorrect number of arguments supplied for call to method 'TestMessage2 get_Item(Int32)'
Parameter name: property

I get the same result in both NCrunch and VSTest, so I don't think this is the runner problem that you've encountered. Can you confirm?
Dinesh
#7 Posted : Wednesday, September 5, 2018 7:13:50 AM(UTC)
Rank: Member

Groups: Registered
Joined: 9/3/2018(UTC)
Posts: 11
Location: United Kingdom

Have you run the code in v17, you should not get any error.

Test works in Resharper test runner and MSTest using the NUnit Test Adapter, just does not work with NCrunch. 100% NCrunch issue.

Here is exception from NCrunch

System.Exception : Error registering message type List`1 with type.
----> System.Exception : Error creating WeakGetter for property Item on type System.Collections.Generic.List`1[TestName.Program+TestMessage2].
----> System.InvalidProgramException : Common Language Runtime detected an invalid program.
at TestName.Program.RegisterMessageType[T]() in c:\temp\ConsoleApp1\ConsoleApp1\Program.cs:line 46
at TestName.Program.TestNotWorking() in c:\temp\ConsoleApp1\ConsoleApp1\Program.cs:line 32
--Exception
at TestName.Program.CreateWeakGetter(PropertyInfo propertyInfo) in c:\temp\ConsoleApp1\ConsoleApp1\Program.cs:line 97
at TestName.Program.Build(Type classType) in c:\temp\ConsoleApp1\ConsoleApp1\Program.cs:line 62
at TestName.Program.RegisterMessageType[T]() in c:\temp\ConsoleApp1\ConsoleApp1\Program.cs:line 41
--InvalidProgramException
at System.Runtime.CompilerServices.RuntimeHelpers._CompileMethod(IRuntimeMethodInfo method)
at System.Reflection.Emit.DynamicMethod.CreateDelegate(Type delegateType, Object target)
at System.Linq.Expressions.Compiler.LambdaCompiler.CreateDelegate()
at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda, DebugInfoGenerator debugInfoGenerator)
at System.Linq.Expressions.Expression`1.Compile()
at TestName.Program.CreateWeakGetter(PropertyInfo propertyInfo) in c:\temp\ConsoleApp1\ConsoleApp1\Program.cs:line 93


here is code

Quote:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq.Expressions;
using System.Net.Security;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using Nerdle.AutoConfig;
using NUnit.Framework;

namespace TestName
{
[TestFixture]
class Program
{
static void Main(string[] args)
{
}

private class TestMessage2
{
public DateTime DateTimeData { get; set; }
public string StringData { get; set; }
public int IntData { get; set; }
}

[Test]
public void TestNotWorking()
{
RegisterMessageType<List<TestMessage2>>();
}

private void RegisterMessageType<T>()
{
var messageType = typeof(T);

try
{
Build(messageType);
return;
}
catch (Exception ex)
{
throw new Exception($"Error registering message type {messageType.Name} with type.", ex);
}
}

private void Build(Type classType)
{
if (classType == null) throw new ArgumentNullException(nameof(classType));

// It is important that the schema stores the properties in a predictable order:
// a) as xml comparison tools used in unit tests are sensitive to field order.
// b) it makes log inspection etc much easier.
// For that reason they are sorted alphabetically regardless of definition order on the classes.
var propertyInfos = classType.GetProperties(BindingFlags.Instance | BindingFlags.Public);

foreach (var p in propertyInfos)
{
var weakGetter = CreateWeakGetter(p);
}

return;
}

/// <summary>
/// Create a weakly typed getter, i.e. one that takes an instance of type object as the argument and returns the boxed
/// result as an object.
/// </summary>
/// <param name="propertyInfo">PropertyInfo of the property to create a getter for.</param>
/// <returns>A compiled weakly typed property getter delegate.</returns>
private Func<object, object> CreateWeakGetter(PropertyInfo propertyInfo)
{
try
{
// We want to handle the class instance as an object so that we can work with a consistent delegate type across
// all mapping.
var parameter = Expression.Parameter(typeof(object));

// Convert the class instance argument from object into the declaring type.
// ReSharper disable once AssignNullToNotNullAttribute
var converted = Expression.Convert(parameter, propertyInfo.DeclaringType);

// Invoke the getter on the typed instance.
var property = Expression.Property(converted, propertyInfo);

// Box the result (convert to object).
var boxedResult = Expression.Convert(property, typeof(object));

// Build the delegate.
return Expression.Lambda<Func<object, object>>(boxedResult, parameter).Compile();
}
catch (Exception ex)
{
throw new Exception(
$"Error creating WeakGetter for property {propertyInfo.Name} on type {propertyInfo.ReflectedType}. ",
ex);
}
}
}
}
Remco
#8 Posted : Wednesday, September 5, 2018 8:00:28 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,974

Thanks: 929 times
Was thanked: 1256 time(s) in 1169 post(s)
Can you confirm if you are using a 3rd party profiler such as TypeMock or JustMock? And if so, does turning this off make any difference?
Dinesh
#9 Posted : Wednesday, September 5, 2018 8:12:27 AM(UTC)
Rank: Member

Groups: Registered
Joined: 9/3/2018(UTC)
Posts: 11
Location: United Kingdom

I'm not using them here is my using statement

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using NUnit.Framework;

You should be able to see it the code I published working with v3.17 and NOT working with v3.18.

Also if you change line number 26
from RegisterMessageType<List<TestMessage2>>();
to RegisterMessageType<TestMessage2>();

then it will work, it is to do with generics.
Remco
#10 Posted : Wednesday, September 5, 2018 8:36:02 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,974

Thanks: 929 times
Was thanked: 1256 time(s) in 1169 post(s)
Thanks, I've managed to produce the result you're seeing now. For some reason, when I created a test project, it defaulted to .NET Core 2.1... This gave me a very different result. I'm digging deep into the problem now and will let you know as soon as I have more information.
Dinesh
#11 Posted : Wednesday, September 5, 2018 8:47:17 AM(UTC)
Rank: Member

Groups: Registered
Joined: 9/3/2018(UTC)
Posts: 11
Location: United Kingdom

That is good news you can replicate the problem, hopefully not too long before you resolve it.
Remco
#12 Posted : Wednesday, September 5, 2018 9:27:46 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,974

Thanks: 929 times
Was thanked: 1256 time(s) in 1169 post(s)
Got it! This one really had me perplexed ...

The problem is caused by a regression in v3.18. NCrunch is now incorrectly turning on the MSFakes/Intellitrace profiler when running tests. Most of the time this is harmless, but it looks like the code you've described has a compatibility issue with Intellitrace.

I've done some experimentation with settings, but it doesn't look like there's any way to work around this with configuration. So we'll fix it with a new version. v3.19 is due for release anyway. I should have a build up for you soon.
Dinesh
#13 Posted : Wednesday, September 5, 2018 9:46:42 AM(UTC)
Rank: Member

Groups: Registered
Joined: 9/3/2018(UTC)
Posts: 11
Location: United Kingdom

Thank you, will wait for v3.19 before I upgrade.
Remco
#14 Posted : Wednesday, September 5, 2018 11:03:27 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,974

Thanks: 929 times
Was thanked: 1256 time(s) in 1169 post(s)
Dinesh
#15 Posted : Wednesday, September 5, 2018 11:12:34 AM(UTC)
Rank: Member

Groups: Registered
Joined: 9/3/2018(UTC)
Posts: 11
Location: United Kingdom

That was quick, let me download and test it.
Dinesh
#16 Posted : Wednesday, September 5, 2018 11:16:16 AM(UTC)
Rank: Member

Groups: Registered
Joined: 9/3/2018(UTC)
Posts: 11
Location: United Kingdom

Tested and it works with v3.19, thank you for the quick turnaround, really appreciated.
Remco
#17 Posted : Wednesday, September 5, 2018 12:07:46 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,974

Thanks: 929 times
Was thanked: 1256 time(s) in 1169 post(s)
Dinesh;12635 wrote:
Tested and it works with v3.19, thank you for the quick turnaround, really appreciated.


You're welcome :) Thanks for your patience!
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.129 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download