Skip to content

Commit

Permalink
Try to fix some unit tests for Travis (concurrency issues)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-zurczak committed Dec 30, 2015
1 parent c9b8eee commit 4e45b57
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 20 deletions.
7 changes: 3 additions & 4 deletions build-logging.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
# logs entries during builds. #
############################################

handlers= java.util.logging.ConsoleHandler, java.util.logging.FileHandler
handlers= java.util.logging.ConsoleHandler

.level = WARNING
java.util.logging.FileHandler.level = WARNING
java.util.logging.ConsoleHandler.level = WARNING
.level = SEVERE
java.util.logging.ConsoleHandler.level = SEVERE
2 changes: 1 addition & 1 deletion core/roboconf-dm/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/.settings

# Remains of some builds
#instances
instances

#idea
*.iml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ public void testExchangesBetweenTheDmAndThreeAgents() throws Exception {
agentClient_11.listenToTheDm( ListenerCommand.START );
agentClient_12.listenToTheDm( ListenerCommand.START );
agentClient_2.listenToTheDm( ListenerCommand.START );
Thread.sleep( getDelay());

// The DM sends messages
dmClient.sendMessageToAgent( app1, app1_root1, new MsgCmdSetScopedInstance( app1_root1 ));
Expand Down Expand Up @@ -368,6 +369,7 @@ public void testExportsBetweenAgents() throws Exception {
// MySQL publishes its exports but nobody is listening.
mySqlClient.publishExports( mysql );
Thread.sleep( getDelay());

Assert.assertEquals( 0, mySqlMessages.size());
Assert.assertEquals( 0, apacheMessages.size());
Assert.assertEquals( 0, tomcatMessages.size());
Expand All @@ -377,6 +379,8 @@ public void testExportsBetweenAgents() throws Exception {
// Let's re-export MySQL.
otherClient.listenToExportsFromOtherAgents( ListenerCommand.START, other );
tomcatClient.listenToExportsFromOtherAgents( ListenerCommand.START, tomcat );
Thread.sleep( getDelay());

mySqlClient.publishExports( mysql );
Thread.sleep( getDelay());

Expand All @@ -395,6 +399,7 @@ public void testExportsBetweenAgents() throws Exception {

// Let's publish an unknown facet. Nobody should receive it.
mySqlClient.publishExports( mysql, "an-unknown-facet-or-component-name" );
Thread.sleep( getDelay());

Assert.assertEquals( 0, mySqlMessages.size());
Assert.assertEquals( 0, apacheMessages.size());
Expand All @@ -416,6 +421,8 @@ public void testExportsBetweenAgents() throws Exception {
// Tomcat publishes its exports.
apacheClient.listenToExportsFromOtherAgents( ListenerCommand.START, apache );
mySqlClient.listenToExportsFromOtherAgents( ListenerCommand.START, mysql );
Thread.sleep( getDelay());

tomcatClient.publishExports( tomcat );
Thread.sleep( getDelay());

Expand Down Expand Up @@ -469,6 +476,8 @@ public void testExportsBetweenAgents() throws Exception {
// MySQL publishes (again) its exports
// But this time, Tomcat does not listen anymore
tomcatClient.listenToExportsFromOtherAgents( ListenerCommand.STOP, tomcat );
Thread.sleep( getDelay());

mySqlClient.publishExports( mysql );
Thread.sleep( getDelay());

Expand Down Expand Up @@ -659,8 +668,10 @@ public void testExportsBetweenSiblingAgents() throws Exception {

// Now, instance2 is listening.
client2.listenToRequestsFromOtherAgents( ListenerCommand.START, instance2 );
Thread.sleep( getDelay());
client1.requestExportsFromOtherAgents( instance1 );
Thread.sleep( getDelay());

Assert.assertEquals( 0, messages1.size());
Assert.assertEquals( 2, messages2.size());
Assert.assertEquals( MsgCmdRequestImport.class, messages2.get( 0 ).getClass());
Expand All @@ -677,6 +688,8 @@ public void testExportsBetweenSiblingAgents() throws Exception {
// instance1 should receive the notification it has sent. It will be up to the agent to ignore it.
client2.listenToRequestsFromOtherAgents( ListenerCommand.STOP, instance2 );
client1.listenToRequestsFromOtherAgents( ListenerCommand.START, instance1 );
Thread.sleep( getDelay());

client1.requestExportsFromOtherAgents( instance1 );
Thread.sleep( getDelay());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package net.roboconf.messaging.api.internal.client.in_memory;

import net.roboconf.messaging.api.MessagingConstants;
import net.roboconf.messaging.api.factory.MessagingClientFactoryRegistry;
import net.roboconf.messaging.api.internal.client.AbstractMessagingTest;

import org.junit.Before;
Expand All @@ -40,8 +41,8 @@ public class InMemoryMessagingTest extends AbstractMessagingTest {
@Before
public void registerRabbitMqFactory() {

final InMemoryClientFactory factory = new InMemoryClientFactory();
this.registry.addMessagingClientFactory(factory);
this.registry = new MessagingClientFactoryRegistry();
this.registry.addMessagingClientFactory( new InMemoryClientFactory());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,37 @@ public void start() throws Exception {
}



/**
* Stops all the agent clients.
* <p>
* Invoked by iPojo.
* </p>
*/
public void stop() {
this.logger.fine( "iPojo unregisters a servlet for HTTP messaging." );
resetClients( true );
}


/**
* Stops all the clients (agents and DM).
* <p>
* Mostly for tests.
* </p>
*/
void stopAll() {

try {
this.dmClient.closeConnection();
stop();

} catch( Throwable t ) {
this.logger.warning( "An error occurred while closing the connection of the DM client." );
Utils.logException( this.logger, new RuntimeException( t ));
}
}


@Override
public IMessagingClient createClient( ReconfigurableClient<?> parent ) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

import net.roboconf.messaging.api.extensions.AbstractRoutingClient;
import net.roboconf.messaging.api.extensions.MessagingContext;
Expand All @@ -54,7 +55,7 @@ public class HttpDmClient extends AbstractRoutingClient<Session> {

private final Map<String,Session> ctxToSession;
private LinkedBlockingQueue<Message> messageQueue;
private int openConnections = 0;
private final AtomicInteger openConnections = new AtomicInteger( 0 );

private String httpServerIp;
private int httpPort;
Expand All @@ -75,7 +76,7 @@ public void openConnection() throws IOException {

// There is only one instance per Http Factory.
// So, we do not want to close the connection someone is still using it.
this.openConnections ++;
this.openConnections.incrementAndGet();
super.openConnection();
}

Expand All @@ -85,8 +86,7 @@ public void closeConnection() throws IOException {

// There is only one instance per Http Factory.
// So, we do not want to close the connection someone is still using it.
this.openConnections --;
if( this.openConnections == 0 )
if( this.openConnections.decrementAndGet() == 0 )
super.closeConnection();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import net.roboconf.messaging.http.internal.HttpTestUtils.WebServer;

import org.junit.After;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -42,39 +43,58 @@
*/
public class HttpMessagingTest extends AbstractMessagingTest {

private static final int ATTEMPTS = 30;
private WebServer webServerRunnable;
private HttpClientFactory factory;


@Before
public void registerHttpFactory() throws InterruptedException {

// Register a new factory
final HttpClientFactory factory = new HttpClientFactory();
factory.setHttpServerIp( HttpConstants.DEFAULT_IP );
factory.setHttpPort( HttpTestUtils.TEST_PORT );
this.factory = new HttpClientFactory();
this.factory.setHttpServerIp( HttpConstants.DEFAULT_IP );
this.factory.setHttpPort( HttpTestUtils.TEST_PORT );

this.registry = new MessagingClientFactoryRegistry();
this.registry.addMessagingClientFactory( factory );
this.registry.addMessagingClientFactory( this.factory );

// Launch a new web server
this.webServerRunnable = new WebServer( factory );
this.webServerRunnable = new WebServer( this.factory );
Thread webServerThread = new Thread( this.webServerRunnable, "Test for Roboconf HTTP Messaging" );
webServerThread.start();

for( int i=0; i<15; i++ ) {
// For diagnostic
for( int i=0; i<ATTEMPTS; i++ ) {
if( this.webServerRunnable.isRunning()
&& this.webServerRunnable.isServerStarted())
break;

Thread.sleep( 50 );
}

Assert.assertTrue( this.webServerRunnable.isServerStarted());
}


@After
public void stopWebServer() throws IOException, InterruptedException {

// Stop all the clients
this.factory.stopAll();

// Stop the web server
this.webServerRunnable.stop();

// For diagnostic
for( int i=0; i<ATTEMPTS; i++ ) {
if( this.webServerRunnable.isServerStopped())
break;

Thread.sleep( 50 );
}

Assert.assertTrue( this.webServerRunnable.isServerStopped());
}


Expand Down Expand Up @@ -136,7 +156,7 @@ public void testDmDebug() throws Exception {

@Override
protected long getDelay() {
return 100;
return 300;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ public boolean isServerStarted() {
}


public boolean isServerStopped() {
return this.server.isStopped();
}


public boolean isRunning() {
return this.running;
}
Expand Down
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,10 @@
<systemProperties>
<property>
<name>java.util.logging.config.file</name>
<value>${basedir}/build-logging.properties</value>
<value>${basedir}/../../build-logging.properties</value>
</property>
</systemProperties>
<runOrder>alphabetical</runOrder>
</configuration>
</plugin>

Expand Down

0 comments on commit 4e45b57

Please sign in to comment.