mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-21 01:25:42 -05:00
Block Initializing Edit Sessions When In Progress (#265)
* Implementation of a wait handle for initialize * WIP * Adding more initialize unit tests
This commit is contained in:
@@ -215,6 +215,142 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
edit.Verify(e => e.SetCell(It.IsAny<int>(), It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null, "table", "table")] // Null owner URI
|
||||
[InlineData(Common.OwnerUri, null, "table")] // Null object name
|
||||
[InlineData(Common.OwnerUri, "table", null)] // Null object type
|
||||
public async Task InitializeNullParams(string ownerUri, string objName, string objType)
|
||||
{
|
||||
// Setup: Create an edit data service without a session
|
||||
var eds = new EditDataService(null, null, null);
|
||||
|
||||
// If:
|
||||
// ... I have init params with a null parameter
|
||||
var initParams = new EditInitializeParams
|
||||
{
|
||||
ObjectName = objName,
|
||||
OwnerUri = ownerUri,
|
||||
ObjectType = objType
|
||||
};
|
||||
|
||||
// ... And I initialize an edit session with that
|
||||
var efv = new EventFlowValidator<EditInitializeResult>()
|
||||
.AddErrorValidation<string>(Assert.NotNull)
|
||||
.Complete();
|
||||
await eds.HandleInitializeRequest(initParams, efv.Object);
|
||||
|
||||
// Then:
|
||||
// ... An error event should have been raised
|
||||
efv.Validate();
|
||||
|
||||
// ... There should not be a session
|
||||
Assert.Empty(eds.ActiveSessions);
|
||||
|
||||
// ... There should not be a wait handler
|
||||
Assert.Empty(eds.InitializeWaitHandles);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task InitializeInProgress()
|
||||
{
|
||||
// Setup: Create an edit data service with an "in-progress initialize"
|
||||
var eds = new EditDataService(null, null, null);
|
||||
eds.InitializeWaitHandles[Common.OwnerUri] = new TaskCompletionSource<bool>();
|
||||
|
||||
// If:
|
||||
// ... I ask to initialize a session when an initialize task is already in progress
|
||||
var initParams = new EditInitializeParams
|
||||
{
|
||||
ObjectName = "table",
|
||||
OwnerUri = Common.OwnerUri,
|
||||
ObjectType = "table"
|
||||
};
|
||||
var efv = new EventFlowValidator<EditInitializeResult>()
|
||||
.AddErrorValidation<string>(Assert.NotNull)
|
||||
.Complete();
|
||||
await eds.HandleInitializeRequest(initParams, efv.Object);
|
||||
|
||||
// Then:
|
||||
// ... An error event should have been raised
|
||||
efv.Validate();
|
||||
|
||||
// ... There should not be a session
|
||||
Assert.Empty(eds.ActiveSessions);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task InitializeQueryCreateFailed()
|
||||
{
|
||||
// Setup:
|
||||
// ... Create a query execution service that will throw on creation of the query
|
||||
var qes = QueryExecution.Common.GetPrimedExecutionService(null, false, false, null);
|
||||
|
||||
// ... Create an edit data service that uses the mocked up query service
|
||||
var eds = new EditDataService(qes, null, null);
|
||||
|
||||
// If:
|
||||
// ... I initialize a session
|
||||
var initParams = new EditInitializeParams
|
||||
{
|
||||
ObjectName = "table",
|
||||
OwnerUri = Common.OwnerUri,
|
||||
ObjectType = "table"
|
||||
};
|
||||
var efv = new EventFlowValidator<EditInitializeResult>()
|
||||
.AddErrorValidation<string>(Assert.NotEmpty)
|
||||
.Complete();
|
||||
await eds.HandleInitializeRequest(initParams, efv.Object);
|
||||
|
||||
// Then:
|
||||
// ... We should have gotten an error back
|
||||
efv.Validate();
|
||||
|
||||
// ... There should not be any sessions created
|
||||
Assert.Empty(eds.ActiveSessions);
|
||||
|
||||
// ... There should not be a wait handle
|
||||
Assert.Empty(eds.InitializeWaitHandles);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task InitializeQueryExecutionFails()
|
||||
{
|
||||
// Setup:
|
||||
// ... Create a query execution service that will throw on execution of the query
|
||||
var qes = QueryExecution.Common.GetPrimedExecutionService(null, true, true, null);
|
||||
|
||||
// ... Create an edit data service that uses the mocked up query service
|
||||
var eds = new EditDataService(qes, null, null);
|
||||
|
||||
// If:
|
||||
// ... I initialize a session
|
||||
var initParams = new EditInitializeParams
|
||||
{
|
||||
ObjectName = "table",
|
||||
OwnerUri = Common.OwnerUri,
|
||||
ObjectType = "table"
|
||||
};
|
||||
var efv = new EventFlowValidator<EditInitializeResult>()
|
||||
.AddResultValidation(Assert.NotNull)
|
||||
.AddEventValidation(EditSessionReadyEvent.Type, esrp =>
|
||||
{
|
||||
Assert.NotNull(esrp);
|
||||
Assert.False(esrp.Success);
|
||||
}).Complete();
|
||||
await eds.HandleInitializeRequest(initParams, efv.Object);
|
||||
await eds.InitializeWaitHandles[Common.OwnerUri].Task;
|
||||
|
||||
// Then:
|
||||
// ... We should have started execution, but failed
|
||||
efv.Validate();
|
||||
|
||||
// ... There should not be any sessions created
|
||||
Assert.Empty(eds.ActiveSessions);
|
||||
|
||||
// ... There should not be a wait handle. It should have been cleaned up by now
|
||||
Assert.Empty(eds.InitializeWaitHandles);
|
||||
}
|
||||
|
||||
private static EditSession GetDefaultSession()
|
||||
{
|
||||
// ... Create a session with a proper query and metadata
|
||||
|
||||
Reference in New Issue
Block a user