Skip to content

Commit

Permalink
Merge pull request #40 from dennisdoomen/EnableMoreAsync
Browse files Browse the repository at this point in the history
Enable async code blocks on all Given methods
  • Loading branch information
Erwinvandervalk authored Sep 6, 2016
2 parents 1db2269 + 1a4c989 commit ba55273
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 63 deletions.
2 changes: 1 addition & 1 deletion Build.Packages.bat
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
@echo off
powershell -NoProfile -ExecutionPolicy unrestricted -Command ".\Build\psake.ps1 .\Build\default.ps1
powershell -NoProfile -ExecutionPolicy unrestricted -Command ".\build.ps1"
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
<ProjectConfiguration>
<ProjectConfiguration>
<AutoDetectNugetBuildDependencies>true</AutoDetectNugetBuildDependencies>
<BuildPriority>1000</BuildPriority>
<CopyReferencedAssembliesToWorkspace>false</CopyReferencedAssembliesToWorkspace>
<ConsiderInconclusiveTestsAsPassing>false</ConsiderInconclusiveTestsAsPassing>
<PreloadReferencedAssemblies>false</PreloadReferencedAssemblies>
<AllowDynamicCodeContractChecking>true</AllowDynamicCodeContractChecking>
<AllowStaticCodeContractChecking>false</AllowStaticCodeContractChecking>
<AllowCodeAnalysis>false</AllowCodeAnalysis>
<IgnoreThisComponentCompletely>true</IgnoreThisComponentCompletely>
<IgnoreThisComponentCompletely>false</IgnoreThisComponentCompletely>
<RunPreBuildEvents>false</RunPreBuildEvents>
<RunPostBuildEvents>false</RunPostBuildEvents>
<PreviouslyBuiltSuccessfully>true</PreviouslyBuiltSuccessfully>
<TrackFileDependencies>false</TrackFileDependencies>
<InstrumentAssembly>true</InstrumentAssembly>
<PreventSigningOfAssembly>false</PreventSigningOfAssembly>
<AnalyseExecutionTimes>true</AnalyseExecutionTimes>
Expand Down
34 changes: 0 additions & 34 deletions Source/Core/Chill.Net45.Tests/AsyncSpecs.cs

This file was deleted.

5 changes: 2 additions & 3 deletions Source/Core/Chill.Net45.Tests/Chill.Net45.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>TRACE;DEBUG;WINRT</DefineConstants>
<DefineConstants>TRACE;DEBUG;NET45</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE;WINRT</DefineConstants>
<DefineConstants>TRACE;NET45</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
Expand Down Expand Up @@ -81,7 +81,6 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="AsyncSpecs.cs" />
<Compile Include="TinyIocSpecs.cs" />
<Compile Include="UnityChillContainerSpecs.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
1 change: 1 addition & 0 deletions Source/Core/Chill.Shared/Chill.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<Compile Include="$(MSBuildThisFileDirectory)StateBuilders\IStoreStateBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)StateBuilders\StoreStateBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)StateBuilders\StoreStateBuilderExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TaskExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TestBase.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TestFor.cs" />
</ItemGroup>
Expand Down
19 changes: 0 additions & 19 deletions Source/Core/Chill.Shared/GivenSubject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,23 +169,4 @@ public void Given(Action a)
a();
}
}

public static class TaskExtensions
{
public static void WaitAndFlattenExceptions(this Task t)
{
try
{
#if NET45
Task.Run(() => t.Wait()).Wait();
#else
t.Wait();
#endif
}
catch (AggregateException aggregateException)
{
throw aggregateException.Flatten();
}
}
}
}
29 changes: 26 additions & 3 deletions Source/Core/Chill.Shared/GivenWhenThen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,19 @@ protected void When(Func<Task<TResult>> whenFunc, bool? deferedExecution = null)
#if NET45
When(() => Task.Run(whenFunc).Result, deferedExecution);
#else
When(() => Task.Run(whenFunc).Result, deferedExecution);
When(() => Task.Factory.StartNew(() => whenFunc().Result).Result, deferedExecution);
#endif
}

/// <summary>
/// Records an asynchronous precondition.
/// </summary>
public void Given(Func<Task> givenActionAsync)
{
#if NET45
Given(() => Task.Run(givenActionAsync).Wait());
#else
Given(() => Task.Factory.StartNew(() => givenActionAsync().Wait()).Wait());
#endif
}

Expand All @@ -88,7 +100,6 @@ public void Given(Action a)
EnsureContainer();
a();
}

}

/// <summary>
Expand Down Expand Up @@ -144,13 +155,25 @@ public void When(Func<Task> whenActionAsync, bool? deferedExecution = null)
#if NET45
When(() => Task.Run(whenActionAsync).Wait(), deferedExecution);
#else
When(() => whenActionAsync().Wait(), deferedExecution);
When(() => Task.Factory.StartNew(() => whenActionAsync().Wait()).Wait(), deferedExecution);
#endif
}

internal override void TriggerTest(bool expectExceptions)
{
TriggerTest(() => whenAction(), expectExceptions);
}

/// <summary>
/// Records an asynchronous precondition.
/// </summary>
public void Given(Func<Task> givenActionAsync)
{
#if NET45
Given(() => Task.Run(givenActionAsync).Wait());
#else
Given(() => Task.Factory.StartNew(() => givenActionAsync().Wait()).Wait());
#endif
}

/// <summary>
Expand Down
24 changes: 24 additions & 0 deletions Source/Core/Chill.Shared/TaskExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Threading.Tasks;

namespace Chill
{
internal static class TaskExtensions
{
public static void WaitAndFlattenExceptions(this Task t)
{
try
{
#if NET45
Task.Run(() => t.Wait()).Wait();
#else
Task.Factory.StartNew(t.Wait).Wait();
#endif
}
catch (AggregateException aggregateException)
{
throw aggregateException.Flatten();
}
}
}
}
64 changes: 64 additions & 0 deletions Source/Core/Chill.Tests.Shared/AsyncSpecs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.Collections.Concurrent;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Xunit;

namespace Chill.Tests.Shared
{
namespace AsyncSpecs
{
public class When_an_async_act_is_used : GivenWhenThen
{
private BlockingCollection<int> results = new BlockingCollection<int>();

public When_an_async_act_is_used()
{
When(async () =>
{
foreach (int key in Enumerable.Range(0, 1000))
{
#if NET45
await Task.Delay(10);
#else
await TaskEx.Delay(10);
#endif
results.Add(key);
}
});
}

[Fact]
public void Then_it_should_evaluate_the_sync_code_synchronously()
{
results.Should().HaveCount(1000);
}
}
public class When_an_async_arrange_is_used : GivenWhenThen
{
private BlockingCollection<int> results = new BlockingCollection<int>();

public When_an_async_arrange_is_used()
{
Given(async () =>
{
foreach (int key in Enumerable.Range(0, 1000))
{
#if NET45
await Task.Delay(10);
#else
await TaskEx.Delay(10);
#endif
results.Add(key);
}
});
}

[Fact]
public void Then_it_should_evaluate_the_sync_code_synchronously()
{
results.Should().HaveCount(1000);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<Import_RootNamespace>Chill.Tests</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)AsyncSpecs.cs" />
<Compile Include="$(MSBuildThisFileDirectory)AutoMotherSpecs.cs" />
<Compile Include="$(MSBuildThisFileDirectory)CoreScenarios\AutofacChillContainerSpecs.cs" />
<Compile Include="$(MSBuildThisFileDirectory)CoreScenarios\AutoFacNSubstituteSpecs.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private class AsyncService : IAsyncService
{
public async Task<bool> DoSomething()
{
#if WINRT
#if NET45
await Task.Delay(100);
#else
await TaskEx.Delay(100);
Expand Down

0 comments on commit ba55273

Please sign in to comment.