3
3
using System . Reflection ;
4
4
using JetBrains . Collections . Viewable ;
5
5
using JetBrains . Diagnostics ;
6
+ using JetBrains . Lifetimes ;
6
7
using JetBrains . Platform . Unity . EditorPluginModel ;
7
8
using JetBrains . Rd . Tasks ;
8
9
using NUnit . Framework . Interfaces ;
@@ -15,13 +16,15 @@ namespace JetBrains.Rider.Unity.Editor.AfterUnity56.UnitTesting
15
16
public class UnityEditorTestLauncher
16
17
{
17
18
private readonly UnitTestLaunch myLaunch ;
18
-
19
+ private readonly Lifetime myConnectionLifetime ;
20
+
19
21
private static readonly ILog ourLogger = Log . GetLog ( "RiderPlugin" ) ;
20
22
private static string RunnerAddListener = "AddListener" ;
21
23
22
- public UnityEditorTestLauncher ( UnitTestLaunch launch )
24
+ public UnityEditorTestLauncher ( UnitTestLaunch launch , Lifetime connectionLifetime )
23
25
{
24
26
myLaunch = launch ;
27
+ myConnectionLifetime = connectionLifetime ;
25
28
}
26
29
27
30
public bool TryLaunchUnitTests ( )
@@ -261,46 +264,64 @@ private bool TryLaunchUnitTestsInAssembly(string[] testNames)
261
264
var runner = runnerField . GetValue ( launcher ) ;
262
265
SupportAbort ( runner ) ;
263
266
264
- if ( ! AdviseTestStarted ( runner , "m_TestStartedEvent" , test =>
267
+ var runLifetimeDef = Lifetime . Define ( myConnectionLifetime ) ;
268
+ runLifetimeDef . Lifetime . OnTermination ( ( ) =>
269
+ {
270
+ if ( myConnectionLifetime . IsNotAlive )
271
+ TestEventsSender . RunFinished ( myLaunch , new RunResult ( false ) ) ;
272
+ } ) ;
273
+
274
+ var runStarted = false ;
275
+ try
265
276
{
266
- if ( ! ( test is TestMethod ) ) return ;
267
- ourLogger . Verbose ( "TestStarted : {0}" , test . FullName ) ;
277
+ if ( ! AdviseTestStarted ( runner , "m_TestStartedEvent" , test =>
278
+ {
279
+ if ( ! ( test is TestMethod ) ) return ;
280
+ ourLogger . Verbose ( "TestStarted : {0}" , test . FullName ) ;
268
281
269
- var testId = TestEventsSender . GetIdFromNUnitTest ( test ) ;
270
- var tResult = new TestResult ( testId , test . Method . TypeInfo . Assembly . GetName ( ) . Name , string . Empty , 0 , Status . Running , TestEventsSender . GetIdFromNUnitTest ( test . Parent ) ) ;
282
+ var testId = TestEventsSender . GetIdFromNUnitTest ( test ) ;
283
+ var tResult = new TestResult ( testId , test . Method . TypeInfo . Assembly . GetName ( ) . Name , string . Empty , 0 , Status . Running , TestEventsSender . GetIdFromNUnitTest ( test . Parent ) ) ;
271
284
272
- clientController ? . OnTestStarted ( testId ) ;
273
- TestEventsSender . TestStarted ( myLaunch , tResult ) ;
274
- } ) )
275
- return false ;
285
+ clientController ? . OnTestStarted ( testId ) ;
286
+ TestEventsSender . TestStarted ( myLaunch , tResult ) ;
287
+ } ) )
288
+ return false ;
276
289
277
- if ( ! AdviseTestFinished ( runner , "m_TestFinishedEvent" , result =>
278
- {
279
- if ( ! ( result . Test is TestMethod ) ) return ;
290
+ if ( ! AdviseTestFinished ( runner , "m_TestFinishedEvent" , result =>
291
+ {
292
+ if ( ! ( result . Test is TestMethod ) ) return ;
280
293
281
- clientController ? . OnTestFinished ( ) ;
282
- TestEventsSender . TestFinished ( myLaunch , TestEventsSender . GetTestResult ( result ) ) ;
283
- } ) )
284
- return false ;
294
+ clientController ? . OnTestFinished ( ) ;
295
+ TestEventsSender . TestFinished ( myLaunch , TestEventsSender . GetTestResult ( result ) ) ;
296
+ } ) )
297
+ return false ;
285
298
286
- if ( ! AdviseSessionFinished ( runner , "m_RunFinishedEvent" , result =>
287
- {
288
- clientController ? . OnSessionFinished ( ) ;
289
- var runResult = new RunResult ( Equals ( result . ResultState , ResultState . Success ) ) ;
290
- TestEventsSender . RunFinished ( myLaunch , runResult ) ;
291
- } ) )
292
- return false ;
299
+ if ( ! AdviseSessionFinished ( runner , "m_RunFinishedEvent" , result =>
300
+ {
301
+ clientController ? . OnSessionFinished ( ) ;
302
+ runLifetimeDef . Terminate ( ) ;
303
+ var runResult = new RunResult ( Equals ( result . ResultState , ResultState . Success ) ) ;
304
+ TestEventsSender . RunFinished ( myLaunch , runResult ) ;
305
+ } ) )
306
+ return false ;
307
+
308
+ var runMethod = launcherType . GetMethod ( "Run" , BindingFlags . Instance | BindingFlags . Public ) ;
309
+ if ( runMethod == null )
310
+ {
311
+ ourLogger . Verbose ( "Could not find runMethod via reflection" ) ;
312
+ return false ;
313
+ }
293
314
294
- var runMethod = launcherType . GetMethod ( "Run" , BindingFlags . Instance | BindingFlags . Public ) ;
295
- if ( runMethod == null )
315
+ //run!
316
+ runMethod . Invoke ( launcher , null ) ;
317
+ runStarted = true ;
318
+ return true ;
319
+ }
320
+ finally
296
321
{
297
- ourLogger . Verbose ( "Could not find runMethod via reflection" ) ;
298
- return false ;
322
+ if ( ! runStarted )
323
+ runLifetimeDef . Terminate ( ) ;
299
324
}
300
-
301
- //run!
302
- runMethod . Invoke ( launcher , null ) ;
303
- return true ;
304
325
}
305
326
catch ( Exception e )
306
327
{
0 commit comments