public async Task GivenATaskCanceledButNotComplete_WhenCreateTaskBySameType_ConflictShouldBeReturned() { string queueId = Guid.NewGuid().ToString(); string inputData = "inputData"; short uniqueType = 4321; TaskInfo taskInfo = new TaskInfo() { TaskId = Guid.NewGuid().ToString(), QueueId = queueId, TaskTypeId = uniqueType, InputData = inputData, }; SqlServerTaskManager sqlServerTaskManager = new SqlServerTaskManager(_fixture.SqlConnectionWrapperFactory, NullLogger <SqlServerTaskManager> .Instance); _ = await sqlServerTaskManager.CreateTaskAsync(taskInfo, true, CancellationToken.None); _ = await sqlServerTaskManager.CancelTaskAsync(taskInfo.TaskId, CancellationToken.None); taskInfo = new TaskInfo() { TaskId = Guid.NewGuid().ToString(), QueueId = queueId, TaskTypeId = uniqueType, InputData = inputData, }; await Assert.ThrowsAnyAsync <TaskConflictException>(async() => await sqlServerTaskManager.CreateTaskAsync(taskInfo, true, CancellationToken.None)); }
public async Task GivenARunningTask_WhenGetNextTask_ThenNoTaskShouldBeReturned() { string queueId = Guid.NewGuid().ToString(); TaskHostingConfiguration config = new TaskHostingConfiguration() { Enabled = true, QueueId = queueId, TaskHeartbeatTimeoutThresholdInSeconds = 60, }; IOptions <TaskHostingConfiguration> taskHostingConfig = Substitute.For <IOptions <TaskHostingConfiguration> >(); taskHostingConfig.Value.Returns(config); SqlServerTaskManager sqlServerTaskManager = new SqlServerTaskManager(_fixture.SqlConnectionWrapperFactory, NullLogger <SqlServerTaskManager> .Instance); SqlServerTaskConsumer sqlServerTaskConsumer = new SqlServerTaskConsumer(taskHostingConfig, _fixture.SqlConnectionWrapperFactory, NullLogger <SqlServerTaskConsumer> .Instance); string taskId = Guid.NewGuid().ToString(); string inputData = "inputData"; TaskInfo taskInfo = new TaskInfo() { TaskId = taskId, QueueId = queueId, TaskTypeId = SqlServerTaskConsumerTestsTypeId, InputData = inputData, }; _ = await sqlServerTaskManager.CreateTaskAsync(taskInfo, false, CancellationToken.None); _ = await sqlServerTaskConsumer.GetNextMessagesAsync(1, 60, CancellationToken.None); taskInfo = (await sqlServerTaskConsumer.GetNextMessagesAsync(1, 60, CancellationToken.None)).FirstOrDefault(); Assert.Null(taskInfo); }
public async Task GivenASqlTaskManager_WhenCreate2TaskWithSameTaskId_ThenNewTaskShouldBeCreated() { string queueId = Guid.NewGuid().ToString(); string taskId = Guid.NewGuid().ToString(); string inputData = "inputData"; TaskInfo taskInfo = new TaskInfo() { TaskId = taskId, QueueId = queueId, TaskTypeId = SqlServerTaskManagerTestsTypeId, InputData = inputData, }; SqlServerTaskManager sqlServerTaskManager = new SqlServerTaskManager(_fixture.SqlConnectionWrapperFactory, NullLogger <SqlServerTaskManager> .Instance); taskInfo = await sqlServerTaskManager.CreateTaskAsync(taskInfo, false, CancellationToken.None); await Assert.ThrowsAsync <TaskConflictException>(async() => await sqlServerTaskManager.CreateTaskAsync(taskInfo, false, CancellationToken.None)); }
public async Task GivenActiveTasks_WhenCreateWithSameTypeRunningTask_ThenConflictExceptionShouldBeThrow() { string queueId = Guid.NewGuid().ToString(); string taskId1 = Guid.NewGuid().ToString(); string taskId2 = Guid.NewGuid().ToString(); string inputData = "inputData"; TaskHostingConfiguration config = new TaskHostingConfiguration() { Enabled = true, QueueId = queueId, TaskHeartbeatTimeoutThresholdInSeconds = 60, }; short conflictTestTypeId = 1000; IOptions <TaskHostingConfiguration> taskHostingConfig = Substitute.For <IOptions <TaskHostingConfiguration> >(); taskHostingConfig.Value.Returns(config); SqlServerTaskManager sqlServerTaskManager = new SqlServerTaskManager(_fixture.SqlConnectionWrapperFactory, NullLogger <SqlServerTaskManager> .Instance); TaskInfo taskInfo1 = new TaskInfo() { TaskId = taskId1, QueueId = queueId, TaskTypeId = conflictTestTypeId, InputData = inputData, }; TaskInfo taskInfo2 = new TaskInfo() { TaskId = taskId2, QueueId = queueId, TaskTypeId = conflictTestTypeId, InputData = inputData, }; _ = await sqlServerTaskManager.CreateTaskAsync(taskInfo1, true, CancellationToken.None); await Assert.ThrowsAnyAsync <TaskConflictException>(() => sqlServerTaskManager.CreateTaskAsync(taskInfo2, true, CancellationToken.None)); }
public async Task GivenASqlTaskManager_WhenCreateTask_ThenNewTaskShouldBeCreated() { string queueId = Guid.NewGuid().ToString(); string taskId = Guid.NewGuid().ToString(); short typeId = 1; string inputData = "inputData"; TaskInfo taskInfo = new TaskInfo() { TaskId = taskId, QueueId = queueId, TaskTypeId = typeId, InputData = inputData, }; SqlServerTaskManager sqlServerTaskManager = new SqlServerTaskManager(_fixture.SqlConnectionWrapperFactory, NullLogger <SqlServerTaskManager> .Instance); taskInfo = await sqlServerTaskManager.CreateTaskAsync(taskInfo, CancellationToken.None); Assert.Equal(queueId, taskInfo.QueueId); Assert.Equal(taskId, taskInfo.TaskId); Assert.Equal(typeId, taskInfo.TaskTypeId); Assert.Equal(inputData, taskInfo.InputData); Assert.Equal(TaskStatus.Queued, taskInfo.Status); Assert.NotNull(taskInfo.HeartbeatDateTime); Assert.Null(taskInfo.RunId); Assert.False(taskInfo.IsCanceled); Assert.Equal(0, taskInfo.RetryCount); Assert.Null(taskInfo.Context); Assert.Null(taskInfo.Result); taskInfo = await sqlServerTaskManager.GetTaskAsync(taskId, CancellationToken.None); Assert.Equal(queueId, taskInfo.QueueId); Assert.Equal(taskId, taskInfo.TaskId); Assert.Equal(typeId, taskInfo.TaskTypeId); Assert.Equal(inputData, taskInfo.InputData); Assert.Equal(TaskStatus.Queued, taskInfo.Status); Assert.NotNull(taskInfo.HeartbeatDateTime); Assert.Null(taskInfo.RunId); Assert.False(taskInfo.IsCanceled); Assert.Equal(0, taskInfo.RetryCount); Assert.Null(taskInfo.Context); Assert.Null(taskInfo.Result); }
public async Task GivenARunningTask_WhenReachMaxRetryCount_ThenResetShouldFail() { string queueId = Guid.NewGuid().ToString(); TaskHostingConfiguration config = new TaskHostingConfiguration() { Enabled = true, QueueId = queueId, TaskHeartbeatTimeoutThresholdInSeconds = 60, }; IOptions <TaskHostingConfiguration> taskHostingConfig = Substitute.For <IOptions <TaskHostingConfiguration> >(); taskHostingConfig.Value.Returns(config); SqlServerTaskManager sqlServerTaskManager = new SqlServerTaskManager(_fixture.SqlConnectionWrapperFactory, NullLogger <SqlServerTaskManager> .Instance); SqlServerTaskConsumer sqlServerTaskConsumer = new SqlServerTaskConsumer(taskHostingConfig, _fixture.SqlConnectionWrapperFactory, NullLogger <SqlServerTaskConsumer> .Instance); string taskId = Guid.NewGuid().ToString(); short typeId = 1; string inputData = "inputData"; TaskInfo taskInfo = new TaskInfo() { TaskId = taskId, QueueId = queueId, TaskTypeId = typeId, InputData = inputData, MaxRetryCount = 1, }; _ = await sqlServerTaskManager.CreateTaskAsync(taskInfo, CancellationToken.None); TaskResultData result = new TaskResultData(TaskResult.Fail, "Result"); taskInfo = (await sqlServerTaskConsumer.GetNextMessagesAsync(1, 60, CancellationToken.None)).First(); taskInfo = await sqlServerTaskConsumer.ResetAsync(taskInfo.TaskId, result, taskInfo.RunId, CancellationToken.None); taskInfo = (await sqlServerTaskConsumer.GetNextMessagesAsync(1, 60, CancellationToken.None)).First(); await Assert.ThrowsAsync <TaskAlreadyCompletedException>(async() => await sqlServerTaskConsumer.ResetAsync(taskInfo.TaskId, result, taskInfo.RunId, CancellationToken.None)); taskInfo = await sqlServerTaskManager.GetTaskAsync(taskInfo.TaskId, CancellationToken.None); Assert.Equal(TaskStatus.Completed, taskInfo.Status); Assert.Equal(JsonConvert.SerializeObject(result), taskInfo.Result); }
public async Task GivenARunningTask_WhenResetTask_ThenTaskShouldBeReturned() { string queueId = Guid.NewGuid().ToString(); TaskHostingConfiguration config = new TaskHostingConfiguration() { Enabled = true, QueueId = queueId, TaskHeartbeatTimeoutThresholdInSeconds = 60, }; IOptions <TaskHostingConfiguration> taskHostingConfig = Substitute.For <IOptions <TaskHostingConfiguration> >(); taskHostingConfig.Value.Returns(config); SqlServerTaskManager sqlServerTaskManager = new SqlServerTaskManager(_fixture.SqlConnectionWrapperFactory, NullLogger <SqlServerTaskManager> .Instance); SqlServerTaskConsumer sqlServerTaskConsumer = new SqlServerTaskConsumer(taskHostingConfig, _fixture.SqlConnectionWrapperFactory, NullLogger <SqlServerTaskConsumer> .Instance); string taskId = Guid.NewGuid().ToString(); short typeId = 1; string inputData = "inputData"; TaskInfo taskInfo = new TaskInfo() { TaskId = taskId, QueueId = queueId, TaskTypeId = typeId, InputData = inputData, }; _ = await sqlServerTaskManager.CreateTaskAsync(taskInfo, CancellationToken.None); taskInfo = (await sqlServerTaskConsumer.GetNextMessagesAsync(1, 60, CancellationToken.None)).First(); string firstRunId = taskInfo.RunId; TaskResultData result = new TaskResultData(TaskResult.Success, "Result"); taskInfo = await sqlServerTaskConsumer.ResetAsync(taskInfo.TaskId, result, taskInfo.RunId, CancellationToken.None); Assert.Equal(1, taskInfo.RetryCount); taskInfo = (await sqlServerTaskConsumer.GetNextMessagesAsync(1, 60, CancellationToken.None)).First(); Assert.NotNull(taskInfo); Assert.NotEqual(firstRunId, taskInfo.RunId); }
public async Task GivenARunningTask_WhenUpdateWithWrongRunid_ThenExceptionShouldBeThrew() { string queueId = Guid.NewGuid().ToString(); TaskHostingConfiguration config = new TaskHostingConfiguration() { Enabled = true, QueueId = queueId, TaskHeartbeatTimeoutThresholdInSeconds = 60, }; IOptions <TaskHostingConfiguration> taskHostingConfig = Substitute.For <IOptions <TaskHostingConfiguration> >(); taskHostingConfig.Value.Returns(config); SqlServerTaskManager sqlServerTaskManager = new SqlServerTaskManager(_fixture.SqlConnectionWrapperFactory, NullLogger <SqlServerTaskManager> .Instance); SqlServerTaskConsumer sqlServerTaskConsumer = new SqlServerTaskConsumer(taskHostingConfig, _fixture.SqlConnectionWrapperFactory, NullLogger <SqlServerTaskConsumer> .Instance); string taskId = Guid.NewGuid().ToString(); short typeId = 1; string inputData = "inputData"; TaskInfo taskInfo = new TaskInfo() { TaskId = taskId, QueueId = queueId, TaskTypeId = typeId, InputData = inputData, MaxRetryCount = 1, }; _ = await sqlServerTaskManager.CreateTaskAsync(taskInfo, CancellationToken.None); TaskResultData result = new TaskResultData(TaskResult.Fail, "Result"); taskInfo = (await sqlServerTaskConsumer.GetNextMessagesAsync(1, 60, CancellationToken.None)).First(); await Assert.ThrowsAsync <TaskNotExistException>(async() => await sqlServerTaskConsumer.KeepAliveAsync(taskInfo.TaskId, "invalid", CancellationToken.None)); await Assert.ThrowsAsync <TaskNotExistException>(async() => await sqlServerTaskConsumer.CompleteAsync(taskInfo.TaskId, result, "invalid", CancellationToken.None)); await Assert.ThrowsAsync <TaskNotExistException>(async() => await sqlServerTaskConsumer.ResetAsync(taskInfo.TaskId, result, "invalid", CancellationToken.None)); }
public async Task GivenASqlTaskManager_WhenCancelTask_ThenTaskStatusShouldBeChanged() { string queueId = Guid.NewGuid().ToString(); string taskId = Guid.NewGuid().ToString(); string inputData = "inputData"; TaskInfo taskInfo = new TaskInfo() { TaskId = taskId, QueueId = queueId, TaskTypeId = SqlServerTaskManagerTestsTypeId, InputData = inputData, }; SqlServerTaskManager sqlServerTaskManager = new SqlServerTaskManager(_fixture.SqlConnectionWrapperFactory, NullLogger <SqlServerTaskManager> .Instance); _ = await sqlServerTaskManager.CreateTaskAsync(taskInfo, false, CancellationToken.None); TaskInfo canceledTask = await sqlServerTaskManager.CancelTaskAsync(taskId, CancellationToken.None); Assert.True(canceledTask.IsCanceled); }
public async Task GivenListOfTasksInQueue_WhenGetNextTask_ThenAvailableTasksShouldBeReturned() { string queueId = Guid.NewGuid().ToString(); TaskHostingConfiguration config = new TaskHostingConfiguration() { Enabled = true, QueueId = queueId, TaskHeartbeatTimeoutThresholdInSeconds = 60, }; IOptions <TaskHostingConfiguration> taskHostingConfig = Substitute.For <IOptions <TaskHostingConfiguration> >(); taskHostingConfig.Value.Returns(config); SqlServerTaskManager sqlServerTaskManager = new SqlServerTaskManager(_fixture.SqlConnectionWrapperFactory, NullLogger <SqlServerTaskManager> .Instance); SqlServerTaskConsumer sqlServerTaskConsumer = new SqlServerTaskConsumer(taskHostingConfig, _fixture.SqlConnectionWrapperFactory, NullLogger <SqlServerTaskConsumer> .Instance); for (int i = 0; i < 5; ++i) { string taskId = Guid.NewGuid().ToString(); short typeId = 1; string inputData = "inputData"; TaskInfo taskInfo = new TaskInfo() { TaskId = taskId, QueueId = queueId, TaskTypeId = typeId, InputData = inputData, }; _ = await sqlServerTaskManager.CreateTaskAsync(taskInfo, CancellationToken.None); } var result = (await sqlServerTaskConsumer.GetNextMessagesAsync(3, 60, CancellationToken.None)).ToList(); Assert.Equal(3, result.Count()); }
public async Task GivenATaskCreated_WhenTaskCanceled_ThenCanBePickedUp() { string queueId = Guid.NewGuid().ToString(); TaskHostingConfiguration config = new TaskHostingConfiguration() { Enabled = true, QueueId = queueId, TaskHeartbeatTimeoutThresholdInSeconds = 60, }; IOptions <TaskHostingConfiguration> taskHostingConfig = Substitute.For <IOptions <TaskHostingConfiguration> >(); taskHostingConfig.Value.Returns(config); SqlServerTaskManager sqlServerTaskManager = new SqlServerTaskManager(_fixture.SqlConnectionWrapperFactory, NullLogger <SqlServerTaskManager> .Instance); SqlServerTaskConsumer sqlServerTaskConsumer = new SqlServerTaskConsumer(taskHostingConfig, _fixture.SqlConnectionWrapperFactory, NullLogger <SqlServerTaskConsumer> .Instance); string taskId = Guid.NewGuid().ToString(); string inputData = "inputData"; TaskInfo taskInfo = new TaskInfo() { TaskId = taskId, QueueId = queueId, TaskTypeId = SqlServerTaskConsumerTestsTypeId, InputData = inputData, MaxRetryCount = 1, }; _ = await sqlServerTaskManager.CreateTaskAsync(taskInfo, false, CancellationToken.None); _ = await sqlServerTaskManager.CancelTaskAsync(taskInfo.TaskId, CancellationToken.None); var taskInfoResult = (await sqlServerTaskConsumer.GetNextMessagesAsync(1, 60, CancellationToken.None)).First(); Assert.Equal(taskInfo.TaskId, taskInfoResult.TaskId); }