You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Jenkins并行运行多NUnit3 Console Runner进程时遇SocketException

问题

配置了一台Jenkins测试代理,用于运行两个独立的测试任务,二者均通过NUnit3 Console Runner执行测试。每个任务会在测试代理上启动NUnit3 Console可执行文件,加载不同的测试程序集。当并行运行这两个Jenkins任务时,第二个任务在NUnit Console完成测试发现并开始执行测试流程后,出现如下SocketException错误:

System.Net.Sockets.SocketException : No connection could be made because the target machine actively refused it 127.0.0.1:12363
10:33:41  
10:33:41  --SocketException
10:33:41  No connection could be made because the target machine actively refused it 127.0.0.1:12363
10:33:41  
10:33:41  Server stack trace: 
10:33:41     at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
10:33:41     at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
10:33:41     at System.Runtime.Remoting.Channels.RemoteConnection.CreateNewSocket(EndPoint ipEndPoint)
10:33:41     at System.Runtime.Remoting.Channels.SocketCache.GetSocket(String machinePortAndSid, Boolean openNew)
10:33:41     at System.Runtime.Remoting.Channels.Tcp.TcpClientTransportSink.SendRequestWithRetry(IMessage msg, ITransportHeaders requestHeaders, Stream requestStream)
10:33:41     at System.Runtime.Remoting.Channels.Tcp.TcpClientTransportSink.ProcessMessage(IMessage msg, ITransportHeaders requestHeaders, Stream requestStream, ITransportHeaders& responseHeaders, Stream& responseStream)
10:33:41     at System.Runtime.Remoting.Channels.BinaryClientFormatterSink.SyncProcessMessage(IMessage msg)
10:33:41  
10:33:41  Exception rethrown at [0]: 
10:33:41     at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
10:33:41     at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
10:33:41     at NUnit.Engine.ITestAgent.Stop()
10:33:41     at NUnit.Engine.Runners.ProcessRunner.Dispose(Boolean disposing)
10:33:41     at NUnit.Engine.Runners.AbstractTestRunner.Dispose()
10:33:41     at NUnit.Engine.Runners.MasterTestRunner.Dispose(Boolean disposing)
10:33:41     at NUnit.Engine.Runners.MasterTestRunner.Dispose()
10:33:41     at NUnit.ConsoleRunner.ConsoleRunner.RunTests(TestPackage package, TestFilter filter)
10:33:41     at NUnit.ConsoleRunner.Program.Main(String[] args)

当前使用的NUnit Console Runner命令行:

nunit3-console.exe testAssemblyDLL --x86 --work=.\TestOutput --timeout=1800000 --trace=Off --result="TestResult.xml"

注:两个进程使用的testAssemblyDLL为不同的测试程序集。


解决方案

需要添加命令行参数解决NUnit引擎进程间通信的端口冲突问题,以下是两种可行方案:

  • 方案一:使用--inprocess参数
    让测试直接在NUnit Console进程内运行,不启动独立的测试代理进程,彻底避免Socket通信需求。修改后的命令行:

    nunit3-console.exe testAssemblyDLL --x86 --work=.\TestOutput --timeout=1800000 --trace=Off --result="TestResult.xml" --inprocess
    

    注意:如果测试依赖进程隔离环境(比如测试代码会修改全局状态),此方案可能不适用,但常规测试场景均可正常使用。

  • 方案二:指定独立的端口范围
    通过--range参数为每个并行任务分配不重叠的端口范围,确保NUnit引擎的代理进程不会抢占同一端口。例如:
    任务1命令行:

    nunit3-console.exe testAssemblyDLL1 --x86 --work=.\TestOutput1 --timeout=1800000 --trace=Off --result="TestResult1.xml" --range=12000-12100
    

    任务2命令行:

    nunit3-console.exe testAssemblyDLL2 --x86 --work=.\TestOutput2 --timeout=1800000 --trace=Off --result="TestResult2.xml" --range=12101-12200
    

    此方案保留了进程隔离特性,适合需要独立测试环境的场景。

另外,建议为两个任务配置独立的--work目录,避免测试结果文件、日志文件的读写冲突,进一步提升并行运行的稳定性。


内容的提问来源于stack exchange,提问作者Ken

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 07:57:00