Skip to content

Commit

Permalink
test: disconnect and reconnect to LSP (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
sockmaster27 authored Apr 14, 2024
1 parent 3f06bda commit 6b35ed0
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions client/src/engine/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export enum Request {
apiRemJar = 'api/remJar',
apiVersion = 'api/version',
apiShutdown = 'api/shutdown',
apiDisconnect = 'api/disconnect',

lspCheck = 'lsp/check',
lspCodelens = 'lsp/codelens',
Expand Down
1 change: 1 addition & 0 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export async function activate(context: vscode.ExtensionContext, launchOptions:
// Register commands for command palette
registerCommand('flix.internalRestart', makeHandleRestartClient(context, { shouldUpdateFlix: false }))
registerCommand('flix.internalDownloadLatest', makeHandleRestartClient(context, { shouldUpdateFlix: true }))
registerCommand('flix.simulateDisconnect', handlers.simulateDisconnect(client))
registerCommand('flix.runMain', handlers.runMain(context, launchOptions))

registerCommand('flix.cmdInit', handlers.cmdInit(context, launchOptions))
Expand Down
8 changes: 8 additions & 0 deletions client/src/handlers/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ const _ = require('lodash/fp')

let flixTerminal: vscode.Terminal | null = null

/**
* Request that the server should disconnect. Returns promise that will resolve when the server has been reconnected.
* Used for testing purposes.
*/
export function simulateDisconnect(client: LanguageClient) {
return () => client.sendNotification(jobs.Request.apiDisconnect)
}

export function makeHandleRunJob(client: LanguageClient, request: jobs.Request) {
return function handler() {
client.sendNotification(request)
Expand Down
5 changes: 5 additions & 0 deletions server/src/engine/flix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ export function updateUserConfiguration(userConfiguration: UserConfiguration) {
_.set(userConfiguration, 'userConfiguration', startEngineInput)
}

export function simulateDisconnect() {
const job = { request: jobs.Request.apiDisconnect }
queue.enqueue(job)
}

export async function start(input: StartEngineInput) {
if (flixInstance || socket.isOpen()) {
await stop()
Expand Down
1 change: 1 addition & 0 deletions server/src/engine/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export enum Request {
apiRemJar = 'api/remJar',
apiVersion = 'api/version',
apiShutdown = 'api/shutdown',
apiDisconnect = 'api/disconnect',

lspCheck = 'lsp/check',
lspCodelens = 'lsp/codelens',
Expand Down
8 changes: 8 additions & 0 deletions server/src/handlers/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ export function handleReplaceConfiguration(userConfiguration: engine.UserConfigu
engine.updateUserConfiguration(userConfiguration)
}

/**
* Simulates the language server disconnecting.
* Used for testing.
*/
export function handleDisconnect() {
engine.simulateDisconnect()
}

/**
* Runs when both client and server are ready.
*/
Expand Down
2 changes: 2 additions & 0 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ connection.onInitialize(handlers.handleInitialize)
// The user has changed something in the configuration
connection.onNotification(jobs.Request.internalReplaceConfiguration, handlers.handleReplaceConfiguration)

connection.onNotification(jobs.Request.apiDisconnect, handlers.handleDisconnect)

// Event happens once after either startup or a restart - starts the engine
connection.onNotification(jobs.Request.internalReady, handlers.handleReady)

Expand Down
32 changes: 32 additions & 0 deletions test/src/disconnect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2024 Holger Dal Mogensen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as assert from 'assert'
import * as vscode from 'vscode'
import { getTestDocUri } from './util'

suite('Server disconnect', () => {
test('When server is disconnected a reconnection should happen automatically', async () => {
await vscode.commands.executeCommand('flix.simulateDisconnect')

// Ensure that the server is reconnected
const docUri = getTestDocUri('src/Main.flix')
const position = new vscode.Position(9, 12)
const r = (await vscode.commands.executeCommand('vscode.executeHoverProvider', docUri, position)) as vscode.Hover[]
const contents = r[0].contents[0] as vscode.MarkdownString
assert.strictEqual(contents.value.includes('Type'), true)
})
})

0 comments on commit 6b35ed0

Please sign in to comment.