TaskSchedulerTest.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // TaskSchedulerTest.cs
  2. //
  3. // Copyright (c) 2008 Jérémie "Garuma" Laval
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. //
  24. #if NET_4_0
  25. using System;
  26. using System.Threading;
  27. using System.Threading.Tasks;
  28. using System.Collections.Generic;
  29. using NUnit.Framework;
  30. namespace MonoTests.System.Threading.Tasks
  31. {
  32. [TestFixture]
  33. public class TaskSchedulerTests
  34. {
  35. [Test]
  36. public void BasicRunSynchronouslyTest ()
  37. {
  38. bool ran = false;
  39. var t = new Task (() => ran = true);
  40. t.RunSynchronously ();
  41. Assert.IsTrue (t.IsCompleted);
  42. Assert.IsFalse (t.IsFaulted);
  43. Assert.IsFalse (t.IsCanceled);
  44. Assert.IsTrue (ran);
  45. }
  46. class LazyCatScheduler : TaskScheduler
  47. {
  48. public TaskStatus ExecuteInlineStatus {
  49. get;
  50. set;
  51. }
  52. protected override void QueueTask (Task task)
  53. {
  54. throw new NotImplementedException ();
  55. }
  56. protected override bool TryDequeue (Task task)
  57. {
  58. throw new NotImplementedException ();
  59. }
  60. protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
  61. {
  62. ExecuteInlineStatus = task.Status;
  63. return true;
  64. }
  65. protected override IEnumerable<Task> GetScheduledTasks ()
  66. {
  67. throw new NotImplementedException ();
  68. }
  69. }
  70. [Test]
  71. public void RunSynchronouslyButNoExecutionTest ()
  72. {
  73. TaskSchedulerException ex = null;
  74. var ts = new LazyCatScheduler ();
  75. Task t = new Task (() => {});
  76. try {
  77. t.RunSynchronously (ts);
  78. } catch (TaskSchedulerException e) {
  79. ex = e;
  80. }
  81. Assert.IsNotNull (ex);
  82. Assert.IsNotNull (ex.InnerException);
  83. Assert.IsInstanceOfType (typeof (InvalidOperationException), ex.InnerException);
  84. }
  85. [Test]
  86. public void RunSynchronouslyTaskStatusTest ()
  87. {
  88. var ts = new LazyCatScheduler ();
  89. var t = new Task (() => { });
  90. try {
  91. t.RunSynchronously (ts);
  92. } catch {}
  93. Assert.AreEqual (TaskStatus.WaitingToRun, ts.ExecuteInlineStatus);
  94. }
  95. }
  96. }
  97. #endif