Skip to content

Event debounce for language server #6908

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 3 commits into from
Dec 18, 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
2 changes: 1 addition & 1 deletion docs/specs/lsp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,4 @@ languageServer:
"text": "Invalid change"
}
}
- expectNoNotificationAfterBuildTime: 1
- expectNoNotification: true
2 changes: 1 addition & 1 deletion src/docfx/config/TestQuirks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ internal static class TestQuirks

public static bool? Verbose { get; set; }

public static Action? FinishedBuildCountIncrease { get; set; }
public static Action? HandledEventCountIncrease { get; set; }
}
}
30 changes: 25 additions & 5 deletions src/docfx/serve/LanguageServerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;

Expand All @@ -13,7 +14,7 @@ internal class LanguageServerBuilder
{
private readonly Builder _builder;
private readonly ErrorList _errorList;
private readonly SemaphoreSlim _buildSemaphore;
private readonly Channel<bool> _buildChannel;
private readonly DiagnosticPublisher _diagnosticPublisher;
private readonly LanguageServerPackage _languageServerPackage;
private readonly PathString _workingDirectory;
Expand All @@ -30,28 +31,47 @@ public LanguageServerBuilder(
_errorList = new();
_languageServerPackage = languageServerPackage;
_builder = new(_errorList, languageServerPackage.BasePath, options, _languageServerPackage);
_buildSemaphore = new(0);
_buildChannel = Channel.CreateUnbounded<bool>();
_ = StartAsync();
}

public void QueueBuild()
{
_buildSemaphore.Release();
_buildChannel.Writer.TryWrite(true);
}

private async Task StartAsync()
{
while (true)
{
await _buildSemaphore.WaitAsync();
await WaitToTriggerBuild();
var filesToBuild = _languageServerPackage.GetAllFilesInMemory();
_builder.Build(filesToBuild.Select(f => f.Value).ToArray());

PublishDiagnosticsParams(filesToBuild);
TestQuirks.FinishedBuildCountIncrease?.Invoke();
TestQuirks.HandledEventCountIncrease?.Invoke();
}
}

private async Task WaitToTriggerBuild()
{
await _buildChannel.Reader.ReadAsync();

try
{
while (true)
{
using var cts = new CancellationTokenSource(1000);
await _buildChannel.Reader.ReadAsync(cts.Token);
TestQuirks.HandledEventCountIncrease?.Invoke();
}
}
catch (System.OperationCanceledException)
{
}
return;
}

private void PublishDiagnosticsParams(IEnumerable<PathString> files)
{
foreach (var file in files)
Expand Down
1 change: 1 addition & 0 deletions src/docfx/serve/lsp/TextDocumentHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ private bool TryUpdatePackage(DocumentUri file, string content)
var filePath = new PathString(file.GetFileSystemPath());
if (!filePath.StartsWithPath(_package.BasePath, out _))
{
TestQuirks.HandledEventCountIncrease?.Invoke();
return false;
}

Expand Down
18 changes: 11 additions & 7 deletions test/docfx.Test/DocfxTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public static class DocfxTest
private static readonly AsyncLocal<IReadOnlyDictionary<string, string>> t_repos = new();
private static readonly AsyncLocal<IReadOnlyDictionary<string, string>> t_remoteFiles = new();
private static readonly AsyncLocal<string> t_appDataPath = new();
private static readonly AsyncLocal<StrongBox<int>> t_finishedBuildCount = new();
private static readonly AsyncLocal<StrongBox<int>> t_handledEventCount = new();
private static readonly AsyncLocal<StrongBox<int>> t_sentEventCount = new();

static DocfxTest()
{
Expand All @@ -52,9 +53,9 @@ static DocfxTest()
return null;
};

TestQuirks.FinishedBuildCountIncrease = () =>
TestQuirks.HandledEventCountIncrease = () =>
{
t_finishedBuildCount.Value.Value++;
t_handledEventCount.Value.Value++;
};
}

Expand Down Expand Up @@ -86,7 +87,8 @@ public static void Run(TestData test, DocfxTestSpec spec)
t_repos.Value = repos;
t_remoteFiles.Value = spec.Http;
t_appDataPath.Value = appDataPath;
t_finishedBuildCount.Value = new StrongBox<int>();
t_handledEventCount.Value = new StrongBox<int>();
t_sentEventCount.Value = new StrongBox<int>();
RunCore(docsetPath, outputPath, test, spec, variables, package);
}
catch (Exception exception)
Expand All @@ -102,7 +104,8 @@ public static void Run(TestData test, DocfxTestSpec spec)
t_repos.Value = null;
t_remoteFiles.Value = null;
t_appDataPath.Value = null;
t_finishedBuildCount.Value = null;
t_handledEventCount.Value = null;
t_sentEventCount.Value = null;
}
}
}
Expand Down Expand Up @@ -208,6 +211,7 @@ private static async Task RunLanguageServer(string docsetPath, DocfxTestSpec spe
if (!string.IsNullOrEmpty(lspSpec.Notification))
{
await lspTestHost.SendNotification(new LanguageServerNotification(lspSpec.Notification, lspSpec.Params));
t_sentEventCount.Value.Value++;
}
else if (!string.IsNullOrEmpty(lspSpec.ExpectNotification))
{
Expand Down Expand Up @@ -239,9 +243,9 @@ private static async Task RunLanguageServer(string docsetPath, DocfxTestSpec spe

s_languageServerJsonDiff.Verify(expectedNotifications, actualNotifications);
}
else if (lspSpec.ExpectNoNotificationAfterBuildTime != null)
else if (lspSpec.ExpectNoNotification)
{
while (lspSpec.ExpectNoNotificationAfterBuildTime > t_finishedBuildCount.Value.Value)
while (t_sentEventCount.Value.Value > t_handledEventCount.Value.Value)
{
await Task.Delay(1000);
}
Expand Down
2 changes: 1 addition & 1 deletion test/docfx.Test/lsp/LanguageServerTestSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class LanguageServerTestSpec

public string ExpectNotification { get; set; } = string.Empty;

public int? ExpectNoNotificationAfterBuildTime { get; set; }
public bool ExpectNoNotification { get; set; }

public JToken Params { get; set; }
}
Expand Down