Skip to content

Commit

Permalink
bugfix: Actually wait for and assert initialization in Initialization…
Browse files Browse the repository at this point in the history
…Tests
  • Loading branch information
razzmatazz committed Jun 20, 2024
1 parent 0a2275e commit c1f8c45
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/CSharpLanguageServer/RoslynHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ let tryLoadSolutionFromProjectFiles
let progress = ProgressReporter(lspClient)

async {
do! progress.Begin($"Loading {projs.Length} projects...", false, $"0/{projs.Length}", 0u)
do! progress.Begin($"Loading {projs.Length} project(s)...", false, $"0/{projs.Length}", 0u)
let loadedProj = ref 0

let msbuildWorkspace = MSBuildWorkspace.Create(CSharpLspHostServices())
Expand Down
5 changes: 3 additions & 2 deletions tests/CSharpLanguageServer.Tests/InitializationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ let testServerRegistersCapabilitiesWithTheClient () =
use client = startAndMountServer projectFiles false
client.Start()
client.Initialize()
client.DumpRpcLog()
Assert.IsTrue(client.ServerDidInvoke("client/registerCapability"))
client.WaitForProgressEnd("OK, 1 project file(s) loaded")
Assert.IsTrue(client.ServerDidRespondTo("initialize"))
Assert.IsTrue(client.ServerDidRespondTo("initialized"))

Check warning on line 33 in tests/CSharpLanguageServer.Tests/InitializationTests.fs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 8.0.300)

Main module of program is empty: nothing will happen when it is run

Check warning on line 33 in tests/CSharpLanguageServer.Tests/InitializationTests.fs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 8.0.300)

Main module of program is empty: nothing will happen when it is run

Check warning on line 33 in tests/CSharpLanguageServer.Tests/InitializationTests.fs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 8.0.300)

Main module of program is empty: nothing will happen when it is run

Check warning on line 33 in tests/CSharpLanguageServer.Tests/InitializationTests.fs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 8.0.300)

Main module of program is empty: nothing will happen when it is run
28 changes: 28 additions & 0 deletions tests/CSharpLanguageServer.Tests/Tooling.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ open System.IO
open System.Diagnostics
open System.Text
open System.Threading.Tasks
open System.Threading

open NUnit.Framework
open Newtonsoft.Json.Linq
Expand Down Expand Up @@ -432,6 +433,26 @@ type ClientController (client: MailboxProcessor<ClientEvent>, projectFiles: Map<
client.PostAndReply(fun rc -> InitializeRequest rc)
logMessage "Initialize" "OK, InitializeRequest complete"

member __.WaitForProgressEnd(message: string) =
let timeoutMS = 10 * 1000
let start = DateTime.Now

let progressEndMatch m =
m.Source = Server
&& (m.Message.["method"] |> string) = "$/progress"
&& (m.Message.["params"].["value"].["kind"] |> string) = "end"
&& (m.Message.["params"].["value"].["message"] |> string) = message

let mutable haveProgressEnd = false
while (not haveProgressEnd) do
let rpcLog = client.PostAndReply(fun rc -> GetRpcLog rc)
haveProgressEnd <- rpcLog |> Seq.exists progressEndMatch

if (DateTime.Now - start).TotalMilliseconds > timeoutMS then
failwith (sprintf "WaitForProgressEnd: no $/progress[end] received in %dms"
timeoutMS)
Thread.Sleep(25)

member __.Shutdown () =
client.Post(ShutdownRequest)
client.Post(ExitRequest)
Expand All @@ -448,6 +469,13 @@ type ClientController (client: MailboxProcessor<ClientEvent>, projectFiles: Map<
let rpcLog = client.PostAndReply(fun rc -> GetRpcLog rc)
rpcLog |> Seq.exists (fun m -> m.Source = Server && (string m.Message["method"]) = rpcMethod)

member __.ServerDidRespondTo (rpcMethod: string) =
let rpcLog = client.PostAndReply(fun rc -> GetRpcLog rc)
let invocation = rpcLog |> Seq.find (fun m -> m.Source = Client && (string m.Message["method"]) = rpcMethod)
rpcLog
|> Seq.exists (fun m -> m.Source = Client
&& (m.Message.["id"] |> string) = (invocation.Message.["id"] |> string)
&& (m.Message.["method"] |> string) = rpcMethod)

let startAndMountServer (projectFiles: Map<string, string>) (enableLogging: bool) =
let initialState = { ClientState.Default with LoggingEnabled = enableLogging }
Expand Down

0 comments on commit c1f8c45

Please sign in to comment.