/// <summary> /// Waits up to the specified timeout for the given /// <see cref="Predicate"/> /// to /// become <code>true</code>. /// <p/> /// The timeout time is multiplied by the /// <see cref="GetWaitForRatio()"/> /// . /// </summary> /// <param name="timeout">the timeout in milliseconds to wait for the predicate.</param> /// <param name="failIfTimeout"> /// indicates if the test should be failed if the /// predicate times out. /// </param> /// <param name="predicate">the predicate ot evaluate.</param> /// <returns> /// the effective wait, in milli-seconds until the predicate become /// <code>true</code> or <code>-1</code> if the predicate did not evaluate /// to <code>true</code>. /// </returns> protected internal virtual long WaitFor(int timeout, bool failIfTimeout, HTestCase.Predicate predicate) { long started = Time.Now(); long mustEnd = Time.Now() + (long)(GetWaitForRatio() * timeout); long lastEcho = 0; try { long waiting = mustEnd - Time.Now(); System.Console.Out.WriteLine(MessageFormat.Format("Waiting up to [{0}] msec", waiting )); bool eval; while (!(eval = predicate.Evaluate()) && Time.Now() < mustEnd) { if ((Time.Now() - lastEcho) > 5000) { waiting = mustEnd - Time.Now(); System.Console.Out.WriteLine(MessageFormat.Format("Waiting up to [{0}] msec", waiting )); lastEcho = Time.Now(); } Sharpen.Thread.Sleep(100); } if (!eval) { if (failIfTimeout) { NUnit.Framework.Assert.Fail(MessageFormat.Format("Waiting timed out after [{0}] msec" , timeout)); } else { System.Console.Out.WriteLine(MessageFormat.Format("Waiting timed out after [{0}] msec" , timeout)); } } return((eval) ? Time.Now() - started : -1); } catch (Exception ex) { throw new RuntimeException(ex); } }
/// <summary> /// Waits up to the specified timeout for the given /// <see cref="Predicate"/> /// to /// become <code>true</code>, failing the test if the timeout is reached /// and the Predicate is still <code>false</code>. /// <p/> /// The timeout time is multiplied by the /// <see cref="GetWaitForRatio()"/> /// . /// </summary> /// <param name="timeout">the timeout in milliseconds to wait for the predicate.</param> /// <param name="predicate">the predicate ot evaluate.</param> /// <returns> /// the effective wait, in milli-seconds until the predicate become /// <code>true</code>. /// </returns> protected internal virtual long WaitFor(int timeout, HTestCase.Predicate predicate ) { return(WaitFor(timeout, false, predicate)); }