Skip to content

Commit

Permalink
Make TestKit startup timeouts configurable (#7423)
Browse files Browse the repository at this point in the history
* Added `startup-timeout` property to `TestKitSettings`

* integrated testkit timeout into xUnit2 and testkit base

close #7259

* API approvals and tests
  • Loading branch information
Aaronontheweb authored Dec 19, 2024
1 parent d4aa3fa commit 1b2e3ab
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/contrib/testkits/Akka.TestKit.Xunit2/TestKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ protected void InitializeLogger(ActorSystem system)
{
var extSystem = (ExtendedActorSystem)system;
var logger = extSystem.SystemActorOf(Props.Create(() => new TestOutputLogger(Output)), "log-test");
logger.Ask<LoggerInitialized>(new InitializeLogger(system.EventStream), TimeSpan.FromSeconds(3))
logger.Ask<LoggerInitialized>(new InitializeLogger(system.EventStream), TestKitSettings.TestKitStartupTimeout)
.ConfigureAwait(false).GetAwaiter().GetResult();
}
}
Expand All @@ -154,7 +154,7 @@ protected void InitializeLogger(ActorSystem system, string prefix)
var extSystem = (ExtendedActorSystem)system;
var logger = extSystem.SystemActorOf(Props.Create(() => new TestOutputLogger(
string.IsNullOrEmpty(prefix) ? Output : new PrefixedOutput(Output, prefix))), "log-test");
logger.Ask<LoggerInitialized>(new InitializeLogger(system.EventStream), TimeSpan.FromSeconds(3))
logger.Ask<LoggerInitialized>(new InitializeLogger(system.EventStream), TestKitSettings.TestKitStartupTimeout)
.ConfigureAwait(false).GetAwaiter().GetResult();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ namespace Akka.TestKit
public bool LogTestKitCalls { get; }
public System.TimeSpan SingleExpectDefault { get; }
public System.TimeSpan TestEventFilterLeeway { get; }
public System.TimeSpan TestKitStartupTimeout { get; }
public double TestTimeFactor { get; }
}
public class TestLatch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ namespace Akka.TestKit
public bool LogTestKitCalls { get; }
public System.TimeSpan SingleExpectDefault { get; }
public System.TimeSpan TestEventFilterLeeway { get; }
public System.TimeSpan TestKitStartupTimeout { get; }
public double TestTimeFactor { get; }
}
public class TestLatch
Expand Down
1 change: 1 addition & 0 deletions src/core/Akka.TestKit.Tests/TestKit_Config_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public void DefaultValues_should_be_correct()
TestKitSettings.SingleExpectDefault.ShouldBe(TimeSpan.FromSeconds(3));
TestKitSettings.TestEventFilterLeeway.ShouldBe(TimeSpan.FromSeconds(3));
TestKitSettings.TestTimeFactor.ShouldBe(1);
TestKitSettings.TestKitStartupTimeout.ShouldBe(TimeSpan.FromSeconds(5));
var callingThreadDispatcherTypeName = typeof(CallingThreadDispatcherConfigurator).FullName + ", " + typeof(CallingThreadDispatcher).Assembly.GetName().Name;
Assert.False(Sys.Settings.Config.IsEmpty);
Sys.Settings.Config.GetString("akka.test.calling-thread-dispatcher.type", null).ShouldBe(callingThreadDispatcherTypeName);
Expand Down
1 change: 1 addition & 0 deletions src/core/Akka.TestKit/Configs/TestScheduler.conf
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ akka {
# The timeout that is added as an implicit by DefaultTimeout trait
# This is used for Ask-pattern
default-timeout = 5s


calling-thread-dispatcher {
type = "Akka.TestKit.CallingThreadDispatcherConfigurator, Akka.TestKit"
Expand Down
4 changes: 4 additions & 0 deletions src/core/Akka.TestKit/Internal/Reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ akka {
# The timeout that is added as an implicit by DefaultTimeout trait
# This is used for Ask-pattern
default-timeout = 5s

# The amount of time it takes the testkit to startup
# Increase this value if you're running many tests in parallel
startup-timeout = 5s

calling-thread-dispatcher {
type = "Akka.TestKit.CallingThreadDispatcherConfigurator, Akka.TestKit"
Expand Down
6 changes: 3 additions & 3 deletions src/core/Akka.TestKit/TestKitBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ protected virtual void InitializeTest(ActorSystem system, ActorSystemSetup confi
var testActor = CreateTestActor(system, testActorName);

// Wait for the testactor to start
WaitUntilTestActorIsReady(testActor);
WaitUntilTestActorIsReady(testActor, _testState.TestKitSettings);

if (this is not INoImplicitSender)
{
Expand All @@ -193,9 +193,9 @@ protected virtual void InitializeTest(ActorSystem system, ActorSystemSetup confi

[MethodImpl(MethodImplOptions.AggressiveInlining)]
// Do not convert this method to async, it is being called inside the constructor.
private static void WaitUntilTestActorIsReady(IActorRef testActor)
private static void WaitUntilTestActorIsReady(IActorRef testActor, TestKitSettings settings)
{
var deadline = TimeSpan.FromSeconds(5);
var deadline = settings.TestKitStartupTimeout;
var stopwatch = Stopwatch.StartNew();
var ready = false;
try
Expand Down
33 changes: 15 additions & 18 deletions src/core/Akka.TestKit/TestKitSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ namespace Akka.TestKit
/// </summary>
public class TestKitSettings : IExtension
{
private readonly TimeSpan _defaultTimeout;
private readonly TimeSpan _singleExpectDefault;
private readonly TimeSpan _testEventFilterLeeway;
private readonly double _timefactor;
private readonly bool _logTestKitCalls;

/// <summary>
/// Initializes a new instance of the <see cref="TestKitSettings"/> class.
/// </summary>
Expand All @@ -34,28 +28,31 @@ public TestKitSettings(Config config)
if (config.IsNullOrEmpty())
throw ConfigurationException.NullOrEmptyConfig<TestKitSettings>();

_defaultTimeout = config.GetTimeSpan("akka.test.default-timeout", null, allowInfinite:false);
_singleExpectDefault = config.GetTimeSpan("akka.test.single-expect-default", null, allowInfinite: false);
_testEventFilterLeeway = config.GetTimeSpan("akka.test.filter-leeway", null, allowInfinite: false);
_timefactor = config.GetDouble("akka.test.timefactor", 0);
_logTestKitCalls = config.GetBoolean("akka.test.testkit.debug", false);
DefaultTimeout = config.GetTimeSpan("akka.test.default-timeout", null, allowInfinite:false);
SingleExpectDefault = config.GetTimeSpan("akka.test.single-expect-default", null, allowInfinite: false);
TestKitStartupTimeout = config.GetTimeSpan("akka.test.startup-timeout", null, allowInfinite: false);
TestEventFilterLeeway = config.GetTimeSpan("akka.test.filter-leeway", null, allowInfinite: false);
TestTimeFactor = config.GetDouble("akka.test.timefactor", 0);
LogTestKitCalls = config.GetBoolean("akka.test.testkit.debug", false);

if(_timefactor <= 0)
throw new ConfigurationException($@"Expected a positive value for ""akka.test.timefactor"" but found {_timefactor}");
if(TestTimeFactor <= 0)
throw new ConfigurationException($@"Expected a positive value for ""akka.test.timefactor"" but found {TestTimeFactor}");
}


/// <summary>
/// Gets the default timeout as specified in the setting akka.test.default-timeout.
/// Typically used for Ask-timeouts. It is always finite.
/// </summary>
public TimeSpan DefaultTimeout { get { return _defaultTimeout; } }
public TimeSpan DefaultTimeout { get; }

/// <summary>Gets the config value "akka.test.single-expect-default". It is always finite.</summary>
public TimeSpan SingleExpectDefault { get { return _singleExpectDefault; } }
public TimeSpan SingleExpectDefault { get; }

/// <summary>Gets the config value "akka.test.filter-leeway". It is always finite.</summary>
public TimeSpan TestEventFilterLeeway { get { return _testEventFilterLeeway; } }
public TimeSpan TestEventFilterLeeway { get; }

public TimeSpan TestKitStartupTimeout { get; }

/// <summary>
/// Gets the timefactor by which all values are scaled by.
Expand All @@ -70,12 +67,12 @@ public TestKitSettings(Config config)
/// <see cref="TestKitBase.Dilated">Testkit.Dilated</see>
/// </para>
/// </summary>
public double TestTimeFactor { get { return _timefactor; } }
public double TestTimeFactor { get; }

/// <summary>
/// If set to <c>true</c> calls to testkit will be logged.
/// This is enabled by setting the configuration value "akka.test.testkit.debug" to a true.
/// </summary>
public bool LogTestKitCalls { get { return _logTestKitCalls; } }
public bool LogTestKitCalls { get; }
}
}

0 comments on commit 1b2e3ab

Please sign in to comment.