Skip to content

Commit

Permalink
Convert AwaitCondition in TestKitBase initializer, can not use `.Wait…
Browse files Browse the repository at this point in the history
…()` in ctor (#5721)
  • Loading branch information
Arkatufus authored Mar 16, 2022
1 parent 31493d8 commit fccb23f
Showing 1 changed file with 29 additions and 6 deletions.
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

0 comments on commit fccb23f

Please sign in to comment.