mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 01:25:40 -05:00
Added test driver program for service host (#113)
* Added test driver program for service host * Fix typo
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
//
|
||||
// The following is based upon code from PowerShell Editor Services
|
||||
// License: https://github.com/PowerShell/PowerShellEditorServices/blob/develop/LICENSE
|
||||
//
|
||||
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol;
|
||||
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol.Channel;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.TestDriver.Driver
|
||||
{
|
||||
/// <summary>
|
||||
/// Test driver for the service host
|
||||
/// </summary>
|
||||
public class ServiceTestDriver : TestDriverBase
|
||||
{
|
||||
public ServiceTestDriver(string serviceHostExecutable)
|
||||
{
|
||||
var clientChannel = new StdioClientChannel(serviceHostExecutable);
|
||||
this.protocolClient = new ProtocolEndpoint(clientChannel, MessageProtocolType.LanguageServer);
|
||||
}
|
||||
|
||||
public async Task Start()
|
||||
{
|
||||
await this.protocolClient.Start();
|
||||
await Task.Delay(1000); // Wait for the service host to start
|
||||
}
|
||||
|
||||
public async Task Stop()
|
||||
{
|
||||
await this.protocolClient.Stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
//
|
||||
// The following is based upon code from PowerShell Editor Services
|
||||
// License: https://github.com/PowerShell/PowerShellEditorServices/blob/develop/LICENSE
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol;
|
||||
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Utility;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.TestDriver.Driver
|
||||
{
|
||||
/// <summary>
|
||||
/// Wraps the ProtocolEndpoint class with queues to handle events/requests
|
||||
/// </summary>
|
||||
public class TestDriverBase
|
||||
{
|
||||
protected ProtocolEndpoint protocolClient;
|
||||
|
||||
private ConcurrentDictionary<string, AsyncQueue<object>> eventQueuePerType =
|
||||
new ConcurrentDictionary<string, AsyncQueue<object>>();
|
||||
|
||||
private ConcurrentDictionary<string, AsyncQueue<object>> requestQueuePerType =
|
||||
new ConcurrentDictionary<string, AsyncQueue<object>>();
|
||||
|
||||
public Task<TResult> SendRequest<TParams, TResult>(
|
||||
RequestType<TParams, TResult> requestType,
|
||||
TParams requestParams)
|
||||
{
|
||||
return
|
||||
this.protocolClient.SendRequest(
|
||||
requestType,
|
||||
requestParams);
|
||||
}
|
||||
|
||||
public Task SendEvent<TParams>(EventType<TParams> eventType, TParams eventParams)
|
||||
{
|
||||
return
|
||||
this.protocolClient.SendEvent(
|
||||
eventType,
|
||||
eventParams);
|
||||
}
|
||||
|
||||
public void QueueEventsForType<TParams>(EventType<TParams> eventType)
|
||||
{
|
||||
var eventQueue =
|
||||
this.eventQueuePerType.AddOrUpdate(
|
||||
eventType.MethodName,
|
||||
new AsyncQueue<object>(),
|
||||
(key, queue) => queue);
|
||||
|
||||
this.protocolClient.SetEventHandler(
|
||||
eventType,
|
||||
(p, ctx) =>
|
||||
{
|
||||
return eventQueue.EnqueueAsync(p);
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<TParams> WaitForEvent<TParams>(
|
||||
EventType<TParams> eventType,
|
||||
int timeoutMilliseconds = 5000)
|
||||
{
|
||||
Task<TParams> eventTask = null;
|
||||
|
||||
// Use the event queue if one has been registered
|
||||
AsyncQueue<object> eventQueue = null;
|
||||
if (this.eventQueuePerType.TryGetValue(eventType.MethodName, out eventQueue))
|
||||
{
|
||||
eventTask =
|
||||
eventQueue
|
||||
.DequeueAsync()
|
||||
.ContinueWith<TParams>(
|
||||
task => (TParams)task.Result);
|
||||
}
|
||||
else
|
||||
{
|
||||
TaskCompletionSource<TParams> eventTaskSource = new TaskCompletionSource<TParams>();
|
||||
|
||||
this.protocolClient.SetEventHandler(
|
||||
eventType,
|
||||
(p, ctx) =>
|
||||
{
|
||||
if (!eventTaskSource.Task.IsCompleted)
|
||||
{
|
||||
eventTaskSource.SetResult(p);
|
||||
}
|
||||
|
||||
return Task.FromResult(true);
|
||||
},
|
||||
true); // Override any existing handler
|
||||
|
||||
eventTask = eventTaskSource.Task;
|
||||
}
|
||||
|
||||
await
|
||||
Task.WhenAny(
|
||||
eventTask,
|
||||
Task.Delay(timeoutMilliseconds));
|
||||
|
||||
if (!eventTask.IsCompleted)
|
||||
{
|
||||
throw new TimeoutException(
|
||||
string.Format(
|
||||
"Timed out waiting for '{0}' event!",
|
||||
eventType.MethodName));
|
||||
}
|
||||
|
||||
return await eventTask;
|
||||
}
|
||||
|
||||
public async Task<Tuple<TParams, RequestContext<TResponse>>> WaitForRequest<TParams, TResponse>(
|
||||
RequestType<TParams, TResponse> requestType,
|
||||
int timeoutMilliseconds = 5000)
|
||||
{
|
||||
Task<Tuple<TParams, RequestContext<TResponse>>> requestTask = null;
|
||||
|
||||
// Use the request queue if one has been registered
|
||||
AsyncQueue<object> requestQueue = null;
|
||||
if (this.requestQueuePerType.TryGetValue(requestType.MethodName, out requestQueue))
|
||||
{
|
||||
requestTask =
|
||||
requestQueue
|
||||
.DequeueAsync()
|
||||
.ContinueWith(
|
||||
task => (Tuple<TParams, RequestContext<TResponse>>)task.Result);
|
||||
}
|
||||
else
|
||||
{
|
||||
var requestTaskSource =
|
||||
new TaskCompletionSource<Tuple<TParams, RequestContext<TResponse>>>();
|
||||
|
||||
this.protocolClient.SetRequestHandler(
|
||||
requestType,
|
||||
(p, ctx) =>
|
||||
{
|
||||
if (!requestTaskSource.Task.IsCompleted)
|
||||
{
|
||||
requestTaskSource.SetResult(
|
||||
new Tuple<TParams, RequestContext<TResponse>>(p, ctx));
|
||||
}
|
||||
|
||||
return Task.FromResult(true);
|
||||
});
|
||||
|
||||
requestTask = requestTaskSource.Task;
|
||||
}
|
||||
|
||||
await
|
||||
Task.WhenAny(
|
||||
requestTask,
|
||||
Task.Delay(timeoutMilliseconds));
|
||||
|
||||
if (!requestTask.IsCompleted)
|
||||
{
|
||||
throw new TimeoutException(
|
||||
string.Format(
|
||||
"Timed out waiting for '{0}' request!",
|
||||
requestType.MethodName));
|
||||
}
|
||||
|
||||
return await requestTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user