mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 21:29:54 +01:00
A few more lambdas.
This commit is contained in:
@@ -473,19 +473,16 @@ public class H2DatabaseTest extends BrambleTestCase {
|
|||||||
// Start a transaction
|
// Start a transaction
|
||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
// In another thread, close the database
|
// In another thread, close the database
|
||||||
Thread close = new Thread() {
|
Thread close = new Thread(() -> {
|
||||||
@Override
|
try {
|
||||||
public void run() {
|
closing.countDown();
|
||||||
try {
|
db.close();
|
||||||
closing.countDown();
|
if (!transactionFinished.get()) error.set(true);
|
||||||
db.close();
|
closed.countDown();
|
||||||
if (!transactionFinished.get()) error.set(true);
|
} catch (Exception e) {
|
||||||
closed.countDown();
|
error.set(true);
|
||||||
} catch (Exception e) {
|
|
||||||
error.set(true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
close.start();
|
close.start();
|
||||||
closing.await();
|
closing.await();
|
||||||
// Do whatever the transaction needs to do
|
// Do whatever the transaction needs to do
|
||||||
@@ -510,19 +507,16 @@ public class H2DatabaseTest extends BrambleTestCase {
|
|||||||
// Start a transaction
|
// Start a transaction
|
||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
// In another thread, close the database
|
// In another thread, close the database
|
||||||
Thread close = new Thread() {
|
Thread close = new Thread(() -> {
|
||||||
@Override
|
try {
|
||||||
public void run() {
|
closing.countDown();
|
||||||
try {
|
db.close();
|
||||||
closing.countDown();
|
if (!transactionFinished.get()) error.set(true);
|
||||||
db.close();
|
closed.countDown();
|
||||||
if (!transactionFinished.get()) error.set(true);
|
} catch (Exception e) {
|
||||||
closed.countDown();
|
error.set(true);
|
||||||
} catch (Exception e) {
|
|
||||||
error.set(true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
close.start();
|
close.start();
|
||||||
closing.await();
|
closing.await();
|
||||||
// Do whatever the transaction needs to do
|
// Do whatever the transaction needs to do
|
||||||
|
|||||||
@@ -23,50 +23,44 @@ public class LockFairnessTest extends BrambleTestCase {
|
|||||||
CountDownLatch secondReaderHasLock = new CountDownLatch(1);
|
CountDownLatch secondReaderHasLock = new CountDownLatch(1);
|
||||||
CountDownLatch secondReaderHasFinished = new CountDownLatch(1);
|
CountDownLatch secondReaderHasFinished = new CountDownLatch(1);
|
||||||
// First reader
|
// First reader
|
||||||
Thread first = new Thread() {
|
Thread first = new Thread(() -> {
|
||||||
@Override
|
try {
|
||||||
public void run() {
|
// Acquire the lock
|
||||||
|
lock.readLock().lock();
|
||||||
try {
|
try {
|
||||||
// Acquire the lock
|
// Allow the second reader to acquire the lock
|
||||||
lock.readLock().lock();
|
firstReaderHasLock.countDown();
|
||||||
try {
|
// Wait for the second reader to acquire the lock
|
||||||
// Allow the second reader to acquire the lock
|
assertTrue(secondReaderHasLock.await(10, SECONDS));
|
||||||
firstReaderHasLock.countDown();
|
} finally {
|
||||||
// Wait for the second reader to acquire the lock
|
// Release the lock
|
||||||
assertTrue(secondReaderHasLock.await(10, SECONDS));
|
lock.readLock().unlock();
|
||||||
} finally {
|
|
||||||
// Release the lock
|
|
||||||
lock.readLock().unlock();
|
|
||||||
}
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
fail();
|
|
||||||
}
|
}
|
||||||
firstReaderHasFinished.countDown();
|
} catch (InterruptedException e) {
|
||||||
|
fail();
|
||||||
}
|
}
|
||||||
};
|
firstReaderHasFinished.countDown();
|
||||||
|
});
|
||||||
first.start();
|
first.start();
|
||||||
// Second reader
|
// Second reader
|
||||||
Thread second = new Thread() {
|
Thread second = new Thread(() -> {
|
||||||
@Override
|
try {
|
||||||
public void run() {
|
// Wait for the first reader to acquire the lock
|
||||||
|
assertTrue(firstReaderHasLock.await(10, SECONDS));
|
||||||
|
// Acquire the lock
|
||||||
|
lock.readLock().lock();
|
||||||
try {
|
try {
|
||||||
// Wait for the first reader to acquire the lock
|
// Allow the first reader to release the lock
|
||||||
assertTrue(firstReaderHasLock.await(10, SECONDS));
|
secondReaderHasLock.countDown();
|
||||||
// Acquire the lock
|
} finally {
|
||||||
lock.readLock().lock();
|
// Release the lock
|
||||||
try {
|
lock.readLock().unlock();
|
||||||
// Allow the first reader to release the lock
|
|
||||||
secondReaderHasLock.countDown();
|
|
||||||
} finally {
|
|
||||||
// Release the lock
|
|
||||||
lock.readLock().unlock();
|
|
||||||
}
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
fail();
|
|
||||||
}
|
}
|
||||||
secondReaderHasFinished.countDown();
|
} catch (InterruptedException e) {
|
||||||
|
fail();
|
||||||
}
|
}
|
||||||
};
|
secondReaderHasFinished.countDown();
|
||||||
|
});
|
||||||
second.start();
|
second.start();
|
||||||
// Wait for both readers to finish
|
// Wait for both readers to finish
|
||||||
assertTrue(firstReaderHasFinished.await(10, SECONDS));
|
assertTrue(firstReaderHasFinished.await(10, SECONDS));
|
||||||
@@ -84,78 +78,69 @@ public class LockFairnessTest extends BrambleTestCase {
|
|||||||
AtomicBoolean secondReaderHasHeldLock = new AtomicBoolean(false);
|
AtomicBoolean secondReaderHasHeldLock = new AtomicBoolean(false);
|
||||||
AtomicBoolean writerHasHeldLock = new AtomicBoolean(false);
|
AtomicBoolean writerHasHeldLock = new AtomicBoolean(false);
|
||||||
// First reader
|
// First reader
|
||||||
Thread first = new Thread() {
|
Thread first = new Thread(() -> {
|
||||||
@Override
|
try {
|
||||||
public void run() {
|
// Acquire the lock
|
||||||
|
lock.readLock().lock();
|
||||||
try {
|
try {
|
||||||
// Acquire the lock
|
// Allow the other threads to acquire the lock
|
||||||
lock.readLock().lock();
|
firstReaderHasLock.countDown();
|
||||||
try {
|
// Wait for both other threads to wait for the lock
|
||||||
// Allow the other threads to acquire the lock
|
while (lock.getQueueLength() < 2) Thread.sleep(10);
|
||||||
firstReaderHasLock.countDown();
|
// No other thread should have acquired the lock
|
||||||
// Wait for both other threads to wait for the lock
|
assertFalse(secondReaderHasHeldLock.get());
|
||||||
while (lock.getQueueLength() < 2) Thread.sleep(10);
|
assertFalse(writerHasHeldLock.get());
|
||||||
// No other thread should have acquired the lock
|
} finally {
|
||||||
assertFalse(secondReaderHasHeldLock.get());
|
// Release the lock
|
||||||
assertFalse(writerHasHeldLock.get());
|
lock.readLock().unlock();
|
||||||
} finally {
|
|
||||||
// Release the lock
|
|
||||||
lock.readLock().unlock();
|
|
||||||
}
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
fail();
|
|
||||||
}
|
}
|
||||||
firstReaderHasFinished.countDown();
|
} catch (InterruptedException e) {
|
||||||
|
fail();
|
||||||
}
|
}
|
||||||
};
|
firstReaderHasFinished.countDown();
|
||||||
|
});
|
||||||
first.start();
|
first.start();
|
||||||
// Writer
|
// Writer
|
||||||
Thread writer = new Thread() {
|
Thread writer = new Thread(() -> {
|
||||||
@Override
|
try {
|
||||||
public void run() {
|
// Wait for the first reader to acquire the lock
|
||||||
|
assertTrue(firstReaderHasLock.await(10, SECONDS));
|
||||||
|
// Acquire the lock
|
||||||
|
lock.writeLock().lock();
|
||||||
try {
|
try {
|
||||||
// Wait for the first reader to acquire the lock
|
writerHasHeldLock.set(true);
|
||||||
assertTrue(firstReaderHasLock.await(10, SECONDS));
|
// The second reader should not overtake the writer
|
||||||
// Acquire the lock
|
assertFalse(secondReaderHasHeldLock.get());
|
||||||
lock.writeLock().lock();
|
} finally {
|
||||||
try {
|
lock.writeLock().unlock();
|
||||||
writerHasHeldLock.set(true);
|
|
||||||
// The second reader should not overtake the writer
|
|
||||||
assertFalse(secondReaderHasHeldLock.get());
|
|
||||||
} finally {
|
|
||||||
lock.writeLock().unlock();
|
|
||||||
}
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
fail();
|
|
||||||
}
|
}
|
||||||
writerHasFinished.countDown();
|
} catch (InterruptedException e) {
|
||||||
|
fail();
|
||||||
}
|
}
|
||||||
};
|
writerHasFinished.countDown();
|
||||||
|
});
|
||||||
writer.start();
|
writer.start();
|
||||||
// Second reader
|
// Second reader
|
||||||
Thread second = new Thread() {
|
Thread second = new Thread(() -> {
|
||||||
@Override
|
try {
|
||||||
public void run() {
|
// Wait for the first reader to acquire the lock
|
||||||
|
assertTrue(firstReaderHasLock.await(10, SECONDS));
|
||||||
|
// Wait for the writer to wait for the lock
|
||||||
|
while (lock.getQueueLength() < 1) Thread.sleep(10);
|
||||||
|
// Acquire the lock
|
||||||
|
lock.readLock().lock();
|
||||||
try {
|
try {
|
||||||
// Wait for the first reader to acquire the lock
|
secondReaderHasHeldLock.set(true);
|
||||||
assertTrue(firstReaderHasLock.await(10, SECONDS));
|
// The second reader should not overtake the writer
|
||||||
// Wait for the writer to wait for the lock
|
assertTrue(writerHasHeldLock.get());
|
||||||
while (lock.getQueueLength() < 1) Thread.sleep(10);
|
} finally {
|
||||||
// Acquire the lock
|
lock.readLock().unlock();
|
||||||
lock.readLock().lock();
|
|
||||||
try {
|
|
||||||
secondReaderHasHeldLock.set(true);
|
|
||||||
// The second reader should not overtake the writer
|
|
||||||
assertTrue(writerHasHeldLock.get());
|
|
||||||
} finally {
|
|
||||||
lock.readLock().unlock();
|
|
||||||
}
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
fail();
|
|
||||||
}
|
}
|
||||||
secondReaderHasFinished.countDown();
|
} catch (InterruptedException e) {
|
||||||
|
fail();
|
||||||
}
|
}
|
||||||
};
|
secondReaderHasFinished.countDown();
|
||||||
|
});
|
||||||
second.start();
|
second.start();
|
||||||
// Wait for all the threads to finish
|
// Wait for all the threads to finish
|
||||||
assertTrue(firstReaderHasFinished.await(10, SECONDS));
|
assertTrue(firstReaderHasFinished.await(10, SECONDS));
|
||||||
|
|||||||
Reference in New Issue
Block a user