Skip to content

Commit 1b2e3ab

Browse files
Make TestKit startup timeouts configurable (#7423)
* Added `startup-timeout` property to `TestKitSettings` * integrated testkit timeout into xUnit2 and testkit base close #7259 * API approvals and tests
1 parent d4aa3fa commit 1b2e3ab

File tree

8 files changed

+28
-23
lines changed

8 files changed

+28
-23
lines changed

src/contrib/testkits/Akka.TestKit.Xunit2/TestKit.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ protected void InitializeLogger(ActorSystem system)
142142
{
143143
var extSystem = (ExtendedActorSystem)system;
144144
var logger = extSystem.SystemActorOf(Props.Create(() => new TestOutputLogger(Output)), "log-test");
145-
logger.Ask<LoggerInitialized>(new InitializeLogger(system.EventStream), TimeSpan.FromSeconds(3))
145+
logger.Ask<LoggerInitialized>(new InitializeLogger(system.EventStream), TestKitSettings.TestKitStartupTimeout)
146146
.ConfigureAwait(false).GetAwaiter().GetResult();
147147
}
148148
}
@@ -154,7 +154,7 @@ protected void InitializeLogger(ActorSystem system, string prefix)
154154
var extSystem = (ExtendedActorSystem)system;
155155
var logger = extSystem.SystemActorOf(Props.Create(() => new TestOutputLogger(
156156
string.IsNullOrEmpty(prefix) ? Output : new PrefixedOutput(Output, prefix))), "log-test");
157-
logger.Ask<LoggerInitialized>(new InitializeLogger(system.EventStream), TimeSpan.FromSeconds(3))
157+
logger.Ask<LoggerInitialized>(new InitializeLogger(system.EventStream), TestKitSettings.TestKitStartupTimeout)
158158
.ConfigureAwait(false).GetAwaiter().GetResult();
159159
}
160160
}

src/core/Akka.API.Tests/verify/CoreAPISpec.ApproveTestKit.DotNet.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ namespace Akka.TestKit
522522
public bool LogTestKitCalls { get; }
523523
public System.TimeSpan SingleExpectDefault { get; }
524524
public System.TimeSpan TestEventFilterLeeway { get; }
525+
public System.TimeSpan TestKitStartupTimeout { get; }
525526
public double TestTimeFactor { get; }
526527
}
527528
public class TestLatch

src/core/Akka.API.Tests/verify/CoreAPISpec.ApproveTestKit.Net.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ namespace Akka.TestKit
522522
public bool LogTestKitCalls { get; }
523523
public System.TimeSpan SingleExpectDefault { get; }
524524
public System.TimeSpan TestEventFilterLeeway { get; }
525+
public System.TimeSpan TestKitStartupTimeout { get; }
525526
public double TestTimeFactor { get; }
526527
}
527528
public class TestLatch

src/core/Akka.TestKit.Tests/TestKit_Config_Tests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public void DefaultValues_should_be_correct()
2121
TestKitSettings.SingleExpectDefault.ShouldBe(TimeSpan.FromSeconds(3));
2222
TestKitSettings.TestEventFilterLeeway.ShouldBe(TimeSpan.FromSeconds(3));
2323
TestKitSettings.TestTimeFactor.ShouldBe(1);
24+
TestKitSettings.TestKitStartupTimeout.ShouldBe(TimeSpan.FromSeconds(5));
2425
var callingThreadDispatcherTypeName = typeof(CallingThreadDispatcherConfigurator).FullName + ", " + typeof(CallingThreadDispatcher).Assembly.GetName().Name;
2526
Assert.False(Sys.Settings.Config.IsEmpty);
2627
Sys.Settings.Config.GetString("akka.test.calling-thread-dispatcher.type", null).ShouldBe(callingThreadDispatcherTypeName);

src/core/Akka.TestKit/Configs/TestScheduler.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ akka {
3131
# The timeout that is added as an implicit by DefaultTimeout trait
3232
# This is used for Ask-pattern
3333
default-timeout = 5s
34+
3435

3536
calling-thread-dispatcher {
3637
type = "Akka.TestKit.CallingThreadDispatcherConfigurator, Akka.TestKit"

src/core/Akka.TestKit/Internal/Reference.conf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ akka {
2626
# The timeout that is added as an implicit by DefaultTimeout trait
2727
# This is used for Ask-pattern
2828
default-timeout = 5s
29+
30+
# The amount of time it takes the testkit to startup
31+
# Increase this value if you're running many tests in parallel
32+
startup-timeout = 5s
2933

3034
calling-thread-dispatcher {
3135
type = "Akka.TestKit.CallingThreadDispatcherConfigurator, Akka.TestKit"

src/core/Akka.TestKit/TestKitBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ protected virtual void InitializeTest(ActorSystem system, ActorSystemSetup confi
173173
var testActor = CreateTestActor(system, testActorName);
174174

175175
// Wait for the testactor to start
176-
WaitUntilTestActorIsReady(testActor);
176+
WaitUntilTestActorIsReady(testActor, _testState.TestKitSettings);
177177

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

194194
[MethodImpl(MethodImplOptions.AggressiveInlining)]
195195
// Do not convert this method to async, it is being called inside the constructor.
196-
private static void WaitUntilTestActorIsReady(IActorRef testActor)
196+
private static void WaitUntilTestActorIsReady(IActorRef testActor, TestKitSettings settings)
197197
{
198-
var deadline = TimeSpan.FromSeconds(5);
198+
var deadline = settings.TestKitStartupTimeout;
199199
var stopwatch = Stopwatch.StartNew();
200200
var ready = false;
201201
try

src/core/Akka.TestKit/TestKitSettings.cs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@ namespace Akka.TestKit
1616
/// </summary>
1717
public class TestKitSettings : IExtension
1818
{
19-
private readonly TimeSpan _defaultTimeout;
20-
private readonly TimeSpan _singleExpectDefault;
21-
private readonly TimeSpan _testEventFilterLeeway;
22-
private readonly double _timefactor;
23-
private readonly bool _logTestKitCalls;
24-
2519
/// <summary>
2620
/// Initializes a new instance of the <see cref="TestKitSettings"/> class.
2721
/// </summary>
@@ -34,28 +28,31 @@ public TestKitSettings(Config config)
3428
if (config.IsNullOrEmpty())
3529
throw ConfigurationException.NullOrEmptyConfig<TestKitSettings>();
3630

37-
_defaultTimeout = config.GetTimeSpan("akka.test.default-timeout", null, allowInfinite:false);
38-
_singleExpectDefault = config.GetTimeSpan("akka.test.single-expect-default", null, allowInfinite: false);
39-
_testEventFilterLeeway = config.GetTimeSpan("akka.test.filter-leeway", null, allowInfinite: false);
40-
_timefactor = config.GetDouble("akka.test.timefactor", 0);
41-
_logTestKitCalls = config.GetBoolean("akka.test.testkit.debug", false);
31+
DefaultTimeout = config.GetTimeSpan("akka.test.default-timeout", null, allowInfinite:false);
32+
SingleExpectDefault = config.GetTimeSpan("akka.test.single-expect-default", null, allowInfinite: false);
33+
TestKitStartupTimeout = config.GetTimeSpan("akka.test.startup-timeout", null, allowInfinite: false);
34+
TestEventFilterLeeway = config.GetTimeSpan("akka.test.filter-leeway", null, allowInfinite: false);
35+
TestTimeFactor = config.GetDouble("akka.test.timefactor", 0);
36+
LogTestKitCalls = config.GetBoolean("akka.test.testkit.debug", false);
4237

43-
if(_timefactor <= 0)
44-
throw new ConfigurationException($@"Expected a positive value for ""akka.test.timefactor"" but found {_timefactor}");
38+
if(TestTimeFactor <= 0)
39+
throw new ConfigurationException($@"Expected a positive value for ""akka.test.timefactor"" but found {TestTimeFactor}");
4540
}
4641

4742

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

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

5752
/// <summary>Gets the config value "akka.test.filter-leeway". It is always finite.</summary>
58-
public TimeSpan TestEventFilterLeeway { get { return _testEventFilterLeeway; } }
53+
public TimeSpan TestEventFilterLeeway { get; }
54+
55+
public TimeSpan TestKitStartupTimeout { get; }
5956

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

7572
/// <summary>
7673
/// If set to <c>true</c> calls to testkit will be logged.
7774
/// This is enabled by setting the configuration value "akka.test.testkit.debug" to a true.
7875
/// </summary>
79-
public bool LogTestKitCalls { get { return _logTestKitCalls; } }
76+
public bool LogTestKitCalls { get; }
8077
}
8178
}

0 commit comments

Comments
 (0)