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.TestRunner.TestProtocol;
9
10namespace UnityEngine.TestTools
11{
12 internal class RetryCommand : DelegatingTestCommand, IEnumerableTestMethodCommand
13 {
14 public RetryCommand(TestCommand innerCommand)
15 : base(innerCommand)
16 {
17 }
18
19 public override TestResult Execute(ITestExecutionContext context)
20 {
21 throw new NotImplementedException("Use ExecuteEnumerable");
22 }
23
24 public IEnumerable ExecuteEnumerable(ITestExecutionContext context)
25 {
26 var unityContext = (UnityTestExecutionContext)context;
27 if (unityContext.RetryRepeatState?.GetHashCode() == null)
28 {
29 unityContext.RetryRepeatState = new EnumerableTestState();
30 }
31
32 while(unityContext.RetryRepeatState.Retry < unityContext.RetryCount + 1)
33 {
34 if (innerCommand is IEnumerableTestMethodCommand)
35 {
36 var executeEnumerable = ((IEnumerableTestMethodCommand)innerCommand).ExecuteEnumerable(context);
37 foreach (var iterator in executeEnumerable)
38 {
39 yield return iterator;
40 }
41 }
42 else
43 {
44 context.CurrentResult = innerCommand.Execute(context);
45 }
46
47 if (context.CurrentResult.ResultState != ResultState.Failure || context.CurrentResult.ResultState == ResultState.Error)
48 {
49 unityContext.RetryRepeatState.Retry++;
50 break;
51 }
52
53 if (unityContext.Automated && unityContext.RetryRepeatState.Retry < unityContext.RetryCount)
54 {
55 ReportTestFinishStartPair(unityContext);
56 }
57 unityContext.RetryRepeatState.Retry++;
58 }
59
60 SetIterationProperty(unityContext, unityContext.RetryRepeatState.Retry - 1);
61 unityContext.RetryRepeatState.Retry = 0;
62 }
63
64 private static void ReportTestFinishStartPair(UnityTestExecutionContext unityContext)
65 {
66 unityContext.CurrentResult.StartTime = unityContext.StartTime;
67 unityContext.CurrentResult.EndTime = DateTime.UtcNow;
68 long tickCount = Stopwatch.GetTimestamp() - unityContext.StartTicks;
69 double seconds = (double) tickCount / Stopwatch.Frequency;
70 unityContext.CurrentResult.Duration = seconds;
71 SetIterationProperty(unityContext, unityContext.RetryRepeatState.Retry);
72 unityContext.Listener.TestFinished(unityContext.CurrentResult);
73
74 // Start new test iteration
75 unityContext.CurrentResult = unityContext.CurrentTest.MakeTestResult();
76 unityContext.StartTime = DateTime.UtcNow;
77 unityContext.StartTicks = Stopwatch.GetTimestamp();
78 unityContext.Listener.TestStarted(unityContext.CurrentTest);
79 }
80
81 private static void SetIterationProperty(UnityTestExecutionContext unityContext, int iteration)
82 {
83 unityContext.CurrentResult.Test.Properties.Set("retryIteration", iteration);
84 }
85 }
86}