Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding intelli wait #21

Merged
merged 4 commits into from
Nov 28, 2018
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
75 changes: 75 additions & 0 deletions IntelliTect.IntelliWait.Tests/BaseTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Diagnostics;

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

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();
}
}
125 changes: 125 additions & 0 deletions IntelliTect.IntelliWait.Tests/ExpectedExceptionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using System;
using System.Threading.Tasks;
using Xunit;

namespace IntelliTect.IntelliWait.Tests
{
public class ExpectedExceptionsTests : BaseTest
{
[Fact]
public async Task CheckActionsParamsForBaseTypeChecking()
{
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(() => Wait.Until(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)));
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)));
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));
}

[Fact]
public async Task CheckFuncParamsForMixedTypeChecking()
{
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(
() => Wait.Until(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));
}

[Fact]
public async Task CheckActionsParamsForOneDerivedTypeChecking()
{
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(() => Wait.Until(() => CheckExceptionsVoidReturn(1, 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)));
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)));
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)));
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)));
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)));
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)));
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));
}

[Fact]
public async Task CheckFuncGenericForThreeDerivedTypesChecking()
{
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(() => Wait.Until<InvalidOperationException, InvalidProgramException, IndexOutOfRangeException>(() => CheckExceptionsBoolReturn(1, 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));
}

[Fact]
public async Task CheckActionsGenericForFourDerivedTypesChecking()
{
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(() => Wait.Until<InvalidOperationException, InvalidProgramException, IndexOutOfRangeException, ArgumentNullException>(() => CheckExceptionsVoidReturn(1, 4), 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));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(ArgumentNullException));
}

[Fact]
public async Task CheckFuncGenericForFourDerivedTypesChecking()
{
AggregateException ae = await Assert.ThrowsAsync<AggregateException>(() => Wait.Until<InvalidOperationException, InvalidProgramException, IndexOutOfRangeException, ArgumentNullException>(() => CheckExceptionsBoolReturn(1, 4), 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));
Assert.Contains(ae.InnerExceptions, e => e.GetType() == typeof(ArgumentNullException));
}
}
}
20 changes: 20 additions & 0 deletions IntelliTect.IntelliWait.Tests/IntelliTect.IntelliWait.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\IntelliTect.IntelliWait\IntelliTect.IntelliWait.csproj" />
</ItemGroup>

</Project>
69 changes: 69 additions & 0 deletions IntelliTect.IntelliWait.Tests/NoExceptionThrownTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Threading.Tasks;
using Xunit;

namespace IntelliTect.IntelliWait.Tests
{
public class NoExceptionThrownTests : BaseTest
{
[Fact]
public async Task CheckActionParamsForExpectedExpectionDoesNotThrow()
{
await Wait.Until(() => CheckExceptionsVoidReturn(1, 1), TimeSpan.FromSeconds(2), typeof(InvalidOperationException));
}

[Fact]
public async Task CheckFuncParamsForExpectedExpectionDoesNotThrow()
{
await Wait.Until(() => CheckExceptionsBoolReturn(1, 1), TimeSpan.FromSeconds(2), typeof(InvalidOperationException));
}

[Fact]
public async Task CheckActionsGenericForOneExpectedExceptionDoesNotThrow()
{
await Wait.Until<InvalidOperationException>(() => CheckExceptionsVoidReturn(1, 1), TimeSpan.FromSeconds(2));
}

[Fact]
public async Task CheckFuncGenericForOneExpectedExceptionDoesNotThrow()
{
await Wait.Until<InvalidOperationException>(() => CheckExceptionsBoolReturn(1, 1), TimeSpan.FromSeconds(2));
}

[Fact]
public async Task CheckActionsGenericForTwoExpectedExceptiosnDoesNotThrow()
{
await Wait.Until<InvalidOperationException, InvalidProgramException>(() => CheckExceptionsVoidReturn(1, 2), TimeSpan.FromSeconds(2));
}

[Fact]
public async Task CheckFuncGenericForTwoExpectedExceptionsDoesNotThrow()
{
await Wait.Until<InvalidOperationException, InvalidProgramException>(() => CheckExceptionsBoolReturn(1, 2), TimeSpan.FromSeconds(2));
}

[Fact]
public async Task CheckActionsGenericForThreeExpectedExceptiosnDoesNotThrow()
{
await Wait.Until<InvalidOperationException, InvalidProgramException, IndexOutOfRangeException>(() => CheckExceptionsVoidReturn(1, 3), TimeSpan.FromSeconds(2));
}

[Fact]
public async Task CheckFuncGenericForThreeExpectedExceptionsDoesNotThrow()
{
await Wait.Until<InvalidOperationException, InvalidProgramException, IndexOutOfRangeException>(() => CheckExceptionsBoolReturn(1, 3), TimeSpan.FromSeconds(2));
}

[Fact]
public async Task CheckActionsGenericForFourExpectedExceptiosnDoesNotThrow()
{
await Wait.Until<InvalidOperationException, InvalidProgramException, IndexOutOfRangeException, ArgumentNullException>(() => CheckExceptionsVoidReturn(1, 4), TimeSpan.FromSeconds(2));
}

[Fact]
public async Task CheckFuncGenericForFourExpectedExceptionsDoesNotThrow()
{
await Wait.Until<InvalidOperationException, InvalidProgramException, IndexOutOfRangeException, ArgumentNullException>(() => CheckExceptionsBoolReturn(1, 4), TimeSpan.FromSeconds(2));
}
}
}
43 changes: 43 additions & 0 deletions IntelliTect.IntelliWait.Tests/TypeCheckingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Threading.Tasks;
using Xunit;

namespace IntelliTect.IntelliWait.Tests
{
public class TypeCheckingTests : BaseTest
{
[Fact]
public async Task CheckActionParamsForInvalidTypeChecking()
{
Exception ex = await Assert.ThrowsAsync<ArgumentException>(
() => Wait.Until(ThrowExceptionWithNoReturn, TimeSpan.FromSeconds(1), typeof(string)));
Assert.Equal(ExceptionMessage, ex.Message);
}

[Fact]
public async Task CheckFuncParamsForInvalidTypeChecking()
{
Exception ex = await Assert.ThrowsAsync<ArgumentException>(
() => Wait.Until(ThrowExceptionWithReturn, TimeSpan.FromSeconds(1), typeof(string)));
Assert.Equal(ExceptionMessage, ex.Message);
}

[Fact]
public async Task CheckActionParamsForMixedInvalidTypeChecking()
{
Exception ex = await Assert.ThrowsAsync<ArgumentException>(
() => Wait.Until(ThrowNullRefExceptionWithNoReturn, TimeSpan.FromSeconds(1), typeof(Exception), typeof(string)));
Assert.Equal(ExceptionMessage, ex.Message);
}

[Fact]
public async Task CheckFuncParamsForMixedInvalidTypeChecking()
{
Exception ex = await Assert.ThrowsAsync<ArgumentException>(
() => Wait.Until(ThrowNullRefExceptionWithReturn, TimeSpan.FromSeconds(1), typeof(string), typeof(NullReferenceException)));
Assert.Equal(ExceptionMessage, ex.Message);
}

private const string ExceptionMessage = "Invalid type passed into exceptionsToIgnore parameter. Must be of type Exception.";
}
}
Loading