Skip to content

Commit

Permalink
Play around to try and solve nsubstituteGH-26 - I've got it very wron…
Browse files Browse the repository at this point in the history
…g and have broken lot's of stuff

Furthermore, if it did work it wouldn't across multiple threads
However, it might spark an idea for other people...
  • Loading branch information
robdmoore committed Dec 29, 2012
1 parent 754dccc commit d4a740d
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Source/NSubstitute/Core/CallRouter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ public object Route(ICall call)

var routeToUseForThisCall = _currentRoute;
UseDefaultRouteForNextCall();
return routeToUseForThisCall.Handle(call);
using (_context.HandleCall(call))
{
return routeToUseForThisCall.Handle(call);
}
}

public void LastCallShouldReturn(IReturn returnValue, MatchArgs matchArgs)
Expand Down
21 changes: 21 additions & 0 deletions Source/NSubstitute/Core/DisposableAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

namespace NSubstitute.Core
{
public class DisposableAction : IDisposable
{
private readonly Action _action;

public DisposableAction(Action action)
{
_action = action;
}

public void Dispose()
{
_action();
}

public static readonly DisposableAction EmptyAction = new DisposableAction(() => {});
}
}
2 changes: 2 additions & 0 deletions Source/NSubstitute/Core/ISubstitutionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public interface ISubstitutionContext
void RaiseEventForNextCall(Func<ICall, object[]> getArguments);
IQueryResults RunQuery(Action calls);
bool IsQuerying { get; }
bool IsHandlingCall(ICall call);
DisposableAction HandleCall(ICall call);
void AddToQuery(object target, ICallSpecification callSpecification);
}
}
14 changes: 14 additions & 0 deletions Source/NSubstitute/Core/SubstitutionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class SubstitutionContext : ISubstitutionContext
readonly RobustThreadLocal<IList<IArgumentSpecification>> _argumentSpecifications = new RobustThreadLocal<IList<IArgumentSpecification>>(() => new List<IArgumentSpecification>());
readonly RobustThreadLocal<Func<ICall, object[]>> _getArgumentsForRaisingEvent = new RobustThreadLocal<Func<ICall, object[]>>();
readonly RobustThreadLocal<Query> _currentQuery = new RobustThreadLocal<Query>();
private ICall _callBeingHandled;

static SubstitutionContext()
{
Expand All @@ -43,6 +44,19 @@ public SubstitutionContext(ISubstituteFactory substituteFactory)
public ISubstituteFactory SubstituteFactory { get { return _substituteFactory; } }
public SequenceNumberGenerator SequenceNumberGenerator { get { return _sequenceNumberGenerator; } }
public bool IsQuerying { get { return _currentQuery.Value != null; } }
public bool IsHandlingCall(ICall call)
{
return _callBeingHandled == call;
}

public DisposableAction HandleCall(ICall call)
{
if (!IsHandlingCall(call))
return DisposableAction.EmptyAction;

_callBeingHandled = call;
return new DisposableAction(() => _callBeingHandled = null);
}

public void LastCallShouldReturn(IReturn value, MatchArgs matchArgs)
{
Expand Down
1 change: 1 addition & 0 deletions Source/NSubstitute/NSubstitute.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
<Compile Include="Core\SequenceInOrderAssertion.cs" />
<Compile Include="Core\SequenceNumberGenerator.cs" />
<Compile Include="Core\TypeInstanceNumberLookup.cs" />
<Compile Include="Core\DisposableAction.cs" />
<Compile Include="Exceptions\ArgumentIsNotOutOrRefException.cs" />
<Compile Include="Exceptions\ArgumentSetWithIncompatibleValueException.cs" />
<Compile Include="Exceptions\CallSequenceNotFoundException.cs" />
Expand Down
3 changes: 3 additions & 0 deletions Source/NSubstitute/Routing/Handlers/RecordCallHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public RecordCallHandler(ICallStack callStack, SequenceNumberGenerator generator

public RouteAction Handle(ICall call)
{
if (!SubstitutionContext.Current.IsHandlingCall(call))
return RouteAction.Continue();

call.AssignSequenceNumber(_generator.Next());
_callStack.Push(call);
return RouteAction.Continue();
Expand Down

0 comments on commit d4a740d

Please sign in to comment.