Advertisement
mahldcat

ActionRetry

Feb 4th, 2021
1,222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.53 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using Xunit.Abstractions;
  4. using FluentAssertions;
  5.  
  6. namespace EndToEndTesting.Utilities
  7. {
  8.     public class ActionRetry
  9.     {
  10.         public static Action EmptyAction = () => { };
  11.         /// <summary>
  12.         /// This is when to generate log message when an action has exceeded a particular count
  13.         /// </summary>
  14.         public const int WARN_INTERVAL_COUNT = 300;
  15.         /// <summary>
  16.         /// Amount of time to Thread.Sleep
  17.         /// </summary>
  18.         public const int DEFAULT_SLEEP_MS = 1500;
  19.         /// <summary>
  20.         /// Number of times to cycle through a sleep/check process
  21.         /// </summary>
  22.         public const int DEFAULT_MAX_RETRY_CT = 500;
  23.         /// <summary>
  24.         /// the stack frame to look at when logging within the LogMethod
  25.         /// </summary>
  26.         const int DEFAULT_FRAME_IDX= 3;
  27.  
  28.         #region LogMethods
  29.         private readonly ITestOutputHelper output;
  30.         #endregion
  31.  
  32.         public int WarnIntervalCt { get; set; }
  33.         public int MaxRetryCt { get; set; }
  34.         public int SleepIntervalMs { get; set; }
  35.  
  36.         public ActionRetry(ITestOutputHelper output)
  37.         {
  38.             this.output = output;
  39.             WarnIntervalCt = WARN_INTERVAL_COUNT;
  40.             SleepIntervalMs = DEFAULT_SLEEP_MS;
  41.             MaxRetryCt = DEFAULT_MAX_RETRY_CT;
  42.         }
  43.  
  44.         public void CycleAction(Func<bool> breakAction, Action cyclicAction, string actionName, int FrameIdx = 2)
  45.         {
  46.             //we want to name the method calling Cycle Action
  47.  
  48.             output.WriteLine("Start (Cycle Action '{0}')", FrameIdx, actionName);
  49.             int cycle = 0;
  50.  
  51.             do
  52.             {
  53.                 if (cycle == WarnIntervalCt)
  54.                 {
  55.                     output.WriteLine("Warning:  Action '{0}' has exceeded an interval count of {1}", actionName,
  56.                         WarnIntervalCt);
  57.                 }
  58.  
  59.                 if (cyclicAction != null)
  60.                 {
  61.                     cyclicAction();
  62.                 }
  63.                 Thread.Sleep(SleepIntervalMs);
  64.                 ++cycle;
  65.             } while (cycle < MaxRetryCt && !breakAction());
  66.  
  67.             output.WriteLine("End (Cycle Action {3} Count: {0} of {1}, sleep interval {2} ms )", FrameIdx, cycle, MaxRetryCt,
  68.                 SleepIntervalMs, actionName);
  69.  
  70.             cycle.Should().BeLessThan(MaxRetryCt,"Max Retry reached on cyclic action '{0}' ({1} of {2})", actionName, cycle,
  71.                 MaxRetryCt);
  72.         }
  73.     }
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement