From 879f699b2b6720b75a47ac7768e79fc34fa07bcd Mon Sep 17 00:00:00 2001
From: akwizgran <michael@briarproject.org>
Date: Fri, 17 Nov 2017 15:47:37 +0000
Subject: [PATCH] A few more lambdas.

---
 .../bramble/db/H2DatabaseTest.java            |  42 ++---
 .../bramble/db/LockFairnessTest.java          | 175 ++++++++----------
 2 files changed, 98 insertions(+), 119 deletions(-)

diff --git a/bramble-core/src/test/java/org/briarproject/bramble/db/H2DatabaseTest.java b/bramble-core/src/test/java/org/briarproject/bramble/db/H2DatabaseTest.java
index 7eba28093f..4afea4bf42 100644
--- a/bramble-core/src/test/java/org/briarproject/bramble/db/H2DatabaseTest.java
+++ b/bramble-core/src/test/java/org/briarproject/bramble/db/H2DatabaseTest.java
@@ -473,19 +473,16 @@ public class H2DatabaseTest extends BrambleTestCase {
 		// Start a transaction
 		Connection txn = db.startTransaction();
 		// In another thread, close the database
-		Thread close = new Thread() {
-			@Override
-			public void run() {
-				try {
-					closing.countDown();
-					db.close();
-					if (!transactionFinished.get()) error.set(true);
-					closed.countDown();
-				} catch (Exception e) {
-					error.set(true);
-				}
+		Thread close = new Thread(() -> {
+			try {
+				closing.countDown();
+				db.close();
+				if (!transactionFinished.get()) error.set(true);
+				closed.countDown();
+			} catch (Exception e) {
+				error.set(true);
 			}
-		};
+		});
 		close.start();
 		closing.await();
 		// Do whatever the transaction needs to do
@@ -510,19 +507,16 @@ public class H2DatabaseTest extends BrambleTestCase {
 		// Start a transaction
 		Connection txn = db.startTransaction();
 		// In another thread, close the database
-		Thread close = new Thread() {
-			@Override
-			public void run() {
-				try {
-					closing.countDown();
-					db.close();
-					if (!transactionFinished.get()) error.set(true);
-					closed.countDown();
-				} catch (Exception e) {
-					error.set(true);
-				}
+		Thread close = new Thread(() -> {
+			try {
+				closing.countDown();
+				db.close();
+				if (!transactionFinished.get()) error.set(true);
+				closed.countDown();
+			} catch (Exception e) {
+				error.set(true);
 			}
-		};
+		});
 		close.start();
 		closing.await();
 		// Do whatever the transaction needs to do
diff --git a/bramble-core/src/test/java/org/briarproject/bramble/db/LockFairnessTest.java b/bramble-core/src/test/java/org/briarproject/bramble/db/LockFairnessTest.java
index 161e2a2631..7c89c0f08c 100644
--- a/bramble-core/src/test/java/org/briarproject/bramble/db/LockFairnessTest.java
+++ b/bramble-core/src/test/java/org/briarproject/bramble/db/LockFairnessTest.java
@@ -23,50 +23,44 @@ public class LockFairnessTest extends BrambleTestCase {
 		CountDownLatch secondReaderHasLock = new CountDownLatch(1);
 		CountDownLatch secondReaderHasFinished = new CountDownLatch(1);
 		// First reader
-		Thread first = new Thread() {
-			@Override
-			public void run() {
+		Thread first = new Thread(() -> {
+			try {
+				// Acquire the lock
+				lock.readLock().lock();
 				try {
-					// Acquire the lock
-					lock.readLock().lock();
-					try {
-						// Allow the second reader to acquire the lock
-						firstReaderHasLock.countDown();
-						// Wait for the second reader to acquire the lock
-						assertTrue(secondReaderHasLock.await(10, SECONDS));
-					} finally {
-						// Release the lock
-						lock.readLock().unlock();
-					}
-				} catch (InterruptedException e) {
-					fail();
+					// Allow the second reader to acquire the lock
+					firstReaderHasLock.countDown();
+					// Wait for the second reader to acquire the lock
+					assertTrue(secondReaderHasLock.await(10, SECONDS));
+				} finally {
+					// Release the lock
+					lock.readLock().unlock();
 				}
-				firstReaderHasFinished.countDown();
+			} catch (InterruptedException e) {
+				fail();
 			}
-		};
+			firstReaderHasFinished.countDown();
+		});
 		first.start();
 		// Second reader
-		Thread second = new Thread() {
-			@Override
-			public void run() {
+		Thread second = new Thread(() -> {
+			try {
+				// Wait for the first reader to acquire the lock
+				assertTrue(firstReaderHasLock.await(10, SECONDS));
+				// Acquire the lock
+				lock.readLock().lock();
 				try {
-					// Wait for the first reader to acquire the lock
-					assertTrue(firstReaderHasLock.await(10, SECONDS));
-					// Acquire the lock
-					lock.readLock().lock();
-					try {
-						// Allow the first reader to release the lock
-						secondReaderHasLock.countDown();
-					} finally {
-						// Release the lock
-						lock.readLock().unlock();
-					}
-				} catch (InterruptedException e) {
-					fail();
+					// Allow the first reader to release the lock
+					secondReaderHasLock.countDown();
+				} finally {
+					// Release the lock
+					lock.readLock().unlock();
 				}
-				secondReaderHasFinished.countDown();
+			} catch (InterruptedException e) {
+				fail();
 			}
-		};
+			secondReaderHasFinished.countDown();
+		});
 		second.start();
 		// Wait for both readers to finish
 		assertTrue(firstReaderHasFinished.await(10, SECONDS));
@@ -84,78 +78,69 @@ public class LockFairnessTest extends BrambleTestCase {
 		AtomicBoolean secondReaderHasHeldLock = new AtomicBoolean(false);
 		AtomicBoolean writerHasHeldLock = new AtomicBoolean(false);
 		// First reader
-		Thread first = new Thread() {
-			@Override
-			public void run() {
+		Thread first = new Thread(() -> {
+			try {
+				// Acquire the lock
+				lock.readLock().lock();
 				try {
-					// Acquire the lock
-					lock.readLock().lock();
-					try {
-						// Allow the other threads to acquire the lock
-						firstReaderHasLock.countDown();
-						// Wait for both other threads to wait for the lock
-						while (lock.getQueueLength() < 2) Thread.sleep(10);
-						// No other thread should have acquired the lock
-						assertFalse(secondReaderHasHeldLock.get());
-						assertFalse(writerHasHeldLock.get());
-					} finally {
-						// Release the lock
-						lock.readLock().unlock();
-					}
-				} catch (InterruptedException e) {
-					fail();
+					// Allow the other threads to acquire the lock
+					firstReaderHasLock.countDown();
+					// Wait for both other threads to wait for the lock
+					while (lock.getQueueLength() < 2) Thread.sleep(10);
+					// No other thread should have acquired the lock
+					assertFalse(secondReaderHasHeldLock.get());
+					assertFalse(writerHasHeldLock.get());
+				} finally {
+					// Release the lock
+					lock.readLock().unlock();
 				}
-				firstReaderHasFinished.countDown();
+			} catch (InterruptedException e) {
+				fail();
 			}
-		};
+			firstReaderHasFinished.countDown();
+		});
 		first.start();
 		// Writer
-		Thread writer = new Thread() {
-			@Override
-			public void run() {
+		Thread writer = new Thread(() -> {
+			try {
+				// Wait for the first reader to acquire the lock
+				assertTrue(firstReaderHasLock.await(10, SECONDS));
+				// Acquire the lock
+				lock.writeLock().lock();
 				try {
-					// Wait for the first reader to acquire the lock
-					assertTrue(firstReaderHasLock.await(10, SECONDS));
-					// Acquire the lock
-					lock.writeLock().lock();
-					try {
-						writerHasHeldLock.set(true);
-						// The second reader should not overtake the writer
-						assertFalse(secondReaderHasHeldLock.get());
-					} finally {
-						lock.writeLock().unlock();
-					}
-				} catch (InterruptedException e) {
-					fail();
+					writerHasHeldLock.set(true);
+					// The second reader should not overtake the writer
+					assertFalse(secondReaderHasHeldLock.get());
+				} finally {
+					lock.writeLock().unlock();
 				}
-				writerHasFinished.countDown();
+			} catch (InterruptedException e) {
+				fail();
 			}
-		};
+			writerHasFinished.countDown();
+		});
 		writer.start();
 		// Second reader
-		Thread second = new Thread() {
-			@Override
-			public void run() {
+		Thread second = new Thread(() -> {
+			try {
+				// 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 {
-					// 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 {
-						secondReaderHasHeldLock.set(true);
-						// The second reader should not overtake the writer
-						assertTrue(writerHasHeldLock.get());
-					} finally {
-						lock.readLock().unlock();
-					}
-				} catch (InterruptedException e) {
-					fail();
+					secondReaderHasHeldLock.set(true);
+					// The second reader should not overtake the writer
+					assertTrue(writerHasHeldLock.get());
+				} finally {
+					lock.readLock().unlock();
 				}
-				secondReaderHasFinished.countDown();
+			} catch (InterruptedException e) {
+				fail();
 			}
-		};
+			secondReaderHasFinished.countDown();
+		});
 		second.start();
 		// Wait for all the threads to finish
 		assertTrue(firstReaderHasFinished.await(10, SECONDS));
-- 
GitLab