Skip to content

Commit

Permalink
Add HeadlessTestRunner with simulated MultiThreaded mode
Browse files Browse the repository at this point in the history
  • Loading branch information
smoogipoo committed Jul 17, 2024
1 parent 5cc041c commit 824d7f4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
3 changes: 3 additions & 0 deletions osu.Framework/Platform/HeadlessGameHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using osu.Framework.Graphics.Rendering.Dummy;
using osu.Framework.Input.Handlers;
using osu.Framework.Logging;
using osu.Framework.Testing;
using osu.Framework.Threading;
using osu.Framework.Timing;

Expand Down Expand Up @@ -106,6 +107,8 @@ protected override void UpdateFrame()

protected override IEnumerable<InputHandler> CreateAvailableInputHandlers() => Array.Empty<InputHandler>();

protected override ThreadRunner CreateThreadRunner(InputThread mainThread) => new HeadlessThreadRunner(mainThread);

private class FastClock : IClock
{
private readonly double increment;
Expand Down
51 changes: 51 additions & 0 deletions osu.Framework/Testing/HeadlessThreadRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using osu.Framework.Development;
using osu.Framework.Platform;
using osu.Framework.Threading;
using osu.Framework.Utils;

namespace osu.Framework.Testing
{
public class HeadlessThreadRunner : ThreadRunner
{
public HeadlessThreadRunner(InputThread mainThread)
: base(mainThread)
{
}

public override void RunMainLoop()
{
// Run true single-threaded mode.
if (ThreadSafety.ExecutionMode == ExecutionMode.SingleThread)
{
base.RunMainLoop();
return;
}

// Run a simulated multi-threaded mode.
// This is because the CI test runners are usually CPU-limited and may be preferring one thread (audio) over another (update).

ThreadSafety.ExecutionMode = ExecutionMode.SingleThread;

for (int i = 0; i < Threads.Count; i++)
{
int from;
int to;

do
{
from = RNG.Next(Threads.Count);
to = RNG.Next(Threads.Count);
} while (from == to);

(Threads[from], Threads[to]) = (Threads[to], Threads[from]);
}

base.RunMainLoop();

ThreadSafety.ExecutionMode = ExecutionMode.MultiThreaded;
}
}
}

0 comments on commit 824d7f4

Please sign in to comment.