Skip to content

Conversation

Aaronontheweb
Copy link
Member

Summary

Replaces Thread.Sleep(10) with SpinWait in TestKit initialization to reduce startup failures and improve performance.

Fixes #7744

Changes

  • Replace Thread.Sleep(10) with SpinWait in WaitUntilTestActorIsReady
  • SpinWait provides adaptive waiting that detects TestActor readiness in microseconds
  • Better thread yielding under resource pressure

Performance Impact

  • Before: Always waits minimum 10ms even if TestActor ready in microseconds
  • After: Detects readiness almost instantly, yields properly under load
  • Should significantly reduce "Timeout waiting for test actor to be ready" failures in CI

Compatibility

This is a non-breaking internal optimization. All existing tests continue to work exactly as before.

- Changed WaitUntilTestActorIsReady to use SpinWait instead of Thread.Sleep(10)
- SpinWait provides better performance under both light and heavy load
- Detects TestActor readiness in microseconds instead of minimum 10ms
- Yields more efficiently to other threads under resource pressure
- Should reduce TestKit timeout failures in CI environments

This addresses the sync-over-async issue without breaking changes.
var logger = extSystem.SystemActorOf(Props.Create(() => new TestOutputLogger(Output)), "log-test");
logger.Ask<LoggerInitialized>(new InitializeLogger(system.EventStream), TestKitSettings.TestKitStartupTimeout)
.ConfigureAwait(false).GetAwaiter().GetResult();
// Start the logger initialization task but don't wait for it yet
Copy link
Member Author

Choose a reason for hiding this comment

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

The idea behind this was to be able to wrap it around the WaitUntilTestActorReady call but that's a bit more work than I thought

@Aaronontheweb Aaronontheweb added the akka-testkit Akka.NET Testkit issues label Aug 11, 2025
Copy link
Contributor

@Arkatufus Arkatufus left a comment

Choose a reason for hiding this comment

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

LGTM

@Arkatufus Arkatufus enabled auto-merge (squash) August 11, 2025 21:24
@Arkatufus Arkatufus merged commit b09d40f into akkadotnet:dev Aug 11, 2025
11 checks passed
@Aaronontheweb Aaronontheweb deleted the fix/testkit-spinwait-optimization branch August 12, 2025 00:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
akka-testkit Akka.NET Testkit issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TestKit: Replace Thread.Sleep with SpinWait to reduce startup failures under load
2 participants