public async Task VerifyTaskWithExecutionMode(TaskExecutionMode executionMode, bool makeTaskFail = false) { serviceHostMock.AddEventHandling(TaskStatusChangedNotification.Type, null); using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile()) { //To make the task fail don't create the schema so create table fails string query = string.Empty; if (!makeTaskFail) { query = $"CREATE SCHEMA [test]"; } SqlTestDb testDb = await SqlTestDb.CreateNewAsync(TestServerType.OnPrem, false, null, query, "TaskService"); try { TestConnectionResult connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync(testDb.DatabaseName, queryTempFile.FilePath); string taskName = "task name"; Server server = CreateServerObject(connectionResult.ConnectionInfo); RequstParamStub requstParam = new RequstParamStub { TaskExecutionMode = executionMode, OwnerUri = queryTempFile.FilePath }; SmoScriptableTaskOperationStub taskOperation = new SmoScriptableTaskOperationStub(server); taskOperation.DatabaseName = testDb.DatabaseName; taskOperation.TableName = "newTable"; TaskMetadata taskMetadata = TaskMetadata.Create(requstParam, taskName, taskOperation, ConnectionService.Instance); SqlTask sqlTask = service.TaskManager.CreateTask <SqlTask>(taskMetadata); Task taskToVerify = sqlTask.RunAsync().ContinueWith(task => { if (!makeTaskFail) { if (executionMode == TaskExecutionMode.Script || executionMode == TaskExecutionMode.ExecuteAndScript) { serviceHostMock.Verify(x => x.SendEvent(TaskStatusChangedNotification.Type, It.Is <TaskProgressInfo>(t => !string.IsNullOrEmpty(t.Script))), Times.AtLeastOnce()); } //Verify if the table created if execution mode includes execute bool expected = executionMode == TaskExecutionMode.Execute || executionMode == TaskExecutionMode.ExecuteAndScript; Server serverToverfiy = CreateServerObject(connectionResult.ConnectionInfo); bool actual = serverToverfiy.Databases[testDb.DatabaseName].Tables.Contains(taskOperation.TableName, "test"); Assert.Equal(expected, actual); } else { serviceHostMock.Verify(x => x.SendEvent(TaskStatusChangedNotification.Type, It.Is <TaskProgressInfo>(t => t.Status == SqlTaskStatus.Failed)), Times.AtLeastOnce()); } }); await taskToVerify; } finally { testDb.Cleanup(); } } }
private async Task <SessionCreatedParameters> CreateSession() { ConnectionDetails details = new ConnectionDetails() { UserName = "******", Password = "******", DatabaseName = "msdb", ServerName = "serverName" }; SessionCreatedParameters sessionResult = null; serviceHostMock.AddEventHandling(CreateSessionCompleteNotification.Type, (et, p) => sessionResult = p); CreateSessionResponse result = default(CreateSessionResponse); var contextMock = RequestContextMocks.Create <CreateSessionResponse>(r => result = r).AddErrorHandling(null); connectionServiceMock.Setup(c => c.Connect(It.IsAny <ConnectParams>())) .Returns((ConnectParams connectParams) => Task.FromResult(GetCompleteParamsForConnection(connectParams.OwnerUri, details))); ConnectionInfo connectionInfo = new ConnectionInfo(null, null, null); string fakeConnectionString = "Data Source=server;Initial Catalog=database;Integrated Security=False;User Id=user"; connectionInfo.AddConnection("Default", new SqlConnection(fakeConnectionString)); connectionServiceMock.Setup((c => c.TryFindConnection(It.IsAny <string>(), out connectionInfo))). OutCallback((string t, out ConnectionInfo v) => v = connectionInfo) .Returns(true); connectionServiceMock.Setup(c => c.Disconnect(It.IsAny <DisconnectParams>())).Returns(true); await service.HandleCreateSessionRequest(details, contextMock.Object); await service.CreateSessionTask; return(sessionResult); }
private async Task <SessionCreatedParameters> CreateSession() { SessionCreatedParameters sessionResult = null; serviceHostMock.AddEventHandling(CreateSessionCompleteNotification.Type, (et, p) => sessionResult = p); CreateSessionResponse result = default(CreateSessionResponse); var contextMock = RequestContextMocks.Create <CreateSessionResponse>(r => result = r).AddErrorHandling(null); connectionServiceMock.Setup(c => c.Connect(It.IsAny <ConnectParams>())) .Returns((ConnectParams connectParams) => Task.FromResult(GetCompleteParamsForConnection(connectParams.OwnerUri, details))); ConnectionInfo connectionInfo = new ConnectionInfo(null, null, details); connectionInfo.AddConnection("Default", new SqlConnection(fakeConnectionString)); connectionServiceMock.Setup((c => c.TryFindConnection(It.IsAny <string>(), out connectionInfo))). OutCallback((string t, out ConnectionInfo v) => v = connectionInfo) .Returns(true); connectionServiceMock.Setup(c => c.Disconnect(It.IsAny <DisconnectParams>())).Returns(true); await service.HandleCreateSessionRequest(details, contextMock.Object); await service.CreateSessionTask; return(sessionResult); }
public void NewTaskShouldSendNotification() { serviceHostMock.AddEventHandling(TaskCreatedNotification.Type, null); serviceHostMock.AddEventHandling(TaskStatusChangedNotification.Type, null); DatabaseOperationStub operation = new DatabaseOperationStub(); SqlTask sqlTask = service.TaskManager.CreateTask(taskMetaData, operation.FunctionToRun); sqlTask.Run(); serviceHostMock.Verify(x => x.SendEvent(TaskCreatedNotification.Type, It.Is <TaskInfo>(t => t.TaskId == sqlTask.TaskId.ToString() && t.ProviderName == "MSSQL")), Times.Once()); operation.Stop(); Thread.Sleep(2000); serviceHostMock.Verify(x => x.SendEvent(TaskStatusChangedNotification.Type, It.Is <TaskProgressInfo>(t => t.TaskId == sqlTask.TaskId.ToString())), Times.AtLeastOnce()); }