Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Client/Processes/NamedPipeServerProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ public override async Task Start()
{
ServerExitCompletion = new TaskCompletionSource<object>();

ServerInputStream = new NamedPipeServerStream(BaseName + "/in", PipeDirection.Out, 2, PipeTransmissionMode.Byte, PipeOptions.Asynchronous, inBufferSize: 1024, outBufferSize: 1024);
ServerOutputStream = new NamedPipeServerStream(BaseName + "/out", PipeDirection.In, 2, PipeTransmissionMode.Byte, PipeOptions.Asynchronous, inBufferSize: 1024, outBufferSize: 1024);
ClientInputStream = new NamedPipeClientStream(".", BaseName + "/out", PipeDirection.Out, PipeOptions.Asynchronous);
ClientOutputStream = new NamedPipeClientStream(".", BaseName + "/in", PipeDirection.In, PipeOptions.Asynchronous);
ServerInputStream = new NamedPipeServerStream(BaseName + "_in", PipeDirection.Out, 2, PipeTransmissionMode.Byte, PipeOptions.Asynchronous, inBufferSize: 1024, outBufferSize: 1024);
ServerOutputStream = new NamedPipeServerStream(BaseName + "_out", PipeDirection.In, 2, PipeTransmissionMode.Byte, PipeOptions.Asynchronous, inBufferSize: 1024, outBufferSize: 1024);
ClientInputStream = new NamedPipeClientStream(".", BaseName + "_out", PipeDirection.Out, PipeOptions.Asynchronous);
ClientOutputStream = new NamedPipeClientStream(".", BaseName + "_in", PipeDirection.In, PipeOptions.Asynchronous);

// Ensure all pipes are connected before proceeding.
await Task.WhenAll(
Expand Down
19 changes: 17 additions & 2 deletions src/Client/Protocol/LspConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ public sealed class LspConnection
/// </summary>
int _nextRequestId = 0;

/// <summary>
/// Has the connection been disposed?
/// </summary>
bool _disposed;

/// <summary>
/// The cancellation source for the read and write loops.
/// </summary>
Expand Down Expand Up @@ -158,9 +163,19 @@ public LspConnection(ILoggerFactory loggerFactory, Stream input, Stream output)
/// </summary>
public void Dispose()
{
Disconnect();
if (_disposed)
return;

_cancellationSource?.Dispose();
try
{
Disconnect();

_cancellationSource?.Dispose();
}
finally
{
_disposed = true;
}
}

/// <summary>
Expand Down
144 changes: 144 additions & 0 deletions test/Client.Tests/ClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
using Microsoft.Extensions.Logging;
using OmniSharp.Extensions.LanguageServer.Capabilities.Server;
using OmniSharp.Extensions.LanguageServer.Models;
using OmniSharp.Extensions.LanguageServerProtocol.Client.Dispatcher;
using OmniSharp.Extensions.LanguageServerProtocol.Client.Protocol;
using OmniSharp.Extensions.LanguageServerProtocol.Client.Utilities;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;

namespace OmniSharp.Extensions.LanguageServerProtocol.Client.Tests
{
/// <summary>
/// Tests for <see cref="LanguageClient"/>.
/// </summary>
public class ClientTests
: PipeServerTestBase
{
/// <summary>
/// Create a new <see cref="LanguageClient"/> test suite.
/// </summary>
/// <param name="testOutput">
/// Output for the current test.
/// </param>
public ClientTests(ITestOutputHelper testOutput)
: base(testOutput)
{
}

/// <summary>
/// Get an absolute document path for use in tests.
/// </summary>
string AbsoluteDocumentPath => Path.DirectorySeparatorChar + "Foo.txt"; // Absolute path, cross-platform compatible.

/// <summary>
/// The <see cref="LanguageClient"/> under test.
/// </summary>
LanguageClient LanguageClient { get; set; }

/// <summary>
/// The server-side dispatcher.
/// </summary>
LspDispatcher ServerDispatcher { get; } = new LspDispatcher();

/// <summary>
/// The server-side connection.
/// </summary>
LspConnection ServerConnection { get; set; }

/// <summary>
/// Ensure that the language client can successfully request Hover information.
/// </summary>
[Fact(DisplayName = "Language client can successfully request hover info")]
public async Task Hover_Success()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should makes things so much easier to integration test... and we can finally make sure LanguageServer is working correctly.

{
await Connect();

const int line = 5;
const int column = 5;
var expectedHoverContent = new MarkedStringContainer("123", "456", "789");

ServerDispatcher.HandleRequest<TextDocumentPositionParams, Hover>("textDocument/hover", (request, cancellationToken) =>
{
Assert.NotNull(request.TextDocument);

Assert.Equal(AbsoluteDocumentPath,
DocumentUri.GetFileSystemPath(request.TextDocument.Uri)
);

Assert.Equal(line, request.Position.Line);
Assert.Equal(column, request.Position.Character);

return Task.FromResult(new Hover
{
Contents = expectedHoverContent,
Range = new Range
{
Start = request.Position,
End = request.Position
}
});
});

Hover hover = await LanguageClient.TextDocument.Hover(AbsoluteDocumentPath, line, column);

Assert.NotNull(hover.Range);
Assert.NotNull(hover.Range.Start);
Assert.NotNull(hover.Range.End);

Assert.Equal(line, hover.Range.Start.Line);
Assert.Equal(column, hover.Range.Start.Character);

Assert.Equal(line, hover.Range.End.Line);
Assert.Equal(column, hover.Range.End.Character);

Assert.NotNull(hover.Contents);
Assert.Equal(expectedHoverContent.Select(markedString => markedString.Value),
hover.Contents.Select(
markedString => markedString.Value
)
);
}

/// <summary>
/// Connect the client and server.
/// </summary>
/// <param name="handleServerInitialize">
/// Add standard handlers for server initialisation?
/// </param>
async Task Connect(bool handleServerInitialize = true)
{
ServerConnection = await CreateServerConnection();
ServerConnection.Connect(ServerDispatcher);

if (handleServerInitialize)
HandleServerInitialize();

LanguageClient = await CreateClient(initialize: true);
}

/// <summary>
/// Add standard handlers for sever initialisation.
/// </summary>
void HandleServerInitialize()
{
ServerDispatcher.HandleRequest<InitializeParams, InitializeResult>("initialize", (request, cancellationToken) =>
{
return Task.FromResult(new InitializeResult
{
Capabilities = new ServerCapabilities
{
HoverProvider = true
}
});
});
ServerDispatcher.HandleEmptyNotification("initialized", () =>
{
Log.LogInformation("Server initialized.");
});
}
}
}
162 changes: 161 additions & 1 deletion test/Client.Tests/ConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public async Task Client_HandleEmptyNotification_Success()
{
var testCompletion = new TaskCompletionSource<object>();

LspConnection serverConnection = await CreateServerConnection();
LspConnection clientConnection = await CreateClientConnection();
LspConnection serverConnection = await CreateServerConnection();

var serverDispatcher = new LspDispatcher();
serverDispatcher.HandleEmptyNotification("test", () =>
Expand Down Expand Up @@ -85,5 +85,165 @@ public async Task Server_HandleEmptyNotification_Success()

await Task.WhenAll(clientConnection.HasHasDisconnected, serverConnection.HasHasDisconnected);
}

/// <summary>
/// Verify that a client <see cref="LspConnection"/> can handle a request from a server <see cref="LspConnection"/>.
/// </summary>
[Fact(DisplayName = "Client connection can handle request from server")]
public async Task Server_HandleRequest_Success()
{
LspConnection clientConnection = await CreateClientConnection();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nitpick, if we make CreateClientConnection return a disposable we can wrap all these calls in a using and remove some boilerplate with disconnect.

However that can be changed later.

LspConnection serverConnection = await CreateServerConnection();

var clientDispatcher = new LspDispatcher();
clientDispatcher.HandleRequest<TestRequest, TestResponse>("test", (request, cancellationToken) =>
{
Log.LogInformation("Got request: {@Request}", request);

return Task.FromResult(new TestResponse
{
Value = request.Value.ToString()
});
});
clientConnection.Connect(clientDispatcher);

serverConnection.Connect(new LspDispatcher());
TestResponse response = await serverConnection.SendRequest<TestResponse>("test", new TestRequest
{
Value = 1234
});

Assert.Equal("1234", response.Value);

Log.LogInformation("Got response: {@Response}", response);

serverConnection.Disconnect(flushOutgoing: true);
clientConnection.Disconnect();

await Task.WhenAll(clientConnection.HasHasDisconnected, serverConnection.HasHasDisconnected);
}

/// <summary>
/// Verify that a server <see cref="LspConnection"/> can handle a request from a client <see cref="LspConnection"/>.
/// </summary>
[Fact(DisplayName = "Server connection can handle request from client")]
public async Task Client_HandleRequest_Success()
{
LspConnection clientConnection = await CreateClientConnection();
LspConnection serverConnection = await CreateServerConnection();

var serverDispatcher = new LspDispatcher();
serverDispatcher.HandleRequest<TestRequest, TestResponse>("test", (request, cancellationToken) =>
{
Log.LogInformation("Got request: {@Request}", request);

return Task.FromResult(new TestResponse
{
Value = request.Value.ToString()
});
});
serverConnection.Connect(serverDispatcher);

clientConnection.Connect(new LspDispatcher());
TestResponse response = await clientConnection.SendRequest<TestResponse>("test", new TestRequest
{
Value = 1234
});

Assert.Equal("1234", response.Value);

Log.LogInformation("Got response: {@Response}", response);

clientConnection.Disconnect(flushOutgoing: true);
serverConnection.Disconnect();

await Task.WhenAll(clientConnection.HasHasDisconnected, serverConnection.HasHasDisconnected);
}

/// <summary>
/// Verify that a client <see cref="LspConnection"/> can handle a command-style request (i.e. no response body) from a server <see cref="LspConnection"/>.
/// </summary>
[Fact(DisplayName = "Client connection can handle command request from server")]
public async Task Server_HandleCommandRequest_Success()
{
LspConnection clientConnection = await CreateClientConnection();
LspConnection serverConnection = await CreateServerConnection();

var clientDispatcher = new LspDispatcher();
clientDispatcher.HandleRequest<TestRequest>("test", (request, cancellationToken) =>
{
Log.LogInformation("Got request: {@Request}", request);

Assert.Equal(1234, request.Value);

return Task.CompletedTask;
});
clientConnection.Connect(clientDispatcher);

serverConnection.Connect(new LspDispatcher());
await serverConnection.SendRequest("test", new TestRequest
{
Value = 1234
});

serverConnection.Disconnect(flushOutgoing: true);
clientConnection.Disconnect();

await Task.WhenAll(clientConnection.HasHasDisconnected, serverConnection.HasHasDisconnected);
}

/// <summary>
/// Verify that a server <see cref="LspConnection"/> can handle a command-style request (i.e. no response body) from a client <see cref="LspConnection"/>.
/// </summary>
[Fact(DisplayName = "Server connection can handle command request from client")]
public async Task Client_HandleCommandRequest_Success()
{
LspConnection clientConnection = await CreateClientConnection();
LspConnection serverConnection = await CreateServerConnection();

var serverDispatcher = new LspDispatcher();
serverDispatcher.HandleRequest<TestRequest>("test", (request, cancellationToken) =>
{
Log.LogInformation("Got request: {@Request}", request);

Assert.Equal(1234, request.Value);

return Task.CompletedTask;
});
serverConnection.Connect(serverDispatcher);

clientConnection.Connect(new LspDispatcher());
await clientConnection.SendRequest("test", new TestRequest
{
Value = 1234
});

clientConnection.Disconnect(flushOutgoing: true);
serverConnection.Disconnect();

await Task.WhenAll(clientConnection.HasHasDisconnected, serverConnection.HasHasDisconnected);
}
}

/// <summary>
/// A test request.
/// </summary>
class TestRequest
{
/// <summary>
/// A test value for the request.
/// </summary>
public int Value { get; set; }
}

/// <summary>
/// A test response.
/// </summary>
class TestResponse
{
/// <summary>
/// A test value for the response.
/// </summary>
public string Value { get; set; }
}
}
11 changes: 10 additions & 1 deletion test/Client.Tests/Logging/TestOutputLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,16 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
if (exception != null)
message += "\n" + exception.ToString();

_testOutput.WriteLine(message);
try
{
_testOutput.WriteLine(message);
}
catch (InvalidOperationException)
{
// Test has already terminated.

System.Diagnostics.Debug.WriteLine(message);
}
}
}
}