Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing SocketException caused by --explore #223

Merged
merged 3 commits into from
May 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void SummaryReportTest()
var expected = new [] {
"Test Run Summary",
" Overall result: Failed",
" Test Count: 29, Passed: 15, Failed: 5, Warnings: 1, Inconclusive: 1, Skipped: 7",
$" Test Count: {MockAssembly.Tests}, Passed: {MockAssembly.Passed}, Failed: 5, Warnings: 1, Inconclusive: 1, Skipped: 7",
" Failed Tests - Failures: 1, Errors: 1, Invalid: 3",
" Skipped Tests - Ignored: 4, Explicit: 3, Other: 0",
" Start time: 2015-10-19 02:12:28Z",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<Compile Include="..\EngineVersion.cs" Link="Properties\EngineVersion.cs" />
<Compile Include="..\mock-assembly\Properties\AssemblyInfo.cs" Link="Properties\AssemblyInfo.cs" />
<Compile Include="..\mock-assembly\MockAssembly.cs" Link="MockAssembly.cs" />
<Compile Include="..\mock-assembly\AccessesCurrentTestContextDuringDiscovery.cs" Link="AccessesCurrentTestContextDuringDiscovery.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using NUnit.Framework;

namespace NUnit.Tests
{
public class AccessesCurrentTestContextDuringDiscovery
{
public const int Tests = 2;
public const int Suites = 1;

public static int[] TestCases()
{
var _ = TestContext.CurrentContext;
return new[] { 0 };
}

[TestCaseSource(nameof(TestCases))]
public void Access_by_TestCaseSource(int arg) { }

[Test]
public void Access_by_ValueSource([ValueSource(nameof(TestCases))] int arg) { }
}
}
7 changes: 5 additions & 2 deletions src/NUnitEngine/mock-assembly/MockAssembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public class MockAssembly
+ BadFixture.Tests
+ FixtureWithTestCases.Tests
+ ParameterizedFixture.Tests
+ GenericFixtureConstants.Tests;
+ GenericFixtureConstants.Tests
+ AccessesCurrentTestContextDuringDiscovery.Tests;

public const int Suites = MockTestFixture.Suites
+ Singletons.OneTestCase.Suites
Expand All @@ -67,6 +68,7 @@ public class MockAssembly
+ FixtureWithTestCases.Suites
+ ParameterizedFixture.Suites
+ GenericFixtureConstants.Suites
+ AccessesCurrentTestContextDuringDiscovery.Suites
+ NamespaceSuites;

public const int TestStartedEvents = Tests - IgnoredFixture.Tests - BadFixture.Tests - ExplicitFixture.Tests;
Expand All @@ -84,7 +86,8 @@ public class MockAssembly
+ TestAssembly.MockTestFixture.Tests
+ FixtureWithTestCases.Tests
+ ParameterizedFixture.Tests
+ GenericFixtureConstants.Tests;
+ GenericFixtureConstants.Tests
+ AccessesCurrentTestContextDuringDiscovery.Tests;

public const int Skipped_Ignored = MockTestFixture.Skipped_Ignored + IgnoredFixture.Tests;
public const int Skipped_Explicit = MockTestFixture.Skipped_Explicit + ExplicitFixture.Tests;
Expand Down
1 change: 1 addition & 0 deletions src/NUnitEngine/mock-assembly/mock-assembly.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="AccessesCurrentTestContextDuringDiscovery.cs" />
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
Expand Down
56 changes: 23 additions & 33 deletions src/NUnitEngine/nunit-agent/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
// ***********************************************************************

using System;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Diagnostics;
using NUnit.Engine;
using NUnit.Engine.Agents;
Expand All @@ -45,12 +43,6 @@ public class NUnitTestAgent
static ITestAgency Agency;
static RemoteTestAgent Agent;

/// <summary>
/// Channel used for communications with the agency
/// and with clients
/// </summary>
static TcpChannel Channel;

private const string LOG_FILE_FORMAT = "nunit-agent_{0}.log";

/// <summary>
Expand Down Expand Up @@ -133,7 +125,8 @@ public static int Main(string[] args)
log.Info("Initializing Services");
engine.Initialize();

Channel = ServerUtilities.GetTcpChannel();
// Owns the channel used for communications with the agency and with clients
var testAgencyServer = engine.Services.GetService<TestAgency>();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have been starting two separate channels for no good reason which caused no small amount of confusion when prototyping the fix at 1 am last night. =D


log.Info("Connecting to TestAgency at {0}", AgencyUrl);
try
Expand All @@ -145,32 +138,29 @@ public static int Main(string[] args)
log.Error("Unable to connect", ex);
}

if (Channel != null)
{
log.Info("Starting RemoteTestAgent");
Agent = new RemoteTestAgent(AgentId, Agency, engine.Services);
log.Info("Starting RemoteTestAgent");
Agent = new RemoteTestAgent(AgentId, Agency, engine.Services);

try
{
if (Agent.Start())
WaitForStop();
else
log.Error("Failed to start RemoteTestAgent");
}
catch (Exception ex)
{
log.Error("Exception in RemoteTestAgent", ex);
}
try
{
if (Agent.Start())
WaitForStop();
else
log.Error("Failed to start RemoteTestAgent");
}
catch (Exception ex)
{
log.Error("Exception in RemoteTestAgent", ex);
}

//log.Info("Unregistering Channel");
try
{
ChannelServices.UnregisterChannel(Channel);
}
catch (Exception ex)
{
log.Error("ChannelServices.UnregisterChannel threw an exception", ex);
}
try
{
// Unregister the channel
testAgencyServer.Stop();
}
catch (Exception ex)
{
log.Error("Exception in TestAgency.Stop", ex);
}

log.Info("Agent process {0} exiting", Process.GetCurrentProcess().Id);
Expand Down
116 changes: 116 additions & 0 deletions src/NUnitEngine/nunit.engine.tests/Helpers/ProcessUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace NUnit.Engine.Tests.Helpers
{
public static class ProcessUtils
{
public static ProcessResult Run(ProcessStartInfo startInfo)
{
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.CreateNoWindow = true;

using (var process = new Process { StartInfo = startInfo })
{
var standardStreamData = new List<StandardStreamData>();
var currentData = new StringBuilder();
var currentDataIsError = false;

process.OutputDataReceived += (sender, e) =>
{
if (e.Data == null) return;
if (currentDataIsError)
{
if (currentData.Length != 0)
standardStreamData.Add(new StandardStreamData(currentDataIsError, currentData.ToString()));
currentData = new StringBuilder();
currentDataIsError = false;
}
currentData.AppendLine(e.Data);
};
process.ErrorDataReceived += (sender, e) =>
{
if (e.Data == null) return;
if (!currentDataIsError)
{
if (currentData.Length != 0)
standardStreamData.Add(new StandardStreamData(currentDataIsError, currentData.ToString()));
currentData = new StringBuilder();
currentDataIsError = true;
}
currentData.AppendLine(e.Data);
};

process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();

if (currentData.Length != 0)
standardStreamData.Add(new StandardStreamData(currentDataIsError, currentData.ToString()));

return new ProcessResult(process.ExitCode, standardStreamData.ToArray());
}
}

[DebuggerDisplay("{ToString(),nq}")]
public struct ProcessResult
{
public ProcessResult(int exitCode, StandardStreamData[] standardStreamData)
{
ExitCode = exitCode;
StandardStreamData = standardStreamData;
}

public int ExitCode { get; }
public StandardStreamData[] StandardStreamData { get; }

public override string ToString() => ToString(true);

/// <param name="showStreamSource">If true, appends "[stdout] " or "[stderr] " to the beginning of each line.</param>
public string ToString(bool showStreamSource)
{
var r = new StringBuilder("Exit code ").Append(ExitCode);

if (StandardStreamData.Length != 0) r.AppendLine();

foreach (var data in StandardStreamData)
{
if (showStreamSource)
{
var lines = data.Data.Split(new[] { Environment.NewLine }, StringSplitOptions.None);

// StandardStreamData.Data always ends with a blank line, so skip that
for (var i = 0; i < lines.Length - 1; i++)
r.Append(data.IsError ? "[stderr] " : "[stdout] ").AppendLine(lines[i]);
}
else
{
r.Append(data.Data);
}
}

return r.ToString();
}
}

[DebuggerDisplay("{ToString(),nq}")]
public struct StandardStreamData
{
public StandardStreamData(bool isError, string data)
{
IsError = isError;
Data = data;
}

public bool IsError { get; }
public string Data { get; }

public override string ToString() => (IsError ? "[stderr] " : "[stdout] ") + Data;
}
}
}
36 changes: 36 additions & 0 deletions src/NUnitEngine/nunit.engine.tests/Helpers/ShadowCopyUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

namespace NUnit.Engine.Tests.Helpers
{
public static class ShadowCopyUtils
{
/// <summary>
/// Returns the transitive closure of assemblies needed to copy.
/// Deals with assembly names rather than paths to work with runners that shadow copy.
/// </summary>
public static ICollection<string> GetAllNeededAssemblyPaths(params string[] assemblyNames)
{
var r = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

var dependencies = StackEnumerator.Create(
from assemblyName in assemblyNames
select new AssemblyName(assemblyName));

foreach (var dependencyName in dependencies)
{
var dependency = Assembly.ReflectionOnlyLoad(dependencyName.FullName);

if (!dependency.GlobalAssemblyCache && r.Add(Path.GetFullPath(dependency.Location)))
{
dependencies.Recurse(dependency.GetReferencedAssemblies());
}
}

return r;
}
}
}
76 changes: 76 additions & 0 deletions src/NUnitEngine/nunit.engine.tests/Helpers/StackEnumerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

namespace NUnit.Engine.Tests.Helpers
{
public static class StackEnumerator
{
public static StackEnumerator<T> Create<T>(params T[] initial) => new StackEnumerator<T>(initial);
public static StackEnumerator<T> Create<T>(IEnumerable<T> initial) => new StackEnumerator<T>(initial);
public static StackEnumerator<T> Create<T>(IEnumerator<T> initial) => new StackEnumerator<T>(initial);
}

public sealed class StackEnumerator<T> : IDisposable
{
private readonly Stack<IEnumerator<T>> stack = new Stack<IEnumerator<T>>();
private IEnumerator<T> current;

public bool MoveNext()
{
while (!current.MoveNext())
{
current.Dispose();
if (stack.Count == 0) return false;
current = stack.Pop();
}

return true;
}

public T Current => current.Current;

public void Recurse(IEnumerator<T> newCurrent)
{
if (newCurrent == null) return;
stack.Push(current);
current = newCurrent;
}
public void Recurse(IEnumerable<T> newCurrent)
{
if (newCurrent == null) return;
Recurse(newCurrent.GetEnumerator());
}
public void Recurse(params T[] newCurrent)
{
Recurse((IEnumerable<T>)newCurrent);
}

public StackEnumerator(IEnumerator<T> initial)
{
current = initial ?? Enumerable.Empty<T>().GetEnumerator();
}
public StackEnumerator(IEnumerable<T> initial) : this(initial?.GetEnumerator())
{
}
public StackEnumerator(params T[] initial) : this((IEnumerable<T>)initial)
{
}

// Foreach support
[EditorBrowsable(EditorBrowsableState.Never)]
public StackEnumerator<T> GetEnumerator()
{
return this;
}

public void Dispose()
{
current.Dispose();
foreach (var item in stack)
item.Dispose();
stack.Clear();
}
}
}
Loading