Skip to content

Commit 02e6d85

Browse files
Fix Invalid thread access in RunTestsOfSelectedMemberAction
Extract UI-dependent data (method under cursor) on the UI thread before starting the background job in RunTestsActionExecutor. This prevents the "Invalid thread access" error reported in issue #231. Closes #231 Co-authored-by: RoiSoleil <3462260+RoiSoleil@users.noreply.github.com>
1 parent 8676491 commit 02e6d85

1 file changed

Lines changed: 9 additions & 18 deletions

File tree

org.moreunit.plugin/src/org/moreunit/handler/RunTestsActionExecutor.java

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,11 @@ private void saveIfNeeded(ICompilationUnit compilationUnit)
150150
public void executeRunTestsOfSelectedMemberAction(IEditorPart editorPart, String launchMode)
151151
{
152152
ICompilationUnit compilationUnit = createCompilationUnitFrom(editorPart);
153-
executeRunTestsOfSelectedMemberAction(editorPart, compilationUnit, launchMode);
153+
IMethod methodFromEditor = editorPart == null ? null : new EditorPartFacade(editorPart).getFirstNonAnonymousMethodSurroundingCursorPosition();
154+
executeRunTestsOfSelectedMemberAction(methodFromEditor, compilationUnit, launchMode);
154155
}
155156

156-
private void executeRunTestsOfSelectedMemberAction(IEditorPart editorPart, ICompilationUnit compilationUnit, String launchMode)
157+
private void executeRunTestsOfSelectedMemberAction(IMethod methodFromEditor, ICompilationUnit compilationUnit, String launchMode)
157158
{
158159
saveIfNeeded(compilationUnit);
159160
Jobs.waitForIndexExecuteAndRunInUI("Running tests ... ", () -> {
@@ -162,19 +163,15 @@ private void executeRunTestsOfSelectedMemberAction(IEditorPart editorPart, IComp
162163

163164
if(TypeFacade.isTestCase(selectedJavaType))
164165
{
165-
testElements.add(getTestElementFromTestCase(editorPart, selectedJavaType));
166+
testElements.add(getTestElementFromTestCase(methodFromEditor, selectedJavaType));
166167
}
167168
else
168169
{
169170
IJavaProject javaProject = compilationUnit.getJavaProject();
170171
MethodSearchMode searchMode = Preferences.getInstance().getMethodSearchMode(javaProject);
171172
ClassTypeFacade typeFacade = new ClassTypeFacade(compilationUnit);
172173

173-
IMethod methodUnderTest = null;
174-
if(editorPart != null)
175-
{
176-
methodUnderTest = new EditorPartFacade(editorPart).getFirstNonAnonymousMethodSurroundingCursorPosition();
177-
}
174+
IMethod methodUnderTest = methodFromEditor;
178175

179176
if(methodUnderTest != null && featureDetector.isTestSelectionRunSupported(selectedJavaType.getJavaProject()))
180177
{
@@ -195,7 +192,7 @@ private void executeRunTestsOfSelectedMemberAction(IEditorPart editorPart, IComp
195192

196193
if(testElements.isEmpty())
197194
{
198-
testElements.add(getTestElementFromTestCase(editorPart, selectedJavaType));
195+
testElements.add(getTestElementFromTestCase(methodFromEditor, selectedJavaType));
199196
}
200197
return testElements;
201198
}, testElements -> runTests(testElements, launchMode));
@@ -205,17 +202,11 @@ private void executeRunTestsOfSelectedMemberAction(IEditorPart editorPart, IComp
205202
* Returns the test method that is selected in editor if any, otherwise
206203
* returns the test case.
207204
*/
208-
private IMember getTestElementFromTestCase(IEditorPart editorPart, IType testCaseType)
205+
private IMember getTestElementFromTestCase(IMethod methodFromEditor, IType testCaseType)
209206
{
210-
if(editorPart == null)
211-
{
212-
return testCaseType;
213-
}
214-
215-
IMethod method = new EditorPartFacade(editorPart).getFirstNonAnonymousMethodSurroundingCursorPosition();
216-
if(method != null && new MethodFacade(method).isTestMethod())
207+
if(methodFromEditor != null && new MethodFacade(methodFromEditor).isTestMethod())
217208
{
218-
return method;
209+
return methodFromEditor;
219210
}
220211

221212
return testCaseType;

0 commit comments

Comments
 (0)