Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- using Xunit.Abstractions;
- using FluentAssertions;
- namespace EndToEndTesting.Utilities
- {
- public class ActionRetry
- {
- public static Action EmptyAction = () => { };
- /// <summary>
- /// This is when to generate log message when an action has exceeded a particular count
- /// </summary>
- public const int WARN_INTERVAL_COUNT = 300;
- /// <summary>
- /// Amount of time to Thread.Sleep
- /// </summary>
- public const int DEFAULT_SLEEP_MS = 1500;
- /// <summary>
- /// Number of times to cycle through a sleep/check process
- /// </summary>
- public const int DEFAULT_MAX_RETRY_CT = 500;
- /// <summary>
- /// the stack frame to look at when logging within the LogMethod
- /// </summary>
- const int DEFAULT_FRAME_IDX= 3;
- #region LogMethods
- private readonly ITestOutputHelper output;
- #endregion
- public int WarnIntervalCt { get; set; }
- public int MaxRetryCt { get; set; }
- public int SleepIntervalMs { get; set; }
- public ActionRetry(ITestOutputHelper output)
- {
- this.output = output;
- WarnIntervalCt = WARN_INTERVAL_COUNT;
- SleepIntervalMs = DEFAULT_SLEEP_MS;
- MaxRetryCt = DEFAULT_MAX_RETRY_CT;
- }
- public void CycleAction(Func<bool> breakAction, Action cyclicAction, string actionName, int FrameIdx = 2)
- {
- //we want to name the method calling Cycle Action
- output.WriteLine("Start (Cycle Action '{0}')", FrameIdx, actionName);
- int cycle = 0;
- do
- {
- if (cycle == WarnIntervalCt)
- {
- output.WriteLine("Warning: Action '{0}' has exceeded an interval count of {1}", actionName,
- WarnIntervalCt);
- }
- if (cyclicAction != null)
- {
- cyclicAction();
- }
- Thread.Sleep(SleepIntervalMs);
- ++cycle;
- } while (cycle < MaxRetryCt && !breakAction());
- output.WriteLine("End (Cycle Action {3} Count: {0} of {1}, sleep interval {2} ms )", FrameIdx, cycle, MaxRetryCt,
- SleepIntervalMs, actionName);
- cycle.Should().BeLessThan(MaxRetryCt,"Max Retry reached on cyclic action '{0}' ({1} of {2})", actionName, cycle,
- MaxRetryCt);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement