-
Notifications
You must be signed in to change notification settings - Fork 46
Add cancellation token support #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
5eda310
df88b69
12f4e86
6667077
6bd6112
7a3b047
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,9 @@ | |
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
using FluentAssertions; | ||
using Gofer.NET.Utils; | ||
|
||
using Xunit; | ||
|
||
namespace Gofer.NET.Tests | ||
|
@@ -15,11 +16,11 @@ public class GivenATaskClient | |
public async Task ItContinuesListeningWhenATaskThrowsAnException() | ||
{ | ||
var waitTime = 5000; | ||
|
||
var taskQueue = TaskQueueTestFixture.UniqueRedisTaskQueue(); | ||
var taskClient = new TaskClient(taskQueue); | ||
var semaphoreFile = Path.GetTempFileName(); | ||
|
||
await taskClient.TaskQueue.Enqueue(() => Throw()); | ||
await taskClient.TaskQueue.Enqueue(() => TaskQueueTestFixture.WriteSemaphore(semaphoreFile)); | ||
|
||
|
@@ -28,37 +29,63 @@ public async Task ItContinuesListeningWhenATaskThrowsAnException() | |
|
||
taskClient.CancelListen(); | ||
await task; | ||
|
||
|
||
TaskQueueTestFixture.EnsureSemaphore(semaphoreFile); | ||
} | ||
|
||
[Fact] | ||
public async Task ItStopsOnCancellation() | ||
{ | ||
var semaphoreFile = Path.GetTempFileName(); | ||
var timeout = TimeSpan.FromMinutes(1); | ||
|
||
var waitTime = TimeSpan.FromSeconds(2); | ||
|
||
var taskQueue = TaskQueueTestFixture.UniqueRedisTaskQueue(); | ||
var taskClient = new TaskClient(taskQueue); | ||
var cancellation = new CancellationTokenSource(); | ||
|
||
await taskClient.TaskQueue.Enqueue(() => | ||
TaskQueueTestFixture.WaitForTaskClientCancellationAndWriteSemaphore( | ||
semaphoreFile, | ||
timeout)); | ||
|
||
var task = Task.Run(async () => await taskClient.Listen(cancellation.Token), CancellationToken.None); | ||
await Task.Delay(waitTime, CancellationToken.None); | ||
cancellation.Cancel(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Enqueued task (WaitForTaskClientCancellationAndWriteSemaphore) will be canceled at this moment. |
||
await Task.Delay(waitTime, CancellationToken.None); | ||
await task; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If a bug is introduced, and the task fails to exit on cancellation will this introduce a hanging test? |
||
|
||
TaskQueueTestFixture.EnsureSemaphore(semaphoreFile); | ||
} | ||
|
||
[Fact] | ||
public async Task ItDoesNotDelayScheduledTaskPromotionWhenRunningLongTasks() | ||
{ | ||
var waitTime = 4000; | ||
|
||
var taskQueue = TaskQueueTestFixture.UniqueRedisTaskQueue(); | ||
var taskClient = new TaskClient(taskQueue); | ||
|
||
var semaphoreFile = Path.GetTempFileName(); | ||
File.Delete(semaphoreFile); | ||
File.Exists(semaphoreFile).Should().BeFalse(); | ||
|
||
await taskClient.TaskQueue.Enqueue(() => Wait(waitTime)); | ||
|
||
await taskClient.TaskScheduler.AddScheduledTask( | ||
() => TaskQueueTestFixture.WriteSemaphore(semaphoreFile), | ||
TimeSpan.FromMilliseconds(waitTime / 4)); | ||
|
||
var task = Task.Run(async () => await taskClient.Listen()); | ||
|
||
await Task.Delay(waitTime / 2); | ||
|
||
// Ensure we did not run the scheduled task | ||
File.Exists(semaphoreFile).Should().BeFalse(); | ||
|
||
var dequeuedScheduledTask = await taskQueue.Dequeue(); | ||
|
||
File.Exists(semaphoreFile).Should().BeFalse(); | ||
dequeuedScheduledTask.Should().NotBeNull(); | ||
dequeuedScheduledTask.MethodName.Should().Be(nameof(TaskQueueTestFixture.WriteSemaphore)); | ||
|
@@ -83,19 +110,19 @@ public async Task ItExecutesImmediateAndScheduledTasksInOrder() | |
File.Delete(semaphoreFile); | ||
File.Exists(semaphoreFile).Should().BeFalse(); | ||
|
||
for (var i=0; i<immediateTasks; ++i) | ||
for (var i = 0; i < immediateTasks; ++i) | ||
{ | ||
await taskClient.TaskQueue.Enqueue(() => | ||
TaskQueueTestFixture.WriteSemaphoreValue(semaphoreFile, (i+1).ToString())); | ||
await taskClient.TaskQueue.Enqueue(() => | ||
TaskQueueTestFixture.WriteSemaphoreValue(semaphoreFile, (i + 1).ToString())); | ||
} | ||
|
||
for (var i=0; i<scheduledTasks; ++i) | ||
for (var i = 0; i < scheduledTasks; ++i) | ||
{ | ||
await taskClient.TaskScheduler.AddScheduledTask( | ||
() => TaskQueueTestFixture.WriteSemaphoreValue(semaphoreFile, (immediateTasks+i+1).ToString()), | ||
TimeSpan.FromMilliseconds(scheduledTasksStart + (scheduledTasksIncrement*i))); | ||
() => TaskQueueTestFixture.WriteSemaphoreValue(semaphoreFile, (immediateTasks + i + 1).ToString()), | ||
TimeSpan.FromMilliseconds(scheduledTasksStart + (scheduledTasksIncrement * i))); | ||
} | ||
|
||
var task = Task.Run(async () => await taskClient.Listen()); | ||
Thread.Sleep(scheduledTasks * scheduledTasksIncrement + 2000); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like a bug in the test, good catch!