A game framework written with osu! in mind.
at master 2.0 kB view raw
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. 2// See the LICENCE file in the repository root for full licence text. 3 4using System.Threading; 5using System.Threading.Tasks; 6using NUnit.Framework; 7using osu.Framework.Threading; 8 9namespace osu.Framework.Tests.Threading 10{ 11 [TestFixture] 12 public class ThreadedTaskSchedulerTest 13 { 14 /// <summary> 15 /// On disposal, <see cref="ThreadedTaskScheduler"/> does a blocking shutdown sequence. 16 /// This asserts all outstanding tasks are run before the shutdown completes. 17 /// </summary> 18 [Test] 19 public void EnsureThreadedTaskSchedulerProcessesBeforeDispose() 20 { 21 int runCount = 0; 22 23 const int target_count = 128; 24 25 using (var taskScheduler = new ThreadedTaskScheduler(4, "test")) 26 { 27 for (int i = 0; i < target_count; i++) 28 { 29 Task.Factory.StartNew(() => 30 { 31 Interlocked.Increment(ref runCount); 32 Thread.Sleep(100); 33 }, default, TaskCreationOptions.HideScheduler, taskScheduler); 34 } 35 } 36 37 Assert.AreEqual(target_count, runCount); 38 } 39 40 [Test] 41 public void EnsureEventualDisposalWithStuckTasks() 42 { 43 ManualResetEventSlim exited = new ManualResetEventSlim(); 44 45 Task.Run(() => 46 { 47 using (var taskScheduler = new ThreadedTaskScheduler(4, "test")) 48 { 49 Task.Factory.StartNew(() => 50 { 51 while (!exited.IsSet) 52 Thread.Sleep(100); 53 }, default, TaskCreationOptions.HideScheduler, taskScheduler); 54 } 55 56 exited.Set(); 57 }); 58 59 Assert.That(exited.Wait(30000)); 60 } 61 } 62}