diff --git a/src/core/Akka.TestKit/TestKitBase.cs b/src/core/Akka.TestKit/TestKitBase.cs index d4e2c4f8746..d00870f5b9e 100644 --- a/src/core/Akka.TestKit/TestKitBase.cs +++ b/src/core/Akka.TestKit/TestKitBase.cs @@ -6,6 +6,8 @@ //----------------------------------------------------------------------- using System; +using System.Diagnostics; +using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using Akka.Actor; @@ -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)) { @@ -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"); + } + /// /// Initializes the for a new spec. ///