Skip to content

Commit

Permalink
Synchronize LanguageServerWrapper.isActive()
Browse files Browse the repository at this point in the history
  • Loading branch information
basilevs committed Jun 7, 2023
1 parent 97cb4e3 commit 1baa8d4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Expand Up @@ -15,7 +15,10 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.util.Collection;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
Expand Down Expand Up @@ -63,4 +66,46 @@ public void testConnect() throws Exception {
TestUtils.closeEditor(editor1, false);
TestUtils.closeEditor(editor2, false);
}

/**
* Check if {@code isActive()} is correctly synchronized with {@code stop()}
* @see https://github.com/eclipse/lsp4e/pull/688
*/
@Test
public void testStopAndActive() throws CoreException, IOException, AssertionError, InterruptedException, ExecutionException {
IFile testFile1 = TestUtils.createFile(project1, "shouldUseExtension.lsptWithMultiRoot", "");
IEditorPart editor1 = TestUtils.openEditor(testFile1);
@NonNull Collection<LanguageServerWrapper> wrappers = LanguageServiceAccessor.getLSWrappers(testFile1, request -> true);
assertEquals(1, wrappers.size());
LanguageServerWrapper wrapper = wrappers.iterator().next();
try {
var startStopJob = ForkJoinPool.commonPool().submit(() -> {
while (!Thread.interrupted()) {
wrapper.stop();
try {
wrapper.start();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
try {
for (int i = 0; i < 10000000; i++) {
// Should not throw
wrapper.isActive();
if (startStopJob.isDone()) {
throw new AssertionError("Job should run indefinitely");
}
}
} finally {
startStopJob.cancel(true);
if (!startStopJob.isCancelled()) {
startStopJob.get();
}
}
} finally {
TestUtils.closeEditor(editor1, false);
}

}
}
Expand Up @@ -389,7 +389,7 @@ private void logMessage(Message message) {
/**
* @return whether the underlying connection to language server is still active
*/
public boolean isActive() {
public synchronized boolean isActive() {
return this.launcherFuture != null && !this.launcherFuture.isDone() && !this.launcherFuture.isCancelled();
}

Expand Down

0 comments on commit 1baa8d4

Please sign in to comment.