Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Sep 11, 2024
1 parent e4e9c58 commit c4c1efc
Showing 1 changed file with 48 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using starsky.foundation.platform.Helpers;
Expand Down Expand Up @@ -29,20 +30,26 @@ bool Test()
}

[TestMethod]
[ExpectedException(typeof(AggregateException))]
public void RetryFail_expect_AggregateException()
{
var count = 0;

var result = RetryHelper.Do(Test, TimeSpan.Zero);
Assert.IsTrue(result);
return;
// Act & Assert
var ex = Assert.ThrowsException<AggregateException>(() =>
{
RetryHelper.Do(Test, TimeSpan.Zero);
});

// Verify that the AggregateException contains the expected inner exceptions
var innerExceptions = ex.InnerExceptions ;
Assert.IsTrue(innerExceptions.Any(e => e is FormatException));
Assert.IsTrue(innerExceptions.Any(e => e is ApplicationException));

bool Test()
{
if ( count == 2 )
if (count == 2)
{
throw new FormatException(); // <= does combine it with AggregateException
throw new FormatException(); // <= combines with AggregateException
}

count++;
Expand All @@ -51,16 +58,19 @@ bool Test()
}

[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void RetryFail_expect_ArgumentOutOfRangeException()
{
bool Test()
// Act & Assert
Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
{
return true;
}
bool Test()
{
return true;
}
// should not be negative
RetryHelper.Do(Test, TimeSpan.Zero, 0);
// should not be negative
RetryHelper.Do(Test, TimeSpan.Zero, 0);
});
}

[TestMethod]
Expand All @@ -85,39 +95,48 @@ async Task<bool> Test()
}

[TestMethod]
[ExpectedException(typeof(AggregateException))]
public async Task Async_RetryFail_expect_AggregateException()
{
var count = 0;
#pragma warning disable 1998
async Task<bool> Test()
#pragma warning restore 1998

// Act & Assert
var ex = await Assert.ThrowsExceptionAsync<AggregateException>(async () =>
{
if ( count == 2 )
var result = await RetryHelper.DoAsync(Test, TimeSpan.Zero);
Assert.IsTrue(result); // This will not be reached if exception is thrown
});

// Verify the AggregateException contains the expected inner exceptions
Assert.IsTrue(ex.InnerExceptions.Any(e => e is FormatException));
Assert.IsTrue(ex.InnerExceptions.Any(e => e is ApplicationException));

Task<bool> Test()
{
if (count == 2)
{
throw new FormatException(); // <= does combine it with AggregateException
throw new FormatException(); // Combined with AggregateException
}

count++;
throw new ApplicationException();
}

var result = await RetryHelper.DoAsync(Test, TimeSpan.Zero);
Assert.IsTrue(result);
}

[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public async Task Async_RetryFail_expect_ArgumentOutOfRangeException()
{
#pragma warning disable 1998
async Task<bool> Test()
#pragma warning restore 1998
// Act & Assert
await Assert.ThrowsExceptionAsync<ArgumentOutOfRangeException>(async () =>
{
return true;
}
// should not be negative
await RetryHelper.DoAsync(Test, TimeSpan.Zero, 0);
});

// should not be negative
await RetryHelper.DoAsync(Test, TimeSpan.Zero, 0);
// Arrange
static async Task<bool> Test()
{
// Just return true for the purpose of this test
return await Task.FromResult(true);
}
}
}

0 comments on commit c4c1efc

Please sign in to comment.