/// <summary> /// Test whether the constraint is satisfied by a given value /// </summary> /// <param name="actual">The value to be tested</param> /// <returns>True for if the base constraint fails, false if it succeeds</returns> public override ConstraintResult ApplyTo <TActual>(TActual actual) { long now = Stopwatch.GetTimestamp(); long delayEnd = TimestampOffset(now, DelayInterval.AsTimeSpan); if (PollingInterval.IsNotZero) { long nextPoll = TimestampOffset(now, PollingInterval.AsTimeSpan); while ((now = Stopwatch.GetTimestamp()) < delayEnd) { if (nextPoll > now) { ThreadUtility.BlockingDelay((int)TimestampDiff(delayEnd < nextPoll ? delayEnd : nextPoll, now).TotalMilliseconds); } nextPoll = TimestampOffset(now, PollingInterval.AsTimeSpan); ConstraintResult result = BaseConstraint.ApplyTo(actual); if (result.IsSuccess) { return(new ConstraintResult(this, actual, true)); } } } if ((now = Stopwatch.GetTimestamp()) < delayEnd) { ThreadUtility.BlockingDelay((int)TimestampDiff(delayEnd, now).TotalMilliseconds); } return(new ConstraintResult(this, actual, BaseConstraint.ApplyTo(actual).IsSuccess)); }
/// <summary> /// Test whether the constraint is satisfied by a delegate /// </summary> /// <param name="del">The delegate whose value is to be tested</param> /// <returns>A ConstraintResult</returns> public override ConstraintResult ApplyTo <TActual>(ActualValueDelegate <TActual> del) { long now = Stopwatch.GetTimestamp(); long delayEnd = TimestampOffset(now, DelayInterval.AsTimeSpan); object actual; if (PollingInterval.IsNotZero) { long nextPoll = TimestampOffset(now, PollingInterval.AsTimeSpan); while ((now = Stopwatch.GetTimestamp()) < delayEnd) { if (nextPoll > now) { ThreadUtility.BlockingDelay((int)TimestampDiff(delayEnd < nextPoll ? delayEnd : nextPoll, now).TotalMilliseconds); } nextPoll = TimestampOffset(now, PollingInterval.AsTimeSpan); actual = InvokeDelegate(del); try { ConstraintResult result = BaseConstraint.ApplyTo(actual); if (result.IsSuccess) { return(new ConstraintResult(this, actual, true)); } } catch (Exception) { // Ignore any exceptions when polling } } } if ((now = Stopwatch.GetTimestamp()) < delayEnd) { ThreadUtility.BlockingDelay((int)TimestampDiff(delayEnd, now).TotalMilliseconds); } actual = InvokeDelegate(del); return(new ConstraintResult(this, actual, BaseConstraint.ApplyTo(actual).IsSuccess)); }