No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TimeoutCommand.cs 2.0KB

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