コード例 #1
0
        public void TestFreezableClockAsync()
        {
            var et    = 0;
            var clock = new FreezableClock(AlignedClock.FromNow(Clocks.SystemTicksClock.As(TimeUnit.Millisecond)));

            Task.Delay(3000).ContinueWith(task =>
            {
                clock.Freeze();
                Thread.Sleep(1000);
                clock.Unfreeze();
            });

            Thread.Sleep(2000);
            et += 2000;
            var t = clock.Time;

            Debug.WriteLine($"t = {t}");
            Assert.IsTrue(Math.Abs(et - t) < ToleranceInMillis);

            Thread.Sleep(5000);
            et += 4000;
            t   = clock.Time;
            Debug.WriteLine($"t = {t}");
            Assert.IsTrue(Math.Abs(et - t) < ToleranceInMillis);
        }
コード例 #2
0
        public void Setup()
        {
            _mockClock = new Mock <IClock>();

            _subject = new FreezableClock(_mockClock.Object);

            _numberOfRequestsForTime = new Random().Next(1, 10);
        }
コード例 #3
0
 /// <summary>
 /// Stop program.
 /// </summary>
 /// <returns>Successfully stopped</returns>
 public bool Stop()
 {
     if (_cyclicExecutor.Stop(false))
     {
         CurrentStage = null;
         FreezableClock.Unfreeze();
         return(true);
     }
     return(false);
 }
コード例 #4
0
 /// <summary>
 /// Create a stage program.
 /// </summary>
 /// <param name="clock">Clock for timing</param>
 /// <param name="stageProvider">Stage provider that providing stages</param>
 /// <param name="preferPreloaded">Prefer pre-loaded stages if possible</param>
 public StageProgram([NotNull] IClock clock, [NotNull] IStageProvider stageProvider, bool preferPreloaded = true)
 {
     if (stageProvider == null)
     {
         throw new ArgumentNullException(nameof(stageProvider));
     }
     OriginalClock             = clock ?? throw new ArgumentNullException(nameof(clock));
     FreezableClock            = new FreezableClock(clock.As(TimeUnit.Millisecond));
     FreezableClock.Frozen    += (sender, e) => _pausedEvent.Set();
     FreezableClock.Unfrozen  += (sender, e) => _pausedEvent.Reset();
     _stageProvider            = preferPreloaded && stageProvider.TryPreload(out var preloaded) ? preloaded : stageProvider;
     _cyclicExecutor           = new AsyncCyclicExecutor("StageProgram Worker", DoWork, true, ThreadPriority.Highest, null, StoppingAction.Abort);
     _cyclicExecutor.Starting += (sender, e) =>
     {
         CurrentStage = null;
         _pausedEvent.Set();
         StartTime       = Time;
         _nextUpdateTime = 0;
     };
     _cyclicExecutor.Started += (sender, e) => Started?.Invoke(this, EventArgs.Empty);
     _cyclicExecutor.Stopped += (sender, e) => Stopped?.Invoke(this, EventArgs.Empty);
     _stageSkipped            = new AtomicBool(false);
     _pausedEvent             = new ManualResetEvent(false);
 }
コード例 #5
0
 /// <summary>
 /// Resume program.
 /// </summary>
 /// <returns>Successfully resumed</returns>
 public bool Resume() => FreezableClock.Unfreeze();
コード例 #6
0
 /// <summary>
 /// Pause program.
 /// </summary>
 /// <returns>Successfully paused</returns>
 public bool Pause() => FreezableClock.Freeze();