Skip to content
Merged
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
35 changes: 29 additions & 6 deletions src/core/Akka.TestKit/TestKitBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
//-----------------------------------------------------------------------

using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Akka.Actor;
Expand Down Expand Up @@ -166,12 +168,9 @@ protected void InitializeTest(ActorSystem system, ActorSystemSetup config, strin
testActorName = "testActor" + _testActorId.IncrementAndGet();

var testActor = CreateTestActor(system, testActorName);
//Wait for the testactor to start
// Calling sync version here, since .Wait() causes deadlock
AwaitCondition(() =>
{
return !(testActor is IRepointableRef repRef) || repRef.IsStarted;
}, TimeSpan.FromSeconds(5), TimeSpan.FromMilliseconds(10));

// Wait for the testactor to start
WaitUntilTestActorIsReady(testActor);

if (!(this is INoImplicitSender))
{
Expand All @@ -189,6 +188,30 @@ protected void InitializeTest(ActorSystem system, ActorSystemSetup config, strin
_testState.TestActor = testActor;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void WaitUntilTestActorIsReady(IActorRef testActor)
{
var deadline = TimeSpan.FromSeconds(5);
var stopwatch = Stopwatch.StartNew();
var ready = false;
try
{
while (stopwatch.Elapsed < deadline)
{
ready = !(testActor is IRepointableRef repRef) || repRef.IsStarted;
if (ready) break;
Thread.Sleep(10);
}
}
finally
{
stopwatch.Stop();
}

if (!ready)
throw new Exception("Timeout waiting for test actor to be ready");
}

/// <summary>
/// Initializes the <see cref="TestState"/> for a new spec.
/// </summary>
Expand Down