Skip to content

Commit 3af18b3

Browse files
CharliePooleCopilotCopilot
authored
Refactor to support all three agent types (#1924)
* Refactor to support all three agent types * Conditionally add agent providers to the list Add agent providers conditionally if none exist. Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Restore agent exit regression tests Co-authored-by: CharliePoole <2678661+CharliePoole@users.noreply.github.com> --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: CharliePoole <2678661+CharliePoole@users.noreply.github.com>
1 parent 6ee68b3 commit 3af18b3

7 files changed

Lines changed: 562 additions & 287 deletions

File tree

‎src/NUnitEngine/nunit.engine.tests/Services/TestAgencyExitTests.cs‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt
2-
32
using System;
43
using System.Diagnostics;
54
using System.Globalization;
@@ -81,16 +80,17 @@ internal static int RunProbe(string[] args)
8180
EnableRaisingEvents = enableEvents
8281
};
8382
process.StartInfo.RedirectStandardInput = delay < 0;
84-
using var agency = new ExitTestAgency(process, delay < 0);
83+
using var agency = new TestAgency();
84+
var provider = new ExitTestAgentProvider(agency, process, delay < 0);
8585
var package = new TestPackage("test-assembly.dll");
8686
package.AddSetting(SettingDefinitions.TargetFrameworkName.WithValue(".NETCoreApp,Version=v10.0"), true);
8787

8888
try
8989
{
90-
agency.GetAgent(package);
90+
provider.GetAgent(package);
9191
if (delay < 0)
9292
{
93-
agency.Registration!.GetAwaiter().GetResult();
93+
provider.Registration!.GetAwaiter().GetResult();
9494
if (delay == -2)
9595
process.Kill();
9696
else
@@ -108,21 +108,21 @@ internal static int RunProbe(string[] args)
108108
return 5;
109109

110110
Thread.Sleep(250);
111-
if (agency.IsAgentActive(agency.AgentId, out _))
111+
if (agency._agentStore.IsAgentActive(provider.AgentId, out _))
112112
{
113113
Console.Error.WriteLine("Exited agent is still active.");
114114
return 4;
115115
}
116116

117-
string? message = agency.GetAgentExitMessage(agency.AgentId);
117+
string? message = agency.GetAgentExitMessage(provider.AgentId);
118118
if (message is null || !message.Contains("exit code " + process.ExitCode))
119119
{
120120
Console.Error.WriteLine("Registered agent exit diagnostic was lost.");
121121
return 6;
122122
}
123123

124124
process.Dispose();
125-
if (agency.GetAgentExitMessage(agency.AgentId) is not null)
125+
if (agency.GetAgentExitMessage(provider.AgentId) is not null)
126126
{
127127
Console.Error.WriteLine("Disposed process produced an exit diagnostic.");
128128
return 7;
@@ -196,13 +196,15 @@ private static ProcessStartInfo CreateStartInfo(string mode, int exitCode, bool
196196
};
197197
}
198198

199-
private sealed class ExitTestAgency : TestAgency
199+
private sealed class ExitTestAgentProvider : LocalProcessAgentProvider
200200
{
201+
private readonly TestAgency _agency;
201202
private readonly Process _process;
202203
private readonly bool _register;
203204

204-
public ExitTestAgency(Process process, bool register)
205+
public ExitTestAgentProvider(TestAgency agency, Process process, bool register) : base(agency)
205206
{
207+
_agency = agency;
206208
_process = process;
207209
_register = register;
208210
}
@@ -211,8 +213,6 @@ public ExitTestAgency(Process process, bool register)
211213

212214
public Guid AgentId { get; private set; }
213215

214-
internal override string TcpEndPoint => "tcp://unused";
215-
216216
internal override Process CreateAgentProcess(Guid agentId, string agencyUrl, TestPackage package)
217217
{
218218
AgentId = agentId;
@@ -227,7 +227,7 @@ internal override Process CreateAgentProcess(Guid agentId, string agencyUrl, Tes
227227
{
228228
try
229229
{
230-
Register(agent);
230+
_agency.Register(agent);
231231
return;
232232
}
233233
catch (ArgumentException)
@@ -244,4 +244,4 @@ internal override Process CreateAgentProcess(Guid agentId, string agencyUrl, Tes
244244
}
245245
}
246246
}
247-
}
247+
}

‎src/NUnitEngine/nunit.engine/Runners/ProcessRunner.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ protected override void Dispose(bool disposing)
250250
log.Error(ExceptionHelper.BuildMessageAndStackTrace(ex));
251251
}
252252

253-
if (_agent is not null && _agency.IsAgentActive(_agent.Id, out _))
253+
if (_agent is not null && _agency.IsAgentActive(_agent.Id))
254254
{
255255
try
256256
{
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt
2+
3+
using NUnit.Engine.Extensibility;
4+
using NUnit.Extensibility;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Diagnostics;
8+
using System.Linq;
9+
using System.Net.Sockets;
10+
using System.Runtime.Versioning;
11+
using System.Text;
12+
using System.Threading.Tasks;
13+
14+
namespace NUnit.Engine.Services
15+
{
16+
internal class InProcessAgentProvider : ITestAgentProvider
17+
{
18+
private static readonly Logger log = InternalTrace.GetLogger(typeof(InProcessAgentProvider));
19+
20+
// The TestAgency instance for which we are providing local process agents.
21+
private TestAgency _agency;
22+
23+
public InProcessAgentProvider(TestAgency agency)
24+
{
25+
_agency = agency;
26+
}
27+
28+
#region ITestAgentInfo Implementation
29+
30+
//private List<TestAgentInfo>? _availableAgents;
31+
32+
/// <summary>
33+
/// Gets a list containing <see cref="TestAgentInfo"/> for all available agents.
34+
/// </summary>
35+
public IList<TestAgentInfo> AvailableAgents
36+
{
37+
get
38+
{
39+
// Pseudocode
40+
// if the current runtime is eiher .NET Framework or .NET Core
41+
// return a list containing a single TestAgentInfo for the current runtime
42+
43+
// Currently, no agents available
44+
return Array.Empty<TestAgentInfo>();
45+
}
46+
}
47+
48+
public IList<TestAgentInfo> GetAgentsForPackage(TestPackage package)
49+
{
50+
// Pseudocode
51+
// if package is a leaf assembly package and is compatible with the current runtime
52+
// return a list containing a single TestAgentInfo for an InProcessTestAgent
53+
54+
// Currently, no agents available
55+
return Array.Empty<TestAgentInfo>();
56+
}
57+
58+
#endregion
59+
60+
#region ITestAgentProvider Implementation
61+
62+
public bool IsAgentAvailable(TestPackage package)
63+
{
64+
// TODO: Return true if an InProcessTestAgent is available for the package.
65+
return false;
66+
}
67+
68+
public ITestAgent GetAgent(TestPackage package)
69+
{
70+
// TODO: Return a new InProcessTestAgent instance for the package.
71+
throw new InvalidOperationException(
72+
$"No agents are available for package {package.Name}.\r\nGetAgent should not be called when IsAgentAvailable returns false.");
73+
}
74+
75+
public void ReleaseAgent(ITestAgent agent)
76+
{
77+
// TODO: Close any open tests, dispose resources, keep the agent available for reuse.
78+
}
79+
80+
#endregion
81+
}
82+
}

0 commit comments

Comments
 (0)