mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-27 01:25:42 -05:00
Convert most tools service tests to nunit (#1037)
* Remove xunit dependency from testdriver * swap expected/actual as needed * Convert Test.Common to nunit * port hosting unit tests to nunit * port batchparser integration tests to nunit * port testdriver.tests to nunit * fix target to copy dependency * port servicelayer unittests to nunit * more unit test fixes * port integration tests to nunit * fix test method type * try using latest windows build for PRs * reduce test memory use
This commit is contained in:
@@ -4,14 +4,16 @@
|
||||
//
|
||||
|
||||
using System.Collections.Concurrent;
|
||||
using System.Linq;
|
||||
using Microsoft.SqlTools.Hosting.Protocol;
|
||||
using Xunit;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class EventContextTests
|
||||
{
|
||||
[Fact]
|
||||
[Test]
|
||||
public void SendEvent()
|
||||
{
|
||||
// Setup: Create collection
|
||||
@@ -20,11 +22,11 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
// If: I construct an event context with a message writer
|
||||
// And send an event with it
|
||||
var eventContext = new EventContext(bc);
|
||||
eventContext.SendEvent(CommonObjects.EventType, CommonObjects.TestMessageContents.DefaultInstance);
|
||||
|
||||
// Then: The message should be added to the queue
|
||||
Assert.Single(bc.ToArray());
|
||||
Assert.Equal(MessageType.Event, bc.ToArray()[0].MessageType);
|
||||
eventContext.SendEvent(CommonObjects.EventType, CommonObjects.TestMessageContents.DefaultInstance);
|
||||
|
||||
// Then: The message should be added to the queue
|
||||
var messages = bc.ToArray().Select(m => m.MessageType);
|
||||
Assert.That(messages, Is.EqualTo(new[] { MessageType.Event }), "Single message of type event in the queue after SendEvent");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,13 +12,14 @@ using Microsoft.SqlTools.Hosting.Channels;
|
||||
using Microsoft.SqlTools.Hosting.Protocol;
|
||||
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class JsonRpcHostTests
|
||||
{
|
||||
[Fact]
|
||||
[Test]
|
||||
public void ConstructWithNullProtocolChannel()
|
||||
{
|
||||
// If: I construct a JSON RPC host with a null protocol channel
|
||||
@@ -28,7 +29,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
|
||||
#region SetRequestHandler Tests
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void SetAsyncRequestHandlerNullRequestType()
|
||||
{
|
||||
// If: I assign a request handler on the JSON RPC host with a null request type
|
||||
@@ -38,7 +39,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
jh.SetAsyncRequestHandler<object, object>(null, (a, b) => Task.FromResult(false)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void SetAsyncRequestHandlerNullRequestHandler()
|
||||
{
|
||||
// If: I assign a request handler on the JSON RPC host with a null request handler
|
||||
@@ -47,7 +48,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
Assert.Throws<ArgumentNullException>(() => jh.SetAsyncRequestHandler(CommonObjects.RequestType, null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void SetSyncRequestHandlerNullRequestHandler()
|
||||
{
|
||||
// If: I assign a request handler on the JSON RPC host with a null request handler
|
||||
@@ -56,10 +57,8 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
Assert.Throws<ArgumentNullException>(() => jh.SetRequestHandler(CommonObjects.RequestType, null));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public async Task SetAsyncRequestHandler(bool nullContents)
|
||||
[Test]
|
||||
public async Task SetAsyncRequestHandler([Values]bool nullContents)
|
||||
{
|
||||
// Setup: Create a mock request handler
|
||||
var requestHandler = new Mock<Func<CommonObjects.TestMessageContents, RequestContext<CommonObjects.TestMessageContents>, Task>>();
|
||||
@@ -69,11 +68,10 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
|
||||
// If: I assign a request handler on the JSON RPC host
|
||||
var jh = new JsonRpcHost(GetChannelBase(null, null).Object);
|
||||
jh.SetAsyncRequestHandler(CommonObjects.RequestType, requestHandler.Object);
|
||||
|
||||
// Then: It should be the only request handler set
|
||||
Assert.Single(jh.requestHandlers);
|
||||
Assert.Contains(CommonObjects.RequestType.MethodName, jh.requestHandlers.Keys);
|
||||
jh.SetAsyncRequestHandler(CommonObjects.RequestType, requestHandler.Object);
|
||||
|
||||
// Then: It should be the only request handler set
|
||||
Assert.That(jh.requestHandlers.Keys, Is.EqualTo(new[] { CommonObjects.RequestType.MethodName }), "requestHandlers.Keys after SetAsyncRequestHandler");
|
||||
|
||||
// If: I call the stored request handler
|
||||
await jh.requestHandlers[CommonObjects.RequestType.MethodName](message);
|
||||
@@ -89,10 +87,8 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
), Times.Exactly(2));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public async Task SetSyncRequestHandler(bool nullContents)
|
||||
[Test]
|
||||
public async Task SetSyncRequestHandler([Values]bool nullContents)
|
||||
{
|
||||
// Setup: Create a mock request handler
|
||||
var requestHandler = new Mock<Action<CommonObjects.TestMessageContents, RequestContext<CommonObjects.TestMessageContents>>>();
|
||||
@@ -102,13 +98,12 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
|
||||
// If: I assign a request handler on the JSON RPC host
|
||||
var jh = new JsonRpcHost(GetChannelBase(null, null).Object);
|
||||
jh.SetRequestHandler(CommonObjects.RequestType, requestHandler.Object);
|
||||
|
||||
// Then: It should be the only request handler set
|
||||
Assert.Single(jh.requestHandlers);
|
||||
Assert.Contains(CommonObjects.RequestType.MethodName, jh.requestHandlers.Keys);
|
||||
|
||||
// If: I call the stored request handler
|
||||
jh.SetRequestHandler(CommonObjects.RequestType, requestHandler.Object);
|
||||
|
||||
// Then: It should be the only request handler set
|
||||
Assert.That(jh.requestHandlers.Keys, Is.EqualTo(new[] { CommonObjects.RequestType.MethodName }), "requestHandlers.Keys after SetRequestHandler");
|
||||
|
||||
// If: I call the stored request handler
|
||||
await jh.requestHandlers[CommonObjects.RequestType.MethodName](message);
|
||||
await jh.requestHandlers[CommonObjects.RequestType.MethodName](message);
|
||||
|
||||
@@ -122,7 +117,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
), Times.Exactly(2));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public async Task SetAsyncRequestHandlerOverrideTrue()
|
||||
{
|
||||
// Setup: Create two mock request handlers
|
||||
@@ -137,8 +132,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
jh.SetAsyncRequestHandler(CommonObjects.RequestType, requestHandler2.Object, true);
|
||||
|
||||
// Then: There should only be one request handler
|
||||
Assert.Single(jh.requestHandlers);
|
||||
Assert.Contains(CommonObjects.RequestType.MethodName, jh.requestHandlers.Keys);
|
||||
Assert.That(jh.requestHandlers.Keys, Is.EqualTo(new[] { CommonObjects.RequestType.MethodName }), "requestHandlers.Keys after SetAsyncRequestHandler with override");
|
||||
|
||||
// If: I call the stored request handler
|
||||
await jh.requestHandlers[CommonObjects.RequestType.MethodName](CommonObjects.RequestMessage);
|
||||
@@ -154,7 +148,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
), Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void SetAsyncRequestHandlerOverrideFalse()
|
||||
{
|
||||
// Setup: Create a mock request handler
|
||||
@@ -166,14 +160,14 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
// Then: I should get an exception
|
||||
var jh = new JsonRpcHost(GetChannelBase(null, null).Object);
|
||||
jh.SetAsyncRequestHandler(CommonObjects.RequestType, requestHandler.Object);
|
||||
Assert.ThrowsAny<Exception>(() => jh.SetAsyncRequestHandler(CommonObjects.RequestType, requestHandler.Object));
|
||||
Assert.That(() => jh.SetAsyncRequestHandler(CommonObjects.RequestType, requestHandler.Object), Throws.InstanceOf<Exception>(), "Reassign request handler without overriding");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region SetEventHandler Tests
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void SetAsyncEventHandlerNullEventType()
|
||||
{
|
||||
// If: I assign an event handler on the JSON RPC host with a null event type
|
||||
@@ -183,7 +177,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
jh.SetAsyncEventHandler<object>(null, (a, b) => Task.FromResult(false)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void SetAsyncEventHandlerNull()
|
||||
{
|
||||
// If: I assign an event handler on the message gispatcher with a null event handler
|
||||
@@ -191,7 +185,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
Assert.Throws<ArgumentNullException>(() => jh.SetAsyncEventHandler(CommonObjects.EventType, null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void SetSyncEventHandlerNull()
|
||||
{
|
||||
// If: I assign an event handler on the message gispatcher with a null event handler
|
||||
@@ -199,10 +193,8 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
Assert.Throws<ArgumentNullException>(() => jh.SetEventHandler(CommonObjects.EventType, null));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(true)]
|
||||
[InlineData(false)]
|
||||
public async Task SetAsyncEventHandler(bool nullContents)
|
||||
[Test]
|
||||
public async Task SetAsyncEventHandler([Values]bool nullContents)
|
||||
{
|
||||
// Setup: Create a mock request handler
|
||||
var eventHandler = new Mock<Func<CommonObjects.TestMessageContents, EventContext, Task>>();
|
||||
@@ -212,13 +204,12 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
|
||||
// If: I assign an event handler on the JSON RPC host
|
||||
var jh = new JsonRpcHost(GetChannelBase(null, null).Object);
|
||||
jh.SetAsyncEventHandler(CommonObjects.EventType, eventHandler.Object);
|
||||
|
||||
// Then: It should be the only event handler set
|
||||
Assert.Single(jh.eventHandlers);
|
||||
Assert.Contains(CommonObjects.EventType.MethodName, jh.eventHandlers.Keys);
|
||||
|
||||
// If: I call the stored event handler
|
||||
jh.SetAsyncEventHandler(CommonObjects.EventType, eventHandler.Object);
|
||||
|
||||
// Then: It should be the only event handler set
|
||||
Assert.That(jh.eventHandlers.Keys, Is.EqualTo(new[] { CommonObjects.EventType.MethodName }), "eventHandlers.Keys after SetAsyncRequestHandler");
|
||||
|
||||
// If: I call the stored event handler
|
||||
await jh.eventHandlers[CommonObjects.EventType.MethodName](message);
|
||||
await jh.eventHandlers[CommonObjects.EventType.MethodName](message);
|
||||
|
||||
@@ -232,10 +223,8 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
), Times.Exactly(2));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(true)]
|
||||
[InlineData(false)]
|
||||
public async Task SetSyncEventHandler(bool nullContents)
|
||||
[Test]
|
||||
public async Task SetSyncEventHandler([Values]bool nullContents)
|
||||
{
|
||||
// Setup: Create a mock request handler
|
||||
var eventHandler = new Mock<Action<CommonObjects.TestMessageContents, EventContext>>();
|
||||
@@ -248,8 +237,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
jh.SetEventHandler(CommonObjects.EventType, eventHandler.Object);
|
||||
|
||||
// Then: It should be the only event handler set
|
||||
Assert.Single(jh.eventHandlers);
|
||||
Assert.Contains(CommonObjects.EventType.MethodName, jh.eventHandlers.Keys);
|
||||
Assert.That(jh.eventHandlers.Keys, Is.EqualTo(new object[] { CommonObjects.EventType.MethodName }), "eventHandlers.Keys after SetEventHandler");
|
||||
|
||||
// If: I call the stored event handler
|
||||
await jh.eventHandlers[CommonObjects.EventType.MethodName](message);
|
||||
@@ -265,7 +253,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
), Times.Exactly(2));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public async Task SetAsyncEventHandlerOverrideTrue()
|
||||
{
|
||||
// Setup: Create two mock event handlers
|
||||
@@ -281,8 +269,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
jh.SetAsyncEventHandler(CommonObjects.EventType, eventHandler2.Object, true);
|
||||
|
||||
// Then: There should only be one event handler
|
||||
Assert.Single(jh.eventHandlers);
|
||||
Assert.Contains(CommonObjects.EventType.MethodName, jh.eventHandlers.Keys);
|
||||
Assert.That(jh.eventHandlers.Keys, Is.EqualTo(new[] { CommonObjects.EventType.MethodName }), "requestHandlers.Keys after SetAsyncEventHandler with override");
|
||||
|
||||
// If: I call the stored event handler
|
||||
await jh.eventHandlers[CommonObjects.EventType.MethodName](CommonObjects.EventMessage);
|
||||
@@ -298,7 +285,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
), Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void SetAsyncEventHandlerOverrideFalse()
|
||||
{
|
||||
// Setup: Create a mock event handler
|
||||
@@ -310,14 +297,14 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
// Then: I should get an exception
|
||||
var jh = new JsonRpcHost(GetChannelBase(null, null).Object);
|
||||
jh.SetAsyncEventHandler(CommonObjects.EventType, eventHandler.Object);
|
||||
Assert.ThrowsAny<Exception>(() => jh.SetAsyncEventHandler(CommonObjects.EventType, eventHandler.Object));
|
||||
Assert.That(() => jh.SetAsyncEventHandler(CommonObjects.EventType, eventHandler.Object), Throws.InstanceOf<Exception>(), "SetAsyncEventHandler without overriding");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region SendEvent Tests
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void SendEventNotConnected()
|
||||
{
|
||||
// If: I send an event when the protocol channel isn't connected
|
||||
@@ -326,36 +313,34 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
Assert.Throws<InvalidOperationException>(() => jh.SendEvent(CommonObjects.EventType, CommonObjects.TestMessageContents.DefaultInstance));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void SendEvent()
|
||||
{
|
||||
// Setup: Create a Json RPC Host with a connected channel
|
||||
var jh = new JsonRpcHost(GetChannelBase(null, null, true).Object);
|
||||
|
||||
// If: I send an event
|
||||
jh.SendEvent(CommonObjects.EventType, CommonObjects.TestMessageContents.DefaultInstance);
|
||||
|
||||
// Then: The message should be added to the output queue
|
||||
Assert.Single(jh.outputQueue.ToArray());
|
||||
var m = jh.outputQueue.ToArray()[0];
|
||||
Assert.Equal(CommonObjects.TestMessageContents.SerializedContents, m.Contents);
|
||||
Assert.Equal(CommonObjects.EventType.MethodName, m.Method);
|
||||
jh.SendEvent(CommonObjects.EventType, CommonObjects.TestMessageContents.DefaultInstance);
|
||||
|
||||
// Then: The message should be added to the output queue
|
||||
Assert.That(jh.outputQueue.ToArray().Select(m => (m.Contents, m.Method)),
|
||||
Is.EqualTo(new[] { (CommonObjects.TestMessageContents.SerializedContents, CommonObjects.EventType.MethodName) }), "outputQueue after SendEvent");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region SendRequest Tests
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public async Task SendRequestNotConnected()
|
||||
{
|
||||
// If: I send an event when the protocol channel isn't connected
|
||||
// Then: I should get an exception
|
||||
var jh = new JsonRpcHost(GetChannelBase(null, null).Object);
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(() => jh.SendRequest(CommonObjects.RequestType, CommonObjects.TestMessageContents.DefaultInstance));
|
||||
Assert.ThrowsAsync<InvalidOperationException>(() => jh.SendRequest(CommonObjects.RequestType, CommonObjects.TestMessageContents.DefaultInstance));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public async Task SendRequest()
|
||||
{
|
||||
// If: I send a request with the JSON RPC host
|
||||
@@ -363,21 +348,21 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
Task<CommonObjects.TestMessageContents> requestTask = jh.SendRequest(CommonObjects.RequestType, CommonObjects.TestMessageContents.DefaultInstance);
|
||||
|
||||
// Then: There should be a pending request
|
||||
Assert.Single(jh.pendingRequests);
|
||||
Assert.That(jh.pendingRequests.Count, Is.EqualTo(1), "pendingRequests.Count after SendRequest");
|
||||
|
||||
// If: I then trick it into completing the request
|
||||
jh.pendingRequests.First().Value.SetResult(CommonObjects.ResponseMessage);
|
||||
var responseContents = await requestTask;
|
||||
|
||||
// Then: The returned results should be the contents of the message
|
||||
Assert.Equal(CommonObjects.TestMessageContents.DefaultInstance, responseContents);
|
||||
Assert.AreEqual(CommonObjects.TestMessageContents.DefaultInstance, responseContents);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region DispatchMessage Tests
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public async Task DispatchMessageRequestWithoutHandler()
|
||||
{
|
||||
// Setup: Create a JSON RPC host without a request handler
|
||||
@@ -385,10 +370,10 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
|
||||
// If: I dispatch a request that doesn't have a handler
|
||||
// Then: I should get an exception
|
||||
await Assert.ThrowsAsync<MethodHandlerDoesNotExistException>(() => jh.DispatchMessage(CommonObjects.RequestMessage));
|
||||
Assert.ThrowsAsync<MethodHandlerDoesNotExistException>(() => jh.DispatchMessage(CommonObjects.RequestMessage));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public async Task DispatchMessageRequestException()
|
||||
{
|
||||
// Setup: Create a JSON RPC host with a request handler that throws an unhandled exception every time
|
||||
@@ -403,11 +388,10 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
|
||||
// If: I dispatch a message whose handler throws
|
||||
// Then: I should get an exception
|
||||
await Assert.ThrowsAsync<Exception>(() => jh.DispatchMessage(CommonObjects.RequestMessage));
|
||||
Assert.ThrowsAsync<Exception>(() => jh.DispatchMessage(CommonObjects.RequestMessage));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(DispatchMessageWithHandlerData))]
|
||||
[TestCaseSource(nameof(DispatchMessageWithHandlerData))]
|
||||
public async Task DispatchMessageRequestWithHandler(Task result)
|
||||
{
|
||||
// Setup: Create a JSON RPC host with a request handler setup
|
||||
@@ -429,7 +413,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public async Task DispatchmessageResponseWithoutHandler()
|
||||
{
|
||||
// Setup: Create a new JSON RPC host without any pending requests
|
||||
@@ -437,10 +421,10 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
|
||||
// If: I dispatch a response that doesn't have a pending request
|
||||
// Then: I should get an exception
|
||||
await Assert.ThrowsAsync<MethodHandlerDoesNotExistException>(() => jh.DispatchMessage(CommonObjects.ResponseMessage));
|
||||
Assert.ThrowsAsync<MethodHandlerDoesNotExistException>(() => jh.DispatchMessage(CommonObjects.ResponseMessage));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public async Task DispatchMessageResponseWithHandler()
|
||||
{
|
||||
// Setup: Create a new JSON RPC host that has a pending request handler
|
||||
@@ -453,10 +437,10 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
|
||||
// Then: The task completion source should have completed with the message that was given
|
||||
await mockPendingRequest.Task.WithTimeout(TimeSpan.FromSeconds(1));
|
||||
Assert.Equal(CommonObjects.ResponseMessage, mockPendingRequest.Task.Result);
|
||||
Assert.AreEqual(CommonObjects.ResponseMessage, mockPendingRequest.Task.Result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public async Task DispatchMessageEventWithoutHandler()
|
||||
{
|
||||
// Setup: Create a JSON RPC host without a request handler
|
||||
@@ -464,10 +448,10 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
|
||||
// If: I dispatch a request that doesn't have a handler
|
||||
// Then: I should get an exception
|
||||
await Assert.ThrowsAsync<MethodHandlerDoesNotExistException>(() => jh.DispatchMessage(CommonObjects.EventMessage));
|
||||
Assert.ThrowsAsync<MethodHandlerDoesNotExistException>(() => jh.DispatchMessage(CommonObjects.EventMessage));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public async Task DispatchMessageEventException()
|
||||
{
|
||||
// Setup: Create a JSON RPC host with a request handler that throws an unhandled exception every time
|
||||
@@ -479,11 +463,10 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
|
||||
// If: I dispatch a message whose handler throws
|
||||
// Then: I should get an exception
|
||||
await Assert.ThrowsAsync<Exception>(() => jh.DispatchMessage(CommonObjects.EventMessage));
|
||||
Assert.ThrowsAsync<Exception>(() => jh.DispatchMessage(CommonObjects.EventMessage));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(DispatchMessageWithHandlerData))]
|
||||
[TestCaseSource(nameof(DispatchMessageWithHandlerData))]
|
||||
public async Task DispatchMessageEventWithHandler(Task result)
|
||||
{
|
||||
// Setup: Create a JSON RPC host with an event handler setup
|
||||
@@ -519,7 +502,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
|
||||
#region ConsumeInput Loop Tests
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public async Task ConsumeInput()
|
||||
{
|
||||
// Setup:
|
||||
@@ -554,7 +537,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
mr.Verify(o => o.ReadMessage(), Times.AtLeastOnce);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public async Task ConsumeInputEndOfStream()
|
||||
{
|
||||
// Setup: Create a message reader that will throw an end of stream exception on read
|
||||
@@ -571,7 +554,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
mr.Verify(o => o.ReadMessage(), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public async Task ConsumeInputException()
|
||||
{
|
||||
// Setup:
|
||||
@@ -591,32 +574,29 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
mr.Verify(o => o.ReadMessage(), Times.Exactly(2));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public async Task ConsumeInputRequestMethodNotFound()
|
||||
{
|
||||
// Setup: Create a message reader that will return a request with method that doesn't exist
|
||||
var mr = new Mock<MessageReader>(Stream.Null, null);
|
||||
mr.SetupSequence(o => o.ReadMessage())
|
||||
.ReturnsAsync(CommonObjects.RequestMessage)
|
||||
.Returns(Task.FromException<Message>(new EndOfStreamException()));
|
||||
|
||||
// If: I start the input consumption loop
|
||||
.Returns(Task.FromException<Message>(new EndOfStreamException()));
|
||||
|
||||
// If: I start the input consumption loop
|
||||
var jh = new JsonRpcHost(GetChannelBase(mr.Object, null).Object);
|
||||
await jh.ConsumeInput().WithTimeout(TimeSpan.FromSeconds(1));
|
||||
|
||||
// Then:
|
||||
// ... Read message should have been called twice
|
||||
mr.Verify(o => o.ReadMessage(), Times.Exactly(2));
|
||||
|
||||
// ... There should be an outgoing message with the error
|
||||
await jh.ConsumeInput().WithTimeout(TimeSpan.FromSeconds(1));
|
||||
|
||||
// Then:
|
||||
// ... Read message should have been called twice
|
||||
mr.Verify(o => o.ReadMessage(), Times.Exactly(2));
|
||||
|
||||
// ...
|
||||
var outgoing = jh.outputQueue.ToArray();
|
||||
Assert.Single(outgoing);
|
||||
Assert.Equal(MessageType.ResponseError, outgoing[0].MessageType);
|
||||
Assert.Equal(CommonObjects.MessageId, outgoing[0].Id);
|
||||
Assert.Equal(-32601, outgoing[0].Contents.Value<int>("code"));
|
||||
Assert.That(outgoing.Select(m => (m.MessageType, m.Id, m.Contents.Value<int>("code"))), Is.EqualTo(new[] { (MessageType.ResponseError, CommonObjects.MessageId, -32601) }), "There should be an outgoing message with the error");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public async Task ConsumeInputRequestException()
|
||||
{
|
||||
// Setup:
|
||||
@@ -641,16 +621,15 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
// ... Read message should have been called twice
|
||||
mr.Verify(o => o.ReadMessage(), Times.Exactly(2));
|
||||
|
||||
// ... There should not be any outgoing messages
|
||||
var outgoing = jh.outputQueue.ToArray();
|
||||
Assert.Empty(outgoing);
|
||||
Assert.That(outgoing, Is.Empty, "outputQueue after ConsumeInput should not have any outgoing messages");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ConsumeOutput Loop Tests
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public async Task ConsumeOutput()
|
||||
{
|
||||
// Setup:
|
||||
@@ -671,7 +650,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
mw.Verify(o => o.WriteMessage(CommonObjects.ResponseMessage), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public async Task ConsumeOutputCancelled()
|
||||
{
|
||||
// NOTE: This test validates that the blocking collection breaks out when cancellation is requested
|
||||
@@ -693,7 +672,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
mw.Verify(o => o.WriteMessage(It.IsAny<Message>()), Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public async Task ConsumeOutputException()
|
||||
{
|
||||
// Setup: Create a mock message writer
|
||||
@@ -713,7 +692,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
|
||||
#region Start/Stop Tests
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public async Task StartStop()
|
||||
{
|
||||
// Setup: Create mocked message reader and writer
|
||||
@@ -737,7 +716,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
cb.Verify(o => o.Stop(), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public async Task StartMultiple()
|
||||
{
|
||||
// Setup: Create mocked message reader and writer
|
||||
@@ -759,7 +738,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
await Task.WhenAll(jh.consumeInputTask, jh.consumeOutputTask).WithTimeout(TimeSpan.FromSeconds(1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void StopMultiple()
|
||||
{
|
||||
// Setup: Create json rpc host and start it
|
||||
@@ -774,7 +753,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
Assert.Throws<InvalidOperationException>(() => jh.Stop());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void StopBeforeStarting()
|
||||
{
|
||||
// If: I stop the JSON RPC host without starting it first
|
||||
@@ -783,7 +762,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
Assert.Throws<InvalidOperationException>(() => jh.Stop());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public async Task WaitForExit()
|
||||
{
|
||||
// Setup: Create json rpc host and start it
|
||||
@@ -801,7 +780,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
await waitForExit.WithTimeout(TimeSpan.FromSeconds(1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void WaitForExitNotStarted()
|
||||
{
|
||||
// If: I wait for exit on the JSON RPC host without starting it
|
||||
|
||||
@@ -10,15 +10,16 @@ using System.Threading.Tasks;
|
||||
using Microsoft.SqlTools.Hosting.Protocol;
|
||||
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
||||
using Newtonsoft.Json;
|
||||
using Xunit;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class MessageReaderTests
|
||||
{
|
||||
#region Construction Tests
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void CreateReaderNullStream()
|
||||
{
|
||||
// If: I create a message reader with a null stream reader
|
||||
@@ -26,35 +27,32 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
Assert.Throws<ArgumentNullException>(() => new MessageReader(null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void CreateReaderStandardEncoding()
|
||||
{
|
||||
// If: I create a message reader without including a message encoding
|
||||
var mr = new MessageReader(Stream.Null);
|
||||
|
||||
// Then: The reader's encoding should be UTF8
|
||||
Assert.Equal(Encoding.UTF8, mr.MessageEncoding);
|
||||
Assert.AreEqual(Encoding.UTF8, mr.MessageEncoding);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void CreateReaderNonStandardEncoding()
|
||||
{
|
||||
// If: I create a message reader with a specific message encoding
|
||||
var mr = new MessageReader(Stream.Null, Encoding.ASCII);
|
||||
|
||||
// Then: The reader's encoding should be ASCII
|
||||
Assert.Equal(Encoding.ASCII, mr.MessageEncoding);
|
||||
Assert.AreEqual(Encoding.ASCII, mr.MessageEncoding);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ReadMessage Tests
|
||||
|
||||
[Theory]
|
||||
[InlineData(512)] // Buffer size can fit everything in one read
|
||||
[InlineData(10)] // Buffer size must use multiple reads to read the headers
|
||||
[InlineData(25)] // Buffer size must use multiple reads to read the contents
|
||||
public async Task ReadMessageSingleRead(int bufferSize)
|
||||
[Test]
|
||||
public async Task ReadMessageSingleRead([Values(512,10,25)]int bufferSize)
|
||||
{
|
||||
// Setup: Reader with a stream that has an entire message in it
|
||||
byte[] testBytes = Encoding.UTF8.GetBytes("Content-Length: 50\r\n\r\n{\"jsonrpc\": \"2.0\", \"method\":\"test\", \"params\":null}");
|
||||
@@ -70,17 +68,19 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
Assert.NotNull(output);
|
||||
|
||||
// ... The reader should be back in header mode
|
||||
Assert.Equal(MessageReader.ReadState.Headers, mr.CurrentState);
|
||||
Assert.AreEqual(MessageReader.ReadState.Headers, mr.CurrentState);
|
||||
|
||||
// ... The buffer should have been trimmed
|
||||
Assert.Equal(MessageReader.DefaultBufferSize, mr.MessageBuffer.Length);
|
||||
Assert.AreEqual(MessageReader.DefaultBufferSize, mr.MessageBuffer.Length);
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("Content-Type: application/json\r\n\r\n")] // Missing content-length header
|
||||
[InlineData("Content-Length: abc\r\n\r\n")] // Content-length is not a number
|
||||
public async Task ReadMessageInvalidHeaders(string testString)
|
||||
[Test]
|
||||
public async Task ReadMessageInvalidHeaders(
|
||||
[Values(
|
||||
"Content-Type: application/json\r\n\r\n",// Missing content-length header
|
||||
"Content-Length: abc\r\n\r\n"// Content-length is not a number
|
||||
)]string testString)
|
||||
{
|
||||
// Setup: Reader with a stream that has an invalid header in it
|
||||
byte[] testBytes = Encoding.UTF8.GetBytes(testString);
|
||||
@@ -90,14 +90,14 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
|
||||
// If: I read a message with invalid headers
|
||||
// Then: ... I should get an exception
|
||||
await Assert.ThrowsAnyAsync<MessageParseException>(() => mr.ReadMessage());
|
||||
Assert.ThrowsAsync<MessageParseException>(() => mr.ReadMessage());
|
||||
|
||||
// ... The buffer should have been trashed (reset to it's original tiny size)
|
||||
Assert.Equal(MessageReader.DefaultBufferSize, mr.MessageBuffer.Length);
|
||||
Assert.AreEqual(MessageReader.DefaultBufferSize, mr.MessageBuffer.Length);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public async Task ReadMessageInvalidJson()
|
||||
{
|
||||
// Setup: Reader with a stream that has an invalid json message in it
|
||||
@@ -110,14 +110,14 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
// If: I read a message with an invalid JSON in it
|
||||
// Then:
|
||||
// ... I should get an exception
|
||||
await Assert.ThrowsAnyAsync<JsonException>(() => mr.ReadMessage());
|
||||
Assert.ThrowsAsync<JsonReaderException>(() => mr.ReadMessage());
|
||||
|
||||
// ... The buffer should have been trashed (reset to it's original tiny size)
|
||||
Assert.Equal(MessageReader.DefaultBufferSize, mr.MessageBuffer.Length);
|
||||
Assert.AreEqual(MessageReader.DefaultBufferSize, mr.MessageBuffer.Length);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public async Task ReadMultipleMessages()
|
||||
{
|
||||
// Setup: Reader with a stream that has multiple messages in it
|
||||
@@ -141,7 +141,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public async Task ReadMultipleMessagesBeforeWriting()
|
||||
{
|
||||
// Setup: Reader with a stream that will have multiple messages in it
|
||||
@@ -169,7 +169,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public async Task ReadRecoverFromInvalidHeaderMessage()
|
||||
{
|
||||
// Setup: Reader with a stream that has incorrect message formatting
|
||||
@@ -182,7 +182,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
|
||||
// If: I read a message with invalid headers
|
||||
// Then: I should get an exception
|
||||
await Assert.ThrowsAnyAsync<MessageParseException>(() => mr.ReadMessage());
|
||||
Assert.ThrowsAsync<MessageParseException>(() => mr.ReadMessage());
|
||||
|
||||
// If: I read another, valid, message
|
||||
var msg = await mr.ReadMessage();
|
||||
@@ -192,7 +192,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public async Task ReadRecoverFromInvalidContentMessage()
|
||||
{
|
||||
// Setup: Reader with a stream that has incorrect message formatting
|
||||
@@ -205,7 +205,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
|
||||
// If: I read a message with invalid content
|
||||
// Then: I should get an exception
|
||||
await Assert.ThrowsAnyAsync<JsonException>(() => mr.ReadMessage());
|
||||
Assert.ThrowsAsync<JsonReaderException>(() => mr.ReadMessage());
|
||||
|
||||
// If: I read another, valid, message
|
||||
var msg = await mr.ReadMessage();
|
||||
|
||||
@@ -8,15 +8,16 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.SqlTools.Hosting.Protocol;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Xunit;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class MessageTests
|
||||
{
|
||||
#region Construction/Serialization Tests
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void CreateRequest()
|
||||
{
|
||||
// If: I create a request
|
||||
@@ -36,7 +37,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
AssertPropertiesSet(expectedResults, message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void CreateError()
|
||||
{
|
||||
// If: I create an error
|
||||
@@ -54,7 +55,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
AssertPropertiesSet(expectedResults, message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void CreateResponse()
|
||||
{
|
||||
// If: I create a response
|
||||
@@ -72,7 +73,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
AssertPropertiesSet(expectedResults, message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void CreateEvent()
|
||||
{
|
||||
// If: I create an event
|
||||
@@ -101,16 +102,16 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
|
||||
// JSON RPC Version
|
||||
List<string> expectedProperties = new List<string> {"jsonrpc"};
|
||||
Assert.Equal("2.0", jObject["jsonrpc"]);
|
||||
Assert.AreEqual("2.0", jObject["jsonrpc"].Value<string>());
|
||||
|
||||
// Message Type
|
||||
Assert.Equal(results.MessageType, message.MessageType);
|
||||
Assert.AreEqual(results.MessageType, message.MessageType);
|
||||
|
||||
// ID
|
||||
if (results.IdSet)
|
||||
{
|
||||
Assert.Equal(CommonObjects.MessageId, message.Id);
|
||||
Assert.Equal(CommonObjects.MessageId, jObject["id"]);
|
||||
Assert.AreEqual(CommonObjects.MessageId, message.Id);
|
||||
Assert.AreEqual(CommonObjects.MessageId, jObject["id"].Value<string>());
|
||||
expectedProperties.Add("id");
|
||||
}
|
||||
else
|
||||
@@ -121,8 +122,8 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
// Method
|
||||
if (results.MethodSetAs != null)
|
||||
{
|
||||
Assert.Equal(results.MethodSetAs, message.Method);
|
||||
Assert.Equal(results.MethodSetAs, jObject["method"]);
|
||||
Assert.AreEqual(results.MethodSetAs, message.Method);
|
||||
Assert.AreEqual(results.MethodSetAs, jObject["method"].Value<string>());
|
||||
expectedProperties.Add("method");
|
||||
}
|
||||
else
|
||||
@@ -133,22 +134,22 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
// Contents
|
||||
if (results.ContentsSetAs != null)
|
||||
{
|
||||
Assert.Equal(CommonObjects.TestMessageContents.SerializedContents, message.Contents);
|
||||
Assert.Equal(CommonObjects.TestMessageContents.SerializedContents, jObject[results.ContentsSetAs]);
|
||||
Assert.AreEqual(CommonObjects.TestMessageContents.SerializedContents, message.Contents);
|
||||
Assert.AreEqual(CommonObjects.TestMessageContents.SerializedContents, jObject[results.ContentsSetAs]);
|
||||
expectedProperties.Add(results.ContentsSetAs);
|
||||
}
|
||||
|
||||
// Error
|
||||
if (results.ErrorSet)
|
||||
{
|
||||
Assert.Equal(CommonObjects.TestErrorContents.SerializedContents, message.Contents);
|
||||
Assert.Equal(CommonObjects.TestErrorContents.SerializedContents, jObject["error"]);
|
||||
Assert.AreEqual(CommonObjects.TestErrorContents.SerializedContents, message.Contents);
|
||||
Assert.AreEqual(CommonObjects.TestErrorContents.SerializedContents, jObject["error"]);
|
||||
expectedProperties.Add("error");
|
||||
}
|
||||
|
||||
// Look for any extra properties set in the JObject
|
||||
IEnumerable<string> setProperties = jObject.Properties().Select(p => p.Name);
|
||||
Assert.Empty(setProperties.Except(expectedProperties));
|
||||
Assert.That(setProperties.Except(expectedProperties), Is.Empty, "extra properties in jObject");
|
||||
}
|
||||
|
||||
private class MessagePropertyResults
|
||||
@@ -164,7 +165,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
|
||||
#region Deserialization Tests
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void DeserializeMissingJsonRpc()
|
||||
{
|
||||
// If: I deserialize a json string that doesn't have a JSON RPC version
|
||||
@@ -172,7 +173,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
Assert.Throws<MessageParseException>(() => Message.Deserialize("{\"id\": 123}"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void DeserializeEvent()
|
||||
{
|
||||
// If: I deserialize an event json string
|
||||
@@ -180,13 +181,13 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
|
||||
// Then: I should get an event message back
|
||||
Assert.NotNull(m);
|
||||
Assert.Equal(MessageType.Event, m.MessageType);
|
||||
Assert.Equal("event", m.Method);
|
||||
Assert.AreEqual(MessageType.Event, m.MessageType);
|
||||
Assert.AreEqual("event", m.Method);
|
||||
Assert.NotNull(m.Contents);
|
||||
Assert.Null(m.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void DeserializeEventMissingMethod()
|
||||
{
|
||||
// If: I deserialize an event json string that is missing a method
|
||||
@@ -194,7 +195,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
Assert.Throws<MessageParseException>(() => Message.Deserialize("{\"jsonrpc\": \"2.0\", \"params\": {}}"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void DeserializeResponse()
|
||||
{
|
||||
// If: I deserialize a response json string
|
||||
@@ -202,13 +203,13 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
|
||||
// Then: I should get a response message back
|
||||
Assert.NotNull(m);
|
||||
Assert.Equal(MessageType.Response, m.MessageType);
|
||||
Assert.Equal("123", m.Id);
|
||||
Assert.AreEqual(MessageType.Response, m.MessageType);
|
||||
Assert.AreEqual("123", m.Id);
|
||||
Assert.NotNull(m.Contents);
|
||||
Assert.Null(m.Method);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void DeserializeErrorResponse()
|
||||
{
|
||||
// If: I deserialize an error response
|
||||
@@ -216,13 +217,13 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
|
||||
// Then: I should get an error response message back
|
||||
Assert.NotNull(m);
|
||||
Assert.Equal(MessageType.ResponseError, m.MessageType);
|
||||
Assert.Equal("123", m.Id);
|
||||
Assert.AreEqual(MessageType.ResponseError, m.MessageType);
|
||||
Assert.AreEqual("123", m.Id);
|
||||
Assert.NotNull(m.Contents);
|
||||
Assert.Null(m.Method);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void DeserializeRequest()
|
||||
{
|
||||
// If: I deserialize a request
|
||||
@@ -230,13 +231,13 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
|
||||
// Then: I should get a request message back
|
||||
Assert.NotNull(m);
|
||||
Assert.Equal(MessageType.Request, m.MessageType);
|
||||
Assert.Equal("123", m.Id);
|
||||
Assert.Equal("request", m.Method);
|
||||
Assert.AreEqual(MessageType.Request, m.MessageType);
|
||||
Assert.AreEqual("123", m.Id);
|
||||
Assert.AreEqual("request", m.Method);
|
||||
Assert.NotNull(m.Contents);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void DeserializeRequestMissingMethod()
|
||||
{
|
||||
// If: I deserialize a request that doesn't have a method parameter
|
||||
@@ -244,7 +245,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
Assert.Throws<MessageParseException>(() => Message.Deserialize("{\"jsonrpc\": \"2.0\", \"params\": {}, \"id\": \"123\"}"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void GetTypedContentsNull()
|
||||
{
|
||||
// If: I have a message that has a null contents, and I get the typed contents of it
|
||||
@@ -255,7 +256,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
Assert.Null(c);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void GetTypedContentsSimpleValue()
|
||||
{
|
||||
// If: I have a message that has simple contents, and I get the typed contents of it
|
||||
@@ -263,10 +264,10 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
var c = m.GetTypedContents<int>();
|
||||
|
||||
// Then: I should get an int back
|
||||
Assert.Equal(123, c);
|
||||
Assert.AreEqual(123, c);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void GetTypedContentsClassValue()
|
||||
{
|
||||
// If: I have a message that has complex contents, and I get the typed contents of it
|
||||
@@ -274,16 +275,16 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
var c = m.GetTypedContents<CommonObjects.TestMessageContents>();
|
||||
|
||||
// Then: I should get the default instance back
|
||||
Assert.Equal(CommonObjects.TestMessageContents.DefaultInstance, c);
|
||||
Assert.AreEqual(CommonObjects.TestMessageContents.DefaultInstance, c);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void GetTypedContentsInvalid()
|
||||
{
|
||||
// If: I have a message that has contents and I get incorrectly typed contents from it
|
||||
// Then: I should get an exception back
|
||||
var m = Message.CreateResponse(CommonObjects.MessageId, CommonObjects.TestMessageContents.DefaultInstance);
|
||||
Assert.ThrowsAny<Exception>(() => m.GetTypedContents<int>());
|
||||
Assert.Throws<ArgumentException>(() => m.GetTypedContents<int>());
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -10,15 +10,16 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlTools.Hosting.Protocol;
|
||||
using Newtonsoft.Json;
|
||||
using Xunit;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class MessageWriterTests
|
||||
{
|
||||
#region Construction Tests
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void ConstructMissingOutputStream()
|
||||
{
|
||||
// If: I attempt to create a message writer without an output stream
|
||||
@@ -30,17 +31,16 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
|
||||
#region WriteMessageTests
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public async Task WriteMessageNullMessage()
|
||||
{
|
||||
// If: I write a null message
|
||||
// Then: I should get an exception
|
||||
var mw = new MessageWriter(Stream.Null);
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(() => mw.WriteMessage(null));
|
||||
var mw = new MessageWriter(Stream.Null);
|
||||
Assert.ThrowsAsync<ArgumentNullException>(() => mw.WriteMessage(null));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(WriteMessageData))]
|
||||
[TestCaseSource(nameof(WriteMessageData))]
|
||||
public async Task WriteMessage(object contents, Dictionary<string, object> expectedDict)
|
||||
{
|
||||
// NOTE: This technically tests the ability of the Message class to properly serialize
|
||||
@@ -53,11 +53,11 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
{
|
||||
// If: I write a message
|
||||
var mw = new MessageWriter(outputStream);
|
||||
await mw.WriteMessage(Message.CreateResponse(CommonObjects.MessageId, contents));
|
||||
|
||||
// Then:
|
||||
// ... The returned bytes on the stream should compose a valid message
|
||||
Assert.NotEqual(0, outputStream.Position);
|
||||
await mw.WriteMessage(Message.CreateResponse(CommonObjects.MessageId, contents));
|
||||
|
||||
// Then:
|
||||
// ... The returned bytes on the stream should compose a valid message
|
||||
Assert.That(outputStream.Position, Is.Not.EqualTo(0), "outputStream.Position after WriteMessage");
|
||||
var messageDict = ValidateMessageHeaders(output, (int) outputStream.Position);
|
||||
|
||||
// ... ID, Params, Method should be present
|
||||
@@ -97,12 +97,12 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
expectedDict.Add("jsonrpc", "2.0");
|
||||
|
||||
// Make sure the number of elements in both dictionaries are the same
|
||||
Assert.Equal(expectedDict.Count, messageDict.Count);
|
||||
Assert.AreEqual(expectedDict.Count, messageDict.Count);
|
||||
|
||||
// Make sure the elements match
|
||||
foreach (var kvp in expectedDict)
|
||||
{
|
||||
Assert.Equal(expectedDict[kvp.Key], messageDict[kvp.Key]);
|
||||
Assert.AreEqual(expectedDict[kvp.Key], messageDict[kvp.Key]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,11 +113,11 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
|
||||
// There should be two sections to the message
|
||||
string[] outputParts = outputString.Split("\r\n\r\n");
|
||||
Assert.Equal(2, outputParts.Length);
|
||||
Assert.AreEqual(2, outputParts.Length);
|
||||
|
||||
// The first section is the headers
|
||||
string[] headers = outputParts[0].Split("\r\n");
|
||||
Assert.Equal(2, outputParts.Length);
|
||||
Assert.AreEqual(2, outputParts.Length);
|
||||
|
||||
// There should be a content-type and a content-length
|
||||
int? contentLength = null;
|
||||
@@ -126,7 +126,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
{
|
||||
// Headers should look like "Header-Key: HeaderValue"
|
||||
string[] headerParts = header.Split(':');
|
||||
Assert.Equal(2, headerParts.Length);
|
||||
Assert.AreEqual(2, headerParts.Length);
|
||||
|
||||
string headerKey = headerParts[0];
|
||||
string headerValue = headerParts[1].Trim();
|
||||
@@ -147,7 +147,7 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
|
||||
// Make sure the headers are correct
|
||||
Assert.True(contentTypeCorrect);
|
||||
Assert.Equal(outputParts[1].Length, contentLength);
|
||||
Assert.AreEqual(outputParts[1].Length, contentLength);
|
||||
|
||||
// Deserialize the body into a dictionary
|
||||
return JsonConvert.DeserializeObject<Dictionary<string, object>>(outputParts[1]);
|
||||
|
||||
@@ -8,15 +8,16 @@ using System.Collections.Concurrent;
|
||||
using System.Linq;
|
||||
using Microsoft.SqlTools.Hosting.Contracts.Internal;
|
||||
using Microsoft.SqlTools.Hosting.Protocol;
|
||||
using Xunit;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class RequestContextTests
|
||||
{
|
||||
#region Send Tests
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void SendResult()
|
||||
{
|
||||
// Setup: Create a blocking collection to collect the output
|
||||
@@ -26,12 +27,10 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
var rc = new RequestContext<CommonObjects.TestMessageContents>(CommonObjects.RequestMessage, bc);
|
||||
rc.SendResult(CommonObjects.TestMessageContents.DefaultInstance);
|
||||
|
||||
// Then: The message writer should have sent a response
|
||||
Assert.Single(bc);
|
||||
Assert.Equal(MessageType.Response, bc.First().MessageType);
|
||||
Assert.That(bc.Select(m => m.MessageType), Is.EqualTo(new[] { MessageType.Response }), "The message writer should have sent a response");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void SendEvent()
|
||||
{
|
||||
// Setup: Create a blocking collection to collect the output
|
||||
@@ -39,14 +38,12 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
|
||||
// If: I write an event with the request context
|
||||
var rc = new RequestContext<CommonObjects.TestMessageContents>(CommonObjects.RequestMessage, bc);
|
||||
rc.SendEvent(CommonObjects.EventType, CommonObjects.TestMessageContents.DefaultInstance);
|
||||
|
||||
// Then: The message writer should have sent an event
|
||||
Assert.Single(bc);
|
||||
Assert.Equal(MessageType.Event, bc.First().MessageType);
|
||||
rc.SendEvent(CommonObjects.EventType, CommonObjects.TestMessageContents.DefaultInstance);
|
||||
|
||||
Assert.That(bc.Select(m => m.MessageType), Is.EqualTo(new[] { MessageType.Event }), "The message writer should have sent an event");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void SendError()
|
||||
{
|
||||
// Setup: Create a blocking collection to collect the output
|
||||
@@ -56,20 +53,17 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
|
||||
// If: I write an error with the request context
|
||||
var rc = new RequestContext<CommonObjects.TestMessageContents>(CommonObjects.RequestMessage, bc);
|
||||
rc.SendError(errorMessage, errorCode);
|
||||
|
||||
// Then:
|
||||
// ... The message writer should have sent an error
|
||||
Assert.Single(bc);
|
||||
Assert.Equal(MessageType.ResponseError, bc.First().MessageType);
|
||||
|
||||
// ... The error object it built should have the reuired fields set
|
||||
rc.SendError(errorMessage, errorCode);
|
||||
|
||||
Assert.That(bc.Select(m => m.MessageType), Is.EqualTo(new[] { MessageType.ResponseError }), "The message writer should have sent an error");
|
||||
|
||||
// ... The error object it built should have the reuired fields set
|
||||
var contents = bc.ToArray()[0].GetTypedContents<Error>();
|
||||
Assert.Equal(errorCode, contents.Code);
|
||||
Assert.Equal(errorMessage, contents.Message);
|
||||
Assert.AreEqual(errorCode, contents.Code);
|
||||
Assert.AreEqual(errorMessage, contents.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Test]
|
||||
public void SendException()
|
||||
{
|
||||
// Setup: Create a blocking collection to collect the output
|
||||
@@ -79,18 +73,14 @@ namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
|
||||
const string errorMessage = "error";
|
||||
var e = new Exception(errorMessage);
|
||||
var rc = new RequestContext<CommonObjects.TestMessageContents>(CommonObjects.RequestMessage, bc);
|
||||
rc.SendError(e);
|
||||
|
||||
// Then:
|
||||
// ... The message writer should have sent an error
|
||||
Assert.Single(bc);
|
||||
var firstMessage = bc.First();
|
||||
Assert.Equal(MessageType.ResponseError, firstMessage.MessageType);
|
||||
|
||||
// ... The error object it built should have the reuired fields set
|
||||
var contents = firstMessage.GetTypedContents<Error>();
|
||||
Assert.Equal(e.HResult, contents.Code);
|
||||
Assert.Equal(errorMessage, contents.Message);
|
||||
rc.SendError(e);
|
||||
|
||||
Assert.That(bc.Select(m => m.MessageType), Is.EqualTo(new[] { MessageType.ResponseError }), "The message writer should have sent an error");
|
||||
|
||||
// ... The error object it built should have the reuired fields set
|
||||
var contents = bc.First().GetTypedContents<Error>();
|
||||
Assert.AreEqual(e.HResult, contents.Code);
|
||||
Assert.AreEqual(errorMessage, contents.Message);
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user