Files
sqltoolsservice/test/Microsoft.SqlTools.ServiceLayer.Test/Utility/TestUtils.cs
Kevin Cunnane 7477642854 TSQL Formatter Service (#229)
- TSqlFormatterService with support for formatting document and text range inside document
- Settings support for all formatting options.
- Extensibility support so that the service can be initialized using MEF extensibility, and can find all necessary TSqlFormatters using the same process

Fix Initialize request error on startup
- Messages were being read from the input channel before all request handlers were registered
- In particular, the Initialize request which is key for any server to talk to the client was getting lost because the message reader thread begins consuming, and we take an extra few hundred milliseconds due to MEF startup before we register the handler
- The solution is to initialize the message handler so request handlers can register, but not actually start processing incoming messages until all handers are ready. This is a safer way to go and should improve reliability overall

Improvements from internal prototype:
- Normalizing baselines to handle the line ending differences on Mac & Linux vs. Windows
- Significantly shortened most lines by implementing base class methods to wrap common objects from Visitor.Context and removing unnecessary "this." syntax
- Refactored the SqlCommonTableExpressionFormatter and related classes to reduce code count significantly. This provides a pattern to follow when refactoring other classes for similar clarity. It's likely a lot of common logic could be found and reused across these.
- Reduced overall code size by adding utility methods
2017-02-14 23:40:17 -08:00

87 lines
3.0 KiB
C#

using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol;
using Moq;
using Xunit;
namespace Microsoft.SqlTools.ServiceLayer.Test.Utility
{
public static class TestUtils
{
public static void RunIfLinux(Action test)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
test();
}
}
public static void RunIfLinuxOrOSX(Action test)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
test();
}
}
public static void RunIfWindows(Action test)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
test();
}
}
/// <summary>
/// Wait for a condition to be true for a limited amount of time.
/// </summary>
/// <param name="condition">Function that returns a boolean on a condition</param>
/// <param name="intervalMilliseconds">Number of milliseconds to wait between test intervals.</param>
/// <param name="intervalCount">Number of test intervals to perform before giving up.</param>
/// <returns>True if the condition was met before the test interval limit.</returns>
public static bool WaitFor(Func<bool> condition, int intervalMilliseconds = 10, int intervalCount = 200)
{
int count = 0;
while (count++ < intervalCount && !condition.Invoke())
{
Thread.Sleep(intervalMilliseconds);
}
return (count < intervalCount);
}
public static async Task RunAndVerify<T>(Func<RequestContext<T>, Task> test, Action<T> verify)
{
T result = default(T);
var contextMock = RequestContextMocks.Create<T>(r => result = r).AddErrorHandling(null);
await test(contextMock.Object);
VerifyResult(contextMock, verify, result);
}
public static void VerifyErrorSent<T>(Mock<RequestContext<T>> contextMock)
{
contextMock.Verify(c => c.SendResult(It.IsAny<T>()), Times.Never);
contextMock.Verify(c => c.SendError(It.IsAny<string>()), Times.Once);
}
public static void VerifyResult<T, U>(Mock<RequestContext<T>> contextMock, U expected, U actual)
{
contextMock.Verify(c => c.SendResult(It.IsAny<T>()), Times.Once);
Assert.Equal(expected, actual);
contextMock.Verify(c => c.SendError(It.IsAny<string>()), Times.Never);
}
public static void VerifyResult<T>(Mock<RequestContext<T>> contextMock, Action<T> verify, T actual)
{
contextMock.Verify(c => c.SendResult(It.IsAny<T>()), Times.Once);
contextMock.Verify(c => c.SendError(It.IsAny<string>()), Times.Never);
verify(actual);
}
}
}