Skip to content

Commit

Permalink
reduce timeouts for Sergey
Browse files Browse the repository at this point in the history
  • Loading branch information
cdracm committed Oct 7, 2018
1 parent 16bb0ed commit f4378ac
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2105,7 +2105,7 @@ public void _testCaretMovementDoesNotRestartHighlighting() {
public void testHighlightingDoesWaitForEmbarrassinglySlowExternalAnnotatorsToFinish() {
configureByText(JavaFileType.INSTANCE, "class X { int f() { int gg<caret> = 11; return 0;} }");
final AtomicBoolean run = new AtomicBoolean();
final int SLEEP = 20000;
final int SLEEP = 2_000;
ExternalAnnotator<Integer, Integer> annotator = new ExternalAnnotator<Integer, Integer>() {
@Override
public Integer collectInformation(@NotNull PsiFile file) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -181,13 +180,17 @@ private boolean ok() throws Throwable {
}

public void testAppLockReadWritePreference() throws Throwable {
ApplicationImpl application = (ApplicationImpl)ApplicationManager.getApplication();
application.assertIsDispatchThread();
assertFalse(application.isWriteAccessAllowed());
assertFalse(application.isWriteActionPending());

// take read lock1.
// try to take write lock - must wait (because of taken read lock)
// try to take read lock2 - must wait (because of write preference - write lock is pending)
// release read lock1 - write lock must be taken first
// release write - read lock2 must be taken
LOG.debug("-----");
ApplicationImpl application = (ApplicationImpl)ApplicationManager.getApplication();
AtomicBoolean holdRead1 = new AtomicBoolean(true);
AtomicBoolean holdWrite = new AtomicBoolean(true);
AtomicBoolean read1Acquired = new AtomicBoolean(false);
Expand All @@ -198,7 +201,7 @@ public void testAppLockReadWritePreference() throws Throwable {
AtomicBoolean writeReleased = new AtomicBoolean(false);
Thread readAction1 = new Thread(() -> {
try {
assertFalse(ApplicationManager.getApplication().isDispatchThread());
assertFalse(application.isDispatchThread());
AccessToken stamp = application.acquireReadActionLock();
try {
LOG.debug("read lock1 acquired");
Expand All @@ -223,10 +226,14 @@ public void testAppLockReadWritePreference() throws Throwable {
// readActions2 should try to acquire read action when write action is pending
Thread readActions2 = new Thread(() -> {
try {
assertFalse(ApplicationManager.getApplication().isDispatchThread());
assertFalse(application.isDispatchThread());
while (!aboutToAcquireWrite.get() && ok());
TimeoutUtil.sleep(1000); // make sure it called writelock
// make sure EDT called writelock
while (!application.myLock.writeRequested && ok());
assertTrue(application.isWriteActionPending());
//assertFalse(application.tryRunReadAction(EmptyRunnable.getInstance()));
AccessToken stamp = application.acquireReadActionLock();
assertFalse(application.isWriteActionPending());
try {
LOG.debug("read lock2 acquired");
read2Acquired.set(true);
Expand All @@ -246,12 +253,13 @@ public void testAppLockReadWritePreference() throws Throwable {

Thread checkThread = new Thread(()->{
try {
assertFalse(ApplicationManager.getApplication().isDispatchThread());
assertFalse(application.isDispatchThread());
while (!aboutToAcquireWrite.get() && ok());
while (!read1Acquired.get() && ok());
TimeoutUtil.sleep(1000); // make sure it called writelock
// make sure EDT called writelock
while (!application.myLock.writeRequested && ok());

long timeout = System.currentTimeMillis() + 10_000;
long timeout = System.currentTimeMillis() + 2_000;
while (System.currentTimeMillis() < timeout && ok()) {
assertTrue(aboutToAcquireWrite.get());
assertTrue(read1Acquired.get());
Expand All @@ -270,7 +278,7 @@ public void testAppLockReadWritePreference() throws Throwable {
holdRead1.set(false);
while (!writeAcquired.get() && ok());

timeout = System.currentTimeMillis() + 10_000;
timeout = System.currentTimeMillis() + 2_000;
while (System.currentTimeMillis() < timeout && ok()) {
assertTrue(aboutToAcquireWrite.get());
assertTrue(read1Acquired.get());
Expand All @@ -290,7 +298,7 @@ public void testAppLockReadWritePreference() throws Throwable {

while (!read2Released.get() && ok());

timeout = System.currentTimeMillis() + 10_000;
timeout = System.currentTimeMillis() + 2_000;
while (System.currentTimeMillis() < timeout && ok()) {
assertTrue(aboutToAcquireWrite.get());
assertTrue(read1Acquired.get());
Expand Down Expand Up @@ -499,10 +507,13 @@ public void testRunProcessWithProgressSynchronouslyInReadAction() throws Throwab
public void testRunProcessWithProgressSynchronouslyInReadActionWithPendingWriteAction() throws Throwable {
//noinspection SSBasedInspection
SwingUtilities.invokeLater(() -> ApplicationManager.getApplication().runWriteAction(EmptyRunnable.getInstance()));
AtomicBoolean ran = new AtomicBoolean();
boolean result = ((ApplicationEx)ApplicationManager.getApplication())
.runProcessWithProgressSynchronouslyInReadAction(getProject(), "title", true, "cancel", null, () -> TimeoutUtil.sleep(10_000));
.runProcessWithProgressSynchronouslyInReadAction(getProject(), "title", true, "cancel", null,
() -> ran.set(true));
assertTrue(result);
UIUtil.dispatchAllInvocationEvents();
assertTrue(ran.get());
if (exception != null) throw exception;
}

Expand Down Expand Up @@ -695,15 +706,18 @@ private static boolean isEscapingThreadAssertion(AssertionError e) {
return e.getMessage().contains("should have been terminated");
}

public void testReadActionInImpatientModeShouldThrowWhenThereIsAPendingWrite() throws ExecutionException, InterruptedException {
public void testReadActionInImpatientModeShouldThrowWhenThereIsAPendingWrite() throws Throwable {
AtomicBoolean stopRead = new AtomicBoolean();
AtomicBoolean readAcquired = new AtomicBoolean();
ApplicationImpl app = (ApplicationImpl)ApplicationManager.getApplication();
Future<?> readAction1 = app.executeOnPooledThread(() ->
app.runReadAction(() -> {
readAcquired.set(true);
try {
while (!stopRead.get()) ;
while (!stopRead.get() && ok());
}
catch (Throwable e) {
exception = e;
}
finally {
readAcquired.set(false);
Expand All @@ -712,50 +726,63 @@ public void testReadActionInImpatientModeShouldThrowWhenThereIsAPendingWrite() t
);
while (!readAcquired.get());
Future<?> readAction2 = app.executeOnPooledThread(() -> {
// wait for write action attempt to start - i.e. app.myLock.writeLock() started to execute
while (!app.myLock.writeRequested);
app.executeByImpatientReader(() -> {
try {
app.runReadAction(EmptyRunnable.getInstance());
fail("Must have been failed");
}
catch (ApplicationUtil.CannotRunReadActionException ignored) {
try {
// wait for write action attempt to start - i.e. app.myLock.writeLock() started to execute
while (!app.myLock.writeRequested && ok());
app.executeByImpatientReader(() -> {
try {
app.runReadAction(EmptyRunnable.getInstance());
fail("Must have been failed");
}
catch (ApplicationUtil.CannotRunReadActionException ignored) {

}
finally {
stopRead.set(true);
}
});
}
catch (Throwable e) {
exception = e;
}
finally {
stopRead.set(true);
}
});
}
catch (Throwable e) {
exception = e;
}
});

app.runWriteAction(EmptyRunnable.getInstance());

readAction2.get();
readAction1.get();
if (exception != null) throw exception;
}

public void testReadActionInImpatientModeMustNotThrowWhenThereIsAPendingWriteAndWeAreUnderNonCancelableSection() throws Exception {
public void testReadActionInImpatientModeMustNotThrowWhenThereIsAPendingWriteAndWeAreUnderNonCancelableSection() throws Throwable {
AtomicBoolean stopRead = new AtomicBoolean();
AtomicBoolean readAcquired = new AtomicBoolean();
ApplicationImpl app = (ApplicationImpl)ApplicationManager.getApplication();
Future<?> readAction1 = app.executeOnPooledThread(() ->
app.runReadAction(() -> {
readAcquired.set(true);
try {
while (!stopRead.get()) ;
}
finally {
readAcquired.set(false);
}
})
app.runReadAction(() -> {
readAcquired.set(true);
try {
while (!stopRead.get() && ok()) ;
}
catch (Throwable e) {
exception = e;
}
finally {
readAcquired.set(false);
}
})
);
while (!readAcquired.get());

AtomicBoolean executingImpatientReader = new AtomicBoolean();

AtomicBoolean readAction2CalledReadAction = new AtomicBoolean();
Future<?> readAction2 = app.executeOnPooledThread(() -> {
// wait for write action attempt to start
while (!app.isWriteActionPending());
while (!app.myLock.writeRequested);
ProgressManager.getInstance().executeNonCancelableSection(()-> app.executeByImpatientReader(() -> {
executingImpatientReader.set(true);
app.runReadAction(EmptyRunnable.getInstance());
Expand All @@ -764,14 +791,21 @@ public void testReadActionInImpatientModeMustNotThrowWhenThereIsAPendingWriteAnd
});

Future<?> readAction1Canceler = app.executeOnPooledThread(() -> {
while (!executingImpatientReader.get());
TimeoutUtil.sleep(300); // make sure readAction2 does call runReadAction()
stopRead.set(true);
try {
while (!executingImpatientReader.get() && ok());
// make sure readAction2 does call runReadAction()
TimeoutUtil.sleep(300);
stopRead.set(true);
}
catch (Throwable throwable) {
exception = throwable;
}
});
app.runWriteAction(EmptyRunnable.getInstance());

readAction1Canceler.get();
readAction2.get();
readAction1.get();
if (exception != null) throw exception;
}
}

0 comments on commit f4378ac

Please sign in to comment.