A game about forced loneliness, made by TACStudios
1using System;
2using System.Collections;
3using System.Diagnostics;
4using NUnit.Framework.Interfaces;
5using NUnit.Framework.Internal;
6using NUnit.Framework.Internal.Commands;
7using UnityEngine.TestRunner.NUnitExtensions.Runner;
8using UnityEngine.TestTools.TestRunner;
9
10namespace UnityEngine.TestTools
11{
12 internal class TimeoutCommand : DelegatingTestCommand, IEnumerableTestMethodCommand
13 {
14 internal const int k_DefaultTimeout = 1000 * 180;
15
16 public TimeoutCommand(TestCommand innerCommand) : base(innerCommand)
17 {
18 }
19
20 public override TestResult Execute(ITestExecutionContext context)
21 {
22 throw new NotImplementedException("Use ExecuteEnumerable");
23 }
24
25 public IEnumerable ExecuteEnumerable(ITestExecutionContext context)
26 {
27 if (context.TestCaseTimeout == 0)
28 {
29 context.TestCaseTimeout = k_DefaultTimeout;
30 }
31
32 var executeEnumerable = ((IEnumerableTestMethodCommand)innerCommand).ExecuteEnumerable(context);
33 foreach (var iterator in executeEnumerable)
34 {
35 if (HasTimedOut(context))
36 {
37 context.CurrentResult.SetResult(ResultState.Error, new UnityTestTimeoutException(context.TestCaseTimeout).Message);
38 yield return new RestoreTestContextAfterDomainReload(); // If this is right after a domain reload, give the editor a chance to restore.
39 yield break;
40 }
41 yield return iterator;
42 }
43
44 if (HasTimedOut(context))
45 {
46 context.CurrentResult.SetResult(ResultState.Error,
47 new UnityTestTimeoutException(context.TestCaseTimeout).Message);
48 }
49 }
50
51 private static bool HasTimedOut(ITestExecutionContext context)
52 {
53 return Stopwatch.GetTimestamp() - context.StartTicks >
54 context.TestCaseTimeout * (Stopwatch.Frequency / 1000f);
55 }
56 }
57}