Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
Allow parallel tests to set their own mocks
Browse files Browse the repository at this point in the history
By storing the Current instance in AsyncLocal, it can be
properly set per-thread and propagated automatically across
async calls.

NOTE: since AsyncLocal<T> only exists in netstandard2.0, I
just removed netstandard1.0 ;). Of course, this is just to
show how we could do this, but if we support this for real,
we'd need to have different implementations for ns1.0 vs
ns2.0.
  • Loading branch information
kzu committed Mar 14, 2018
1 parent 227e9d9 commit 85da8b4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
8 changes: 6 additions & 2 deletions Caboodle/Caboodle.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<PropertyGroup>
<!--Work around so the conditions work below-->
<TargetFrameworks></TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">netstandard1.0;netstandard2.0;Xamarin.iOS10;MonoAndroid80;uap10.0.16299</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">netstandard1.0;netstandard2.0;Xamarin.iOS10;MonoAndroid80</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">netstandard2.0;net461;Xamarin.iOS10;MonoAndroid80;uap10.0.16299</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">netstandard2.0;net461;Xamarin.iOS10;MonoAndroid80</TargetFrameworks>
<AssemblyName>Microsoft.Caboodle</AssemblyName>
<RootNamespace>Microsoft.Caboodle</RootNamespace>
<PackageId>Microsoft.Caboodle</PackageId>
Expand Down Expand Up @@ -42,8 +42,12 @@
<ItemGroup>
<PackageReference Include="mdoc" Version="5.5.0" PrivateAssets="All" />
<PackageReference Include="MSBuild.Sdk.Extras" Version="1.2.0" PrivateAssets="All" />

<Compile Include="**\*.shared.cs" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETFramework' ">
<Compile Include="**\*.net.cs" />
</ItemGroup>
<ItemGroup Condition=" $(TargetFramework.StartsWith('netstandard')) ">
<Compile Include="**\*.netstandard.cs" />
</ItemGroup>
Expand Down
9 changes: 8 additions & 1 deletion Caboodle/Geocoding/Geocoding.netstandard.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.Caboodle
{
public partial class Geocoding
{
public static IGeocoding Current { get; set; }
static AsyncLocal<IGeocoding> current;

public static IGeocoding Current
{
get => current.Value;
set => current.Value = value;
}

public static Task<IEnumerable<Placemark>> GetPlacemarksAsync(double latitude, double longitude) =>
Current?.GetPlacemarksAsync(latitude, longitude) ?? throw new NotImplentedInReferenceAssembly();
Expand Down

0 comments on commit 85da8b4

Please sign in to comment.