<commit_before>// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using osu.Framework.Threading;

namespace osu.Framework.Tests.Threading
{
    [TestFixture]
    public class ThreadedTaskSchedulerTest
    {
        /// <summary>
        /// On disposal, <see cref="ThreadedTaskScheduler"/> does a blocking shutdown sequence.
        /// This asserts all outstanding tasks are run before the shutdown completes.
        /// </summary>
        [Test]
        public void EnsureThreadedTaskSchedulerProcessesBeforeDispose()
        {
            int runCount = 0;

            const int target_count = 128;

            using (var taskScheduler = new ThreadedTaskScheduler(4, "test"))
            {
                for (int i = 0; i < target_count; i++)
                {
                    Task.Factory.StartNew(() =>
                    {
                        Interlocked.Increment(ref runCount);
                        Thread.Sleep(100);
                    }, default, TaskCreationOptions.HideScheduler, taskScheduler);
                }
            }

            Assert.AreEqual(target_count, runCount);
        }<commit_msg>Add test coverage of double disposal crashing<commit_after>// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using osu.Framework.Threading;

namespace osu.Framework.Tests.Threading
{
    [TestFixture]
    public class ThreadedTaskSchedulerTest
    {
        /// <summary>
        /// On disposal, <see cref="ThreadedTaskScheduler"/> does a blocking shutdown sequence.
        /// This asserts all outstanding tasks are run before the shutdown completes.
        /// </summary>
        [Test]
        public void EnsureThreadedTaskSchedulerProcessesBeforeDispose()
        {
            int runCount = 0;

            const int target_count = 128;

            using var taskScheduler = new ThreadedTaskScheduler(4, "test");

            for (int i = 0; i < target_count; i++)
            {
                Task.Factory.StartNew(() =>
                {
                    Interlocked.Increment(ref runCount);
                    Thread.Sleep(100);
                }, default, TaskCreationOptions.HideScheduler, taskScheduler);
            }

            taskScheduler.Dispose();

            // test against double disposal crashes.
            taskScheduler.Dispose();

            Assert.AreEqual(target_count, runCount);
        }
{'fraction_non_alphanumeric': 0.08946395563770795, 'fraction_numerical': 0.006654343807763401, 'mean_word_length': 1.9381107491856677, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6984882', 'n_tokens_mistral': 719, 'n_tokens_neox': 673, 'n_words': 199}