Feature: Progressive Messages (#208)

This change is a reworking of the way that messages are sent to clients from the service layer. It is also a reworking of the protocol to ensure that all formulations of query send back events to the client in a deterministic ordering. To support the first change:
* Added a new event that will be sent when a message is generated
* Messages now indicate which Batch (if any) generated them
* Messages now indicate if they were error level
* Removed message storage in Batch objects and BatchSummary objects
* Batch objects no longer have error state
This commit is contained in:
Benjamin Russell
2017-01-10 16:42:03 -08:00
committed by GitHub
parent a77fb77a85
commit e71bcefb28
16 changed files with 545 additions and 509 deletions

View File

@@ -5,9 +5,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Castle.Components.DictionaryAdapter;
using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.QueryExecution;
using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts;
using Microsoft.SqlTools.ServiceLayer.SqlContext;
using Xunit;
@@ -16,6 +19,22 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.QueryExecution.Execution
public class QueryTests
{
[Fact]
public void QueryCreationCorrect()
{
// If:
// ... I create a query
ConnectionInfo ci = Common.CreateTestConnectionInfo(null, false);
var fileStreamFactory = Common.GetFileStreamFactory(new Dictionary<string, byte[]>());
Query query = new Query(Common.StandardQuery, ci, new QueryExecutionSettings(), fileStreamFactory);
// Then:
// ... I should get back two batches to execute that haven't been executed
Assert.NotEmpty(query.QueryText);
Assert.False(query.HasExecuted);
Assert.Throws<InvalidOperationException>(() => query.BatchSummaries);
}
[Fact]
public void QueryExecuteNoQueryText()
{
@@ -63,44 +82,30 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.QueryExecution.Execution
public void QueryExecuteSingleBatch()
{
// Setup:
// ... Create a callback for atch start
// ... Keep track of how many times the callbacks were called
int batchStartCallbacksReceived = 0;
Batch.BatchAsyncEventHandler batchStartCallback = b =>
{
batchStartCallbacksReceived++;
return Task.FromResult(0);
};
// ... Create a callback for batch completion
int batchCompleteCallbacksReceived = 0;
Batch.BatchAsyncEventHandler batchCompleteCallback = summary =>
{
batchCompleteCallbacksReceived++;
return Task.CompletedTask;
};
int batchMessageCallbacksReceived = 0;
// If:
// ... I create a query from a single batch (without separator)
ConnectionInfo ci = Common.CreateTestConnectionInfo(null, false);
var fileStreamFactory = Common.GetFileStreamFactory(new Dictionary<string, byte[]>());
Query query = new Query(Common.StandardQuery, ci, new QueryExecutionSettings(), fileStreamFactory);
query.BatchStarted += batchStartCallback;
query.BatchCompleted += batchCompleteCallback;
BatchCallbackHelper(query,
b => batchStartCallbacksReceived++,
b => batchCompleteCallbacksReceived++,
m => batchMessageCallbacksReceived++);
// Then:
// ... I should get a single batch to execute that hasn't been executed
Assert.NotEmpty(query.QueryText);
Assert.NotEmpty(query.Batches);
Assert.Equal(1, query.Batches.Length);
Assert.False(query.HasExecuted);
Assert.Throws<InvalidOperationException>(() => query.BatchSummaries);
// If:
// ... I then execute the query
query.Execute();
query.ExecutionTask.Wait();
// Then:
// ... There should be exactly 1 batch
Assert.NotEmpty(query.Batches);
Assert.Equal(1, query.Batches.Length);
// ... The query should have completed successfully with one batch summary returned
Assert.True(query.HasExecuted);
Assert.NotEmpty(query.BatchSummaries);
@@ -109,38 +114,24 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.QueryExecution.Execution
// ... The batch callbacks should have been called precisely 1 time
Assert.Equal(1, batchStartCallbacksReceived);
Assert.Equal(1, batchCompleteCallbacksReceived);
Assert.Equal(1, batchMessageCallbacksReceived);
}
[Fact]
public void QueryExecuteNoOpBatch()
public void QueryExecuteSingleNoOpBatch()
{
// Setup:
// ... Create a callback for batch startup
Batch.BatchAsyncEventHandler batchStartCallback = b =>
{
throw new Exception("Batch startup callback should not have been called.");
};
// ... Create a callback for batch completion
Batch.BatchAsyncEventHandler batchCompletionCallback = summary =>
{
throw new Exception("Batch completion callback was called");
};
// Setup: Keep track of all the messages received
List<ResultMessage> messages = new List<ResultMessage>();
// If:
// ... I create a query from a single batch that does nothing
ConnectionInfo ci = Common.CreateTestConnectionInfo(null, false);
var fileStreamFactory = Common.GetFileStreamFactory(new Dictionary<string, byte[]>());
Query query = new Query(Common.NoOpQuery, ci, new QueryExecutionSettings(), fileStreamFactory);
query.BatchStarted += batchStartCallback;
query.BatchCompleted += batchCompletionCallback;
// Then:
// ... I should get no batches back
Assert.NotEmpty(query.QueryText);
Assert.Empty(query.Batches);
Assert.False(query.HasExecuted);
Assert.Throws<InvalidOperationException>(() => query.BatchSummaries);
BatchCallbackHelper(query,
b => { throw new Exception("Batch startup callback should not have been called."); },
b => { throw new Exception("Batch completion callback was called"); },
m => messages.Add(m));
// If:
// ... I Then execute the query
@@ -148,30 +139,27 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.QueryExecution.Execution
query.ExecutionTask.Wait();
// Then:
// ... There should be no batches
Assert.Empty(query.Batches);
// ... The query should have completed successfully with no batch summaries returned
Assert.True(query.HasExecuted);
Assert.Empty(query.BatchSummaries);
// ... The message callback should have been called exactly once
// ... The message must not have a batch associated with it
Assert.Equal(1, messages.Count);
Assert.Null(messages[0].BatchId);
}
[Fact]
public void QueryExecuteMultipleBatches()
public void QueryExecuteMultipleResultBatches()
{
// Setup:
// ... Create a callback for batch start
// ... Keep track of how many callbacks are received
int batchStartCallbacksReceived = 0;
Batch.BatchAsyncEventHandler batchStartCallback = b =>
{
batchStartCallbacksReceived++;
return Task.FromResult(0);
};
// ... Create a callback for batch completion
int batchCompletedCallbacksReceived = 0;
Batch.BatchAsyncEventHandler batchCompletedCallback = summary =>
{
batchCompletedCallbacksReceived++;
return Task.FromResult(0);
};
int batchMessageCallbacksReceived = 0;
// If:
// ... I create a query from two batches (with separator)
@@ -179,52 +167,39 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.QueryExecution.Execution
string queryText = string.Format("{0}\r\nGO\r\n{0}", Common.StandardQuery);
var fileStreamFactory = Common.GetFileStreamFactory(new Dictionary<string, byte[]>());
Query query = new Query(queryText, ci, new QueryExecutionSettings(), fileStreamFactory);
query.BatchStarted += batchStartCallback;
query.BatchCompleted += batchCompletedCallback;
BatchCallbackHelper(query,
b => batchStartCallbacksReceived++,
b => batchCompletedCallbacksReceived++,
m => batchMessageCallbacksReceived++);
// Then:
// ... I should get back two batches to execute that haven't been executed
Assert.NotEmpty(query.QueryText);
Assert.NotEmpty(query.Batches);
Assert.Equal(2, query.Batches.Length);
Assert.False(query.HasExecuted);
Assert.Throws<InvalidOperationException>(() => query.BatchSummaries);
// If:
// ... I then execute the query
query.Execute();
query.ExecutionTask.Wait();
// Then:
// ... I should get back a query with one batch (no op batch is not included)
Assert.NotEmpty(query.Batches);
Assert.Equal(2, query.Batches.Length);
// ... The query should have completed successfully with two batch summaries returned
Assert.True(query.HasExecuted);
Assert.NotEmpty(query.BatchSummaries);
Assert.Equal(2, query.BatchSummaries.Length);
// ... The batch start and completion callbacks should have been called precisely 2 times
// ... The batch start, complete, and message callbacks should have been called precisely 2 times
Assert.Equal(2, batchStartCallbacksReceived);
Assert.Equal(2, batchCompletedCallbacksReceived);
Assert.Equal(2, batchMessageCallbacksReceived);
}
[Fact]
public void QueryExecuteMultipleBatchesWithNoOp()
{
// Setup:
// ... Create a callback for batch start
// ... Keep track of how many times callbacks are called
int batchStartCallbacksReceived = 0;
Batch.BatchAsyncEventHandler batchStartCallback = b =>
{
batchStartCallbacksReceived++;
return Task.FromResult(0);
};
// ... Create a callback for batch completion
int batchCompletionCallbacksReceived = 0;
Batch.BatchAsyncEventHandler batchCompletionCallback = summary =>
{
batchCompletionCallbacksReceived++;
return Task.CompletedTask;
};
int batchMessageCallbacksReceived = 0;
// If:
// ... I create a query from a two batches (with separator)
@@ -232,22 +207,20 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.QueryExecution.Execution
string queryText = string.Format("{0}\r\nGO\r\n{1}", Common.StandardQuery, Common.NoOpQuery);
var fileStreamFactory = Common.GetFileStreamFactory(new Dictionary<string, byte[]>());
Query query = new Query(queryText, ci, new QueryExecutionSettings(), fileStreamFactory);
query.BatchStarted += batchStartCallback;
query.BatchCompleted += batchCompletionCallback;
BatchCallbackHelper(query,
b => batchStartCallbacksReceived++,
b => batchCompletionCallbacksReceived++,
m => batchMessageCallbacksReceived++);
// Then:
// ... I should get back one batch to execute that hasn't been executed
Assert.NotEmpty(query.QueryText);
Assert.NotEmpty(query.Batches);
Assert.Equal(1, query.Batches.Length);
Assert.False(query.HasExecuted);
Assert.Throws<InvalidOperationException>(() => query.BatchSummaries);
// If:
// .. I then execute the query
query.Execute();
query.ExecutionTask.Wait();
// Then:
// ... I should get back a query with one batch (no op batch is not included)
Assert.NotEmpty(query.Batches);
Assert.Equal(1, query.Batches.Length);
// ... The query should have completed successfully with one batch summary returned
Assert.True(query.HasExecuted);
Assert.NotEmpty(query.BatchSummaries);
@@ -256,61 +229,107 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.QueryExecution.Execution
// ... The batch callbacks should have been called precisely 1 time
Assert.Equal(1, batchStartCallbacksReceived);
Assert.Equal(1, batchCompletionCallbacksReceived);
Assert.Equal(1, batchMessageCallbacksReceived);
}
[Fact]
public async Task QueryExecuteMultipleNoOpBatches()
{
// Setup:
// ... Keep track of how many messages were sent
List<ResultMessage> messages = new List<ResultMessage>();
// If:
// ... I create a query from a two batches (with separator)
ConnectionInfo ci = Common.CreateTestConnectionInfo(null, false);
string queryText = string.Format("{0}\r\nGO\r\n{1}", Common.NoOpQuery, Common.NoOpQuery);
var fileStreamFactory = Common.GetFileStreamFactory(new Dictionary<string, byte[]>());
Query query = new Query(queryText, ci, new QueryExecutionSettings(), fileStreamFactory);
BatchCallbackHelper(query,
b => { throw new Exception("Batch start handler was called"); },
b => { throw new Exception("Batch completed handler was called"); },
m => messages.Add(m));
// .. I then execute the query
query.Execute();
await query.ExecutionTask;
// Then:
// ... I should get back a query with no batches
Assert.Empty(query.Batches);
// ... The query should have completed successfully with one zero batch summaries returned
Assert.True(query.HasExecuted);
Assert.Empty(query.BatchSummaries);
// ... The message callback should have been called exactly once
// ... The message must not have a batch associated with it
Assert.Equal(1, messages.Count);
Assert.Null(messages[0].BatchId);
}
[Fact]
public void QueryExecuteInvalidBatch()
{
// Setup:
// ... Create a callback for batch start
// ... Keep track of how many times a method is called
int batchStartCallbacksReceived = 0;
Batch.BatchAsyncEventHandler batchStartCallback = b =>
{
batchStartCallbacksReceived++;
return Task.FromResult(0);
};
// ... Create a callback for batch completion
int batchCompletionCallbacksReceived = 0;
Batch.BatchAsyncEventHandler batchCompltionCallback = summary =>
{
batchCompletionCallbacksReceived++;
return Task.CompletedTask;
};
List<ResultMessage> messages = new List<ResultMessage>();
// If:
// ... I create a query from an invalid batch
ConnectionInfo ci = Common.CreateTestConnectionInfo(null, true);
var fileStreamFactory = Common.GetFileStreamFactory(new Dictionary<string, byte[]>());
Query query = new Query(Common.InvalidQuery, ci, new QueryExecutionSettings(), fileStreamFactory);
query.BatchStarted += batchStartCallback;
query.BatchCompleted += batchCompltionCallback;
BatchCallbackHelper(query,
b => batchStartCallbacksReceived++,
b => batchCompletionCallbacksReceived++,
m => messages.Add(m));
// Then:
// ... I should get back a query with one batch not executed
Assert.NotEmpty(query.QueryText);
Assert.NotEmpty(query.Batches);
Assert.Equal(1, query.Batches.Length);
Assert.False(query.HasExecuted);
Assert.Throws<InvalidOperationException>(() => query.BatchSummaries);
// If:
// ... I then execute the query
query.Execute();
query.ExecutionTask.Wait();
// Then:
// ... I should get back a query with one batch
Assert.NotEmpty(query.Batches);
Assert.Equal(1, query.Batches.Length);
// ... There should be an error on the batch
Assert.True(query.HasExecuted);
Assert.NotEmpty(query.BatchSummaries);
Assert.Equal(1, query.BatchSummaries.Length);
Assert.True(query.BatchSummaries[0].HasError);
Assert.NotEmpty(query.BatchSummaries[0].Messages);
Assert.True(messages.Any(m => m.IsError));
// ... The batch callbacks should have been called once
Assert.Equal(1, batchStartCallbacksReceived);
Assert.Equal(1, batchCompletionCallbacksReceived);
}
private static void BatchCallbackHelper(Query q, Action<Batch> startCallback, Action<Batch> endCallback,
Action<ResultMessage> messageCallback)
{
// Setup the callback for batch start
q.BatchStarted += b =>
{
startCallback?.Invoke(b);
return Task.FromResult(0);
};
// Setup the callback for batch completion
q.BatchCompleted += b =>
{
endCallback?.Invoke(b);
return Task.FromResult(0);
};
// Setup the callback for batch messages
q.BatchMessageSent += (m) =>
{
messageCallback?.Invoke(m);
return Task.FromResult(0);
};
}
}
}