public async Task ToTaskInfoShouldReturnTaskInfo() { SqlTaskStatus expectedStatus = SqlTaskStatus.Succeeded; DatabaseOperationStub operation = new DatabaseOperationStub(); operation.TaskResult = new TaskResult { TaskStatus = expectedStatus }; SqlTask sqlTask = new SqlTask(new TaskMetadata { ServerName = "server name", DatabaseName = "database name" }, operation.FunctionToRun, operation.FunctionToCancel); Task taskToVerify = sqlTask.RunAsync().ContinueWith(task => { var taskInfo = sqlTask.ToTaskInfo(); Assert.Equal(taskInfo.TaskId, sqlTask.TaskId.ToString()); Assert.Equal(taskInfo.ServerName, "server name"); Assert.Equal(taskInfo.DatabaseName, "database name"); }); operation.Stop(); await taskToVerify; }
/// <summary> /// Add script result of the operation /// </summary> /// <param name="script">Script content</param> /// <param name="errorMessage">Error occured during script</param> /// <param name="status">Status of the script</param> /// <returns></returns> public TaskScript AddScript(SqlTaskStatus status, string script, string errorMessage = null) { var newScript = new TaskScript { Status = status, Script = script, ErrorMessage = errorMessage }; OnScriptAdded(new TaskEventArgs <TaskScript>(newScript, this)); return(newScript); }
public async Task RunScriptShouldReturnScriptContent() { SqlTaskStatus expectedStatus = SqlTaskStatus.Succeeded; DatabaseOperationStub operation = new DatabaseOperationStub(); operation.TaskResult = new TaskResult { TaskStatus = expectedStatus }; SqlTask sqlTask = new SqlTask(new TaskMetadata(), operation.FunctionToScript, null); Assert.Equal(sqlTask.TaskStatus, SqlTaskStatus.NotStarted); Task taskToVerify = sqlTask.RunAsync().ContinueWith(task => { Assert.Equal(sqlTask.TaskStatus, expectedStatus); Assert.Equal(sqlTask.IsCompleted, true); Assert.NotNull(operation.TaskScript); Assert.True(!string.IsNullOrEmpty(operation.TaskScript.Script)); }); await taskToVerify; }
public async Task FailedOperationShouldFailTheTask() { SqlTaskStatus expectedStatus = SqlTaskStatus.Failed; DatabaseOperationStub operation = new DatabaseOperationStub(); operation.TaskResult = new TaskResult { }; SqlTask sqlTask = new SqlTask(new TaskMetadata(), operation.FunctionToRun, operation.FunctionToCancel); Assert.Equal(sqlTask.TaskStatus, SqlTaskStatus.NotStarted); Task taskToVerify = sqlTask.RunAsync().ContinueWith(task => { Assert.Equal(sqlTask.TaskStatus, expectedStatus); Assert.True(sqlTask.Duration > 0); }); Assert.Equal(sqlTask.TaskStatus, SqlTaskStatus.InProgress); Thread.Sleep(1000); operation.FailTheOperation(); await taskToVerify; }
/// <summary> /// Adds a new message to the task messages /// </summary> /// <param name="description">Message description</param> /// <param name="status">Status of the message</param> /// <param name="insertAboveLast">If true, the new messages will be added to the top. Default is false</param> /// <returns></returns> public TaskMessage AddMessage(string description, SqlTaskStatus status = SqlTaskStatus.NotStarted, bool insertAboveLast = false) { ValidateNotDisposed(); if (!insertAboveLast) { // Make sure the last message is set to a completed status if a new message is being added at the bottom CompleteLastMessageStatus(); } var newMessage = new TaskMessage { Description = description, Status = status, }; lock (lockObject) { if (!insertAboveLast || messages.Count == 0) { messages.Add(newMessage); } else { int lastMessageIndex = messages.Count - 1; messages.Insert(lastMessageIndex, newMessage); } } OnMessageAdded(new TaskEventArgs <TaskMessage>(newMessage, this)); // If the slot is completed, this may be the last message, make sure the message is also set to completed. if (IsCompleted) { CompleteLastMessageStatus(); } return(newMessage); }
public async Task CancelTaskShouldCancelTheOperation() { using (SqlTaskManager manager = new SqlTaskManager()) { SqlTaskStatus expectedStatus = SqlTaskStatus.Canceled; DatabaseOperationStub operation = new DatabaseOperationStub(); operation.TaskResult = new TaskResult { }; SqlTask sqlTask = manager.CreateTask(taskMetaData, operation.FunctionToRun, operation.FunctionToCancel); Assert.NotNull(sqlTask); Task taskToVerify = sqlTask.RunAsync().ContinueWith(task => { Assert.Equal(expectedStatus, sqlTask.TaskStatus); Assert.Equal(sqlTask.IsCancelRequested, true); manager.Reset(); }); manager.CancelTask(sqlTask.TaskId); await taskToVerify; } }
public async Task RunShouldRunTheFunctionAndGetTheResult() { SqlTaskStatus expectedStatus = SqlTaskStatus.Succeeded; DatabaseOperationStub operation = new DatabaseOperationStub(); operation.TaskResult = new TaskResult { TaskStatus = expectedStatus }; SqlTask sqlTask = new SqlTask(new TaskMetadata(), operation.FunctionToRun, null); Assert.Equal(sqlTask.TaskStatus, SqlTaskStatus.NotStarted); Task taskToVerify = sqlTask.RunAsync().ContinueWith(task => { Assert.Equal(sqlTask.TaskStatus, expectedStatus); Assert.Equal(sqlTask.IsCompleted, true); Assert.True(sqlTask.Duration > 0); }); Assert.Equal(sqlTask.TaskStatus, SqlTaskStatus.InProgress); Thread.Sleep(1000); operation.Stop(); await taskToVerify; }