Skip to content

Commit

Permalink
Deduplicate
Browse files Browse the repository at this point in the history
  • Loading branch information
jnm2 committed May 17, 2020
1 parent 22fbd70 commit b970c82
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
16 changes: 16 additions & 0 deletions src/NUnitEngine/nunit.engine.core/AsyncTestEngineResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
// ***********************************************************************

using System;
using System.ComponentModel;
using System.Threading;
using System.Xml;
using NUnit.Common;
Expand All @@ -39,6 +40,21 @@ public class AsyncTestEngineResult : ITestRun
private volatile TestEngineResult _result;
private readonly ManualResetEvent _waitHandle = new ManualResetEvent(false);

#if !NETSTANDARD1_6
public static AsyncTestEngineResult RunAsync(Func<TestEngineResult> func)
{
var testRun = new AsyncTestEngineResult();

using (var worker = new BackgroundWorker())
{
worker.DoWork += (sender, e) => testRun.SetResult(func.Invoke());
worker.RunWorkerAsync();
}

return testRun;
}
#endif

/// <summary>
/// Get the result of this run.
/// </summary>
Expand Down
11 changes: 11 additions & 0 deletions src/NUnitEngine/nunit.engine.core/Polyfill.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#if NET20

namespace System
{
// This would cause conflicts if it was public in the API assembly. This is an engine implementation detail since
// even though it's public because this assembly should not be compiled against by anyone that references the
// engine.
public delegate TResult Func<TResult>();
}

#endif
16 changes: 1 addition & 15 deletions src/NUnitEngine/nunit.engine.core/Runners/AbstractTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
// ***********************************************************************

using System;
using System.ComponentModel;
using NUnit.Engine.Services;

namespace NUnit.Engine.Runners
{
Expand Down Expand Up @@ -110,19 +108,7 @@ public virtual void UnloadPackage()
/// <returns>An <see cref="AsyncTestEngineResult"/> that will provide the result of the test execution</returns>
protected virtual AsyncTestEngineResult RunTestsAsync(ITestEventListener listener, TestFilter filter)
{
var testRun = new AsyncTestEngineResult();

using (var worker = new BackgroundWorker())
{
worker.DoWork += (s, ea) =>
{
var result = RunTests(listener, filter);
testRun.SetResult(result);
};
worker.RunWorkerAsync();
}

return testRun;
return AsyncTestEngineResult.RunAsync(() => RunTests(listener, filter));
}
#endif

Expand Down
15 changes: 1 addition & 14 deletions src/NUnitEngine/nunit.engine/Services/TestAgency.AgentLease.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

#if !NETSTANDARD1_6 && !NETSTANDARD2_0
using System;
using System.ComponentModel;
using System.Threading;

namespace NUnit.Engine.Services
Expand Down Expand Up @@ -89,19 +88,7 @@ public TestEngineResult Run(ITestEventListener listener, TestFilter filter)

public AsyncTestEngineResult RunAsync(ITestEventListener listener, TestFilter filter)
{
var testRun = new AsyncTestEngineResult();

using (var worker = new BackgroundWorker())
{
worker.DoWork += (s, ea) =>
{
var result = Run(listener, filter);
testRun.SetResult(result);
};
worker.RunWorkerAsync();
}

return testRun;
return AsyncTestEngineResult.RunAsync(() => Run(listener, filter));
}

public void StopRun(bool force)
Expand Down

0 comments on commit b970c82

Please sign in to comment.