Esempio n. 1
0
        public async Task Adding_tags_synchronizes_with_store()
        {
            var create = CreateNewTask();

            create.Tags.Add("a");

            var created = await Store.SaveAsync(create);

            Assert.True(created, "Task not created");

            var exists = await Store.GetByIdAsync(create.Id);

            Assert.NotNull(exists, $"Created task with ID '{create.Id}' is not visible");

            exists.Tags.Add("b");
            await Store.SaveAsync(exists);

            exists.Tags.Add("c");
            await Store.SaveAsync(exists);

            var all = (await Store.GetAllAsync()).AsList();

            Assert.Equal(1, all.Count);
            Assert.Equal(3, all.Single().Tags.Count);
        }
Esempio n. 2
0
        public async Task Can_cleanup_hanging_tasks()
        {
            using var host = CreateBackgroundTaskHost(o =>
            {
                o.DelayTasks            = true;
                o.MaximumAttempts       = 1;
                o.MaximumRuntimeSeconds = 1;
                o.SleepIntervalSeconds  = 1;

                o.CleanupIntervalSeconds = 1000;
            });

            host.Start();
            {
                await host.TryScheduleTaskAsync(typeof(TerminalTaskHandler));

                var all = (await Store.GetAllAsync()).ToList();
                Assert.Equal(1, all.Count /*, "Queue task should exist"*/);

                await Task.Delay(TimeSpan.FromSeconds(3));                 // <-- enough time to have started the terminal task

                all = (await Store.GetAllAsync()).ToList();
                Assert.Equal(1, all.Count /*, "Queue task should still exist, since it is terminal"*/);

                var task = all.First();
                Assert.True(task.LockedAt.HasValue, "Queue task should be locked");
                Assert.True(task.MaximumRuntime.HasValue, "Queue task should have a maximum runtime set.");
                Assert.True(task.IsRunningOvertime(Store), "Queue task should be running overtime");

                var hanging = (await Store.GetHangingTasksAsync()).ToList();
                Assert.Equal(1, hanging.Count /*, "Hanging task is not considered hanging by the task store"*/);

                var result = await host.CleanUpHangingTasksAsync();

                Assert.True(result, "Hanging task operation did not return successfully.");

                var threadId = 0;

                await Task.Run(async() =>
                {
                    var original = Interlocked.Exchange(ref threadId, Thread.CurrentThread.ManagedThreadId);
                    Assert.Equal(0, original);
                    return(result = await host.CleanUpHangingTasksAsync());
                });

                await Task.Run(async() =>
                {
                    var original = Interlocked.Exchange(ref threadId, Thread.CurrentThread.ManagedThreadId);
                    Assert.Equal(threadId,
                                 original /*, "Unexpected DI resolution of the second async cleanup thread"*/);
                    return(result = await host.CleanUpHangingTasksAsync());
                });
            }

            host.Stop();
        }
Esempio n. 3
0
 public async Task <IActionResult> GetBackgroundTasks()
 {
     return(Ok(await _store.GetAllAsync()));
 }