<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
+ <!-- jvm holds lock on file so use a new one per test -->
+ <reuseForks>false</reuseForks>
<systemPropertyVariables>
<buildDirectory>${project.build.directory}</buildDirectory>
<java.io.tmpdir>${project.build.directory}</java.io.tmpdir>
// TODO currently we create lock for read and write!!
// the idea could be to store lock here with various clients read/write
// only read could be a more simple lock and acquire a write lock means waiting the end of all reading threads
- //private static final ConcurrentMap<File, Lock> lockFiles = new ConcurrentHashMap<File, Lock>( 64 );
+ private static final ConcurrentMap<File, Lock> lockFiles = new ConcurrentHashMap<File, Lock>( 64 );
private boolean skipLocking = false;
mkdirs( file.getParentFile() );
try
{
+
Lock lock = new Lock( file, false );
stopWatch.start();
while ( !acquired )
{
+
if ( timeout > 0 )
{
long delta = stopWatch.getTime();
throw new FileLockTimeoutException();
}
}
+
+ Lock current = lockFiles.get( file );
+
+ if ( current != null )
+ {
+ log.debug( "read lock file exist continue wait" );
+ continue;
+ }
+
try
{
+ file.createNewFile();
lock.openLock( false, timeout > 0 );
acquired = true;
}
log.debug( "openLock {}:{}", e.getClass(), e.getMessage() );
}
}
+ Lock current = lockFiles.putIfAbsent( file, lock );
+ if ( current != null )
+ {
+ lock = current;
+ }
return lock;
}
- catch ( FileNotFoundException e )
+ catch ( IOException e )
{
throw new FileLockException( e.getMessage(), e );
}
throw new FileLockTimeoutException();
}
}
+
+ Lock current = lockFiles.get( file );
+
+ if ( current != null )
+ {
+ log.debug( "write lock file exist continue wait" );
+ continue;
+ }
+
try
{
+ file.createNewFile();
lock.openLock( true, timeout > 0 );
acquired = true;
}
log.debug( "openLock {}:{}", e.getClass(), e.getMessage() );
}
}
+
+ Lock current = lockFiles.putIfAbsent( file, lock );
+ if ( current != null )
+ {
+ lock = current;
+ }
+
return lock;
}
catch ( FileNotFoundException e )
}
try
{
+ lockFiles.remove( lock.getFile() );
lock.close();
}
catch ( IOException e )
}
}
+ public void clearLockFiles()
+ {
+ lockFiles.clear();
+ }
+
private boolean mkdirs( File directory )
{
if ( directory == null )
void release( Lock lock )
throws FileLockException, FileNotFoundException;
+ void clearLockFiles();
+
int getTimeout();
void setTimeout( int timeout );
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.junit.Assert;
+import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
/**
* @author Olivier Lamy
*/
-@RunWith( SpringJUnit4ClassRunner.class )
-@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml" } )
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml" })
public class DefaultFileLockManagerTest
{
final Logger logger = LoggerFactory.getLogger( getClass() );
@Inject
- @Named( value = "fileLockManager#default" )
+ @Named(value = "fileLockManager#default")
FileLockManager fileLockManager;
class ConcurrentFileWrite
}
+
+ @Before
+ public void initialize()
+ {
+ fileLockManager.setSkipLocking( false );
+ fileLockManager.clearLockFiles();
+ }
+
@Test
public void testWrite()
throws Throwable
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.junit.Assert;
+import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
/**
* @author Olivier Lamy
*/
-@RunWith( SpringJUnit4ClassRunner.class )
-@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml" } )
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml" })
public class DefaultFileLockManagerTimeoutTest
{
final Logger logger = LoggerFactory.getLogger( getClass() );
@Inject
- @Named( value = "fileLockManager#default" )
+ @Named(value = "fileLockManager#default")
FileLockManager fileLockManager;
+ @Before
+ public void initialize()
+ {
+ fileLockManager.setSkipLocking( false );
+
+ fileLockManager.setTimeout( 5000 );
+ fileLockManager.clearLockFiles();
+ }
@Test( expected = FileLockTimeoutException.class )
public void testTimeout()
File largeJar = new File( System.getProperty( "basedir" ), "src/test/cassandra-all-2.0.3.jar" );
- fileLockManager.setTimeout( 5000 );
-
Lock lock = fileLockManager.writeFileLock( file );
FileUtils.copyFile( largeJar, lock.getFile() );