Skip to content
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

Fix erratic NPE in LanguageServersTest#testCancellable #986

Merged
merged 1 commit into from
May 7, 2024

Conversation

sebthom
Copy link
Member

@sebthom sebthom commented May 6, 2024

This commit fixes the NPE:

org.eclipse.lsp4e.test.LanguageServersTest.testCancellable -- Time
elapsed: 0.112 s <<< ERROR!
java.lang.NullPointerException: Cannot invoke
"org.eclipse.jface.text.ITextViewer.getTextWidget()" because "viewer" is
null
	at org.eclipse.lsp4e.test.LanguageServersTest.testCancellable(LanguageServersTest.java:789)

which may occur for the code:

IEditorPart editor = TestUtils.openEditor(testFile);
ITextViewer viewer = LSPEclipseUtils.getTextViewer(editor);
IDocument document = viewer.getDocument();

where viewer may be null if it has not been initialized fast enough in the UI thread.

As a remedy, a waitForAndAssertCondition is added to the function openTextViewer:

public static ITextViewer openTextViewer(IFile file) throws PartInitException {
	IEditorPart editor = openEditor(file);
	waitForAndAssertCondition(5_000, () -> LSPEclipseUtils.getTextViewer(editor) != null);
	return LSPEclipseUtils.getTextViewer(editor);
}

and all occurrences of

IEditorPart editor = TestUtils.openEditor(testFile);
ITextViewer viewer = LSPEclipseUtils.getTextViewer(editor);

are replaced with

ITextViewer viewer = TestUtils.openTextViewer(testFile);

This commit fixes the NPE:

```
org.eclipse.lsp4e.test.LanguageServersTest.testCancellable -- Time
elapsed: 0.112 s <<< ERROR!
java.lang.NullPointerException: Cannot invoke
"org.eclipse.jface.text.ITextViewer.getTextWidget()" because "viewer" is
null
	at org.eclipse.lsp4e.test.LanguageServersTest.testCancellable(LanguageServersTest.java:789)
```

which may occur for the code:
```
IEditorPart editor = TestUtils.openEditor(testFile);
ITextViewer viewer = LSPEclipseUtils.getTextViewer(editor);
IDocument document = viewer.getDocument();
```
where viewer may be null if it has not been initialized fast enough in
the UI thread.
@sebthom
Copy link
Member Author

sebthom commented May 6, 2024

Hmm...maybe the NPE is only thrown when an OOM occurred and a TextViewer instance could not be allocated.

Copy link
Contributor

@rubenporras rubenporras left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

@rubenporras rubenporras merged commit 82a852e into eclipse:master May 7, 2024
2 checks passed
@sebthom sebthom deleted the npe2 branch May 8, 2024 20:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants