Skip to content
This repository has been archived by the owner on Jan 3, 2019. It is now read-only.

Commit

Permalink
Allow runtime assignment of test ports and ensure consistency of asyn…
Browse files Browse the repository at this point in the history
…c operations
  • Loading branch information
Andrew Woods committed Aug 23, 2013
1 parent 928f109 commit adb7ba2
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 68 deletions.
32 changes: 32 additions & 0 deletions pom.xml
Expand Up @@ -309,9 +309,41 @@
</executions>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<!-- reserve ports for integration tests -->
<configuration>
<portNames>
<portName>test.port</portName>
<portName>test.mgt.port</portName>
<portName>test.fuseki.port</portName>
</portNames>
</configuration>
<executions>
<execution>
<id>reserve-port</id>
<phase>pre-integration-test</phase>
<goals>
<goal>reserve-network-port</goal>
</goals>
</execution>
</executions>

</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<!-- export ports for integration tests to system.evnv-->
<systemPropertyVariables>
<test.port>${test.port}</test.port>
<test.mgt.port>${test.mgt.port}</test.mgt.port>
<test.fuseki.port>${test.fuseki.port}</test.fuseki.port>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/fcrepo/indexer/SparqlIndexer.java
Expand Up @@ -99,7 +99,8 @@ public void update( String pid, String content ) {
add.addTriple( triples.nextStatement().asTriple() );
}

// send udpate to server
// send update to server
logger.debug("Sending update request for pid: {}", pid);
exec( new UpdateRequest(new UpdateDataInsert(add)) );
}

Expand Down
13 changes: 9 additions & 4 deletions src/test/java/org/fcrepo/indexer/FusekiContainerWrapper.java
Expand Up @@ -41,16 +41,20 @@ public class FusekiContainerWrapper extends ContainerWrapper {
final private Logger logger = LoggerFactory
.getLogger(FusekiContainerWrapper.class);

protected static final int MGT_PORT =

private static final int FUSEKI_PORT =
Integer.parseInt(System.getProperty("test.fuseki.port", "3030"));

private static final int MGT_PORT =
Integer.parseInt(System.getProperty("test.mgt.port", "3031"));

protected static final String serverAddress = "http://localhost:" +
private static final String serverAddress = "http://localhost:" +
MGT_PORT + "/mgt";

protected final PoolingClientConnectionManager connectionManager =
private final PoolingClientConnectionManager connectionManager =
new PoolingClientConnectionManager();

protected static HttpClient client;
private static HttpClient client;

@PostConstruct
public void start() throws Exception {
Expand All @@ -76,6 +80,7 @@ private class FusekiRunner implements Runnable {
public void run() {
FusekiCmd.main("--update",
"--mem",
"--port=" + FUSEKI_PORT,
"--mgtPort=" + MGT_PORT,
"/test");
}
Expand Down
121 changes: 78 additions & 43 deletions src/test/java/org/fcrepo/indexer/IndexerGroupIT.java
Expand Up @@ -16,6 +16,7 @@

package org.fcrepo.indexer;

import static org.apache.commons.io.filefilter.FileFilterUtils.prefixFileFilter;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
Expand All @@ -25,10 +26,10 @@
import static org.apache.abdera.model.Text.Type.TEXT;

import java.io.File;
import java.io.IOException;
import java.io.FilenameFilter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
Expand All @@ -37,14 +38,13 @@
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;

import org.iq80.leveldb.util.FileUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;

Expand All @@ -57,22 +57,16 @@
@ContextConfiguration({"/spring-test/test-container.xml"})
public class IndexerGroupIT {

protected static final int SERVER_PORT = Integer.parseInt(System
private static final int SERVER_PORT = Integer.parseInt(System
.getProperty("test.port", "8080"));

protected static final String serverAddress = "http://localhost:" +
private static final String serverAddress = "http://localhost:" +
SERVER_PORT + "/rest/objects/";

protected final PoolingClientConnectionManager connectionManager =
private final PoolingClientConnectionManager connectionManager =
new PoolingClientConnectionManager();

protected static HttpClient client;

private static String TEST_PID = "changeme_1001";

private static SimpleDateFormat fmt = new SimpleDateFormat("HHmmssSSS");

private Connection connection;
private static HttpClient client;

@Inject
private IndexerGroup indexerGroup;
Expand All @@ -82,21 +76,18 @@ public class IndexerGroupIT {
private FileSerializer fileSerializer;
private File fileSerializerPath;

private static TextMessage getMessage(String operation)
throws JMSException {
private static TextMessage getMessage(String operation,
String pid) throws Exception {
Abdera abdera = new Abdera();

Entry entry = abdera.newEntry();
entry.setTitle(operation, TEXT)
.setBaseUri("http://localhost:8080/rest");
entry.addCategory("xsd:string", TEST_PID, "fedora-types:pid");
.setBaseUri("http://localhost:" + SERVER_PORT + "/rest");
entry.addCategory("xsd:string", pid, "fedora-types:pid");
entry.setContent("contentds");
StringWriter writer = new StringWriter();
try {
entry.writeTo(writer);
} catch (IOException e) {
// hush
}
entry.writeTo(writer);

String atomMessage = writer.toString();

TextMessage msg = mock(TextMessage.class);
Expand All @@ -111,63 +102,107 @@ public void setup() {
}

@Test
public void indexerGroupUpdateTest() throws IOException, JMSException {
public void indexerGroupUpdateTest() throws Exception {
doIndexerGroupUpdateTest("test_pid_0");
}

private void doIndexerGroupUpdateTest(final String pid) throws Exception {
// create dummy object
final HttpPost method = new HttpPost(serverAddress + TEST_PID);
final HttpPost method = new HttpPost(serverAddress + pid);
final HttpResponse response = client.execute(method);
assertEquals(201, response.getStatusLine().getStatusCode());

try {
Thread.sleep(1500); // wait for message to be processed
} catch ( Exception ex ) { }
FilenameFilter filter = prefixFileFilter(pid);
waitForFiles(1, filter); // wait for message to be processed

// file should exist and contain data
File[] files = fileSerializerPath.listFiles();
File[] files = fileSerializerPath.listFiles(filter);
assertNotNull(files);
assertTrue("There should be 1 file", files.length == 1);

File f = fileSerializerPath.listFiles()[0];
File f = files[0];
assertTrue("Filename doesn't match: " + f.getAbsolutePath(),
f.getName().startsWith(TEST_PID) );
f.getName().startsWith(pid));
assertTrue("File size too small: " + f.length(), f.length() > 500);

final int expectedTriples = 4;
waitForTriples(expectedTriples, pid);

// triples should exist in the triplestore
assertTrue("Triples should exist",
sparqlIndexer.countTriples(TEST_PID) > 0 );
sparqlIndexer.countTriples(pid) == expectedTriples);
}

@Test
public void indexerGroupDeleteTest() throws IOException, JMSException {
public void indexerGroupDeleteTest() throws Exception {
// create and verify dummy object
final String pid = "test_pid_5";
doIndexerGroupUpdateTest(pid);

Thread.sleep(1200); // Let the creation event persist

// delete dummy object
final HttpDelete method = new HttpDelete(serverAddress + TEST_PID);
final HttpDelete method = new HttpDelete(serverAddress + pid);
final HttpResponse response = client.execute(method);
assertEquals(204, response.getStatusLine().getStatusCode());

// create update message and send to indexer group
Message deleteMessage = getMessage("purgeObject");
Message deleteMessage = getMessage("purgeObject", pid);
indexerGroup.onMessage( deleteMessage );

try {
Thread.sleep(5000); // wait for message to be processed
} catch ( Exception ex ) { }
FilenameFilter filter = prefixFileFilter(pid);
waitForFiles(2, filter); // wait for message to be processed

// two files should exist: one empty and one with data
File[] files = fileSerializerPath.listFiles();
File[] files = fileSerializerPath.listFiles(filter);

assertNotNull(files);
assertTrue("There should be 2 files", files.length == 2);
assertEquals(2, files.length);

Arrays.sort(files); // sort files by filename (i.e., creation time)
File f1 = files[0];
File f2 = files[1];
assertTrue("Filename doesn't match: " + f1.getAbsolutePath(),
f1.getName().startsWith(TEST_PID) );
f1.getName().startsWith(pid) );
assertTrue("File size too small: " + f1.length(), f1.length() > 500);
assertTrue("Filename doesn't match: " + f2.getAbsolutePath(),
f2.getName().startsWith(TEST_PID) );
f2.getName().startsWith(pid) );
assertTrue("File size should be 0: " + f2.length(), f2.length() == 0);

final int expectedTriples = 0;
waitForTriples(expectedTriples, pid);

// triples should not exist in the triplestore
assertTrue("Triples should not exist",
sparqlIndexer.countTriples(TEST_PID) == 0 );
sparqlIndexer.countTriples(pid) == expectedTriples);
}

private void waitForFiles(int expectedFiles, FilenameFilter filter) throws InterruptedException {
long elapsed = 0;
long restingWait = 500;
long maxWait = 15000; // 15 seconds

List<File> files = FileUtils.listFiles(fileSerializerPath, filter);
while (expectedFiles != files.size() && (elapsed < maxWait)) {
Thread.sleep(restingWait);
files = FileUtils.listFiles(fileSerializerPath, filter);

elapsed += restingWait;
}
}

private void waitForTriples(int expectTriples, String pid) throws InterruptedException {
long elapsed = 0;
long restingWait = 500;
long maxWait = 15000; // 15 seconds

int count = sparqlIndexer.countTriples(pid);
while ((count != expectTriples) && (elapsed < maxWait)) {
Thread.sleep(restingWait);
count = sparqlIndexer.countTriples(pid);

elapsed += restingWait;
}
}

}

0 comments on commit adb7ba2

Please sign in to comment.