PageRenderTime 50ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Microsoft.AspNet.SignalR.Tests/Infrastructure/CountDownRange.cs

https://github.com/mip1983/SignalR
C# | 79 lines | 69 code | 10 blank | 0 comment | 3 complexity | 460b5674ce341f8a569f2115b46b2151 MD5 | raw file
Possible License(s): Apache-2.0, CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. namespace Microsoft.AspNet.SignalR.Tests.Infrastructure
  7. {
  8. public class CountDownRange<T>
  9. {
  10. private HashSet<T> _items;
  11. private HashSet<T> _seen;
  12. private ManualResetEventSlim _wh = new ManualResetEventSlim(false);
  13. public CountDownRange(IEnumerable<T> range)
  14. {
  15. _items = new HashSet<T>(range);
  16. _seen = new HashSet<T>();
  17. }
  18. public int Count
  19. {
  20. get
  21. {
  22. lock (_items)
  23. {
  24. return _items.Count;
  25. }
  26. }
  27. }
  28. public IEnumerable<T> Seen
  29. {
  30. get
  31. {
  32. lock (_seen)
  33. {
  34. return _seen.ToList();
  35. }
  36. }
  37. }
  38. public IEnumerable<T> Left
  39. {
  40. get
  41. {
  42. lock (_items)
  43. {
  44. return _items.ToList();
  45. }
  46. }
  47. }
  48. public bool Mark(T item)
  49. {
  50. lock (_items)
  51. {
  52. if (_items.Remove(item))
  53. {
  54. if (_items.Count == 0)
  55. {
  56. _wh.Set();
  57. }
  58. _seen.Add(item);
  59. return true;
  60. }
  61. }
  62. return false;
  63. }
  64. public bool Wait(TimeSpan timeout)
  65. {
  66. return _wh.Wait(timeout);
  67. }
  68. }
  69. }