Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 3 additions & 63 deletions IntelliTect.IntelliWait.Tests/BaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,71 +5,11 @@ namespace IntelliTect.IntelliWait.Tests
{
public class BaseTest
{
protected void ThrowExceptionWithNoReturn()
public BaseTest()
{
throw new Exception();
Test = new FakeTestClass();
}

protected bool ThrowExceptionWithReturn()
{
throw new Exception();
}

protected void ThrowNullRefExceptionWithNoReturn()
{
throw new NullReferenceException();
}

protected bool ThrowNullRefExceptionWithReturn()
{
throw new NullReferenceException();
}

protected void CheckExceptionsVoidReturn(int secondsToFail = 1, int numberOfDifferentExceptions = 1)
{
ThrowExceptions(secondsToFail, numberOfDifferentExceptions);
}

protected bool CheckExceptionsBoolReturn(int secondsToFail = 1, int numberOfDifferentExceptions = 1)
{
ThrowExceptions(secondsToFail, numberOfDifferentExceptions);
return true;
}

// Find a better way to do this to better facilitate test parallelization
private void ThrowExceptions(int secondsToFail, int numberOfExceptions)
{
if (_Timeout == TimeSpan.MinValue)
{
_Timeout = TimeSpan.FromSeconds(secondsToFail);
_Sw.Start();
}

while (_Sw.Elapsed <= _Timeout)
{
_Attempts++;
if (_Attempts > numberOfExceptions)
{
_Attempts = 1;
}
switch (_Attempts)
{
case 1:
throw new InvalidOperationException();
case 2:
throw new InvalidProgramException();
case 3:
throw new IndexOutOfRangeException();
case 4:
throw new ArgumentNullException();
default:
throw new ArgumentException();
}
}
}

private int _Attempts = 0;
private TimeSpan _Timeout = TimeSpan.MinValue;
private Stopwatch _Sw = new Stopwatch();
protected FakeTestClass Test { get; }
}
}
54 changes: 40 additions & 14 deletions IntelliTect.IntelliWait.Tests/ExpectedExceptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ public class ExpectedExceptionsTests : BaseTest
[Fact]
public async Task CheckActionsParamsForBaseTypeChecking()
{
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(() => Wait.Until(ThrowExceptionWithNoReturn, TimeSpan.FromSeconds(1), typeof(Exception)));
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(() => Poll.UntilNoExceptions(Test.ThrowExceptionWithNoReturn, TimeSpan.FromSeconds(1), typeof(Exception)));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(Exception));
}

[Fact]
public async Task CheckFuncParamsForBaseTypeChecking()
{
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(() => Wait.Until(ThrowExceptionWithReturn, TimeSpan.FromSeconds(1), typeof(Exception)));
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(() => Poll.UntilNoExceptions(Test.ThrowExceptionWithReturn, TimeSpan.FromSeconds(1), typeof(Exception)));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(Exception));
}

[Fact]
public async Task CheckActionParamsForMixedTypeChecking()
{
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(
() => Wait.Until(ThrowExceptionWithNoReturn, TimeSpan.FromSeconds(1), typeof(Exception), typeof(NullReferenceException)));
() => Poll.UntilNoExceptions(Test.ThrowExceptionWithNoReturn, TimeSpan.FromSeconds(1), typeof(Exception), typeof(NullReferenceException)));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(Exception));
// Verify that we only catch the actual exceptions thrown
Assert.DoesNotContain(ae.InnerExceptions, e => e.GetType() == typeof(NullReferenceException));
Expand All @@ -34,7 +34,7 @@ public async Task CheckActionParamsForMixedTypeChecking()
public async Task CheckFuncParamsForMixedTypeChecking()
{
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(
() => Wait.Until(ThrowExceptionWithReturn, TimeSpan.FromSeconds(1), typeof(Exception), typeof(NullReferenceException)));
() => Poll.UntilNoExceptions(Test.ThrowExceptionWithReturn, TimeSpan.FromSeconds(1), typeof(Exception), typeof(NullReferenceException)));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(Exception));
// Verify that we only catch the actual exceptions thrown
Assert.DoesNotContain(ae.InnerExceptions, e => e.GetType() == typeof(NullReferenceException));
Expand All @@ -43,51 +43,51 @@ public async Task CheckFuncParamsForMixedTypeChecking()
[Fact]
public async Task CheckActionsParamsForOneDerivedTypeChecking()
{
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(() => Wait.Until(() => CheckExceptionsVoidReturn(1, 1), TimeSpan.FromSeconds(1), typeof(InvalidOperationException)));
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(() => Poll.UntilNoExceptions(() => Test.CheckExceptionsVoidReturn(2, 1), TimeSpan.FromSeconds(1), typeof(InvalidOperationException)));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(InvalidOperationException));
}

[Fact]
public async Task CheckFuncParamsForOneDerivedTypeChecking()
{
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(() => Wait.Until(() => CheckExceptionsBoolReturn(1, 1), TimeSpan.FromSeconds(1), typeof(InvalidOperationException)));
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(() => Poll.UntilNoExceptions(() => Test.CheckExceptionsBoolReturn(2, 1), TimeSpan.FromSeconds(1), typeof(InvalidOperationException)));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(InvalidOperationException));
}

[Fact]
public async Task CheckActionsGenericForOneDerivedTypeChecking()
{
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(() => Wait.Until<InvalidOperationException>(() => CheckExceptionsVoidReturn(1, 1), TimeSpan.FromSeconds(1)));
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(() => Poll.UntilNoExceptions<InvalidOperationException>(() => Test.CheckExceptionsVoidReturn(2, 1), TimeSpan.FromSeconds(1)));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(InvalidOperationException));
}

[Fact]
public async Task CheckFuncGenericForOneDerivedTypeChecking()
{
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(() => Wait.Until<InvalidOperationException>(() => CheckExceptionsBoolReturn(1, 1), TimeSpan.FromSeconds(1)));
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(() => Poll.UntilNoExceptions<InvalidOperationException>(() => Test.CheckExceptionsBoolReturn(2, 1), TimeSpan.FromSeconds(1)));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(InvalidOperationException));
}

[Fact]
public async Task CheckActionsGenericForTwoDerivedTypesChecking()
{
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(() => Wait.Until<InvalidOperationException, InvalidProgramException>(() => CheckExceptionsVoidReturn(1, 2), TimeSpan.FromSeconds(1)));
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(() => Poll.UntilNoExceptions<InvalidOperationException, InvalidProgramException>(() => Test.CheckExceptionsVoidReturn(2, 2), TimeSpan.FromSeconds(1)));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(InvalidOperationException));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(InvalidProgramException));
}

[Fact]
public async Task CheckFuncGenericForTwoDerivedTypesChecking()
{
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(() => Wait.Until<InvalidOperationException, InvalidProgramException>(() => CheckExceptionsBoolReturn(1, 2), TimeSpan.FromSeconds(1)));
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(() => Poll.UntilNoExceptions<InvalidOperationException, InvalidProgramException>(() => Test.CheckExceptionsBoolReturn(2, 2), TimeSpan.FromSeconds(1)));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(InvalidOperationException));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(InvalidProgramException));
}

[Fact]
public async Task CheckActionsGenericForThreeDerivedTypesChecking()
{
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(() => Wait.Until<InvalidOperationException, InvalidProgramException, IndexOutOfRangeException>(() => CheckExceptionsVoidReturn(1, 3), TimeSpan.FromSeconds(1)));
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(() => Poll.UntilNoExceptions<InvalidOperationException, InvalidProgramException, IndexOutOfRangeException>(() => Test.CheckExceptionsVoidReturn(2, 3), TimeSpan.FromSeconds(1)));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(InvalidOperationException));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(InvalidProgramException));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(IndexOutOfRangeException));
Expand All @@ -96,7 +96,7 @@ public async Task CheckActionsGenericForThreeDerivedTypesChecking()
[Fact]
public async Task CheckFuncGenericForThreeDerivedTypesChecking()
{
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(() => Wait.Until<InvalidOperationException, InvalidProgramException, IndexOutOfRangeException>(() => CheckExceptionsBoolReturn(1, 3), TimeSpan.FromSeconds(1)));
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(() => Poll.UntilNoExceptions<InvalidOperationException, InvalidProgramException, IndexOutOfRangeException>(() => Test.CheckExceptionsBoolReturn(2, 3), TimeSpan.FromSeconds(1)));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(InvalidOperationException));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(InvalidProgramException));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(IndexOutOfRangeException));
Expand All @@ -105,7 +105,8 @@ public async Task CheckFuncGenericForThreeDerivedTypesChecking()
[Fact]
public async Task CheckActionsGenericForFourDerivedTypesChecking()
{
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(() => Wait.Until<InvalidOperationException, InvalidProgramException, IndexOutOfRangeException, ArgumentNullException>(() => CheckExceptionsVoidReturn(1, 4), TimeSpan.FromSeconds(1)));
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(() =>
Poll.UntilNoExceptions<InvalidOperationException, InvalidProgramException, IndexOutOfRangeException, ArgumentNullException>(() => Test.CheckExceptionsVoidReturn(4, 4), TimeSpan.FromSeconds(3)));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(InvalidOperationException));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(InvalidProgramException));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(IndexOutOfRangeException));
Expand All @@ -115,11 +116,36 @@ public async Task CheckActionsGenericForFourDerivedTypesChecking()
[Fact]
public async Task CheckFuncGenericForFourDerivedTypesChecking()
{
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(() => Wait.Until<InvalidOperationException, InvalidProgramException, IndexOutOfRangeException, ArgumentNullException>(() => CheckExceptionsBoolReturn(1, 4), TimeSpan.FromSeconds(1)));
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(() =>
Poll.UntilNoExceptions<InvalidOperationException, InvalidProgramException, IndexOutOfRangeException, ArgumentNullException>(() => Test.CheckExceptionsBoolReturn(4, 4), TimeSpan.FromSeconds(3)));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(InvalidOperationException));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(InvalidProgramException));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(IndexOutOfRangeException));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(ArgumentNullException));
}

[Fact]
public async Task CheckActionsGenericForFiveDerivedTypesChecking()
{
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(() =>
Poll.UntilNoExceptions<InvalidOperationException, InvalidProgramException, IndexOutOfRangeException, ArgumentNullException, FieldAccessException>(() => Test.CheckExceptionsVoidReturn(5, 5), TimeSpan.FromSeconds(4)));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(InvalidOperationException));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(InvalidProgramException));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(IndexOutOfRangeException));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(ArgumentNullException));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(FieldAccessException));
}

[Fact]
public async Task CheckFuncGenericForFiveDerivedTypesChecking()
{
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(() =>
Poll.UntilNoExceptions<InvalidOperationException, InvalidProgramException, IndexOutOfRangeException, ArgumentNullException, FieldAccessException>(() => Test.CheckExceptionsBoolReturn(5, 5), TimeSpan.FromSeconds(4)));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(InvalidOperationException));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(InvalidProgramException));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(IndexOutOfRangeException));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(ArgumentNullException));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(FieldAccessException));
}
}
}
77 changes: 77 additions & 0 deletions IntelliTect.IntelliWait.Tests/FakeTestClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.Diagnostics;

namespace IntelliTect.IntelliWait.Tests
{
public class FakeTestClass
{
public void ThrowExceptionWithNoReturn()
{
throw new Exception();
}

public bool ThrowExceptionWithReturn()
{
throw new Exception();
}

public void ThrowNullRefExceptionWithNoReturn()
{
throw new NullReferenceException();
}

public bool ThrowNullRefExceptionWithReturn()
{
throw new NullReferenceException();
}

public void CheckExceptionsVoidReturn(int secondsToFail = 1, int numberOfDifferentExceptions = 1)
{
ThrowExceptions(secondsToFail, numberOfDifferentExceptions);
}

public bool CheckExceptionsBoolReturn(int secondsToFail = 1, int numberOfDifferentExceptions = 1)
{
ThrowExceptions(secondsToFail, numberOfDifferentExceptions);
return true;
}

// Find a better way to do this to better facilitate test parallelization
private void ThrowExceptions(int secondsToFail, int numberOfExceptions)
{
if (_Timeout == TimeSpan.MinValue)
{
_Timeout = TimeSpan.FromSeconds(secondsToFail);
_Sw.Start();
}

while (_Sw.Elapsed <= _Timeout)
{
_Attempts++;
if (_Attempts > numberOfExceptions)
{
_Attempts = 1;
}
switch (_Attempts)
{
case 1:
throw new InvalidOperationException();
case 2:
throw new InvalidProgramException();
case 3:
throw new IndexOutOfRangeException();
case 4:
throw new ArgumentNullException();
case 5:
throw new FieldAccessException();
default:
throw new ArgumentException();
}
}
}

private int _Attempts = 0;
private TimeSpan _Timeout = TimeSpan.MinValue;
private Stopwatch _Sw = new Stopwatch();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are a couple of logic errors here.

  1. These fields only appear to be used inside of the ThrowExceptions method. By keeping them as fields instead of local variables inside of the method it means you are keeping the state around. Meaning subsequent calls to this method are dependant of previous calls. If this is not intended I would move them inside of the method.
  2. This does not actually start a stopwatch. In the ThrowExceptions you do start it on the first pass, but then you never Stop/Restart the stop watch. Meaning that you might never get inside of the while loop.

}
}
Loading