System.ComponentModel.EventBasedAsync.Tests threw System.Runtime.Serialization.SerializationException
No results generated. Details: https://mc.dot.net/#/product/netcore/master/source/official~2Fcorefx~2Fmaster~2F/type/test~2Ffunctional~2Fdesktop~2Fcli~2F/build/20161104.503/workItem/System.ComponentModel.EventBasedAsync.Tests
2016-11-05 00:05:11,038: INFO: proc(55): run_and_log_output: Output: System.Runtime.Serialization.SerializationException: Type 'Xunit.Sdk.ThrowsException' in Assembly 'xunit.assert, Version=2.2.0.3300, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c' is not marked as serializable.
2016-11-05 00:05:11,038: INFO: proc(55): run_and_log_output: Output: at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)
2016-11-05 00:05:11,038: INFO: proc(55): run_and_log_output: Output: at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)
2016-11-05 00:05:11,038: INFO: proc(55): run_and_log_output: Output: at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
2016-11-05 00:05:11,038: INFO: proc(55): run_and_log_output: Output: at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)
2016-11-05 00:05:11,038: INFO: proc(55): run_and_log_output: Output: at System.Runtime.Remoting.Channels.CrossAppDomainSerializer.SerializeObject(Object obj, MemoryStream stm)
2016-11-05 00:05:11,038: INFO: proc(55): run_and_log_output: Output: at System.AppDomain.Serialize(Object o)
2016-11-05 00:05:11,038: INFO: proc(55): run_and_log_output: Output: at System.AppDomain.MarshalObject(Object o)
Build : Master - 20161104.503 (Full Framework Tests) Failing configurations: Windows.10.Amd64:AnyCPU-Debug Windows.10.Amd64:AnyCPU-Release
After getting a repro state in my local machine and debugging I found out this is what's happening in this test fail.
- The test
BackgroundWorkerTests.TestThrowExceptionInDoWorkis failing (Source code) - When an
Assert.Throwsfails on Xunit it throws aXunit.Sdk.ThrowsException. xunitruns in multiple App Domains and theAssert.Throwsruns in one App Domain, since the Exception is being thrown in a different App Domain this Exception needs to be Marshaled so that it can be used in a different App Domain.- When Marshaling an object it needs to be Serialized, which means that it should be a
Serializableobject. SinceXunit.Sdk.ThrowsExceptionis notSerializableit is throwing aSystem.Runtime.Serialization.SerializationException
If we run the tests using xunit.console.exe instead of using msbuild we could call it with -noappdomain as a parameter so that it runs in one App Domain and instead of getting a System.Runtime.Serialization.SerializationException we would get a test failure on that specific Assert.Throws, and we would get the expected test failure summary explaining what is the test, line and reason, instead of getting a random exception.
Why is the test failing only for Full Framework and not for .NET Core?
The test is expecting that the BackgroundWorker.DoWork delegate throws a TestException which is actually happening. The problem here is that when we are raising the event BackgroundWorker.RunWorkerCompleted we are calling our handler that is expecting a RunWorkerCompletedEventArgs object as the event parameter called e (Source code).
RunWorkerCompletedEventArgs has a property called Result which we are expecting it to throw the TestException.
Here is the issue
When accessing that Result property, if it is an Exception that should be thrown:
- In Full Framework we throw a
System.Reflection.TargetInvocationExceptionwith the expected Exception (in this caseTestException) as anInnerException. TheAssert.Throwsis expecting aTestExceptionbut is finding aSystem.Reflection.TargetInvocationException. - In .NET Core when accessing that
Resultproperty we throw the actual expected Exception.
I Find two problems here
- Why do we have a different behavior in between Full Framework and .NET Core? Should we throw a
System.Reflection.TargetInvocationExceptionwith the expected Exception as anInnerExceptionin .NET Core as well? - Should we make
msbuildto runxunit.console.exewith argument-noappdomainto avoid this kind of exceptions onAssertfailures when using xunit and so that we get the actualAssertsummary and is easier to track and fix this issues?
cc @danmosemsft @weshaggard @joperezr @ericstj
Good analysis @safern . I'm surprised that ThrowsException isn't serializable as it's so simple to do (probably just add [Serializable]) and it seems a fairly common need.
MSBuild just wraps xunit as you can see in the build log. We can figure out how to change the parameters to xunit, but if I read correctly the appdomain is being created within S.ComponentModel and not (or not only) by xunit?
Sorry I had an error there. I meant xunit is the one that is creating the appdomain.
Yes we could just add that parameter Here in the test.targets file
I was surprized as well with the exception not being serialized, but the what I found out is that it is not only ThrowsException which is not serializable, all of their Assert failures are handled with exceptions and none of them are serializable. So that's why I'm suggesting to update the build tools. Even an Assert.True(false) would cause a System.Runtime.Serialization.SerializationException if xunit creates multiple appdomains.
@danmosemsft
That seems to make XUnit's appdomain architecture (which is the default apparently) not very useful, if I understand right... I'd open an issue against them.
I suggest you update the targets to add a line like
<XunitOptions Condition="'$(XUnitNoAppDomain)' == 'true'">$(XunitOptions) -noappdomain </XunitOptions>
then we can switch it off for just some tests if we want.
@weshaggard is it expected to have different behaviours between .NET Core and Desktop? If not, I suppose Desktop has the right behaviour?
There is some issues already opened against them but apparently they can't make their exceptions serializable because their assertion library is a PCL which targets platforms which do not support serialization. Issue opened agains them -- Another issue
@joperezr already spoke with @weshaggard and yes we should have the same behavior on both. Opening an issue on that.
What isn't clear to me is whether or not all failed assertions in our desktop runs are hitting this issue. If they are then we should probably start passing the noappdomain flag but if it is only on some tests where we are doing background work that happens go go into another appdomain then I'm less inclined to start passing the noappdomain flag.