PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/framework/Constraints/DelayedConstraint.cs

#
C# | 162 lines | 90 code | 16 blank | 56 comment | 13 complexity | bc595c60e22e445a9f5768f674e19738 MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // Copyright 2008, Charlie Poole
  3. // This is free software licensed under the NUnit license. You may
  4. // obtain a copy of the license at http://nunit.org
  5. // ****************************************************************
  6. using System;
  7. using System.Threading;
  8. namespace NUnit.Framework.Constraints
  9. {
  10. ///<summary>
  11. /// Applies a delay to the match so that a match can be evaluated in the future.
  12. ///</summary>
  13. public class DelayedConstraint : PrefixConstraint
  14. {
  15. private readonly int delayInMilliseconds;
  16. private readonly int pollingInterval;
  17. ///<summary>
  18. /// Creates a new DelayedConstraint
  19. ///</summary>
  20. ///<param name="baseConstraint">The inner constraint two decorate</param>
  21. ///<param name="delayInMilliseconds">The time interval after which the match is performed</param>
  22. ///<exception cref="InvalidOperationException">If the value of <paramref name="delayInMilliseconds"/> is less than 0</exception>
  23. public DelayedConstraint(Constraint baseConstraint, int delayInMilliseconds)
  24. : this(baseConstraint, delayInMilliseconds, 0) { }
  25. ///<summary>
  26. /// Creates a new DelayedConstraint
  27. ///</summary>
  28. ///<param name="baseConstraint">The inner constraint two decorate</param>
  29. ///<param name="delayInMilliseconds">The time interval after which the match is performed</param>
  30. ///<param name="pollingInterval">The time interval used for polling</param>
  31. ///<exception cref="InvalidOperationException">If the value of <paramref name="delayInMilliseconds"/> is less than 0</exception>
  32. public DelayedConstraint(Constraint baseConstraint, int delayInMilliseconds, int pollingInterval)
  33. : base(baseConstraint)
  34. {
  35. if (delayInMilliseconds < 0)
  36. throw new ArgumentException("Cannot check a condition in the past", "delayInMilliseconds");
  37. this.delayInMilliseconds = delayInMilliseconds;
  38. this.pollingInterval = pollingInterval;
  39. }
  40. /// <summary>
  41. /// Test whether the constraint is satisfied by a given value
  42. /// </summary>
  43. /// <param name="actual">The value to be tested</param>
  44. /// <returns>True for if the base constraint fails, false if it succeeds</returns>
  45. public override bool Matches(object actual)
  46. {
  47. Thread.Sleep(delayInMilliseconds);
  48. this.actual = actual;
  49. return baseConstraint.Matches(actual);
  50. }
  51. /// <summary>
  52. /// Test whether the constraint is satisfied by a delegate
  53. /// </summary>
  54. /// <param name="del">The delegate whose value is to be tested</param>
  55. /// <returns>True for if the base constraint fails, false if it succeeds</returns>
  56. public override bool Matches(ActualValueDelegate del)
  57. {
  58. int remainingDelay = delayInMilliseconds;
  59. while (pollingInterval > 0 && pollingInterval < remainingDelay)
  60. {
  61. remainingDelay -= pollingInterval;
  62. Thread.Sleep(pollingInterval);
  63. this.actual = del();
  64. if (baseConstraint.Matches(actual))
  65. return true;
  66. }
  67. if ( remainingDelay > 0 )
  68. Thread.Sleep(remainingDelay);
  69. this.actual = del();
  70. return baseConstraint.Matches(actual);
  71. }
  72. #if NET_2_0
  73. /// <summary>
  74. /// Test whether the constraint is satisfied by a given reference.
  75. /// Overridden to wait for the specified delay period before
  76. /// calling the base constraint with the dereferenced value.
  77. /// </summary>
  78. /// <param name="actual">A reference to the value to be tested</param>
  79. /// <returns>True for success, false for failure</returns>
  80. public override bool Matches<T>(ref T actual)
  81. {
  82. int remainingDelay = delayInMilliseconds;
  83. while (pollingInterval > 0 && pollingInterval < remainingDelay)
  84. {
  85. remainingDelay -= pollingInterval;
  86. Thread.Sleep(pollingInterval);
  87. this.actual = actual;
  88. if (baseConstraint.Matches(actual))
  89. return true;
  90. }
  91. if ( remainingDelay > 0 )
  92. Thread.Sleep(remainingDelay);
  93. this.actual = actual;
  94. return baseConstraint.Matches(actual);
  95. }
  96. #else
  97. /// <summary>
  98. /// Test whether the constraint is satisfied by a given boolean reference.
  99. /// Overridden to wait for the specified delay period before
  100. /// calling the base constraint with the dereferenced value.
  101. /// </summary>
  102. /// <param name="actual">A reference to the value to be tested</param>
  103. /// <returns>True for success, false for failure</returns>
  104. public override bool Matches(ref bool actual)
  105. {
  106. int remainingDelay = delayInMilliseconds;
  107. while (pollingInterval > 0 && pollingInterval < remainingDelay)
  108. {
  109. remainingDelay -= pollingInterval;
  110. Thread.Sleep(pollingInterval);
  111. this.actual = actual;
  112. if (baseConstraint.Matches(actual))
  113. return true;
  114. }
  115. if ( remainingDelay > 0 )
  116. Thread.Sleep(remainingDelay);
  117. this.actual = actual;
  118. return baseConstraint.Matches(actual);
  119. }
  120. #endif
  121. /// <summary>
  122. /// Write the constraint description to a MessageWriter
  123. /// </summary>
  124. /// <param name="writer">The writer on which the description is displayed</param>
  125. public override void WriteDescriptionTo(MessageWriter writer)
  126. {
  127. baseConstraint.WriteDescriptionTo(writer);
  128. writer.Write(string.Format(" after {0} millisecond delay", delayInMilliseconds));
  129. }
  130. /// <summary>
  131. /// Write the actual value for a failing constraint test to a MessageWriter.
  132. /// </summary>
  133. /// <param name="writer">The writer on which the actual value is displayed</param>
  134. public override void WriteActualValueTo(MessageWriter writer)
  135. {
  136. baseConstraint.WriteActualValueTo(writer);
  137. }
  138. /// <summary>
  139. /// Returns the string representation of the constraint.
  140. /// </summary>
  141. protected override string GetStringRepresentation()
  142. {
  143. return string.Format("<after {0} {1}>", delayInMilliseconds, baseConstraint);
  144. }
  145. }
  146. }