fix timeout

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1550562 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Olivier Lamy 2013-12-12 22:37:34 +00:00
parent 7fc6fb2158
commit 79329d262e
4 changed files with 88 additions and 3 deletions

View File

@ -41,6 +41,11 @@
</exclusions>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>

View File

@ -19,10 +19,10 @@
* under the License.
*/
import org.apache.commons.lang.time.StopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;
import java.io.File;
import java.io.FileNotFoundException;
@ -67,7 +67,8 @@ public Lock readFileLock( File file )
{
if ( timeout > 0 )
{
long delta = stopWatch.getTotalTimeMillis();
long delta = stopWatch.getTime();
log.debug( "delta {}, timeout {}", delta, timeout );
if ( delta > timeout )
{
log.warn( "Cannot acquire read lock within {} millis. Will skip the file: {}", timeout, file );
@ -120,7 +121,8 @@ public Lock writeFileLock( File file )
{
if ( timeout > 0 )
{
long delta = stopWatch.getTotalTimeMillis();
long delta = stopWatch.getTime();
log.debug( "delta {}, timeout {}", delta, timeout );
if ( delta > timeout )
{
log.warn( "Cannot acquire read lock within {} millis. Will skip the file: {}", timeout, file );

View File

@ -274,4 +274,5 @@ public void testWrite()
Assert.assertEquals( 10, concurrentFileWrite.success.intValue() );
}
}

View File

@ -0,0 +1,77 @@
package org.apache.archiva.common.filelock;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import edu.umd.cs.mtc.MultithreadedTestCase;
import edu.umd.cs.mtc.TestFramework;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author Olivier Lamy
*/
@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" )
FileLockManager fileLockManager;
@Test( expected = FileLockTimeoutException.class )
public void testTimeout()
throws Throwable
{
File file = new File( System.getProperty( "buildDirectory" ), "foo.txt" );
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() );
lock = fileLockManager.writeFileLock( file );
}
}