use of com.intellij.util.concurrency.Semaphore in project intellij-community by JetBrains.
the class PsiDocumentManagerImplTest method testCommitInBackground.
public void testCommitInBackground() {
PsiFile file = findFile(createFile());
assertNotNull(file);
assertTrue(file.isPhysical());
final Document document = getDocument(file);
assertNotNull(document);
final Semaphore semaphore = new Semaphore();
semaphore.down();
getPsiDocumentManager().performWhenAllCommitted(() -> {
assertTrue(getPsiDocumentManager().isCommitted(document));
semaphore.up();
});
waitAndPump(semaphore, TIMEOUT);
assertTrue(getPsiDocumentManager().isCommitted(document));
WriteCommandAction.runWriteCommandAction(null, () -> document.insertString(0, "class X {}"));
semaphore.down();
getPsiDocumentManager().performWhenAllCommitted(() -> {
assertTrue(getPsiDocumentManager().isCommitted(document));
semaphore.up();
});
waitAndPump(semaphore, TIMEOUT);
assertTrue(getPsiDocumentManager().isCommitted(document));
final AtomicInteger count = new AtomicInteger();
WriteCommandAction.runWriteCommandAction(null, () -> {
document.insertString(0, "/**/");
boolean executed = getPsiDocumentManager().cancelAndRunWhenAllCommitted("xxx", count::incrementAndGet);
assertFalse(executed);
executed = getPsiDocumentManager().cancelAndRunWhenAllCommitted("xxx", count::incrementAndGet);
assertFalse(executed);
assertEquals(0, count.get());
});
while (!getPsiDocumentManager().isCommitted(document)) {
UIUtil.dispatchAllInvocationEvents();
}
assertTrue(getPsiDocumentManager().isCommitted(document));
assertEquals(1, count.get());
count.set(0);
WriteCommandAction.runWriteCommandAction(null, () -> {
document.insertString(0, "/**/");
boolean executed = getPsiDocumentManager().performWhenAllCommitted(count::incrementAndGet);
assertFalse(executed);
executed = getPsiDocumentManager().performWhenAllCommitted(count::incrementAndGet);
assertFalse(executed);
assertEquals(0, count.get());
});
while (!getPsiDocumentManager().isCommitted(document)) {
UIUtil.dispatchAllInvocationEvents();
}
assertTrue(getPsiDocumentManager().isCommitted(document));
assertEquals(2, count.get());
}
use of com.intellij.util.concurrency.Semaphore in project intellij-community by JetBrains.
the class SMTRunnerConsoleTest method testEnsureOrderedClearFlush.
public void testEnsureOrderedClearFlush() throws Exception {
StringBuffer buf = new StringBuffer();
String expected = "";
for (int i = 0; i < 100; i++) {
expected += "1";
expected += "2";
CompositePrintable.invokeInAlarm(() -> buf.append("1"), false);
CompositePrintable.invokeInAlarm(() -> buf.append("2"), false);
}
Semaphore s = new Semaphore();
s.down();
CompositePrintable.invokeInAlarm(s::up, false);
assertTrue(s.waitFor(1000));
assertEquals(expected, buf.toString());
}
use of com.intellij.util.concurrency.Semaphore in project intellij-community by JetBrains.
the class TransferToEDTQueue method waitFor.
// blocks until all elements in the queue are processed
public void waitFor() {
final Semaphore semaphore = new Semaphore();
semaphore.down();
schedule(new Runnable() {
@Override
public void run() {
semaphore.up();
}
});
semaphore.waitFor();
}
use of com.intellij.util.concurrency.Semaphore in project intellij-community by JetBrains.
the class ChangeListManagerImpl method freeze.
public void freeze(@NotNull String reason) {
myUpdater.setIgnoreBackgroundOperation(true);
Semaphore sem = new Semaphore();
sem.down();
invokeAfterUpdate(() -> {
myUpdater.setIgnoreBackgroundOperation(false);
myUpdater.pause();
myFreezeName.set(reason);
sem.up();
}, InvokeAfterUpdateMode.SILENT_CALLBACK_POOLED, "", ModalityState.defaultModalityState());
boolean free = false;
while (!free) {
ProgressIndicator pi = ProgressManager.getInstance().getProgressIndicator();
if (pi != null)
pi.checkCanceled();
free = sem.waitFor(500);
}
}
use of com.intellij.util.concurrency.Semaphore in project intellij-community by JetBrains.
the class SmoothProgressAdapter method stop.
@Override
public synchronized void stop() {
if (myOriginal.isRunning()) {
myOriginal.stop();
} else {
myStartupAlarm.cancel(false);
if (!myOriginalStarted && myOriginal instanceof Disposable) {
// dispose original because start & stop were not called so original progress might not have released its resources
Disposer.dispose(((Disposable) myOriginal));
}
}
// needed only for correct assertion of !progress.isRunning() in ApplicationImpl.runProcessWithProgressSynchroniously
final Semaphore semaphore = new Semaphore();
semaphore.down();
SwingUtilities.invokeLater(() -> {
semaphore.waitFor();
if (myDialog != null) {
//System.out.println("myDialog.destroyProcess()");
myDialog.close(DialogWrapper.OK_EXIT_CODE);
myDialog = null;
}
});
try {
// should be last to not leaveModal before closing the dialog
super.stop();
} finally {
semaphore.up();
}
}
Aggregations