Modified SimpleMessaging.close() to be synchronous and avoid thread synch problems in stop/start cycle used in integration tests. Fix issue #19

This commit is contained in:
andrea
2013-12-26 21:10:34 +01:00
parent 33705c52e0
commit 224bcc1721
3 changed files with 21 additions and 10 deletions
@@ -4,8 +4,10 @@ import com.lmax.disruptor.BatchEventProcessor;
import com.lmax.disruptor.EventHandler; import com.lmax.disruptor.EventHandler;
import com.lmax.disruptor.RingBuffer; import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.SequenceBarrier; import com.lmax.disruptor.SequenceBarrier;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.dna.mqtt.moquette.messaging.spi.IMessaging; import org.dna.mqtt.moquette.messaging.spi.IMessaging;
import org.dna.mqtt.moquette.messaging.spi.IStorageService; import org.dna.mqtt.moquette.messaging.spi.IStorageService;
import org.dna.mqtt.moquette.messaging.spi.impl.events.*; import org.dna.mqtt.moquette.messaging.spi.impl.events.*;
@@ -42,6 +44,8 @@ public class SimpleMessaging implements IMessaging, EventHandler<ValueEvent> {
private ProtocolProcessor m_processor = new ProtocolProcessor(); private ProtocolProcessor m_processor = new ProtocolProcessor();
CountDownLatch m_stopLatch;
private SimpleMessaging() { private SimpleMessaging() {
} }
@@ -92,7 +96,17 @@ public class SimpleMessaging implements IMessaging, EventHandler<ValueEvent> {
} }
public void stop() { public void stop() {
m_stopLatch = new CountDownLatch(1);
disruptorPublish(new StopEvent()); disruptorPublish(new StopEvent());
try {
//wait the callback notification from the protocol processor thread
boolean elapsed = !m_stopLatch.await(10, TimeUnit.SECONDS);
if (elapsed) {
LOG.error("Can't stop the server in 10 seconds");
}
} catch (InterruptedException ex) {
LOG.error(null, ex);
}
} }
public void onEvent(ValueEvent t, long l, boolean bln) throws Exception { public void onEvent(ValueEvent t, long l, boolean bln) throws Exception {
@@ -182,5 +196,6 @@ public class SimpleMessaging implements IMessaging, EventHandler<ValueEvent> {
m_executor.shutdown(); m_executor.shutdown();
subscriptions = null; subscriptions = null;
m_stopLatch.countDown();
} }
} }
@@ -45,19 +45,16 @@ public class Server {
m_acceptor.initialize(messaging); m_acceptor.initialize(messaging);
} }
/**
* It's an asynchronous method, it's only a request to stop
*/
public void stopServer() { public void stopServer() {
System.out.println("Server stopping..."); System.out.println("Server stopping...");
messaging.stop(); messaging.stop();
m_acceptor.close(); m_acceptor.close();
System.out.println("Server stopped"); System.out.println("Server stopped");
try { // try {
//sleep one second to give the disruptor // //sleep one second to give the disruptor
Thread.sleep(100); // Thread.sleep(100);
} catch (InterruptedException ex) { // } catch (InterruptedException ex) {
LOG.error(null, ex); // LOG.error(null, ex);
} // }
} }
} }
@@ -315,7 +315,6 @@ public class ServerIntegrationPahoTest {
anotherClient.disconnect(); anotherClient.disconnect();
} }
//BUG
@Test @Test
public void testPublishWithQoS2() throws Exception { public void testPublishWithQoS2() throws Exception {
LOG.info("*** testPublishWithQoS2 ***"); LOG.info("*** testPublishWithQoS2 ***");