Skip to content

Commit

Permalink
Abort old-way unit test launch when model connection lifetime is term…
Browse files Browse the repository at this point in the history
…inated (#1439)
  • Loading branch information
ESolovova authored Dec 3, 2019
1 parent 9cbb686 commit e157ce5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static void OnModelInitializationHandler(UnityModelAndLifetime modelAndLi
modelValue.RunUnitTestLaunch.Set(rdVoid =>
{
if (!modelValue.UnitTestLaunch.HasValue()) return false;
var testLauncher = new UnityEditorTestLauncher(modelValue.UnitTestLaunch.Value);
var testLauncher = new UnityEditorTestLauncher(modelValue.UnitTestLaunch.Value, connectionLifetime);
return testLauncher.TryLaunchUnitTests();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Reflection;
using JetBrains.Collections.Viewable;
using JetBrains.Diagnostics;
using JetBrains.Lifetimes;
using JetBrains.Platform.Unity.EditorPluginModel;
using JetBrains.Rd.Tasks;
using NUnit.Framework.Interfaces;
Expand All @@ -15,13 +16,15 @@ namespace JetBrains.Rider.Unity.Editor.AfterUnity56.UnitTesting
public class UnityEditorTestLauncher
{
private readonly UnitTestLaunch myLaunch;

private readonly Lifetime myConnectionLifetime;

private static readonly ILog ourLogger = Log.GetLog("RiderPlugin");
private static string RunnerAddListener = "AddListener";

public UnityEditorTestLauncher(UnitTestLaunch launch)
public UnityEditorTestLauncher(UnitTestLaunch launch, Lifetime connectionLifetime)
{
myLaunch = launch;
myConnectionLifetime = connectionLifetime;
}

public bool TryLaunchUnitTests()
Expand Down Expand Up @@ -261,46 +264,64 @@ private bool TryLaunchUnitTestsInAssembly(string[] testNames)
var runner = runnerField.GetValue(launcher);
SupportAbort(runner);

if (!AdviseTestStarted(runner, "m_TestStartedEvent", test =>
var runLifetimeDef = Lifetime.Define(myConnectionLifetime);
runLifetimeDef.Lifetime.OnTermination(() =>
{
if (myConnectionLifetime.IsNotAlive)
TestEventsSender.RunFinished(myLaunch, new RunResult(false));
});

var runStarted = false;
try
{
if (!(test is TestMethod)) return;
ourLogger.Verbose("TestStarted : {0}", test.FullName);
if (!AdviseTestStarted(runner, "m_TestStartedEvent", test =>
{
if (!(test is TestMethod)) return;
ourLogger.Verbose("TestStarted : {0}", test.FullName);
var testId = TestEventsSender.GetIdFromNUnitTest(test);
var tResult = new TestResult(testId, test.Method.TypeInfo.Assembly.GetName().Name,string.Empty, 0, Status.Running, TestEventsSender.GetIdFromNUnitTest(test.Parent));
var testId = TestEventsSender.GetIdFromNUnitTest(test);
var tResult = new TestResult(testId, test.Method.TypeInfo.Assembly.GetName().Name,string.Empty, 0, Status.Running, TestEventsSender.GetIdFromNUnitTest(test.Parent));
clientController?.OnTestStarted(testId);
TestEventsSender.TestStarted(myLaunch, tResult);
}))
return false;
clientController?.OnTestStarted(testId);
TestEventsSender.TestStarted(myLaunch, tResult);
}))
return false;

if (!AdviseTestFinished(runner, "m_TestFinishedEvent", result =>
{
if (!(result.Test is TestMethod)) return;
if (!AdviseTestFinished(runner, "m_TestFinishedEvent", result =>
{
if (!(result.Test is TestMethod)) return;
clientController?.OnTestFinished();
TestEventsSender.TestFinished(myLaunch, TestEventsSender.GetTestResult(result));
}))
return false;
clientController?.OnTestFinished();
TestEventsSender.TestFinished(myLaunch, TestEventsSender.GetTestResult(result));
}))
return false;

if (!AdviseSessionFinished(runner, "m_RunFinishedEvent", result =>
{
clientController?.OnSessionFinished();
var runResult = new RunResult(Equals(result.ResultState, ResultState.Success));
TestEventsSender.RunFinished(myLaunch, runResult);
}))
return false;
if (!AdviseSessionFinished(runner, "m_RunFinishedEvent", result =>
{
clientController?.OnSessionFinished();
runLifetimeDef.Terminate();
var runResult = new RunResult(Equals(result.ResultState, ResultState.Success));
TestEventsSender.RunFinished(myLaunch, runResult);
}))
return false;

var runMethod = launcherType.GetMethod("Run", BindingFlags.Instance | BindingFlags.Public);
if (runMethod == null)
{
ourLogger.Verbose("Could not find runMethod via reflection");
return false;
}

var runMethod = launcherType.GetMethod("Run", BindingFlags.Instance | BindingFlags.Public);
if (runMethod == null)
//run!
runMethod.Invoke(launcher, null);
runStarted = true;
return true;
}
finally
{
ourLogger.Verbose("Could not find runMethod via reflection");
return false;
if (!runStarted)
runLifetimeDef.Terminate();
}

//run!
runMethod.Invoke(launcher, null);
return true;
}
catch (Exception e)
{
Expand Down

0 comments on commit e157ce5

Please sign in to comment.