From ab8f54b0661755a3b5d8afbd18341e15ab4fe38c Mon Sep 17 00:00:00 2001 From: Clebert Suconic Date: Wed, 8 Apr 2015 11:14:21 -0400 Subject: [PATCH] fixing a race on SharedLockFile --- .../activemq/store/SharedFileLocker.java | 2 +- .../activemq/store/SharedFileLockerTest.java | 38 +++++++++++++++++-- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/activemq-broker/src/main/java/org/apache/activemq/store/SharedFileLocker.java b/activemq-broker/src/main/java/org/apache/activemq/store/SharedFileLocker.java index 66c70e2096..36ebe62ddb 100644 --- a/activemq-broker/src/main/java/org/apache/activemq/store/SharedFileLocker.java +++ b/activemq-broker/src/main/java/org/apache/activemq/store/SharedFileLocker.java @@ -44,7 +44,7 @@ public class SharedFileLocker extends AbstractLocker { public void doStart() throws Exception { if (lockFile == null) { File lockFileName = new File(directory, "lock"); - lockFile = new LockFile(lockFileName, true); + lockFile = new LockFile(lockFileName, false); if (failIfLocked) { lockFile.lock(); } else { diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/store/SharedFileLockerTest.java b/activemq-unit-tests/src/test/java/org/apache/activemq/store/SharedFileLockerTest.java index 623443fbbc..ba768c09d9 100644 --- a/activemq-unit-tests/src/test/java/org/apache/activemq/store/SharedFileLockerTest.java +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/store/SharedFileLockerTest.java @@ -45,8 +45,27 @@ public class SharedFileLockerTest } + @Test + public void testLoop() throws Exception + { + // Increase the number of iterations if you are debugging races + for (int i = 0 ; i < 100; i++) + { + internalLoop(5); + } + + } + + @Test public void testLogging() throws Exception + { + // using a bigger wait here + // to make sure we won't log any extra info + internalLoop(100); + } + + private void internalLoop(long timewait) throws Exception { final AtomicInteger logCounts = new AtomicInteger(0); DefaultTestAppender appender = new DefaultTestAppender() { @@ -76,8 +95,6 @@ public class SharedFileLockerTest Assert.assertTrue(locker1.keepAlive()); - Thread.sleep(10); - thread = new Thread("Locker Thread") { public void run() @@ -95,8 +112,21 @@ public class SharedFileLockerTest thread.start(); - // Waiting some small time here, you shouldn't see many messages - Thread.sleep(100); + // I need to make sure the info was already logged + // but I don't want to have an unecessary wait here, + // as I want the test to run as fast as possible + { + long timeout = System.currentTimeMillis() + 5000; + while (logCounts.get() < 1 && System.currentTimeMillis() < timeout) + { + Thread.sleep(1); + } + } + + if (timewait > 0) + { + Thread.sleep(timewait); + } Assert.assertTrue(thread.isAlive());