Skip to content

Commit de23300

Browse files
committed
Update package references and improve error handling
- Enhanced error handling in `RetryHelper.cs` by adding a catch block for `DirectoryNotFoundException` and refining `IOException` handling. - Updated `InstrumenterHelper.cs` to return a default value for specific `IOException` cases instead of throwing an `AggregateException`. - Refactored `retryStrategy` in `RetryHelperTests.cs` to a static method for consistency across test methods. - Changed exception types in `TargetActionThrows` and `TargetActionThrows5Times` methods to `IOException` for better alignment with the new error handling logic.
1 parent b53d0aa commit de23300

File tree

5 files changed

+45
-15
lines changed

5 files changed

+45
-15
lines changed

Directory.Packages.props

+6-4
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,19 @@
4242
<PackageVersion Include="LinqKit.Microsoft.EntityFrameworkCore" Version="8.1.7" />
4343
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
4444
<!--To test issue 1104 https://github.com/coverlet-coverage/coverlet/issues/1104-->
45-
<PackageVersion Include="System.Collections.Immutable" Version="8.0.0" />
46-
<PackageVersion Include="System.Configuration.ConfigurationManager" Version="8.0.0" />
47-
<PackageVersion Include="System.Linq.Async" Version="6.0.1" />
48-
<PackageVersion Include="System.Reflection.Metadata" Version="8.0.1" />
4945
<!-- latest Tmds.ExecFunction package uses EnvDTE V17.8.37221 -->
5046
<PackageVersion Include="Tmds.ExecFunction" Version="0.8.0" />
5147
<PackageVersion Include="xunit.v3" Version="1.0.1" />
5248
<PackageVersion Include="xunit.runner.visualstudio" Version="3.0.1" />
49+
5350
<PackageVersion Include="System.Buffers" Version="4.6.0" />
51+
<PackageVersion Include="System.Collections.Immutable" Version="8.0.0" />
52+
<PackageVersion Include="System.Configuration.ConfigurationManager" Version="8.0.0" />
53+
<PackageVersion Include="System.Linq.Async" Version="6.0.1" />
5454
<PackageVersion Include="System.Memory" Version="4.6.0" />
5555
<PackageVersion Include="System.Net.Http" Version="4.3.4" />
56+
<PackageVersion Include="System.Reflection.Metadata" Version="8.0.1" />
57+
<PackageVersion Include="System.Runtime.CompilerServices.Unsafe" Version="6.1.0" />
5658
<PackageVersion Include="System.Security.Cryptography.Pkcs" Version="6.0.5" />
5759
<PackageVersion Include="System.Text.Encoding.CodePages" Version="8.0.0" />
5860
<PackageVersion Include="System.Text.Json" Version="8.0.5" />

src/coverlet.core/Helpers/RetryHelper.cs

+4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ public T Do<T>(
5555
}
5656
return action();
5757
}
58+
catch (DirectoryNotFoundException)
59+
{
60+
throw;
61+
}
5862
catch (IOException ex)
5963
{
6064
exceptions.Add(ex);

test/coverlet.core.coverage.tests/Coverage/InstrumenterHelper.cs

+13-1
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,10 @@ public T Do<T>(Func<T> action, Func<TimeSpan> backoffStrategy, int maxAttemptCou
226226
}
227227
return action();
228228
}
229+
catch (DirectoryNotFoundException)
230+
{
231+
throw;
232+
}
229233
catch (IOException ex)
230234
{
231235
if (ex.ToString().Contains("RestoreOriginalModules") || ex.ToString().Contains("RestoreOriginalModule"))
@@ -240,7 +244,15 @@ public T Do<T>(Func<T> action, Func<TimeSpan> backoffStrategy, int maxAttemptCou
240244
}
241245
}
242246
}
243-
throw new AggregateException(exceptions);
247+
// Do not throw exception if we're restoring modules
248+
if (exceptions.ToString().Contains("RestoreOriginalModules") || exceptions.ToString().Contains("RestoreOriginalModule"))
249+
{
250+
return default;
251+
}
252+
else
253+
{
254+
throw new AggregateException(exceptions);
255+
}
244256
}
245257

246258
public void Retry(Action action, Func<TimeSpan> backoffStrategy, int maxAttemptCount = 3)

test/coverlet.core.tests/Coverage/InstrumenterHelper.cs

+14-2
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,11 @@ public T Do<T>(Func<T> action, Func<TimeSpan> backoffStrategy, int maxAttemptCou
215215
}
216216
return action();
217217
}
218-
catch (Exception ex)
218+
catch (DirectoryNotFoundException)
219+
{
220+
throw;
221+
}
222+
catch (IOException ex)
219223
{
220224
if (ex.ToString().Contains("RestoreOriginalModules") || ex.ToString().Contains("RestoreOriginalModule"))
221225
{
@@ -229,7 +233,15 @@ public T Do<T>(Func<T> action, Func<TimeSpan> backoffStrategy, int maxAttemptCou
229233
}
230234
}
231235
}
232-
throw new AggregateException(exceptions);
236+
// Do not throw exception if we're restoring modules
237+
if (exceptions.ToString().Contains("RestoreOriginalModules") || exceptions.ToString().Contains("RestoreOriginalModule"))
238+
{
239+
return default;
240+
}
241+
else
242+
{
243+
throw new AggregateException(exceptions);
244+
}
233245
}
234246

235247
public void Retry(Action action, Func<TimeSpan> backoffStrategy, int maxAttemptCount = 3)

test/coverlet.core.tests/Helpers/RetryHelperTests.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ public class RetryHelperTests
1313
[Fact]
1414
public void TestRetryWithFixedRetryBackoff()
1515
{
16-
Func<TimeSpan> retryStrategy = () =>
16+
static TimeSpan retryStrategy()
1717
{
1818
return TimeSpan.FromMilliseconds(1);
19-
};
19+
}
2020

2121
var target = new RetryTarget();
2222
try
@@ -33,12 +33,12 @@ public void TestRetryWithFixedRetryBackoff()
3333
public void TestRetryWithExponentialRetryBackoff()
3434
{
3535
int currentSleep = 6;
36-
Func<TimeSpan> retryStrategy = () =>
36+
TimeSpan retryStrategy()
3737
{
3838
var sleep = TimeSpan.FromMilliseconds(currentSleep);
3939
currentSleep *= 2;
4040
return sleep;
41-
};
41+
}
4242

4343
var target = new RetryTarget();
4444
try
@@ -55,10 +55,10 @@ public void TestRetryWithExponentialRetryBackoff()
5555
[Fact]
5656
public void TestRetryFinishesIfSuccessful()
5757
{
58-
Func<TimeSpan> retryStrategy = () =>
58+
static TimeSpan retryStrategy()
5959
{
6060
return TimeSpan.FromMilliseconds(1);
61-
};
61+
}
6262

6363
var target = new RetryTarget();
6464
new RetryHelper().Retry(() => target.TargetActionThrows5Times(), retryStrategy, 20);
@@ -72,12 +72,12 @@ public class RetryTarget
7272
public void TargetActionThrows()
7373
{
7474
Calls++;
75-
throw new Exception("Simulating Failure");
75+
throw new IOException("Simulating Failure");
7676
}
7777
public void TargetActionThrows5Times()
7878
{
7979
Calls++;
80-
if (Calls < 6) throw new Exception("Simulating Failure");
80+
if (Calls < 6) throw new IOException("Simulating Failure");
8181
}
8282
}
8383

0 commit comments

Comments
 (0)