EventLoop.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. #if STRESS
  5. using System;
  6. using System.Linq;
  7. using System.Reactive.Concurrency;
  8. using System.Reactive.Disposables;
  9. using System.Reactive.Linq;
  10. using System.Reflection;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using Xunit;
  14. namespace ReactiveTests.Stress.Schedulers
  15. {
  16. /// <summary>
  17. /// Test for <see href="https://rx.codeplex.com/workitem/37">work item #37</see>.
  18. /// </summary>
  19. public static class EventLoop
  20. {
  21. private static readonly FieldInfo semaphore = typeof(EventLoopScheduler).GetField("_evt", BindingFlags.NonPublic | BindingFlags.Instance);
  22. public static void NoSemaphoreFullException()
  23. {
  24. var failed = new TaskCompletionSource<int>();
  25. using (var scheduler = new EventLoopScheduler())
  26. {
  27. Assert.Equal(0, scheduler.CurrentCount());
  28. var maxCount = Environment.ProcessorCount;
  29. using (Enumerable.Range(1, maxCount)
  30. .Select(_ => scheduler.SchedulePeriodic(TimeSpan.Zero, () =>
  31. {
  32. var count = scheduler.CurrentCount();
  33. if (count > maxCount)
  34. failed.SetResult(count);
  35. }))
  36. .Aggregate(
  37. new CompositeDisposable(),
  38. (c, d) =>
  39. {
  40. c.Add(d);
  41. return c;
  42. }))
  43. {
  44. if (failed.Task.Wait(TimeSpan.FromSeconds(10)))
  45. {
  46. Assert.Fail("Semaphore count is too high: {0}", failed.Task.Result);
  47. }
  48. }
  49. }
  50. }
  51. private static int CurrentCount(this EventLoopScheduler scheduler)
  52. {
  53. #if !NO_CDS
  54. return ((SemaphoreSlim)semaphore.GetValue(scheduler)).CurrentCount;
  55. #else
  56. return 0;
  57. #endif
  58. }
  59. }
  60. }
  61. #endif