dimanche 1 mars 2015

Neo4j tests and lock file issue

My application is written with Neo4j and Spring/Spring Boot/REST Spring MVC. I have a bunch of unit/integration tests. Everything work properly when I'm using for tests Neo4j Server via REST but when I'm using Embedded Neo4j I continuously get following exception:



Caused by: java.io.IOException: Couldn't lock lock file test-db\lock because another process already holds the lock.
at org.neo4j.kernel.impl.nioneo.store.FileLock.getLockFileBasedFileLock(FileLock.java:120)
at org.neo4j.kernel.impl.nioneo.store.FileLock.getOsSpecificFileLock(FileLock.java:64)
at org.neo4j.kernel.DefaultFileSystemAbstraction.tryLock(DefaultFileSystemAbstraction.java:93)
at org.neo4j.kernel.StoreLocker.checkLock(StoreLocker.java:74)
... 167 common frames omitted


The second test is always fails.. Obviously, some resources(Neo4j, web application) are not correctly released between tests invocation. Right now I have no idea how to correctly release resources..


This is my Neo4Config:



@Configuration
@EnableNeo4jRepositories(basePackages = "com.example")
@EnableTransactionManagement(mode = AdviceMode.ASPECTJ)
public class Neo4jConfig extends Neo4jConfiguration implements BeanFactoryAware {

private static final String NEO4J_EMBEDDED_DATABASE_PATH_PROPERTY = "neo4j.embedded.database.path";
private static final String NEO4J_SERVER_DATABASE_URI_PROPERTY = "neo4j.server.database.uri";

@Resource
private Environment environment;

private BeanFactory beanFactory;

public Neo4jConfig() {
setBasePackage("com.example");
}

@Bean(destroyMethod = "shutdown")
public GraphDatabaseService graphDatabaseService() {
return new GraphDatabaseFactory().newEmbeddedDatabase(environment.getProperty(NEO4J_EMBEDDED_DATABASE_PATH_PROPERTY));
// return new SpringRestGraphDatabase(environment.getProperty(NEO4J_SERVER_DATABASE_URI_PROPERTY));
}

@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}

public BeanFactory getBeanFactory() {
return beanFactory;
}

}


This is my Application main class:



@PropertySources({ @PropertySource(value = "classpath:application.properties") })
@ComponentScan("com.example")
@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}


And for example, one of my tests



@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@SpringApplicationConfiguration(classes = Application.class)
@IntegrationTest("server.port: 0")
public class UsersControllerTest {

private final static String USERNAME = "username";
private final static String PASSWORD = "password";
private final static String EMAIL = "test@test.com";

@Value("${local.server.port}")
protected int port;

private RestTemplate restTemplate = new RestTemplate();

@Before
public void setUp() {
restTemplate.setErrorHandler(new DefaultResponseErrorHandler());
restTemplate.delete(format("http://localhost:%d/users/delete", port));
}

@After
public void tearDown() {
restTemplate.setErrorHandler(new DefaultResponseErrorHandler());
restTemplate.delete(format("http://localhost:%d/users/delete", port));
}

@Test
public void createUser() {
restTemplate.setErrorHandler(new ResponseErrorHandler() {

@Override
public boolean hasError(ClientHttpResponse response) throws IOException {
return RestUtils.isError(response.getStatusCode());
}

@Override
public void handleError(ClientHttpResponse response) throws IOException {
Assert.assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
}

});

User user = new User(USERNAME, PASSWORD, EMAIL);

Assert.assertNull(user.getId());
Assert.assertNull(user.getCreateDate());

User registeredUser = restTemplate.postForObject(format("http://localhost:%d/users/create", port), user, User.class);

Assert.assertNotNull(registeredUser);
Assert.assertNotNull(registeredUser.getId());
Assert.assertNotNull(registeredUser.getCreateDate());

restTemplate.postForObject(format("http://localhost:%d/users/create", port), user, User.class);
}
}


What can be a reason of this locking issue ?


Aucun commentaire:

Enregistrer un commentaire