Skip to content

Commit 2116e88

Browse files
authored
test: Retry tests on known unrelated errors. (#1644)
1 parent cbf0389 commit 2116e88

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

tests/Agent/IntegrationTests/IntegrationTestHelpers/RemoteServiceFixtures/RemoteApplicationFixture.cs

+33-11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Collections.Generic;
77
using System.IO;
8+
using System.Linq;
89
using System.Net;
910
using System.Net.Http;
1011
using System.Threading;
@@ -21,6 +22,12 @@ public abstract class RemoteApplicationFixture : IDisposable
2122

2223
private Action _setupConfiguration;
2324
private Action _exerciseApplication;
25+
private HashSet<uint> _errorsToRetryOn = new HashSet<uint>
26+
{
27+
0xC000_0005 // System.AccessViolationException. This is a .NET bug that
28+
// is supposed to be fixed but we're still seeing
29+
// https://github.com/dotnet/runtime/issues/62145
30+
};
2431

2532
public Dictionary<string, string> EnvironmentVariables;
2633

@@ -88,8 +95,8 @@ public bool KeepWorkingDirectory
8895
set { RemoteApplication.KeepWorkingDirectory = value; }
8996
}
9097

91-
// We think that the test retry loop may be masking real problems and/or actually causing them, so we've disabled it for now
92-
protected virtual int MaxTries => 1;
98+
// Tests are only retried if they return a known error not related to the test
99+
protected virtual int MaxTries => 3;
93100

94101
public void DisableAsyncLocalCallStack()
95102
{
@@ -190,11 +197,23 @@ public RemoteApplicationFixture SetAdditionalEnvironmentVariable(string key, str
190197
return this;
191198
}
192199

200+
public void AddErrorToRetryOn(uint error)
201+
{
202+
_errorsToRetryOn.Add(error);
203+
}
204+
193205
private void ExerciseApplication()
194206
{
195207
_exerciseApplication?.Invoke();
196208
}
197209

210+
private string FormatExitCode(int? exitCode)
211+
{
212+
if (exitCode == null) return "[null]";
213+
if (Math.Abs(exitCode.Value) < 10) return exitCode.Value.ToString();
214+
return exitCode.Value.ToString("X8");
215+
}
216+
198217
public virtual void Initialize()
199218
{
200219
lock (_initializeLock)
@@ -214,16 +233,14 @@ public virtual void Initialize()
214233
try
215234
{
216235
var retryTest = false;
217-
var exceptionInExerciseApplication = false;
236+
var retryMessage = "";
218237
var applicationHadNonZeroExitCode = false;
219238

220-
221239
do
222240
{
223241
TestLogger?.WriteLine("Test Home" + RemoteApplication.DestinationNewRelicHomeDirectoryPath);
224242

225243
// reset these for each loop iteration
226-
exceptionInExerciseApplication = false;
227244
applicationHadNonZeroExitCode = false;
228245
retryTest = false;
229246

@@ -245,8 +262,9 @@ public virtual void Initialize()
245262
}
246263
catch (Exception ex)
247264
{
248-
exceptionInExerciseApplication = true;
265+
retryTest = true;
249266
TestLogger?.WriteLine("Exception occurred in try number " + (numberOfTries + 1) + " : " + ex.ToString());
267+
retryMessage = "Exception thrown.";
250268
}
251269
finally
252270
{
@@ -278,15 +296,19 @@ public virtual void Initialize()
278296
RemoteApplication.WaitForExit();
279297

280298
applicationHadNonZeroExitCode = RemoteApplication.ExitCode != 0;
299+
var formattedExitCode = FormatExitCode(RemoteApplication.ExitCode);
281300

282-
TestLogger?.WriteLine($"Remote application exited with a {(applicationHadNonZeroExitCode ? "failure" : "success")} exit code of {RemoteApplication.ExitCode}.");
301+
TestLogger?.WriteLine($"Remote application exited with a {(applicationHadNonZeroExitCode ? "failure" : "success")} exit code of {formattedExitCode}.");
283302

284-
retryTest = exceptionInExerciseApplication || applicationHadNonZeroExitCode;
303+
if (applicationHadNonZeroExitCode && _errorsToRetryOn.Contains((uint)RemoteApplication.ExitCode.Value))
304+
{
305+
retryMessage = $"{formattedExitCode} is a known error.";
306+
retryTest = true;
307+
}
285308

286-
if (retryTest)
309+
if (retryTest && (numberOfTries < MaxTries))
287310
{
288-
var message = $"Retrying test. Exception caught when exercising test app = {exceptionInExerciseApplication}, application had non-zero exit code = {applicationHadNonZeroExitCode}.";
289-
TestLogger?.WriteLine(message);
311+
TestLogger?.WriteLine(retryMessage + " Retrying test.");
290312
Thread.Sleep(1000);
291313
numberOfTries++;
292314
}

0 commit comments

Comments
 (0)