Merged topic subscription search and publish into single MessagePublisher method
First step on implementing H2 storage Minor, fixed not imported class Cleaned MessageStore interface to avoid duplication of methods used in couple. Switched from guid + message to move messages Separated ISubscriptionsStore from ISessionStore Implemented H2 queue
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (c) 2012-2017 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 io.moquette.persistence.h2;
|
||||
|
||||
import io.moquette.BrokerConstants;
|
||||
import io.moquette.persistence.MessageStoreTCK;
|
||||
import io.moquette.server.config.IConfig;
|
||||
import io.moquette.server.config.MemoryConfig;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
public class H2MessagesStoreTest extends MessageStoreTCK {
|
||||
|
||||
H2PersistentStore storageService;
|
||||
|
||||
private ScheduledExecutorService scheduler;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
scheduler = Executors.newScheduledThreadPool(1);
|
||||
|
||||
cleanPersistenceFile(BrokerConstants.DEFAULT_PERSISTENT_PATH);
|
||||
Properties props = new Properties();
|
||||
props.setProperty(BrokerConstants.PERSISTENT_STORE_PROPERTY_NAME, BrokerConstants.DEFAULT_PERSISTENT_PATH);
|
||||
IConfig conf = new MemoryConfig(props);
|
||||
storageService = new H2PersistentStore(conf, scheduler);
|
||||
storageService.initStore();
|
||||
messagesStore = storageService.messagesStore();
|
||||
sessionsStore = storageService.sessionsStore();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
if (storageService != null) {
|
||||
storageService.close();
|
||||
}
|
||||
|
||||
scheduler.shutdown();
|
||||
cleanPersistenceFile(BrokerConstants.DEFAULT_PERSISTENT_PATH);
|
||||
}
|
||||
|
||||
public static void cleanPersistenceFile(String fileName) {
|
||||
File dbFile = new File(fileName);
|
||||
if (dbFile.exists()) {
|
||||
dbFile.delete();
|
||||
}
|
||||
assertFalse(dbFile.exists());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package io.moquette.persistence.h2;
|
||||
|
||||
import io.moquette.BrokerConstants;
|
||||
import org.h2.mvstore.MVStore;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class H2PersistentQueueTest {
|
||||
|
||||
private MVStore mvStore;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.mvStore = new MVStore.Builder()
|
||||
.fileName(BrokerConstants.DEFAULT_PERSISTENT_PATH)
|
||||
.autoCommitDisabled()
|
||||
.open();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
File dbFile = new File(BrokerConstants.DEFAULT_PERSISTENT_PATH);
|
||||
if (dbFile.exists()) {
|
||||
dbFile.delete();
|
||||
}
|
||||
assertFalse(dbFile.exists());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdd() {
|
||||
H2PersistentQueue<String> sut = new H2PersistentQueue<>(this.mvStore, "test");
|
||||
|
||||
sut.add("Hello");
|
||||
sut.add("world");
|
||||
|
||||
assertEquals("Hello", sut.peek());
|
||||
assertEquals("Hello", sut.peek());
|
||||
assertEquals("peek just return elements, doesn't remove them", 2, sut.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPoll() {
|
||||
H2PersistentQueue<String> sut = new H2PersistentQueue<>(this.mvStore, "test");
|
||||
sut.add("Hello");
|
||||
sut.add("world");
|
||||
|
||||
assertEquals("Hello", sut.poll());
|
||||
assertEquals("world", sut.poll());
|
||||
assertTrue("after poll 2 elements inserted before, should be empty", sut.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPerformance() {
|
||||
H2PersistentQueue<String> sut = new H2PersistentQueue<>(this.mvStore, "test");
|
||||
|
||||
int numIterations = 10000000;
|
||||
for (int i=0; i < numIterations; i++) {
|
||||
sut.add("Hello");
|
||||
}
|
||||
mvStore.commit();
|
||||
|
||||
for (int i=0; i < numIterations; i++) {
|
||||
assertEquals("Hello", sut.poll());
|
||||
}
|
||||
|
||||
assertTrue("should be empty", sut.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReloadFromPersistedState() {
|
||||
H2PersistentQueue<String> before = new H2PersistentQueue<>(this.mvStore, "test");
|
||||
before.add("Hello");
|
||||
before.add("crazy");
|
||||
before.add("world");
|
||||
assertEquals("Hello", before.poll());
|
||||
this.mvStore.commit();
|
||||
this.mvStore.close();
|
||||
|
||||
this.mvStore = new MVStore.Builder()
|
||||
.fileName(BrokerConstants.DEFAULT_PERSISTENT_PATH)
|
||||
.autoCommitDisabled()
|
||||
.open();
|
||||
|
||||
//now reload the persisted state
|
||||
H2PersistentQueue<String> after = new H2PersistentQueue<>(this.mvStore, "test");
|
||||
|
||||
assertEquals("crazy", after.poll());
|
||||
assertEquals("world", after.poll());
|
||||
assertTrue("should be empty", after.isEmpty());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user