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
4 changes: 2 additions & 2 deletions src/Stunts.UnitTests/BehaviorPipelineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,13 @@ public async Task WhenRunningParallel_ThenCanReplaceLocalPipelineFactory()
await Task.WhenAll(
Task.Run(() =>
{
BehaviorPipelineFactory.LocalDefault = factory1;
using var ambient = BehaviorPipelineFactory.UseAmbient(factory1);
Thread.Sleep(50);
Assert.Same(factory1, BehaviorPipelineFactory.Default);
}),
Task.Run(() =>
{
BehaviorPipelineFactory.LocalDefault = factory2;
using var ambient = BehaviorPipelineFactory.UseAmbient(factory2);
Thread.Sleep(50);
Assert.Same(factory2, BehaviorPipelineFactory.Default);
})
Expand Down
14 changes: 3 additions & 11 deletions src/Stunts.UnitTests/Castle/ClassProxyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,10 @@ public void ParamsConstructorAcceptsAnArray()

public void VirtualCallDuringConstructionUsesThePipelineFactory()
{
var previous = BehaviorPipelineFactory.LocalDefault;
BehaviorPipelineFactory.LocalDefault = new CtorBehaviorFactory();
try
{
var stunt = Stunt.Of<CtorCaller>();
using var ambient = BehaviorPipelineFactory.UseAmbient(new CtorBehaviorFactory());
var stunt = Stunt.Of<CtorCaller>();

Assert.Equal("proxy", stunt.Seen);
}
finally
{
BehaviorPipelineFactory.LocalDefault = previous;
}
Assert.Equal("proxy", stunt.Seen);
}

public void NestedClassAndCharReturn()
Expand Down
2 changes: 1 addition & 1 deletion src/Stunts.UnitTests/DynamicStuntFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void DelegateWithoutImplementationThrows()
[Fact]
public void ConstructorInterceptionNotSupported()
{
BehaviorPipelineFactory.LocalDefault = new RecordingBehaviorPipelineFactory();
using var ambient = BehaviorPipelineFactory.UseAmbient(new RecordingBehaviorPipelineFactory());
StuntFactory.LocalDefault = new DynamicStuntFactory();

var calculator = Stunt.Of<ICalculator>();
Expand Down
2 changes: 1 addition & 1 deletion src/Stunts.UnitTests/Scenarios/InterfaceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Test : IRunnable
{
public void Run()
{
BehaviorPipelineFactory.LocalDefault = new RecordingBehaviorPipelineFactory();
using var ambient = BehaviorPipelineFactory.UseAmbient(new RecordingBehaviorPipelineFactory());
var stunt = Stunt.Of<IBasicInterface>();

Assert.NotNull(stunt);
Expand Down
27 changes: 19 additions & 8 deletions src/Stunts/BehaviorPipelineFactory.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;
using System.Threading;

namespace Stunts
{
/// <summary>
/// Provides the global <see cref="Default"/> and <see cref="LocalDefault"/>

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-macOS-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-macOS-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-macOS-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-macOS-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-macOS-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-macOS-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-macOS-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-macOS-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-macOS-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-macOS-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-macOS-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-macOS-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-windows-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-windows-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-windows-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-windows-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-windows-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-windows-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-windows-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-windows-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-windows-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-windows-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-windows-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved

Check warning on line 7 in src/Stunts/BehaviorPipelineFactory.cs

View workflow job for this annotation

GitHub Actions / build-windows-latest

XML comment has cref attribute 'LocalDefault' that could not be resolved
/// behavior pipeline factory used when creating new stunts.
/// </summary>
/// <remarks>
Expand All @@ -24,8 +25,8 @@
/// for creating the initial pipelines used during a stunt instantiation.
/// </summary>
/// <remarks>
/// A <see cref="LocalDefault"/> can override the value of this global
/// default, if assigned to a non-null value.
/// An ambient <see cref="IBehaviorPipelineFactory"/> can override the value of this global
/// default, via <see cref="UseAmbient(IBehaviorPipelineFactory)"/>. This is typically only needed for testing.
/// </remarks>
public static IBehaviorPipelineFactory Default
{
Expand All @@ -34,14 +35,24 @@
}

/// <summary>
/// Gets or sets the <see cref="IBehaviorPipelineFactory"/> to use
/// in the current (async) flow, so it does not affect other threads/flows.
/// This is typically used in tests to isolate the default pipeline configurations.
/// Sets an ambient <see cref="IBehaviorPipelineFactory"/> to use for <see cref="Default"/> for the current execution context.
/// </summary>
public static IBehaviorPipelineFactory? LocalDefault
/// <param name="factory">The <see cref="IBehaviorPipelineFactory"/> to set as the ambient <see cref="Default"/>.</param>
/// <returns>An <see cref="IDisposable"/> that, when disposed, restores the original execution context default.</returns>
public static IDisposable UseAmbient(IBehaviorPipelineFactory factory) => new AmbientDisposable(localFactory, factory);

class AmbientDisposable : IDisposable
{
get => localFactory.Value;
set => localFactory.Value = value;
readonly AsyncLocal<IBehaviorPipelineFactory?> storage;
readonly IBehaviorPipelineFactory? original;

public AmbientDisposable(AsyncLocal<IBehaviorPipelineFactory?> storage, IBehaviorPipelineFactory factory)
{
this.storage = storage;
original = storage.Value;
storage.Value = factory;
}
public void Dispose() => storage.Value = original;
}

class DefaultBehaviorPipelineFactory : IBehaviorPipelineFactory
Expand Down
Loading