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
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ namespace Akka.TestKit
class ActorCellKeepingSynchronizationContext : SynchronizationContext
{
private readonly ActorCell _cell;

internal static ActorCell AsyncCache { get; set; }

/// <summary>
/// TBD
Expand All @@ -46,7 +44,8 @@ public override void Post(SendOrPostCallback d, object state)
var oldCell = InternalCurrentActorCellKeeper.Current;
var oldContext = Current;
SetSynchronizationContext(this);
InternalCurrentActorCellKeeper.Current = AsyncCache ?? _cell;

InternalCurrentActorCellKeeper.Current = _cell;

try
{
Expand Down
18 changes: 17 additions & 1 deletion src/core/Akka.TestKit/CallingThreadDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//-----------------------------------------------------------------------

using System;
using System.Threading;
using Akka.Configuration;
using Akka.Dispatch;

Expand Down Expand Up @@ -53,7 +54,22 @@ public CallingThreadDispatcher(MessageDispatcherConfigurator configurator) : bas

protected override void ExecuteTask(IRunnable run)
{
run.Run();
var currentSyncContext = SynchronizationContext.Current;

try
{
// Actors should not run with ActorCellKeepingSynchronizationContext
// (or any sync context that wraps ActorCellKeepingSynchronizationContext, e.g. Xunit's AsyncTestSyncContext)
// otherwise continuations in async message handlers will use ActorCellKeepingSynchronizationContext
// instead of ActorTaskScheduler which causes ActorContext to be incorrect.
SynchronizationContext.SetSynchronizationContext(null);

run.Run();
}
finally
{
SynchronizationContext.SetSynchronizationContext(currentSyncContext);
}
}

protected override void Shutdown()
Expand Down
12 changes: 0 additions & 12 deletions src/core/Akka.TestKit/Internal/InternalTestActorRef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,18 +323,6 @@ internal TestActorTaskScheduler(ActorCell testActorCell, Action<object, Exceptio
_testActorCell = (TestActorCell) testActorCell;
}

/// <inheritdoc />
protected override void OnBeforeTaskStarted()
{
ActorCellKeepingSynchronizationContext.AsyncCache = _testActorCell;
}

/// <inheritdoc />
protected override void OnAfterTaskCompleted()
{
ActorCellKeepingSynchronizationContext.AsyncCache = null;
}

public void OnTaskCompleted(object message, Exception exception)
{
_taskCallback(message, exception);
Expand Down
Loading