예제 #1
0
        public async Task DequeueAsync_ReturnTaskInGoodOrder()
        {
            var backgroundTasksQueue = new BackgroundTasksQueue();

            var tmp = 0;

            backgroundTasksQueue.QueueBackgroundTask((sp, ct) =>
            {
                tmp = 1;
                return(Task.CompletedTask);
            });
            backgroundTasksQueue.QueueBackgroundTask((sp, ct) =>
            {
                tmp = 2;
                return(Task.CompletedTask);
            });

            Assert.Equal(0, tmp);
            var backgroundTaskItem1 = await backgroundTasksQueue.DequeueAsync();

            await backgroundTaskItem1.Delegate.Invoke(null, default);

            Assert.Equal(1, tmp);

            var backgroundTaskItem2 = await backgroundTasksQueue.DequeueAsync();

            await backgroundTaskItem2.Delegate.Invoke(null, default);

            Assert.Equal(2, tmp);
        }
예제 #2
0
        public async Task QueueBackgroundTask_CancelTask_Timeout()
        {
            var backgroundTasksQueue = new BackgroundTasksQueue();

            var     cancelled = false;
            Boolean?completed = null;

            backgroundTasksQueue.QueueBackgroundTask(async(sp, ct) =>
            {
                try
                {
                    await Task.Delay(10000, ct);
                    completed = true;
                }
                catch (TaskCanceledException)
                {
                    cancelled = true;
                }
            }, TimeSpan.FromMilliseconds(1));

            var backgroundTaskItem = await backgroundTasksQueue.DequeueAsync();

            await backgroundTaskItem.Delegate.Invoke(null, backgroundTaskItem.CancellationToken);

            Assert.True(cancelled);
            Assert.True(!completed.HasValue);
        }