From 4e45b573ffef962512460e296586428075852cdd Mon Sep 17 00:00:00 2001 From: Vincent Zurczak Date: Fri, 18 Dec 2015 16:59:23 +0100 Subject: [PATCH] Try to fix some unit tests for Travis (concurrency issues) --- build-logging.properties | 7 ++-- core/roboconf-dm/.gitignore | 2 +- .../client/AbstractMessagingTest.java | 13 +++++++ .../in_memory/InMemoryMessagingTest.java | 5 +-- .../http/internal/HttpClientFactory.java | 26 +++++++++++++- .../http/internal/clients/HttpDmClient.java | 8 ++--- .../http/internal/HttpMessagingTest.java | 34 +++++++++++++++---- .../http/internal/HttpTestUtils.java | 5 +++ pom.xml | 3 +- 9 files changed, 83 insertions(+), 20 deletions(-) diff --git a/build-logging.properties b/build-logging.properties index 9fb06622..1690e009 100644 --- a/build-logging.properties +++ b/build-logging.properties @@ -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 diff --git a/core/roboconf-dm/.gitignore b/core/roboconf-dm/.gitignore index b7947600..4ca06609 100644 --- a/core/roboconf-dm/.gitignore +++ b/core/roboconf-dm/.gitignore @@ -15,7 +15,7 @@ /.settings # Remains of some builds -#instances +instances #idea *.iml diff --git a/core/roboconf-messaging-api/src/test/java/net/roboconf/messaging/api/internal/client/AbstractMessagingTest.java b/core/roboconf-messaging-api/src/test/java/net/roboconf/messaging/api/internal/client/AbstractMessagingTest.java index 9ae96a6f..1c21e059 100644 --- a/core/roboconf-messaging-api/src/test/java/net/roboconf/messaging/api/internal/client/AbstractMessagingTest.java +++ b/core/roboconf-messaging-api/src/test/java/net/roboconf/messaging/api/internal/client/AbstractMessagingTest.java @@ -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 )); @@ -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()); @@ -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()); @@ -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()); @@ -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()); @@ -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()); @@ -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()); @@ -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()); diff --git a/core/roboconf-messaging-api/src/test/java/net/roboconf/messaging/api/internal/client/in_memory/InMemoryMessagingTest.java b/core/roboconf-messaging-api/src/test/java/net/roboconf/messaging/api/internal/client/in_memory/InMemoryMessagingTest.java index edbd0f1e..f959d0be 100644 --- a/core/roboconf-messaging-api/src/test/java/net/roboconf/messaging/api/internal/client/in_memory/InMemoryMessagingTest.java +++ b/core/roboconf-messaging-api/src/test/java/net/roboconf/messaging/api/internal/client/in_memory/InMemoryMessagingTest.java @@ -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; @@ -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()); } diff --git a/core/roboconf-messaging-http/src/main/java/net/roboconf/messaging/http/internal/HttpClientFactory.java b/core/roboconf-messaging-http/src/main/java/net/roboconf/messaging/http/internal/HttpClientFactory.java index d074d81c..43610eae 100644 --- a/core/roboconf-messaging-http/src/main/java/net/roboconf/messaging/http/internal/HttpClientFactory.java +++ b/core/roboconf-messaging-http/src/main/java/net/roboconf/messaging/http/internal/HttpClientFactory.java @@ -176,13 +176,37 @@ public void start() throws Exception { } - + /** + * Stops all the agent clients. + *

+ * Invoked by iPojo. + *

+ */ public void stop() { this.logger.fine( "iPojo unregisters a servlet for HTTP messaging." ); resetClients( true ); } + /** + * Stops all the clients (agents and DM). + *

+ * Mostly for tests. + *

+ */ + 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 ) { diff --git a/core/roboconf-messaging-http/src/main/java/net/roboconf/messaging/http/internal/clients/HttpDmClient.java b/core/roboconf-messaging-http/src/main/java/net/roboconf/messaging/http/internal/clients/HttpDmClient.java index b5352915..3dd97d3e 100644 --- a/core/roboconf-messaging-http/src/main/java/net/roboconf/messaging/http/internal/clients/HttpDmClient.java +++ b/core/roboconf-messaging-http/src/main/java/net/roboconf/messaging/http/internal/clients/HttpDmClient.java @@ -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; @@ -54,7 +55,7 @@ public class HttpDmClient extends AbstractRoutingClient { private final Map ctxToSession; private LinkedBlockingQueue messageQueue; - private int openConnections = 0; + private final AtomicInteger openConnections = new AtomicInteger( 0 ); private String httpServerIp; private int httpPort; @@ -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(); } @@ -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(); } diff --git a/core/roboconf-messaging-http/src/test/java/net/roboconf/messaging/http/internal/HttpMessagingTest.java b/core/roboconf-messaging-http/src/test/java/net/roboconf/messaging/http/internal/HttpMessagingTest.java index 63312d57..748c56f7 100644 --- a/core/roboconf-messaging-http/src/test/java/net/roboconf/messaging/http/internal/HttpMessagingTest.java +++ b/core/roboconf-messaging-http/src/test/java/net/roboconf/messaging/http/internal/HttpMessagingTest.java @@ -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; @@ -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 java.util.logging.config.file - ${basedir}/build-logging.properties + ${basedir}/../../build-logging.properties + alphabetical