Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make TestKit startup timeouts configurable #7423

Merged
merged 3 commits into from
Dec 19, 2024
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
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));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Validate that this value can be written / overwritten via HOCON.

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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New setting here - you can increase this value to give tests more time to initialize, which might be useful if you're running many in parallel.


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; }
}
}
Loading