From b7169271ac132b9a8fb1a930078caac74d757247 Mon Sep 17 00:00:00 2001 From: andsel Date: Tue, 29 Sep 2015 22:07:45 +0200 Subject: [PATCH] Moved test of MapDBPersistenceStore into the test unit --- .../spi/persistence/MapDBPersistentStore.java | 6 +- .../moquette/server/ServerShutdownTest.java | 73 ------------------- .../MapDBPersistentStoreTest.java | 10 ++- 3 files changed, 14 insertions(+), 75 deletions(-) delete mode 100644 broker/src/test/java/org/eclipse/moquette/server/ServerShutdownTest.java rename broker/src/test/java/org/eclipse/moquette/spi/{impl => persistence}/MapDBPersistentStoreTest.java (93%) diff --git a/broker/src/main/java/org/eclipse/moquette/spi/persistence/MapDBPersistentStore.java b/broker/src/main/java/org/eclipse/moquette/spi/persistence/MapDBPersistentStore.java index 0b3971fd..b3175fb8 100644 --- a/broker/src/main/java/org/eclipse/moquette/spi/persistence/MapDBPersistentStore.java +++ b/broker/src/main/java/org/eclipse/moquette/spi/persistence/MapDBPersistentStore.java @@ -59,7 +59,7 @@ public class MapDBPersistentStore implements IMessagesStore, ISessionsStore { private DB m_db; private String m_storePath; - private final ScheduledExecutorService m_scheduler = Executors.newScheduledThreadPool(1); + protected final ScheduledExecutorService m_scheduler = Executors.newScheduledThreadPool(1); /* * The default constructor will create an in memory store as no file path was specified @@ -311,6 +311,10 @@ public class MapDBPersistentStore implements IMessagesStore, ISessionsStore { } public void close() { + if (this.m_db.isClosed()) { + LOG.debug("already closed"); + return; + } this.m_db.commit(); LOG.debug("persisted subscriptions {}", m_persistentSubscriptions); this.m_db.close(); diff --git a/broker/src/test/java/org/eclipse/moquette/server/ServerShutdownTest.java b/broker/src/test/java/org/eclipse/moquette/server/ServerShutdownTest.java deleted file mode 100644 index 907bb3dd..00000000 --- a/broker/src/test/java/org/eclipse/moquette/server/ServerShutdownTest.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (c) 2012-2015 The original author or authors - * ------------------------------------------------------ - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * - * The Eclipse Public License is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * The Apache License v2.0 is available at - * http://www.opensource.org/licenses/apache2.0.php - * - * You may elect to redistribute this code under either of these licenses. - */ -package org.eclipse.moquette.server; - -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; - -import static org.junit.Assert.assertTrue; - -/** - * This tests that the broker shuts down properly and all the threads it creates are closed. - * - * @author kazvictor - */ -public class ServerShutdownTest { - private static final Logger LOG = LoggerFactory.getLogger(ServerShutdownTest.class); - - private final ScheduledExecutorService threadCounter = Executors.newScheduledThreadPool(1); - - @Test - public void testShutdown() throws IOException, InterruptedException { - final int initialThreadCount = Thread.activeCount(); - LOG.info("*** testShutdown ***"); - LOG.debug("Initial threads count {}", initialThreadCount); - - //Start the server - Server broker = new Server(); - broker.startServer(); - //stop the server - broker.stopServer(); - - //wait till the thread count is back to the initial thread count - final CountDownLatch threadsStoppedLatch = new CountDownLatch(1); - threadCounter.scheduleAtFixedRate(new Runnable() { - @Override - public void run() { - int threadCount = Thread.activeCount(); - LOG.debug("Current Thread Count = " + threadCount); - //plus 1 for this thread - if (threadCount == initialThreadCount + 1) { - threadsStoppedLatch.countDown(); - } - } - }, 0, 100, TimeUnit.MILLISECONDS); - - //wait till the countdown latch is triggered. Either the threads stop or the timeout is readed. - boolean threadsStopped = threadsStoppedLatch.await(5, TimeUnit.SECONDS); - //shutdown the threadCounter. - threadCounter.shutdown(); - - assertTrue("Broker did not shutdown properly. Not all broker threads were stopped.", threadsStopped); - } -} diff --git a/broker/src/test/java/org/eclipse/moquette/spi/impl/MapDBPersistentStoreTest.java b/broker/src/test/java/org/eclipse/moquette/spi/persistence/MapDBPersistentStoreTest.java similarity index 93% rename from broker/src/test/java/org/eclipse/moquette/spi/impl/MapDBPersistentStoreTest.java rename to broker/src/test/java/org/eclipse/moquette/spi/persistence/MapDBPersistentStoreTest.java index 1b50cfbb..ba3d5ec9 100644 --- a/broker/src/test/java/org/eclipse/moquette/spi/impl/MapDBPersistentStoreTest.java +++ b/broker/src/test/java/org/eclipse/moquette/spi/persistence/MapDBPersistentStoreTest.java @@ -13,7 +13,7 @@ * * You may elect to redistribute this code under either of these licenses. */ -package org.eclipse.moquette.spi.impl; +package org.eclipse.moquette.spi.persistence; import org.eclipse.moquette.proto.messages.AbstractMessage; import org.eclipse.moquette.spi.impl.subscriptions.Subscription; @@ -102,4 +102,12 @@ public class MapDBPersistentStoreTest { packetId = m_storageService.nextPacketID("CLIENT"); assertEquals(1, packetId); } + + @Test + public void testCloseShutdownCommitTask() { + m_storageService.close(); + + //verify the executor is shutdown + assertTrue(m_storageService.m_scheduler.isTerminated()); + } } \ No newline at end of file