forked from ppy/osu-framework
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add HeadlessTestRunner with simulated MultiThreaded mode
- Loading branch information
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |