Skip to content

Commit 2a4218d

Browse files
authored
Merge pull request #1672 from nunit/port-1354
Eliminate BinaryFormatter
2 parents 9f56c2b + 9fc8be1 commit 2a4218d

29 files changed

+722
-273
lines changed

src/NUnitCommon/nunit.agent.core.tests/AgentDirectRunnerTests.cs

+27-27
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,37 @@ namespace NUnit.Agents
1414
{
1515
public class AgentDirectRunnerTests
1616
{
17-
[Test]
18-
public void RunAgentDirectly()
19-
{
20-
RunTestUnderTestBed(typeof(MockAssembly).Assembly.Location);
21-
}
17+
//[Test]
18+
// public void RunAgentDirectly()
19+
// {
20+
// RunTestUnderTestBed(typeof(MockAssembly).Assembly.Location);
21+
// }
2222

23-
private static void RunTestUnderTestBed(string testAssembly)
24-
{
25-
string agentAssembly = typeof(DirectTestAgent).Assembly.Location;
23+
// private static void RunTestUnderTestBed(string testAssembly)
24+
// {
25+
// string agentAssembly = typeof(DirectTestAgent).Assembly.Location;
2626

27-
#if NETFRAMEWORK
28-
string agentExe = agentAssembly;
29-
#else
30-
string agentExe = Path.ChangeExtension(agentAssembly, ".exe");
31-
#endif
32-
var startInfo = new ProcessStartInfo(agentExe);
33-
startInfo.Arguments = testAssembly;
34-
startInfo.RedirectStandardOutput = true;
35-
startInfo.UseShellExecute = false;
27+
//#if NETFRAMEWORK
28+
// string agentExe = agentAssembly;
29+
//#else
30+
// string agentExe = Path.ChangeExtension(agentAssembly, ".exe");
31+
//#endif
32+
// var startInfo = new ProcessStartInfo(agentExe);
33+
// startInfo.Arguments = testAssembly;
34+
// startInfo.RedirectStandardOutput = true;
35+
// startInfo.UseShellExecute = false;
3636

37-
Process? process = Process.Start(startInfo);
37+
// Process? process = Process.Start(startInfo);
3838

39-
if (process is not null)
40-
{
41-
process.WaitForExit();
42-
Console.WriteLine($"Agent process exited with rc={process.ExitCode}");
39+
// if (process is not null)
40+
// {
41+
// process.WaitForExit();
42+
// Console.WriteLine($"Agent process exited with rc={process.ExitCode}");
4343

44-
string output = process.StandardOutput.ReadToEnd();
45-
if (!output.Contains("Test Run Summary"))
46-
Assert.Fail("No Summary Report found");
47-
}
48-
}
44+
// string output = process.StandardOutput.ReadToEnd();
45+
// if (!output.Contains("Test Run Summary"))
46+
// Assert.Fail("No Summary Report found");
47+
// }
48+
// }
4949
}
5050
}

src/NUnitCommon/nunit.agent.core.tests/Runners/TestAgentRunnerExceptionTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public void StopRun_Passes_Along_NUnitEngineException()
114114
_driver.When(x => x.StopRun(Arg.Any<bool>()))
115115
.Do(x => { throw new NUnitEngineException("Message"); });
116116

117-
var ex = Assert.Throws<NUnitEngineException>(() => _runner.StopRun(true));
117+
var ex = Assert.Throws<NUnitEngineException>(() => _runner.ForcedStop());
118118
Assert.That(ex.Message, Is.EqualTo("Message"));
119119
}
120120

@@ -124,7 +124,7 @@ public void StopRun_Throws_NUnitEngineException()
124124
_driver.When(x => x.StopRun(Arg.Any<bool>()))
125125
.Do(x => { throw new ArgumentException("Message"); });
126126

127-
var ex = Assert.Throws<NUnitEngineException>(() => _runner.StopRun(true));
127+
var ex = Assert.Throws<NUnitEngineException>(() => _runner.ForcedStop());
128128
Assert.That(ex.InnerException, Is.Not.Null);
129129
Assert.That(ex.InnerException, Is.InstanceOf<ArgumentException>());
130130
Assert.That(ex.InnerException.Message, Is.EqualTo("Message"));

src/NUnitCommon/nunit.agent.core/Communication/Transports/Remoting/TestAgentRemotingTransport.cs

+8-6
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,16 @@ public AsyncTestEngineResult RunAsync(ITestEventListener listener, TestFilter fi
172172
}
173173

174174
/// <summary>
175-
/// Cancel the ongoing test run. If no test is running, the call is ignored.
175+
/// Request the current test run to stop. If no tests are running,
176+
/// the call is ignored.
176177
/// </summary>
177-
/// <param name="force">If true, cancel any ongoing test threads, otherwise wait for them to complete.</param>
178-
public void StopRun(bool force)
179-
{
180-
_runner?.StopRun(force);
181-
}
178+
public void RequestStop() => _runner?.RequestStop();
182179

180+
/// <summary>
181+
/// Force the current test run to stop, killing threads or processes if necessary.
182+
/// If no tests are running, the call is ignored.
183+
/// </summary>
184+
public void ForcedStop() => _runner?.ForcedStop();
183185
#endregion
184186

185187
/// <summary>

src/NUnitCommon/nunit.agent.core/Communication/Transports/Tcp/TestAgentTcpTransport.cs

+27-25
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt
22

3+
using System.Diagnostics;
34
using System.Net;
45
using System.Net.Sockets;
56
using System.Threading;
7+
using System.Xml.Serialization;
68
using NUnit.Engine.Agents;
79
using NUnit.Engine.Communication.Messages;
810
using NUnit.Engine.Communication.Protocols;
@@ -22,6 +24,7 @@ public class TestAgentTcpTransport : ITestAgentTransport, ITestEventListener
2224
private readonly string _agencyUrl;
2325
private Socket? _clientSocket;
2426
private ITestEngineRunner? _runner;
27+
private XmlSerializer _testPackageSerializer = new XmlSerializer(typeof(TestPackage));
2528

2629
public TestAgentTcpTransport(RemoteTestAgent agent, string serverUrl)
2730
{
@@ -75,43 +78,42 @@ private void CommandLoop()
7578

7679
while (keepRunning)
7780
{
78-
var command = socketReader.GetNextMessage<CommandMessage>();
81+
var command = socketReader.GetNextMessage();
7982

80-
switch (command.CommandName)
83+
switch (command.Code)
8184
{
82-
case "CreateRunner":
83-
var package = (TestPackage)command.Arguments[0];
84-
_runner?.Unload();
85+
case MessageCode.CreateRunner:
86+
var package = new TestPackage().FromXml(command.Data!);
8587
_runner = CreateRunner(package);
8688
break;
87-
case "Load":
88-
SendResult(_runner.ShouldNotBeNull().Load());
89+
case MessageCode.LoadCommand:
90+
SendResult(_runner.ShouldNotBeNull().Load().Xml.OuterXml);
8991
break;
90-
case "Reload":
91-
SendResult(_runner.ShouldNotBeNull().Reload());
92+
case MessageCode.ReloadCommand:
93+
SendResult(_runner.ShouldNotBeNull().Reload().Xml.OuterXml);
9294
break;
93-
case "Unload":
95+
case MessageCode.UnloadCommand:
9496
_runner.ShouldNotBeNull().Unload();
9597
break;
96-
case "Explore":
97-
var filter = (TestFilter)command.Arguments[0];
98-
SendResult(_runner.ShouldNotBeNull().Explore(filter));
98+
case MessageCode.ExploreCommand:
99+
var filter = new TestFilter(command.Data!);
100+
SendResult(_runner.ShouldNotBeNull().Explore(filter).Xml.OuterXml);
99101
break;
100-
case "CountTestCases":
101-
filter = (TestFilter)command.Arguments[0];
102-
SendResult(_runner.ShouldNotBeNull().CountTestCases(filter));
102+
case MessageCode.CountCasesCommand:
103+
filter = new TestFilter(command.Data!);
104+
SendResult(_runner.ShouldNotBeNull().CountTestCases(filter).ToString());
103105
break;
104-
case "Run":
105-
filter = (TestFilter)command.Arguments[0];
106-
SendResult(_runner.ShouldNotBeNull().Run(this, filter));
106+
case MessageCode.RunCommand:
107+
filter = new TestFilter(command.Data!);
108+
SendResult(_runner.ShouldNotBeNull().Run(this, filter).Xml.OuterXml);
107109
break;
108110

109-
case "RunAsync":
110-
filter = (TestFilter)command.Arguments[0];
111+
case MessageCode.RunAsyncCommand:
112+
filter = new TestFilter(command.Data!);
111113
_runner.ShouldNotBeNull().RunAsync(this, filter);
112114
break;
113115

114-
case "Stop":
116+
case MessageCode.StopAgent:
115117
keepRunning = false;
116118
break;
117119
}
@@ -120,16 +122,16 @@ private void CommandLoop()
120122
Stop();
121123
}
122124

123-
private void SendResult(object result)
125+
private void SendResult(string result)
124126
{
125-
var resultMessage = new CommandReturnMessage(result);
127+
var resultMessage = new TestEngineMessage(MessageCode.CommandResult, result);
126128
var bytes = new BinarySerializationProtocol().Encode(resultMessage);
127129
_clientSocket.ShouldNotBeNull().Send(bytes);
128130
}
129131

130132
public void OnTestEvent(string report)
131133
{
132-
var progressMessage = new ProgressMessage(report);
134+
var progressMessage = new TestEngineMessage(MessageCode.ProgressReport, report);
133135
var bytes = new BinarySerializationProtocol().Encode(progressMessage);
134136
_clientSocket.ShouldNotBeNull().Send(bytes);
135137
}

src/NUnitCommon/nunit.agent.core/Runners/TestAgentRunner.cs

+11-3
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,18 @@ public AsyncTestEngineResult RunAsync(ITestEventListener? listener, TestFilter f
186186
}
187187

188188
/// <summary>
189-
/// Cancel the ongoing test run. If no test is running, the call is ignored.
189+
/// Request the current test run to stop. If no tests are running,
190+
/// the call is ignored.
190191
/// </summary>
191-
/// <param name="force">If true, cancel any ongoing test threads, otherwise wait for them to complete.</param>
192-
public void StopRun(bool force)
192+
public void RequestStop() => StopRun(false);
193+
194+
/// <summary>
195+
/// Force the current test run to stop, killing threads or processes if necessary.
196+
/// If no tests are running, the call is ignored.
197+
/// </summary>
198+
public void ForcedStop() => StopRun(true);
199+
200+
private void StopRun(bool force)
193201
{
194202
try
195203
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt
2+
3+
using System.Collections.Generic;
4+
using NUnit.Framework;
5+
using NUnit.Engine.Communication.Protocols;
6+
using NUnit.Engine.Internal;
7+
8+
namespace NUnit.Engine.Communication.Messages
9+
{
10+
public class MessageTests
11+
{
12+
private const string EMPTY_FILTER = "</filter>";
13+
private static readonly string TEST_PACKAGE = new TestPackage("mock-assembly.dll").ToXml();
14+
15+
private BinarySerializationProtocol _wireProtocol = new BinarySerializationProtocol();
16+
17+
private static readonly TestCaseData[] MessageTestData = new TestCaseData[]
18+
{
19+
new TestCaseData(MessageCode.CreateRunner, TEST_PACKAGE),
20+
new TestCaseData(MessageCode.LoadCommand, null),
21+
new TestCaseData(MessageCode.ReloadCommand, null),
22+
new TestCaseData(MessageCode.UnloadCommand, null),
23+
new TestCaseData(MessageCode.ExploreCommand, EMPTY_FILTER),
24+
new TestCaseData(MessageCode.CountCasesCommand, EMPTY_FILTER),
25+
new TestCaseData(MessageCode.RunCommand, EMPTY_FILTER),
26+
new TestCaseData(MessageCode.RunAsyncCommand, EMPTY_FILTER),
27+
new TestCaseData(MessageCode.RequestStopCommand, null),
28+
new TestCaseData(MessageCode.ForcedStopCommand, null)
29+
};
30+
31+
[TestCaseSource(nameof(MessageTestData))]
32+
public void CommandMessageConstructionTests(string code, string data)
33+
{
34+
var cmd = new TestEngineMessage(code, data);
35+
Assert.That(cmd.Code, Is.EqualTo(code));
36+
Assert.That(cmd.Data, Is.EqualTo(data));
37+
}
38+
39+
[TestCaseSource(nameof(MessageTestData))]
40+
public void CommandMessageEncodingTests(string code, string data)
41+
{
42+
var cmd = new TestEngineMessage(code, data);
43+
44+
var bytes = _wireProtocol.Encode(cmd);
45+
var messages = new List<TestEngineMessage>(_wireProtocol.Decode(bytes));
46+
var decoded = messages[0];
47+
Assert.That(decoded.Code, Is.EqualTo(code));
48+
Assert.That(decoded.Data, Is.EqualTo(data));
49+
}
50+
51+
[Test]
52+
public void ProgressMessageTest()
53+
{
54+
const string REPORT = "Progress report";
55+
var msg = new TestEngineMessage(MessageCode.ProgressReport, REPORT);
56+
Assert.That(msg.Code, Is.EqualTo(MessageCode.ProgressReport));
57+
Assert.That(msg.Data, Is.EqualTo(REPORT));
58+
var bytes = _wireProtocol.Encode(msg);
59+
var messages = new List<TestEngineMessage>(_wireProtocol.Decode(bytes));
60+
var decoded = messages[0];
61+
Assert.That(decoded.Code, Is.EqualTo(MessageCode.ProgressReport));
62+
Assert.That(decoded.Data, Is.EqualTo(REPORT));
63+
}
64+
65+
[Test]
66+
public void CommandReturnMessageTest()
67+
{
68+
const string RESULT = "Result text";
69+
var msg = new TestEngineMessage(MessageCode.CommandResult, RESULT);
70+
Assert.That(msg.Code, Is.EqualTo(MessageCode.CommandResult));
71+
Assert.That(msg.Data, Is.EqualTo(RESULT));
72+
var bytes = _wireProtocol.Encode(msg);
73+
var messages = new List<TestEngineMessage>(_wireProtocol.Decode(bytes));
74+
var decoded = messages[0];
75+
Assert.That(decoded.Code, Is.EqualTo(MessageCode.CommandResult));
76+
Assert.That(decoded.Data, Is.EqualTo(RESULT));
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)