Skip to content

Updated router and added jsonrpc integration tests #274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/Shared/LspRequestRouter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,21 @@ private IRequestDescriptor<ILspHandlerDescriptor> FindDescriptor(string method,
return new RequestDescriptor<ILspHandlerDescriptor>();
}

if (@params == null || descriptor.Params == null) return new RequestDescriptor<ILspHandlerDescriptor>(new [] { descriptor });
if (@params == null || descriptor.Params == null) return new RequestDescriptor<ILspHandlerDescriptor>(new[] { descriptor });

object paramsValue = null;
if (descriptor.IsDelegatingHandler)
{
var o = @params?.ToObject(descriptor.Params.GetGenericArguments()[0], _serializer.JsonSerializer);
paramsValue = Activator.CreateInstance(descriptor.Params, new object[] { o });
}
else
{
paramsValue = @params?.ToObject(descriptor.Params, _serializer.JsonSerializer);
}

var lspHandlerDescriptors = _collection.Where(handler => handler.Method == method).ToList();

var paramsValue = @params.ToObject(descriptor.Params, _serializer.JsonSerializer);
var matchDescriptor = _handlerMatchers.SelectMany(strat => strat.FindHandler(paramsValue, lspHandlerDescriptors)).ToArray();
if (matchDescriptor.Length > 0) return new RequestDescriptor<ILspHandlerDescriptor>(matchDescriptor);
// execute command is a special case
Expand Down
247 changes: 247 additions & 0 deletions test/Dap.Tests/JsonRpcIntegrationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
// using System;
// using System.Linq;
// using System.Reactive.Subjects;
// using System.Threading;
// using System.Threading.Tasks;
// using FluentAssertions;
// using MediatR;
// using NSubstitute;
// using OmniSharp.Extensions.DebugAdapter.Testing;
// using OmniSharp.Extensions.JsonRpc;
// using OmniSharp.Extensions.JsonRpc.Server;
// using OmniSharp.Extensions.JsonRpc.Testing;
// using Xunit;
// using Xunit.Abstractions;

// namespace Dap.Tests
// {
// public class JsonRpcIntegrationTests : DebugAdapterProtocolTestBase
// {
// public JsonRpcIntegrationTests(ITestOutputHelper outputHelper) : base(new JsonRpcTestOptions().ConfigureForXUnit(outputHelper))
// {
// }

// class Request : IRequest<Data>
// {

// }

// class Data
// {
// public string Value { get; set; }
// }

// [Fact]
// public async Task Should_Send_and_receive_requests()
// {
// var (client, server) = await Initialize(
// client => { client.OnRequest("myrequest", async () => new Data() {Value = "myresponse"}); },
// server => { server.OnRequest("myrequest", async () => new Data() {Value = string.Join("", "myresponse".Reverse())}); }
// );

// var serverResponse = await client.SendRequest("myrequest").Returning<Data>(CancellationToken);
// serverResponse.Value.Should().Be("esnopserym");

// var clientResponse = await server.SendRequest("myrequest").Returning<Data>(CancellationToken);
// clientResponse.Value.Should().Be("myresponse");
// }

// [Fact(Skip = "DAP does not define invalid parameters semantics")]
// public async Task Should_throw_when_sending_requests()
// {
// var (client, server) = await Initialize(
// client => { client.OnRequest("myrequest", async (Request request) => new Data() {Value = "myresponse"}); },
// server => { server.OnRequest("myrequest", async (Request request) => new Data() {Value = string.Join("", "myresponse".Reverse())}); }
// );

// Func<Task> clientRequest = () => client.SendRequest("myrequest", (Request)null).Returning<Data>(CancellationToken);
// clientRequest.Should().Throw<InvalidParametersException>();

// Func<Task> serverRequest = () => server.SendRequest("myrequest", (Request)null).Returning<Data>(CancellationToken);
// serverRequest.Should().Throw<InvalidParametersException>();
// }

// [Fact]
// public async Task Should_throw_when_receiving_requests()
// {
// var (client, server) = await Initialize(
// client => { client.OnRequest("myrequest", async (Request request) => (Data)null); },
// server => { server.OnRequest("myrequest", async (Request request) => (Data)null); }
// );

// Func<Task> clientRequest = () => client.SendRequest("myrequest", new Request()).Returning<Data>(CancellationToken);
// clientRequest.Should().Throw<InternalErrorException>();

// Func<Task> serverRequest = () => server.SendRequest("myrequest", new Request()).Returning<Data>(CancellationToken);
// serverRequest.Should().Throw<InternalErrorException>();
// }

// [Fact]
// public async Task Should_Send_and_receive_notifications()
// {
// var clientNotification = new AsyncSubject<Data>();
// var serverNotification = new AsyncSubject<Data>();
// var (client, server) = await Initialize(
// client => {
// client.OnNotification("mynotification", (Data data) => {
// clientNotification.OnNext(data);
// clientNotification.OnCompleted();
// });
// },
// server => {
// server.OnNotification("mynotification", (Data data) => {
// serverNotification.OnNext(data);
// serverNotification.OnCompleted();
// });
// }
// );

// client.SendNotification("mynotification", new Data() {Value = "myresponse"});
// var serverResponse = await serverNotification;
// serverResponse.Value.Should().Be("myresponse");

// server.SendNotification("mynotification", new Data() {Value = string.Join("", "myresponse".Reverse())});
// var clientResponse = await clientNotification;
// clientResponse.Value.Should().Be("esnopserym");
// }

// [Fact]
// public async Task Should_Send_and_cancel_requests_immediate()
// {
// var (client, server) = await Initialize(
// client => {
// client.OnRequest("myrequest", async (ct) => {
// await Task.Delay(TimeSpan.FromMinutes(1), ct);
// return new Data() {Value = "myresponse"};
// });
// },
// server => {
// server.OnRequest("myrequest", async (ct) => {
// await Task.Delay(TimeSpan.FromMinutes(1), ct);
// return new Data() {Value = string.Join("", "myresponse".Reverse())};
// });
// }
// );

// var cts = new CancellationTokenSource();
// cts.Cancel();

// {
// Func<Task> action = () => client.SendRequest("myrequest").Returning<Data>(cts.Token);
// await action.Should().ThrowAsync<OperationCanceledException>();
// }

// {
// Func<Task> action = () => server.SendRequest("myrequest").Returning<Data>(cts.Token);
// await action.Should().ThrowAsync<OperationCanceledException>();
// }
// }

// [Fact]
// public async Task Should_Send_and_cancel_requests_from_otherside()
// {
// var (client, server) = await Initialize(
// client => {
// client.OnRequest("myrequest", async (ct) => {
// await Task.Delay(TimeSpan.FromMinutes(1), ct);
// return new Data() {Value = "myresponse"};
// });
// },
// server => {
// server.OnRequest("myrequest", async (ct) => {
// await Task.Delay(TimeSpan.FromMinutes(1), ct);
// return new Data() {Value = string.Join("", "myresponse".Reverse())};
// });
// }
// );

// {
// var cts = new CancellationTokenSource();
// Func<Task> action = () => client.SendRequest("myrequest").Returning<Data>(cts.Token);
// cts.CancelAfter(10);
// await action.Should().ThrowAsync<RequestCancelledException>();
// }

// {
// var cts = new CancellationTokenSource();
// Func<Task> action = () => server.SendRequest("myrequest").Returning<Data>(cts.Token);
// cts.CancelAfter(10);
// await action.Should().ThrowAsync<RequestCancelledException>();
// }
// }

// [Fact(Skip = "Not supported by the DAP")]
// public async Task Should_Cancel_Parallel_Requests_When_Options_Are_Given()
// {
// var (client, server) = await Initialize(
// client => {
// client.OnRequest(
// "parallelrequest",
// async (ct) => {
// await Task.Delay(TimeSpan.FromSeconds(10), ct);
// return new Data() {Value = "myresponse"};
// },
// new JsonRpcHandlerOptions() {RequestProcessType = RequestProcessType.Parallel});
// client.OnRequest(
// "serialrequest",
// async (ct) => new Data() {Value = "myresponse"},
// new JsonRpcHandlerOptions() {RequestProcessType = RequestProcessType.Serial}
// );
// },
// server => {
// server.OnRequest(
// "parallelrequest",
// async (ct) => {
// await Task.Delay(TimeSpan.FromSeconds(10), ct);
// return new Data() {Value = "myresponse"};
// },
// new JsonRpcHandlerOptions() {RequestProcessType = RequestProcessType.Parallel});
// server.OnRequest(
// "serialrequest",
// async (ct) => new Data() {Value = "myresponse"},
// new JsonRpcHandlerOptions() {RequestProcessType = RequestProcessType.Serial}
// );
// }
// );

// {
// var task = client.SendRequest("parallelrequest").Returning<Data>(CancellationToken);
// await client.SendRequest("serialrequest").Returning<Data>(CancellationToken);
// Func<Task> action = () => task;
// await action.Should().ThrowAsync<ContentModifiedException>();
// }

// {
// var task = server.SendRequest("parallelrequest").Returning<Data>(CancellationToken);
// await server.SendRequest("serialrequest").Returning<Data>(CancellationToken);
// Func<Task> action = () => task;
// await action.Should().ThrowAsync<ContentModifiedException>();
// }
// }

// [Fact]
// public async Task Should_Link_Request_A_to_Request_B()
// {
// var (client, server) = await Initialize(
// client => {
// client
// .OnRequest("myrequest", async () => new Data() {Value = "myresponse"})
// .WithLink("myrequest", "myrequest2")
// ;
// },
// server => {
// server
// .OnRequest("myrequest", async () => new Data() {Value = string.Join("", "myresponse".Reverse())})
// .WithLink("myrequest", "myrequest2")
// ;
// }
// );

// var serverResponse = await client.SendRequest("myrequest2").Returning<Data>(CancellationToken);
// serverResponse.Value.Should().Be("esnopserym");

// var clientResponse = await server.SendRequest("myrequest2").Returning<Data>(CancellationToken);
// clientResponse.Value.Should().Be("myresponse");
// }
// }
// }
4 changes: 3 additions & 1 deletion test/Lsp.Tests/Integration/DynamicRegistrationTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Threading.Tasks;
Expand Down Expand Up @@ -61,6 +61,8 @@ public async Task Should_Register_Dynamically_While_Server_Is_Running()

await SettleNext();

await SettleNext();

client.RegistrationManager.CurrentRegistrations.Should().Contain(x =>
x.Method == TextDocumentNames.Completion && SelectorMatches(x, z=> z.HasLanguage && z.Language == "vb")
);
Expand Down
Loading