vcs: BackgroundTaskQueue - run task synchronously in launcher thread in tests

restore original behaviour
This commit is contained in:
Aleksey Pivovarov
2016-05-25 18:59:48 +03:00
parent ea6143f6fd
commit 801995bcb7
@@ -17,6 +17,7 @@
package com.intellij.openapi.progress;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.progress.impl.BackgroundableProcessIndicator;
@@ -40,7 +41,8 @@ public class BackgroundTaskQueue {
@NotNull private final String myTitle;
@NotNull private final QueueProcessor<TaskData> myProcessor;
private boolean myForceAsyncInTests = false;
@NotNull private final Object TEST_TASK_LOCK = new Object();
private volatile boolean myForceAsyncInTests = false;
public BackgroundTaskQueue(@Nullable Project project, @NotNull String title) {
myTitle = title;
@@ -56,6 +58,7 @@ public class BackgroundTaskQueue {
indicator = new EmptyProgressIndicator();
}
else {
// BackgroundableProcessIndicator should be created from EDT
indicator = new BackgroundableProcessIndicator(task);
}
}
@@ -102,7 +105,13 @@ public class BackgroundTaskQueue {
}
public void run(@NotNull Task.Backgroundable task, @Nullable ModalityState modalityState, @Nullable ProgressIndicator indicator) {
myProcessor.add(new TaskData(task, modalityState, indicator), modalityState);
TaskData taskData = new TaskData(task, modalityState, indicator);
if (!myForceAsyncInTests && ApplicationManager.getApplication().isUnitTestMode()) {
runTaskInCurrentThread(taskData);
}
else {
myProcessor.add(taskData, modalityState);
}
}
private static class TaskData {
@@ -119,7 +128,6 @@ public class BackgroundTaskQueue {
@TestOnly
public void setForceAsyncInTests(boolean value, @Nullable Disposable disposable) {
ApplicationManager.getApplication().assertIsDispatchThread();
myForceAsyncInTests = value;
if (disposable != null) {
Disposer.register(disposable, new Disposable() {
@@ -130,4 +138,55 @@ public class BackgroundTaskQueue {
});
}
}
private void runTaskInCurrentThread(@NotNull TaskData data) {
Task.Backgroundable task = data.task;
ProgressIndicator indicator = data.indicator;
if (indicator == null) indicator = new EmptyProgressIndicator();
ModalityState modalityState = data.modalityState;
if (modalityState == null) modalityState = ModalityState.NON_MODAL;
boolean processCanceled = false;
Exception exception = null;
try {
synchronized (TEST_TASK_LOCK) {
task.run(indicator);
}
}
catch (ProcessCanceledException e) {
processCanceled = true;
}
catch (Exception e) {
exception = e;
}
final boolean finalCanceled = processCanceled || indicator.isCanceled();
final Exception finalException = exception;
Runnable finishTask = () -> {
try {
if (finalException != null) {
task.onError(finalException);
}
else if (finalCanceled) {
task.onCancel();
}
else {
task.onSuccess();
}
}
finally {
task.onFinished();
}
};
Application application = ApplicationManager.getApplication();
if (application.isDispatchThread()) {
finishTask.run();
}
else {
application.invokeLater(finishTask, modalityState);
}
}
}