3

在 Mono 上结合 NUnit 和 TPL 时,我遇到了奇怪的崩溃。这是代码:

using System;
using NUnit.Engine;

namespace BadMono
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Mono is bad.");
            DryRun();
        }

        private static void DryRun()
        {
            // Code based on:
            // http://stackoverflow.com/questions/33826500/how-to-run-a-nunit-test

            string path = @"<snip>/bin/Debug/TestChannelA.dll";

            TestPackage package = new TestPackage(path);
            package.AddSetting("WorkDirectory", Environment.CurrentDirectory);

            // prepare the engine
            ITestEngine engine = TestEngineActivator.CreateInstance();

            // ERROR happens on this line
            var _filterService = engine.Services.GetService<ITestFilterService>();
        }
    }
}

我添加了以下 NuGet 包:

NUnit.Engine v3.2.0
NUnit v3.2.0
Microsoft.Tpl.Dataflow v4.5.24

因此,如果我在 Windows 10 (.NET 4.6.1) 上运行此代码,程序会打印“Mono is bad”。没有崩溃。

我在 Visual Studio 2015 中编译,然后使用单声道在 Linux 上进行测试。如果我在 Linux (OpenSUSE 13.2) 上运行它,我会得到这个堆栈跟踪:

BadMono/bin/Debug $ mono BadMono.exe
Mono is bad.

Unhandled Exception:
Mono.Cecil.AssemblyResolutionException: Failed to resolve assembly: 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
  at Mono.Cecil.BaseAssemblyResolver.Resolve (Mono.Cecil.AssemblyNameReference name, Mono.Cecil.ReaderParameters parameters) <0x41f5df10 + 0x00333> in <filename unknown>:0 
  at Mono.Cecil.BaseAssemblyResolver.Resolve (Mono.Cecil.AssemblyNameReference name) <0x41f5dec0 + 0x0003e> in <filename unknown>:0 
  at Mono.Cecil.DefaultAssemblyResolver.Resolve (Mono.Cecil.AssemblyNameReference name) <0x41f5d890 + 0x00073> in <filename unknown>:0 
  at Mono.Cecil.MetadataResolver.Resolve (Mono.Cecil.TypeReference type) <0x41f5d530 + 0x000f3> in <filename unknown>:0 
  at Mono.Cecil.ModuleDefinition.Resolve (Mono.Cecil.TypeReference type) <0x41f5d2a0 + 0x0002a> in <filename unknown>:0 
  at Mono.Cecil.TypeReference.Resolve () <0x41f5d240 + 0x00033> in <filename unknown>:0 
  at NUnit.Engine.Internal.CecilExtensions.GetAttribute (Mono.Cecil.TypeDefinition type, System.String fullName) <0x41f5a300 + 0x000b6> in <filename unknown>:0 
  at NUnit.Engine.Services.ExtensionService.FindExtensionsInAssembly (System.String assemblyName, Boolean specifiedInConfig) <0x41f46f80 + 0x0032f> in <filename unknown>:0 
  at NUnit.Engine.Services.ExtensionService.FindExtensionsInDirectory (System.IO.DirectoryInfo startDir) <0x41f46b10 + 0x00177> in <filename unknown>:0 
  at NUnit.Engine.Services.ExtensionService.StartService () <0x41f45640 + 0x000ab> in <filename unknown>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: Mono.Cecil.AssemblyResolutionException: Failed to resolve assembly: 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
  at Mono.Cecil.BaseAssemblyResolver.Resolve (Mono.Cecil.AssemblyNameReference name, Mono.Cecil.ReaderParameters parameters) <0x41f5df10 + 0x00333> in <filename unknown>:0 
  at Mono.Cecil.BaseAssemblyResolver.Resolve (Mono.Cecil.AssemblyNameReference name) <0x41f5dec0 + 0x0003e> in <filename unknown>:0 
  at Mono.Cecil.DefaultAssemblyResolver.Resolve (Mono.Cecil.AssemblyNameReference name) <0x41f5d890 + 0x00073> in <filename unknown>:0 
  at Mono.Cecil.MetadataResolver.Resolve (Mono.Cecil.TypeReference type) <0x41f5d530 + 0x000f3> in <filename unknown>:0 
  at Mono.Cecil.ModuleDefinition.Resolve (Mono.Cecil.TypeReference type) <0x41f5d2a0 + 0x0002a> in <filename unknown>:0 
  at Mono.Cecil.TypeReference.Resolve () <0x41f5d240 + 0x00033> in <filename unknown>:0 
  at NUnit.Engine.Internal.CecilExtensions.GetAttribute (Mono.Cecil.TypeDefinition type, System.String fullName) <0x41f5a300 + 0x000b6> in <filename unknown>:0 
  at NUnit.Engine.Services.ExtensionService.FindExtensionsInAssembly (System.String assemblyName, Boolean specifiedInConfig) <0x41f46f80 + 0x0032f> in <filename unknown>:0 
  at NUnit.Engine.Services.ExtensionService.FindExtensionsInDirectory (System.IO.DirectoryInfo startDir) <0x41f46b10 + 0x00177> in <filename unknown>:0 
  at NUnit.Engine.Services.ExtensionService.StartService () <0x41f45640 + 0x000ab> in <filename unknown>:0 

这是我正在使用的 Mono 版本:

$ mono --version
Mono JIT compiler version 4.4.0 (Stable 4.4.0.40/f8474c4 Wed Mar 30 18:05:27 PDT 2016)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
        TLS:           __thread
        SIGSEGV:       altstack
        Notifications: epoll
        Architecture:  amd64
        Disabled:      none
        Misc:          softdebug 
        LLVM:          supported, not enabled.
        GC:            sgen

奇怪的修复 #1 如果我注释掉以下行,程序在 Linux/mono 上运行良好:

private static void DryRun()
{
    ... // other code the same

    // If I comment out this SINGLE LINE, the program works fine.
    // var _filterService = engine.Services.GetService<ITestFilterService>();
}

奇怪的修复 #2

现在假设我将代码保持原样(_filterService未注释该行)。如果我删除工作目录中的单个 .dll 文件,我不会崩溃。让我给你看原始文件:

<snip>BadMono/bin/Debug $ ls
 .
 ..
 BadMono.exe
 BadMono.exe.config
 BadMono.pdb
 BadMono.vshost.exe
 BadMono.vshost.exe.config
 Mono.Cecil.dll
 System.Threading.Tasks.Dataflow.dll
 System.Threading.Tasks.Dataflow.xml
 nunit-agent-x86.exe
 nunit-agent.exe
 nunit.engine.api.dll
 nunit.engine.dll
 nunit.framework.dll
 nunit.framework.xml

现在,如果我删除System.Threading.Tasks.Dataflow.dll,程序又可以正常工作了。Windows 不会出现此问题,并且在我描述的任何情况下都可以正常工作;只有单声道/Linux 有问题。

这对我来说真的很奇怪,因为在这个示例代码中我什至没有真正调用任何 Tasks.Dataflow (TPL) 东西。有人可以解释一下这里发生了什么吗?

4

0 回答 0