PageRenderTime 151ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/System.Reactive.Tests/System.Reactive.Concurrency/ThreadPoolSchedulerTest.cs

https://github.com/gshackles/mono-reactive
C# | 57 lines | 52 code | 3 blank | 2 comment | 0 complexity | 02962e295c467e992159496dbbb1de7a MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reactive;
  6. using System.Reactive.Concurrency;
  7. using System.Reactive.Disposables;
  8. using System.Reactive.Linq;
  9. using System.Threading;
  10. using NUnit.Framework;
  11. namespace System.Reactive.Concurrency.Tests
  12. {
  13. [TestFixture]
  14. public class ThreadPoolSchedulerTest
  15. {
  16. [Test]
  17. public void Cancellation ()
  18. {
  19. bool raised = false;
  20. var dis = Scheduler.ThreadPool.Schedule<object> (null, TimeSpan.FromMilliseconds (100), (sch, stat) => raised = true);
  21. Assert.IsFalse (raised, "#1");
  22. dis.Dispose (); // immediately, to not raise event.
  23. Thread.Sleep (200);
  24. Assert.IsFalse (raised, "#2");
  25. }
  26. [Test]
  27. public void ScheduleErrorneousAction ()
  28. {
  29. var s = Scheduler.ThreadPool;
  30. bool done = false;
  31. s.Schedule (() => { try { throw new Exception (); } finally { done = true; } });
  32. SpinWait.SpinUntil (() => done, 1000);
  33. Assert.IsTrue (done, "#1");
  34. // the exception does not occur in *this* thread, so it passes here.
  35. }
  36. [Test]
  37. public void Order ()
  38. {
  39. // It is time-dependent test (i.e. lengthy and inconsistent), which is not very good but we cannot use HistoricalScheduler to test it...
  40. var s = Scheduler.ThreadPool;
  41. var l = new List<int> ();
  42. var dis = new CompositeDisposable ();
  43. try {
  44. dis.Add (s.Schedule (() => { Thread.Sleep (1200); l.Add (1); }));
  45. dis.Add (s.Schedule (() => { Thread.Sleep (800); l.Add (2); }));
  46. dis.Add (s.Schedule (() => { Thread.Sleep (50); l.Add (3); }));
  47. Thread.Sleep (1500);
  48. Assert.AreEqual (new int [] {3, 2, 1}, l.ToArray (), "#1");
  49. } finally {
  50. dis.Dispose ();
  51. }
  52. }
  53. }
  54. }