introduce timeouts

This commit is contained in:
Alexey Kudravtsev
2015-09-24 18:45:12 +03:00
parent 305dae3a13
commit c351671a18
@@ -53,6 +53,7 @@ public class ApplicationImplTest extends PlatformTestCase {
protected void setUp() throws Exception {
super.setUp();
exception = null;
timeOut = System.currentTimeMillis() + 2*60*1000;
}
private volatile Throwable exception;
@@ -187,7 +188,7 @@ public class ApplicationImplTest extends PlatformTestCase {
try {
System.out.println("read lock1 acquired");
read1Acquired.set(true);
while (holdRead1.get());
while (holdRead1.get() && ok());
}
finally {
lock.unlockRead(stamp);
@@ -195,20 +196,20 @@ public class ApplicationImplTest extends PlatformTestCase {
System.out.println("read lock1 released");
}
}
catch (Exception e) {
catch (Throwable e) {
exception = e;
throw e;
throw new RuntimeException(e);
}
}, "read lock1");
readAction1.start();
while (!read1Acquired.get());
while (!read1Acquired.get() && ok());
AtomicBoolean aboutToAcquireWrite = new AtomicBoolean();
// readActions2 should try to acquire read action when write action is pending
Thread readActions2 = new Thread(() -> {
try {
assertFalse(ApplicationManager.getApplication().isDispatchThread());
while (!aboutToAcquireWrite.get());
while (!aboutToAcquireWrite.get() && ok());
TimeoutUtil.sleep(2000); // make sure it called writelock
long stamp = lock.readLock();
try {
@@ -221,9 +222,9 @@ public class ApplicationImplTest extends PlatformTestCase {
System.out.println("read lock2 released");
}
}
catch (Exception e) {
catch (Throwable e) {
exception = e;
throw e;
throw new RuntimeException(e);
}
}, "read lock2");
readActions2.start();
@@ -231,12 +232,12 @@ public class ApplicationImplTest extends PlatformTestCase {
Thread checkThread = new Thread(()->{
try {
assertFalse(ApplicationManager.getApplication().isDispatchThread());
while (!aboutToAcquireWrite.get());
while (!read1Acquired.get());
while (!aboutToAcquireWrite.get() && ok());
while (!read1Acquired.get() && ok());
TimeoutUtil.sleep(2000); // make sure it called writelock
long timeout = System.currentTimeMillis() + 10000;
while (System.currentTimeMillis() < timeout) {
while (System.currentTimeMillis() < timeout && ok()) {
assertTrue(aboutToAcquireWrite.get());
assertTrue(read1Acquired.get());
assertFalse(read1Released.get());
@@ -250,10 +251,10 @@ public class ApplicationImplTest extends PlatformTestCase {
}
holdRead1.set(false);
while (!writeAcquired.get());
while (!writeAcquired.get() && ok());
timeout = System.currentTimeMillis() + 10000;
while (System.currentTimeMillis() < timeout) {
while (System.currentTimeMillis() < timeout && ok()) {
assertTrue(aboutToAcquireWrite.get());
assertTrue(read1Acquired.get());
assertTrue(read1Released.get());
@@ -267,11 +268,11 @@ public class ApplicationImplTest extends PlatformTestCase {
holdWrite.set(false);
while (!read2Acquired.get());
while (!read2Acquired.get() && ok());
TimeoutUtil.sleep(1000); // wait for immediate release of read lock2
timeout = System.currentTimeMillis() + 10000;
while (System.currentTimeMillis() < timeout) {
while (System.currentTimeMillis() < timeout && ok()) {
assertTrue(aboutToAcquireWrite.get());
assertTrue(read1Acquired.get());
assertTrue(read1Released.get());
@@ -281,9 +282,9 @@ public class ApplicationImplTest extends PlatformTestCase {
assertTrue(writeReleased.get());
}
}
catch (Exception e) {
catch (Throwable e) {
exception = e;
throw e;
throw new RuntimeException(e);
}
}, "check");
checkThread.start();
@@ -293,7 +294,7 @@ public class ApplicationImplTest extends PlatformTestCase {
try {
System.out.println("write lock acquired");
writeAcquired.set(true);
while (holdWrite.get() && exception == null);
while (holdWrite.get() && ok());
}
finally {
lock.unlockWrite(stamp);
@@ -307,6 +308,12 @@ public class ApplicationImplTest extends PlatformTestCase {
if (exception != null) throw exception;
}
private static long timeOut;
private boolean ok() throws Throwable {
if (exception != null) throw exception;
return System.currentTimeMillis() < timeOut;
}
public void testAppLockReadWritePreference() throws Throwable {
// take read lock1.
// try to take write lock - must wait (because of taken read lock)
@@ -330,7 +337,7 @@ public class ApplicationImplTest extends PlatformTestCase {
try {
System.out.println("read lock1 acquired");
read1Acquired.set(true);
while (holdRead1.get());
while (holdRead1.get() && ok());
}
finally {
stamp.finish();
@@ -338,20 +345,20 @@ public class ApplicationImplTest extends PlatformTestCase {
System.out.println("read lock1 released");
}
}
catch (Exception e) {
catch (Throwable e) {
exception = e;
throw e;
throw new RuntimeException(e);
}
}, "read lock1");
readAction1.start();
while (!read1Acquired.get());
while (!read1Acquired.get() && ok());
AtomicBoolean aboutToAcquireWrite = new AtomicBoolean();
// readActions2 should try to acquire read action when write action is pending
Thread readActions2 = new Thread(() -> {
try {
assertFalse(ApplicationManager.getApplication().isDispatchThread());
while (!aboutToAcquireWrite.get());
while (!aboutToAcquireWrite.get() && ok());
TimeoutUtil.sleep(2000); // make sure it called writelock
AccessToken stamp = application.acquireReadActionLock();
try {
@@ -364,9 +371,9 @@ public class ApplicationImplTest extends PlatformTestCase {
System.out.println("read lock2 released");
}
}
catch (Exception e) {
catch (Throwable e) {
exception = e;
throw e;
throw new RuntimeException(e);
}
}, "read lock2");
readActions2.start();
@@ -374,12 +381,12 @@ public class ApplicationImplTest extends PlatformTestCase {
Thread checkThread = new Thread(()->{
try {
assertFalse(ApplicationManager.getApplication().isDispatchThread());
while (!aboutToAcquireWrite.get());
while (!read1Acquired.get());
while (!aboutToAcquireWrite.get() && ok());
while (!read1Acquired.get() && ok());
TimeoutUtil.sleep(2000); // make sure it called writelock
long timeout = System.currentTimeMillis() + 10000;
while (System.currentTimeMillis() < timeout) {
while (System.currentTimeMillis() < timeout && ok()) {
assertTrue(aboutToAcquireWrite.get());
assertTrue(read1Acquired.get());
assertFalse(read1Released.get());
@@ -392,10 +399,10 @@ public class ApplicationImplTest extends PlatformTestCase {
}
holdRead1.set(false);
while (!writeAcquired.get());
while (!writeAcquired.get() && ok());
timeout = System.currentTimeMillis() + 10000;
while (System.currentTimeMillis() < timeout) {
while (System.currentTimeMillis() < timeout && ok()) {
assertTrue(aboutToAcquireWrite.get());
assertTrue(read1Acquired.get());
assertTrue(read1Released.get());
@@ -409,11 +416,11 @@ public class ApplicationImplTest extends PlatformTestCase {
holdWrite.set(false);
while (!read2Acquired.get());
while (!read2Acquired.get() && ok());
TimeoutUtil.sleep(1000); // wait for immediate release of read lock2
timeout = System.currentTimeMillis() + 10000;
while (System.currentTimeMillis() < timeout) {
while (System.currentTimeMillis() < timeout && ok()) {
assertTrue(aboutToAcquireWrite.get());
assertTrue(read1Acquired.get());
assertTrue(read1Released.get());
@@ -423,9 +430,9 @@ public class ApplicationImplTest extends PlatformTestCase {
assertTrue(writeReleased.get());
}
}
catch (Exception e) {
catch (Throwable e) {
exception = e;
throw e;
throw new RuntimeException(e);
}
}, "check");
checkThread.start();
@@ -435,7 +442,7 @@ public class ApplicationImplTest extends PlatformTestCase {
try {
System.out.println("write lock acquired");
writeAcquired.set(true);
while (holdWrite.get() && exception == null);
while (holdWrite.get() && ok());
}
finally {
stamp.finish();
@@ -462,10 +469,10 @@ public class ApplicationImplTest extends PlatformTestCase {
final StringBuffer LOG = new StringBuffer();
new Thread(() -> {
try {
ApplicationManager.getApplication().runReadAction(() -> {
ApplicationManager.getApplication().runReadAction((ThrowableComputable<Object, Throwable>)() -> {
LOG.append("inside read action\n");
readStarted = true;
while (!tryingToStartWriteAction);
while (!tryingToStartWriteAction && ok());
TimeoutUtil.sleep(100);
for (int i = 0; i < anotherReadActionStarted.length; i++) {
@@ -490,7 +497,7 @@ public class ApplicationImplTest extends PlatformTestCase {
}
for (AtomicBoolean threadStarted : anotherThreadStarted) {
while (!threadStarted.get()) ;
while (!threadStarted.get() && ok()) ;
}
// now the other threads try to get read lock. we should not let them
for (int i=0; i<10; i++) {
@@ -500,6 +507,7 @@ public class ApplicationImplTest extends PlatformTestCase {
TimeoutUtil.sleep(20);
}
LOG.append("\nfinished read action");
return null;
});
}
catch (Throwable e) {
@@ -508,7 +516,7 @@ public class ApplicationImplTest extends PlatformTestCase {
}, "read").start();
while (!readStarted);
while (!readStarted && ok());
tryingToStartWriteAction = true;
LOG.append("\nwrite about to start");
ApplicationManager.getApplication().runWriteAction(() -> {