示例#1
0
        public void ShouldBeStoppedWhenCancelled()
        {
            using (var reset = new ManualResetEventSlim(false))
                using (var cts = new CancellationTokenSource())
                    using (var scheduler = new SystemTimerScheduledTaskManager())
                    {
                        var task = new TestBackgroundTask()
                        {
                            ExecutionDelay = TimeSpan.FromMilliseconds(10),
                            Reset          = reset,
                            Interval       = TimeSpan.FromMilliseconds(10),
                        };

                        scheduler.Configure(new List <IUnleashScheduledTask>()
                        {
                            task
                        }, cts.Token);

                        cts.CancelAfter(75);

                        reset.Wait(TimeSpan.FromMilliseconds(200));

                        task.Counter.Should().BeInRange(4, 6);
                    }
        }
        public async Task Scheduler_WhenCancellationTokenIsCancelled_ShutsDownAsExpected()
        {
            using (var cts = new CancellationTokenSource())
                using (var scheduler = new SystemTimerScheduledTaskManager())
                {
                    var task = new TestBackgroundTask(
                        5,
                        executionDelay: TimeSpan.Zero,
                        interval: TimeSpan.FromMilliseconds(200),
                        cancellationToken: cts.Token);

                    cts.CancelAfter(500);

                    scheduler.Configure(new[] { task }, cts.Token);

                    try
                    {
                        await task.Completion;
                        Assert.False(true);
                    }
                    catch (OperationCanceledException)
                    {
                        _testOutputHelper.WriteLine("Cancelled: " + task.Counter);
                        Assert.InRange(task.Counter, 2, 3);
                    }
                }

            Assert.True(true);
        }
示例#3
0
        public async Task When_Executing_BackgroundTask_Can_Omit_Return()
        {
            // Arrange
            var inMemoryProvider   = new InMemoryBackgroundTaskScheduleProvider();
            var testBackgroundTask = new TestBackgroundTask();

            var executor = TestApiOperationExecutor.Create(
                o => o.AddBackgroundTasks(p => p.UseInMemory(inMemoryProvider)),
                o => o.WithOperation <TestBackgroundTask>());

            // Act
            await executor.ExecuteAsync(testBackgroundTask);

            // Assert
            testBackgroundTask.HasExecuted.Should().BeTrue();
        }
        public async Task Scheduler_WhenTaskExceptionOccurs_ShouldContinueToRun()
        {
            using (var scheduler = new SystemTimerScheduledTaskManager())
            {
                var task = new TestBackgroundTask(5, 1);

                scheduler.Configure(new[] { task }, CancellationToken.None);

                try
                {
                    await task.Completion;
                }
                catch (OperationCanceledException)
                {
                    Assert.False(true);
                }

                Assert.Equal(5, task.Counter);
            }
        }
        public void ShouldContinueToRunEvenThoughExceptionsOccurs()
        {
            using (var reset = new ManualResetEventSlim(false))
                using (var scheduler = new SystemTimerScheduledTaskManager())
                {
                    var task = new TestBackgroundTask()
                    {
                        ExecutionDelay       = TimeSpan.FromMilliseconds(10),
                        ExecuteDuringStartup = false,
                        Reset            = reset,
                        Interval         = TimeSpan.FromMilliseconds(20),
                        ThrowsExceptions = true
                    };

                    scheduler.Configure(new List <IUnleashScheduledTask>()
                    {
                        task
                    }, CancellationToken.None);
                    reset.Wait();

                    task.Counter.Should().Be(5);
                }
        }