Skip to content

Commit c1f8c45

Browse files
committed
bugfix: Actually wait for and assert initialization in InitializationTests
1 parent 0a2275e commit c1f8c45

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

src/CSharpLanguageServer/RoslynHelpers.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ let tryLoadSolutionFromProjectFiles
410410
let progress = ProgressReporter(lspClient)
411411

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

416416
let msbuildWorkspace = MSBuildWorkspace.Create(CSharpLspHostServices())

tests/CSharpLanguageServer.Tests/InitializationTests.fs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ let testServerRegistersCapabilitiesWithTheClient () =
2828
use client = startAndMountServer projectFiles false
2929
client.Start()
3030
client.Initialize()
31-
client.DumpRpcLog()
32-
Assert.IsTrue(client.ServerDidInvoke("client/registerCapability"))
31+
client.WaitForProgressEnd("OK, 1 project file(s) loaded")
32+
Assert.IsTrue(client.ServerDidRespondTo("initialize"))
33+
Assert.IsTrue(client.ServerDidRespondTo("initialized"))

tests/CSharpLanguageServer.Tests/Tooling.fs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ open System.IO
66
open System.Diagnostics
77
open System.Text
88
open System.Threading.Tasks
9+
open System.Threading
910

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

436+
member __.WaitForProgressEnd(message: string) =
437+
let timeoutMS = 10 * 1000
438+
let start = DateTime.Now
439+
440+
let progressEndMatch m =
441+
m.Source = Server
442+
&& (m.Message.["method"] |> string) = "$/progress"
443+
&& (m.Message.["params"].["value"].["kind"] |> string) = "end"
444+
&& (m.Message.["params"].["value"].["message"] |> string) = message
445+
446+
let mutable haveProgressEnd = false
447+
while (not haveProgressEnd) do
448+
let rpcLog = client.PostAndReply(fun rc -> GetRpcLog rc)
449+
haveProgressEnd <- rpcLog |> Seq.exists progressEndMatch
450+
451+
if (DateTime.Now - start).TotalMilliseconds > timeoutMS then
452+
failwith (sprintf "WaitForProgressEnd: no $/progress[end] received in %dms"
453+
timeoutMS)
454+
Thread.Sleep(25)
455+
435456
member __.Shutdown () =
436457
client.Post(ShutdownRequest)
437458
client.Post(ExitRequest)
@@ -448,6 +469,13 @@ type ClientController (client: MailboxProcessor<ClientEvent>, projectFiles: Map<
448469
let rpcLog = client.PostAndReply(fun rc -> GetRpcLog rc)
449470
rpcLog |> Seq.exists (fun m -> m.Source = Server && (string m.Message["method"]) = rpcMethod)
450471

472+
member __.ServerDidRespondTo (rpcMethod: string) =
473+
let rpcLog = client.PostAndReply(fun rc -> GetRpcLog rc)
474+
let invocation = rpcLog |> Seq.find (fun m -> m.Source = Client && (string m.Message["method"]) = rpcMethod)
475+
rpcLog
476+
|> Seq.exists (fun m -> m.Source = Client
477+
&& (m.Message.["id"] |> string) = (invocation.Message.["id"] |> string)
478+
&& (m.Message.["method"] |> string) = rpcMethod)
451479

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

0 commit comments

Comments
 (0)