public void When_EnabledTwiceByHost_Then_SubscriptionCreatedOnlyOnce() { int totalActivations = 0, pendingActivations = 0; var host = new TestAutomationHost(_scheduler); var automation = new TestAutomation(host, OnEnabled); _scheduler.AdvanceBy(100); Assert.AreEqual(0, totalActivations); Assert.AreEqual(0, pendingActivations); host.SetIsEnabled(automation, true); _scheduler.AdvanceBy(100); Assert.AreEqual(1, totalActivations); Assert.AreEqual(1, pendingActivations); host.SetIsEnabled(automation, true); _scheduler.AdvanceBy(100); Assert.AreEqual(1, totalActivations); Assert.AreEqual(1, pendingActivations); IDisposable OnEnabled() { Interlocked.Increment(ref totalActivations); Interlocked.Increment(ref pendingActivations); return(Disposable.Create(() => Interlocked.Decrement(ref pendingActivations))); } }
public void When_SchedulePeriodic_Then_CancelSubscription() { _scheduler.AdvanceTo(new DateTimeOffset(1983, 9, 9, 15, 15, 0, TimeSpan.FromHours(-4)).Ticks); var ran = 0; var host = new TestAutomationHost(_scheduler); var automation = new TestAutomation(host); IDisposable subscription; using (new AsyncContext(_scheduler)) { subscription = automation.At(new TimeSpan(22, 0, 0), Action); } _scheduler.AdvanceBy(TimeSpan.FromDays(3).Ticks); subscription.Dispose(); _scheduler.AdvanceBy(TimeSpan.FromDays(5).Ticks); Assert.AreEqual(3, ran); async Task Action(DateTimeOffset arg) { Interlocked.Increment(ref ran); } }
public void When_Enable_Then_AsyncContextIsSet() { var ctx = default(AsyncContext); var ctxScheduler = default(IScheduler); var host = new TestAutomationHost(_scheduler); var automation = new TestAutomation(host, OnEnabled); _scheduler.AdvanceBy(100); host.SetIsEnabled(automation, true); _scheduler.AdvanceBy(100); Assert.IsNotNull(ctx); Assert.IsNotNull(ctxScheduler); Assert.AreEqual(_scheduler, ctxScheduler); IDisposable OnEnabled() { ctx = AsyncContext.Current; ctxScheduler = AsyncContext.Current?.Scheduler; return(Disposable.Empty); } }