Skip to content

Commit df4b37a

Browse files
committed
Adding a demo test of AutoRetryAttribute.
1 parent 4922729 commit df4b37a

File tree

5 files changed

+126
-5
lines changed

5 files changed

+126
-5
lines changed

Framework/PostSharp.Samples.AutoRetry/AutoRetryAttribute.cs

+10-4
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,11 @@ public override void OnInvoke(MethodInterceptionArgs args)
7373
Console.WriteLine(
7474
"Method failed with exception {0}. Sleeping {1} s and retrying. This was our attempt #{2}.",
7575
e.GetType().Namespace, Delay, i + 1);
76-
77-
Thread.Sleep(TimeSpan.FromSeconds(Delay));
76+
77+
if (Delay > 0)
78+
{
79+
Thread.Sleep(TimeSpan.FromSeconds(Delay));
80+
}
7881

7982
// Continue to the next iteration.
8083
}
@@ -110,8 +113,11 @@ public override async Task OnInvokeAsync(MethodInterceptionArgs args)
110113
Console.WriteLine(
111114
"Method failed with exception {0}. Sleeping {1} s and retrying. This was our attempt #{2}.",
112115
e.GetType().Namespace, Delay, i + 1);
113-
114-
await Task.Delay(TimeSpan.FromSeconds(Delay));
116+
117+
if (Delay > 0)
118+
{
119+
await Task.Delay(TimeSpan.FromSeconds(Delay));
120+
}
115121

116122
// Continue to the next iteration.
117123
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace PostSharp.Samples.AutoRetry
9+
{
10+
11+
[TestClass]
12+
public class AutoRetryTest
13+
{
14+
int counter;
15+
16+
17+
// Success tests: the number of failures is SMALLER than the number of retries.
18+
[DataTestMethod]
19+
[DataRow(0)]
20+
[DataRow(1)]
21+
[DataRow(2)]
22+
public void SucceedingTest(int failures)
23+
{
24+
counter = 0;
25+
TestMethod(failures);
26+
27+
}
28+
29+
30+
// Failure tests: the number of failures is SMALLER than the number of retries.
31+
[DataTestMethod]
32+
[DataRow(3)]
33+
[DataRow(4)]
34+
[ExpectedException(typeof(TestException))]
35+
36+
public void FailingTest(int failures)
37+
{
38+
counter = 0;
39+
TestMethod(failures);
40+
41+
}
42+
43+
44+
45+
46+
[AutoRetry(MaxRetries = 3, HandledExceptions = new Type[] { typeof(TestException) }, Delay = 0)]
47+
private void TestMethod(int failures)
48+
{
49+
this.counter++;
50+
51+
if (this.counter - 1 <= failures)
52+
{
53+
throw new TestException();
54+
}
55+
56+
}
57+
58+
}
59+
60+
61+
public class TestException : Exception
62+
{
63+
}
64+
}

Framework/PostSharp.Samples.AutoRetry/PostSharp.Samples.AutoRetry.csproj

+17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="..\..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.props')" />
34
<Import Project="..\..\packages\PostSharp.6.2.7\build\PostSharp.props" Condition="Exists('..\..\packages\PostSharp.6.2.7\build\PostSharp.props')" />
45
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
56
<PropertyGroup>
@@ -40,11 +41,23 @@
4041
<Prefer32Bit>false</Prefer32Bit>
4142
</PropertyGroup>
4243
<ItemGroup>
44+
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
45+
<HintPath>..\..\packages\MSTest.TestFramework.1.4.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
46+
</Reference>
47+
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
48+
<HintPath>..\..\packages\MSTest.TestFramework.1.4.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
49+
</Reference>
4350
<Reference Include="PostSharp, Version=6.2.7.0, Culture=neutral, PublicKeyToken=b13fd38b8f9c99d7, processorArchitecture=MSIL">
4451
<HintPath>..\..\packages\PostSharp.Redist.6.2.7\lib\net45\PostSharp.dll</HintPath>
4552
</Reference>
4653
<Reference Include="System" />
54+
<Reference Include="System.ComponentModel.Composition" />
4755
<Reference Include="System.Core" />
56+
<Reference Include="System.IO.Compression" />
57+
<Reference Include="System.Numerics" />
58+
<Reference Include="System.Runtime.InteropServices.RuntimeInformation, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
59+
<HintPath>..\..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
60+
</Reference>
4861
<Reference Include="System.Xml.Linq" />
4962
<Reference Include="System.Data.DataSetExtensions" />
5063
<Reference Include="Microsoft.CSharp" />
@@ -54,6 +67,7 @@
5467
</ItemGroup>
5568
<ItemGroup>
5669
<Compile Include="AutoRetryAttribute.cs" />
70+
<Compile Include="AutoRetryTest.cs" />
5771
<Compile Include="Program.cs" />
5872
<Compile Include="Properties\AssemblyInfo.cs" />
5973
</ItemGroup>
@@ -69,8 +83,11 @@
6983
</PropertyGroup>
7084
<Error Condition="!Exists('..\..\packages\PostSharp.6.2.7\build\PostSharp.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\PostSharp.6.2.7\build\PostSharp.props'))" />
7185
<Error Condition="!Exists('..\..\packages\PostSharp.6.2.7\build\PostSharp.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\PostSharp.6.2.7\build\PostSharp.targets'))" />
86+
<Error Condition="!Exists('..\..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.props'))" />
87+
<Error Condition="!Exists('..\..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.targets'))" />
7288
</Target>
7389
<Import Project="..\..\packages\PostSharp.6.2.7\build\PostSharp.targets" Condition="Exists('..\..\packages\PostSharp.6.2.7\build\PostSharp.targets')" />
90+
<Import Project="..\..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.targets')" />
7491
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
7592
Other similar extension points exist, see Microsoft.Common.targets.
7693
<Target Name="BeforeBuild">

Framework/PostSharp.Samples.AutoRetry/Program.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ private static async Task DownloadFileAsync(string url)
9494
private static void WriteMessage(string message)
9595
{
9696
Console.WriteLine("{0} ms - {1}", stopwatch.ElapsedMilliseconds, message);
97-
}
97+
}
98+
9899
}
99100
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3+
<package id="Microsoft.NETCore.Platforms" version="1.1.0" targetFramework="net45" />
4+
<package id="MSTest.TestAdapter" version="1.4.0" targetFramework="net45" />
5+
<package id="MSTest.TestFramework" version="1.4.0" targetFramework="net45" />
6+
<package id="NETStandard.Library" version="1.6.1" targetFramework="net45" />
37
<package id="PostSharp" version="6.2.7" targetFramework="net45" developmentDependency="true" />
48
<package id="PostSharp.Redist" version="6.2.7" targetFramework="net45" />
9+
<package id="System.Collections" version="4.3.0" targetFramework="net45" />
10+
<package id="System.Collections.Concurrent" version="4.3.0" targetFramework="net45" />
11+
<package id="System.Diagnostics.Debug" version="4.3.0" targetFramework="net45" />
12+
<package id="System.Diagnostics.Tools" version="4.3.0" targetFramework="net45" />
13+
<package id="System.Diagnostics.Tracing" version="4.3.0" targetFramework="net45" />
14+
<package id="System.Globalization" version="4.3.0" targetFramework="net45" />
15+
<package id="System.IO" version="4.3.0" targetFramework="net45" />
16+
<package id="System.IO.Compression" version="4.3.0" targetFramework="net45" />
17+
<package id="System.Linq" version="4.3.0" targetFramework="net45" />
18+
<package id="System.Linq.Expressions" version="4.3.0" targetFramework="net45" />
19+
<package id="System.Net.Http" version="4.3.0" targetFramework="net45" />
20+
<package id="System.Net.Primitives" version="4.3.0" targetFramework="net45" />
21+
<package id="System.ObjectModel" version="4.3.0" targetFramework="net45" />
22+
<package id="System.Reflection" version="4.3.0" targetFramework="net45" />
23+
<package id="System.Reflection.Extensions" version="4.3.0" targetFramework="net45" />
24+
<package id="System.Reflection.Primitives" version="4.3.0" targetFramework="net45" />
25+
<package id="System.Resources.ResourceManager" version="4.3.0" targetFramework="net45" />
26+
<package id="System.Runtime" version="4.3.0" targetFramework="net45" />
27+
<package id="System.Runtime.Extensions" version="4.3.0" targetFramework="net45" />
28+
<package id="System.Runtime.InteropServices" version="4.3.0" targetFramework="net45" />
29+
<package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net45" />
30+
<package id="System.Runtime.Numerics" version="4.3.0" targetFramework="net45" />
31+
<package id="System.Text.Encoding" version="4.3.0" targetFramework="net45" />
32+
<package id="System.Text.Encoding.Extensions" version="4.3.0" targetFramework="net45" />
33+
<package id="System.Text.RegularExpressions" version="4.3.0" targetFramework="net45" />
34+
<package id="System.Threading" version="4.3.0" targetFramework="net45" />
35+
<package id="System.Threading.Tasks" version="4.3.0" targetFramework="net45" />
36+
<package id="System.Xml.ReaderWriter" version="4.3.0" targetFramework="net45" />
37+
<package id="System.Xml.XDocument" version="4.3.0" targetFramework="net45" />
538
</packages>

0 commit comments

Comments
 (0)