refactor AbstractServerReplicationTest to enable tests with multiple replicable services in future

This commit is contained in:
Fredrik Teschke
2015-02-13 20:28:45 +01:00
parent df3b798bda
commit de39d6a40a
14 changed files with 260 additions and 114 deletions
@@ -35,8 +35,9 @@ public abstract class AbstractLogReplicationTest<LogT extends AbstractLog<EventT
/** /**
* Uses a new master that loads the existing regatta and race log to append an event to the race log. This will store it to the DB so that if the original * Uses a new master that loads the existing regatta and race log to append an event to the race log. This will store it to the DB so that if the original
* master re-loads the race log it should see the new race log event. * master re-loads the race log it should see the new race log event.
* @throws Exception
*/ */
protected void addEventToDB(RaceLogIdentifier raceLogIdentifier, RaceLogRaceStatusEvent createRaceStatusEvent, String regattaName, String raceColumnName, String fleetName) { protected void addEventToDB(RaceLogIdentifier raceLogIdentifier, RaceLogRaceStatusEvent createRaceStatusEvent, String regattaName, String raceColumnName, String fleetName) throws Exception {
final RacingEventServiceImpl temporaryMaster = createNewMaster(); final RacingEventServiceImpl temporaryMaster = createNewMaster();
Regatta regatta = temporaryMaster.getRegatta(new RegattaName(regattaName+" ("+BOAT_CLASS_NAME_49er+")")); Regatta regatta = temporaryMaster.getRegatta(new RegattaName(regattaName+" ("+BOAT_CLASS_NAME_49er+")"));
Series series = regatta.getSeries().iterator().next(); Series series = regatta.getSeries().iterator().next();
@@ -11,32 +11,43 @@ import com.sap.sailing.server.RacingEventService;
import com.sap.sailing.server.impl.RacingEventServiceImpl; import com.sap.sailing.server.impl.RacingEventServiceImpl;
import com.sap.sse.mongodb.MongoDBService; import com.sap.sse.mongodb.MongoDBService;
public abstract class AbstractServerReplicationTest extends com.sap.sse.replication.testsupport.AbstractServerReplicationTest<RacingEventService, RacingEventServiceImpl> { public abstract class AbstractServerReplicationTest extends com.sap.sse.replication.testsupport.AbstractServerWithSingleServiceReplicationTest<RacingEventService, RacingEventServiceImpl> {
protected MongoDBService mongoDBService; protected ServerReplicationTestSetUp testSetUp;
protected MongoObjectFactory mongoObjectFactory;
/** public AbstractServerReplicationTest() {
* Drops the test DB, if <code>dropDB</code> is <code>true</code> and requests the DB to start. super(new ServerReplicationTestSetUp());
*/ testSetUp = (ServerReplicationTestSetUp) super.testSetUp;
@Override
protected void persistenceSetUp(boolean dropDB) {
mongoDBService = MongoDBService.INSTANCE;
if (dropDB) {
mongoDBService.getDB().dropDatabase();
}
mongoObjectFactory = PersistenceFactory.INSTANCE.getMongoObjectFactory(mongoDBService);
} }
protected RacingEventServiceImpl createNewMaster() { protected static class ServerReplicationTestSetUp extends com.sap.sse.replication.testsupport.AbstractServerReplicationTestSetUp<RacingEventService, RacingEventServiceImpl> {
return new RacingEventServiceImpl(PersistenceFactory.INSTANCE.getDomainObjectFactory(mongoDBService, protected MongoDBService mongoDBService;
DomainFactory.INSTANCE), mongoObjectFactory, MediaDBFactory.INSTANCE.getMediaDB(mongoDBService), protected MongoObjectFactory mongoObjectFactory;
EmptyWindStore.INSTANCE, EmptyGPSFixStore.INSTANCE);
} /**
* Drops the test DB, if <code>dropDB</code> is <code>true</code> and requests the DB to start.
protected RacingEventServiceImpl createNewReplica() { */
return new RacingEventServiceImpl(PersistenceFactory.INSTANCE.getDomainObjectFactory(mongoDBService, @Override
// replica gets its own base DomainFactory: protected void persistenceSetUp(boolean dropDB) {
new DomainFactoryImpl()), mongoObjectFactory, MediaDBFactory.INSTANCE.getMediaDB(mongoDBService), mongoDBService = MongoDBService.INSTANCE;
EmptyWindStore.INSTANCE, EmptyGPSFixStore.INSTANCE); if (dropDB) {
mongoDBService.getDB().dropDatabase();
}
mongoObjectFactory = PersistenceFactory.INSTANCE.getMongoObjectFactory(mongoDBService);
}
@Override
public RacingEventServiceImpl createNewMaster() {
return new RacingEventServiceImpl(PersistenceFactory.INSTANCE.getDomainObjectFactory(mongoDBService,
DomainFactory.INSTANCE), mongoObjectFactory, MediaDBFactory.INSTANCE.getMediaDB(mongoDBService),
EmptyWindStore.INSTANCE, EmptyGPSFixStore.INSTANCE);
}
@Override
public RacingEventServiceImpl createNewReplica() {
return new RacingEventServiceImpl(PersistenceFactory.INSTANCE.getDomainObjectFactory(mongoDBService,
// replica gets its own base DomainFactory:
new DomainFactoryImpl()), mongoObjectFactory, MediaDBFactory.INSTANCE.getMediaDB(mongoDBService),
EmptyWindStore.INSTANCE, EmptyGPSFixStore.INSTANCE);
}
} }
} }
@@ -81,16 +81,20 @@ public class ConnectionResetAndReconnectTest extends AbstractServerReplicationTe
} }
@Before protected static class ServerReplicationTestSetUp extends
@Override com.sap.sailing.server.replication.test.AbstractServerReplicationTest.ServerReplicationTestSetUp {
public void setUp() throws Exception { @Before
try { @Override
Pair<com.sap.sse.replication.testsupport.AbstractServerReplicationTest.ReplicationServiceTestImpl<RacingEventService>, ReplicationMasterDescriptor> result = basicSetUp(/* dropDB */ true, public void setUp() throws Exception {
/* master=null means create a new one */ null, /* replica=null means create a new one */null); try {
masterDescriptor = MasterReplicationDescriptorMock.from(result.getB()); Pair<ReplicationServiceTestImpl<RacingEventService>, ReplicationMasterDescriptor> result = basicSetUp(
} catch (Exception e) { /* dropDB */true,
e.printStackTrace(); /* master=null means create a new one */null, /* replica=null means create a new one */null);
tearDown(); masterDescriptor = MasterReplicationDescriptorMock.from(result.getB());
} catch (Exception e) {
e.printStackTrace();
tearDown();
}
} }
} }
@@ -102,7 +106,7 @@ public class ConnectionResetAndReconnectTest extends AbstractServerReplicationTe
/* until here both instances should have the same in-memory state. /* until here both instances should have the same in-memory state.
* now lets add an event on master and stop the messaging queue. */ * now lets add an event on master and stop the messaging queue. */
stopMessagingExchange(); stopMessagingExchange();
replicaReplicator.startToReplicateFrom(masterDescriptor); testSetUp.getReplicaReplicator().startToReplicateFrom(testSetUp.getMasterDescriptor());
Event event = addEventOnMaster(); Event event = addEventOnMaster();
Thread.sleep(1000); // wait for master queue to get filled Thread.sleep(1000); // wait for master queue to get filled
assertNull(replica.getEvent(event.getId())); assertNull(replica.getEvent(event.getId()));
@@ -46,6 +46,7 @@ import com.sap.sse.common.impl.MillisecondsTimePoint;
import com.sap.sse.mongodb.MongoDBService; import com.sap.sse.mongodb.MongoDBService;
import com.sap.sse.replication.ReplicationMasterDescriptor; import com.sap.sse.replication.ReplicationMasterDescriptor;
import com.sap.sse.replication.ReplicationService; import com.sap.sse.replication.ReplicationService;
import com.sap.sse.replication.testsupport.AbstractServerReplicationTestSetUp.ReplicationServiceTestImpl;
public class DelayedLeaderboardCorrectionsReplicationTest extends AbstractServerReplicationTest { public class DelayedLeaderboardCorrectionsReplicationTest extends AbstractServerReplicationTest {
private static final Logger logger = Logger.getLogger(DelayedLeaderboardCorrectionsReplicationTest.class.getName()); private static final Logger logger = Logger.getLogger(DelayedLeaderboardCorrectionsReplicationTest.class.getName());
@@ -115,7 +116,7 @@ public class DelayedLeaderboardCorrectionsReplicationTest extends AbstractServer
masterLeaderboardReloaded.getRaceColumnByName(Q2), MillisecondsTimePoint.now())); masterLeaderboardReloaded.getRaceColumnByName(Q2), MillisecondsTimePoint.now()));
// replicate the re-loaded environment // replicate the re-loaded environment
Pair<com.sap.sse.replication.testsupport.AbstractServerReplicationTest.ReplicationServiceTestImpl<RacingEventService>, ReplicationMasterDescriptor> descriptors = basicSetUp( Pair<ReplicationServiceTestImpl<RacingEventService>, ReplicationMasterDescriptor> descriptors = basicSetUp(
/* dropDB */false, master, replica); /* dropDB */false, master, replica);
replicaReplicator = descriptors.getA(); replicaReplicator = descriptors.getA();
masterDescriptor = descriptors.getB(); masterDescriptor = descriptors.getB();
@@ -73,9 +73,10 @@ import com.sap.sse.common.impl.MillisecondsDurationImpl;
import com.sap.sse.common.impl.MillisecondsTimePoint; import com.sap.sse.common.impl.MillisecondsTimePoint;
import com.sap.sse.replication.ReplicationMasterDescriptor; import com.sap.sse.replication.ReplicationMasterDescriptor;
import com.sap.sse.replication.impl.ReplicationReceiver; import com.sap.sse.replication.impl.ReplicationReceiver;
import com.sap.sse.replication.testsupport.AbstractServerReplicationTestSetUp.ReplicationServiceTestImpl;
public class InitialLoadReplicationObjectIdentityTest extends AbstractServerReplicationTest { public class InitialLoadReplicationObjectIdentityTest extends AbstractServerReplicationTest {
private Pair<com.sap.sse.replication.testsupport.AbstractServerReplicationTest.ReplicationServiceTestImpl<RacingEventService>, ReplicationMasterDescriptor> replicationDescriptorPair; private Pair<ReplicationServiceTestImpl<RacingEventService>, ReplicationMasterDescriptor> replicationDescriptorPair;
/** /**
* Drops the test DB. Sets up master and replica, starts the JMS message broker and registers the replica with the master. * Drops the test DB. Sets up master and replica, starts the JMS message broker and registers the replica with the master.
@@ -84,10 +85,10 @@ public class InitialLoadReplicationObjectIdentityTest extends AbstractServerRepl
@Override @Override
public void setUp() throws Exception { public void setUp() throws Exception {
persistenceSetUp(/* dropDB */ true); persistenceSetUp(/* dropDB */ true);
this.master = new RacingEventServiceImpl(PersistenceFactory.INSTANCE.getDomainObjectFactory(mongoDBService, DomainFactory.INSTANCE), PersistenceFactory.INSTANCE this.master = new RacingEventServiceImpl(PersistenceFactory.INSTANCE.getDomainObjectFactory(testSetUp.mongoDBService, DomainFactory.INSTANCE), PersistenceFactory.INSTANCE
.getMongoObjectFactory(mongoDBService), MediaDBFactory.INSTANCE.getMediaDB(mongoDBService), EmptyWindStore.INSTANCE, EmptyGPSFixStore.INSTANCE); .getMongoObjectFactory(testSetUp.mongoDBService), MediaDBFactory.INSTANCE.getMediaDB(testSetUp.mongoDBService), EmptyWindStore.INSTANCE, EmptyGPSFixStore.INSTANCE);
this.replica = new RacingEventServiceImpl(PersistenceFactory.INSTANCE.getDomainObjectFactory(mongoDBService, DomainFactory.INSTANCE), PersistenceFactory.INSTANCE this.replica = new RacingEventServiceImpl(PersistenceFactory.INSTANCE.getDomainObjectFactory(testSetUp.mongoDBService, DomainFactory.INSTANCE), PersistenceFactory.INSTANCE
.getMongoObjectFactory(mongoDBService), MediaDBFactory.INSTANCE.getMediaDB(mongoDBService), EmptyWindStore.INSTANCE, EmptyGPSFixStore.INSTANCE); .getMongoObjectFactory(testSetUp.mongoDBService), MediaDBFactory.INSTANCE.getMediaDB(testSetUp.mongoDBService), EmptyWindStore.INSTANCE, EmptyGPSFixStore.INSTANCE);
} }
private void performReplicationSetup() throws Exception { private void performReplicationSetup() throws Exception {
@@ -15,6 +15,7 @@ import com.sap.sailing.server.operationaltransformation.CreateFlexibleLeaderboar
import com.sap.sse.common.Util.Pair; import com.sap.sse.common.Util.Pair;
import com.sap.sse.replication.ReplicationMasterDescriptor; import com.sap.sse.replication.ReplicationMasterDescriptor;
import com.sap.sse.replication.impl.ReplicationReceiver; import com.sap.sse.replication.impl.ReplicationReceiver;
import com.sap.sse.replication.testsupport.AbstractServerReplicationTestSetUp.ReplicationServiceTestImpl;
public class PrematureOperationReceiptTest extends AbstractServerReplicationTest { public class PrematureOperationReceiptTest extends AbstractServerReplicationTest {
private ReplicationReceiver replicator; private ReplicationReceiver replicator;
@@ -25,7 +26,7 @@ public class PrematureOperationReceiptTest extends AbstractServerReplicationTest
@Before @Before
@Override @Override
public void setUp() throws Exception { public void setUp() throws Exception {
Pair<com.sap.sse.replication.testsupport.AbstractServerReplicationTest.ReplicationServiceTestImpl<RacingEventService>, ReplicationMasterDescriptor> result = Pair<ReplicationServiceTestImpl<RacingEventService>, ReplicationMasterDescriptor> result =
basicSetUp(/* dropDB */ true, /* master=null means create a new one */ null, /* replica=null means create a new one */ null); basicSetUp(/* dropDB */ true, /* master=null means create a new one */ null, /* replica=null means create a new one */ null);
replicator = replicaReplicator.startToReplicateFromButDontYetFetchInitialLoad(result.getB(), /* startReplicatorSuspended */ true); replicator = replicaReplicator.startToReplicateFromButDontYetFetchInitialLoad(result.getB(), /* startReplicatorSuspended */ true);
} }
@@ -47,10 +47,11 @@ import com.sap.sse.common.Util;
import com.sap.sse.common.Util.Pair; import com.sap.sse.common.Util.Pair;
import com.sap.sse.common.impl.MillisecondsTimePoint; import com.sap.sse.common.impl.MillisecondsTimePoint;
import com.sap.sse.replication.ReplicationMasterDescriptor; import com.sap.sse.replication.ReplicationMasterDescriptor;
import com.sap.sse.replication.testsupport.AbstractServerReplicationTestSetUp.ReplicationServiceTestImpl;
public class RaceLogReplicationTest extends AbstractLogReplicationTest<RaceLog, RaceLogEvent, RaceLogEventVisitor> { public class RaceLogReplicationTest extends AbstractLogReplicationTest<RaceLog, RaceLogEvent, RaceLogEventVisitor> {
private Pair<com.sap.sse.replication.testsupport.AbstractServerReplicationTest.ReplicationServiceTestImpl<RacingEventService>, ReplicationMasterDescriptor> replicationDescriptorPair; private Pair<ReplicationServiceTestImpl<RacingEventService>, ReplicationMasterDescriptor> replicationDescriptorPair;
private RaceLogEvent raceLogEvent; private RaceLogEvent raceLogEvent;
private RaceLogEvent anotherRaceLogEvent; private RaceLogEvent anotherRaceLogEvent;
@@ -217,7 +218,7 @@ public class RaceLogReplicationTest extends AbstractLogReplicationTest<RaceLog,
* has been added to the DB and then race log is re-loaded on the master, the events added through the DB also show up on the replica. * has been added to the DB and then race log is re-loaded on the master, the events added through the DB also show up on the replica.
*/ */
@Test @Test
public void testRaceLogReloadReplication() throws ClassNotFoundException, IOException, InterruptedException { public void testRaceLogReloadReplication() throws Exception {
final String regattaName = "Test"; final String regattaName = "Test";
final String seriesName = "Default"; final String seriesName = "Default";
final String fleetName = "Default"; final String fleetName = "Default";
@@ -22,11 +22,12 @@ import com.sap.sse.common.TimePoint;
import com.sap.sse.common.Util.Pair; import com.sap.sse.common.Util.Pair;
import com.sap.sse.common.impl.MillisecondsTimePoint; import com.sap.sse.common.impl.MillisecondsTimePoint;
import com.sap.sse.replication.ReplicationMasterDescriptor; import com.sap.sse.replication.ReplicationMasterDescriptor;
import com.sap.sse.replication.testsupport.AbstractServerReplicationTestSetUp.ReplicationServiceTestImpl;
public class RegattaLogReplicationTest extends public class RegattaLogReplicationTest extends
AbstractLogReplicationTest<RegattaLog, RegattaLogEvent, RegattaLogEventVisitor> { AbstractLogReplicationTest<RegattaLog, RegattaLogEvent, RegattaLogEventVisitor> {
private Pair<com.sap.sse.replication.testsupport.AbstractServerReplicationTest.ReplicationServiceTestImpl<RacingEventService>, ReplicationMasterDescriptor> replicationDescriptorPair; private Pair<ReplicationServiceTestImpl<RacingEventService>, ReplicationMasterDescriptor> replicationDescriptorPair;
private RegattaLogEvent regattaLogEvent; private RegattaLogEvent regattaLogEvent;
// private RegattaLogEvent anotherRegattaLogEvent; // private RegattaLogEvent anotherRegattaLogEvent;
@@ -10,43 +10,49 @@ import com.sap.sse.filestorage.impl.EmptyFileStorageServicePropertyStoreImpl;
import com.sap.sse.filestorage.impl.FileStorageManagementServiceImpl; import com.sap.sse.filestorage.impl.FileStorageManagementServiceImpl;
import com.sap.sse.filestorage.testsupport.DummyFileStorageService; import com.sap.sse.filestorage.testsupport.DummyFileStorageService;
import com.sap.sse.mongodb.MongoDBService; import com.sap.sse.mongodb.MongoDBService;
import com.sap.sse.replication.testsupport.AbstractServerReplicationTest; import com.sap.sse.replication.testsupport.AbstractServerWithSingleServiceReplicationTest;
public abstract class AbstractFileStorageManagementServiceReplicationTest extends public abstract class AbstractFileStorageManagementServiceReplicationTest extends
AbstractServerReplicationTest<FileStorageManagementService, FileStorageManagementServiceImpl> { AbstractServerWithSingleServiceReplicationTest<FileStorageManagementService, FileStorageManagementServiceImpl> {
private MongoDBService mongoDBService; public AbstractFileStorageManagementServiceReplicationTest() {
super(new ServerReplicationTestSetUp());
}
@Override private static class ServerReplicationTestSetUp extends com.sap.sse.replication.testsupport.AbstractServerReplicationTestSetUp<FileStorageManagementService, FileStorageManagementServiceImpl> {
protected void persistenceSetUp(boolean dropDB) { private MongoDBService mongoDBService;
mongoDBService = MongoDBService.INSTANCE;
if (dropDB) { @Override
protected void persistenceSetUp(boolean dropDB) {
mongoDBService = MongoDBService.INSTANCE;
if (dropDB) {
mongoDBService.getDB().dropDatabase();
}
}
protected FileStorageManagementServiceImpl createNewService() {
return new FileStorageManagementServiceImpl(
new SingleTypeBasedServiceFinderImpl<>(new DummyFileStorageService(),
DummyFileStorageService.NAME), EmptyFileStorageServicePropertyStoreImpl.INSTANCE);
}
@Override
public FileStorageManagementServiceImpl createNewMaster() throws MalformedURLException, IOException,
InterruptedException {
FileStorageManagementServiceImpl result = createNewService();
FileStorageService serviceOnMaster = result.getAvailableFileStorageServices()[0];
result.setActiveFileStorageService(serviceOnMaster);
result.setFileStorageServiceProperty(serviceOnMaster, DummyFileStorageService.PROPERTY_NAME, "123");
return result;
}
@Override
public FileStorageManagementServiceImpl createNewReplica() {
return createNewService();
}
@Override
protected void persistenceTearDown() {
mongoDBService.getDB().dropDatabase(); mongoDBService.getDB().dropDatabase();
} }
} }
protected FileStorageManagementServiceImpl createNewService() {
return new FileStorageManagementServiceImpl(
new SingleTypeBasedServiceFinderImpl<>(new DummyFileStorageService(),
DummyFileStorageService.NAME), EmptyFileStorageServicePropertyStoreImpl.INSTANCE);
}
@Override
protected FileStorageManagementServiceImpl createNewMaster() throws MalformedURLException, IOException,
InterruptedException {
FileStorageManagementServiceImpl result = createNewService();
FileStorageService serviceOnMaster = result.getAvailableFileStorageServices()[0];
result.setActiveFileStorageService(serviceOnMaster);
result.setFileStorageServiceProperty(serviceOnMaster, DummyFileStorageService.PROPERTY_NAME, "123");
return result;
}
@Override
protected FileStorageManagementServiceImpl createNewReplica() {
return createNewService();
}
@Override
protected void persistenceTearDown() {
mongoDBService.getDB().dropDatabase();
}
} }
@@ -22,8 +22,6 @@ import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.zip.GZIPOutputStream; import java.util.zip.GZIPOutputStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.rules.Timeout; import org.junit.rules.Timeout;
@@ -42,10 +40,12 @@ import com.sap.sse.replication.impl.ReplicationReceiver;
import com.sap.sse.replication.impl.ReplicationServiceImpl; import com.sap.sse.replication.impl.ReplicationServiceImpl;
import com.sap.sse.replication.impl.SingletonReplicablesProvider; import com.sap.sse.replication.impl.SingletonReplicablesProvider;
public abstract class AbstractServerReplicationTest<ReplicableInterface extends Replicable<?, ?>, ReplicableImpl extends ReplicableInterface> { public abstract class AbstractServerReplicationTestSetUp<ReplicableInterface extends Replicable<?, ?>, ReplicableImpl extends ReplicableInterface> {
private static final Logger logger = Logger.getLogger(AbstractServerReplicationTest.class.getName()); private static final Logger logger = Logger.getLogger(AbstractServerReplicationTestSetUp.class.getName());
protected static final int SERVLET_PORT = 9990; protected static final int DEFAULT_SERVLET_PORT = 9990;
private final int servletPort;
protected ReplicableImpl replica; protected ReplicableImpl replica;
protected ReplicableImpl master; protected ReplicableImpl master;
protected ReplicationServiceTestImpl<ReplicableInterface> replicaReplicator; protected ReplicationServiceTestImpl<ReplicableInterface> replicaReplicator;
@@ -53,6 +53,14 @@ public abstract class AbstractServerReplicationTest<ReplicableInterface extends
private ReplicationServiceImpl masterReplicator; private ReplicationServiceImpl masterReplicator;
protected ReplicationMasterDescriptor masterDescriptor; protected ReplicationMasterDescriptor masterDescriptor;
protected AbstractServerReplicationTestSetUp() {
servletPort = DEFAULT_SERVLET_PORT;
}
protected AbstractServerReplicationTestSetUp(int servletPort) {
this.servletPort = servletPort;
}
@Rule public Timeout AbstractTracTracLiveTestTimeout = new Timeout(5 * 60 * 1000); // timeout after 5 minutes @Rule public Timeout AbstractTracTracLiveTestTimeout = new Timeout(5 * 60 * 1000); // timeout after 5 minutes
private Thread initialLoadTestServerThread; private Thread initialLoadTestServerThread;
@@ -64,7 +72,6 @@ public abstract class AbstractServerReplicationTest<ReplicableInterface extends
* or {@link ReplicationServiceTestImpl#startToReplicateFromButDontYetFetchInitialLoad(ReplicationMasterDescriptor, boolean)} * or {@link ReplicationServiceTestImpl#startToReplicateFromButDontYetFetchInitialLoad(ReplicationMasterDescriptor, boolean)}
* on the first component returned by {@link #basicSetUp(boolean, Replicable, Replicable)}. * on the first component returned by {@link #basicSetUp(boolean, Replicable, Replicable)}.
*/ */
@Before
public void setUp() throws Exception { public void setUp() throws Exception {
try { try {
Pair<ReplicationServiceTestImpl<ReplicableInterface>, ReplicationMasterDescriptor> result = basicSetUp( Pair<ReplicationServiceTestImpl<ReplicableInterface>, ReplicationMasterDescriptor> result = basicSetUp(
@@ -120,9 +127,9 @@ public abstract class AbstractServerReplicationTest<ReplicableInterface extends
masterReplicator.registerReplica(replicaDescriptor); masterReplicator.registerReplica(replicaDescriptor);
// connect to exchange host and local server running as master // connect to exchange host and local server running as master
// master server and exchange host can be two different hosts // master server and exchange host can be two different hosts
masterDescriptor = new ReplicationMasterDescriptorImpl(exchangeHost, exchangeName, 0, UUID.randomUUID().toString(), "localhost", SERVLET_PORT); masterDescriptor = new ReplicationMasterDescriptorImpl(exchangeHost, exchangeName, 0, UUID.randomUUID().toString(), "localhost", servletPort);
ReplicationServiceTestImpl<ReplicableInterface> replicaReplicator = new ReplicationServiceTestImpl<ReplicableInterface>(exchangeName, exchangeHost, rim, replicaDescriptor, ReplicationServiceTestImpl<ReplicableInterface> replicaReplicator = new ReplicationServiceTestImpl<ReplicableInterface>(exchangeName, exchangeHost, rim, replicaDescriptor,
this.replica, this.master, masterReplicator, masterDescriptor); this.replica, this.master, masterReplicator, masterDescriptor, servletPort);
Pair<ReplicationServiceTestImpl<ReplicableInterface>, ReplicationMasterDescriptor> result = new Pair<>(replicaReplicator, masterDescriptor); Pair<ReplicationServiceTestImpl<ReplicableInterface>, ReplicationMasterDescriptor> result = new Pair<>(replicaReplicator, masterDescriptor);
logger.info("starting initial load transmission servlet for "+getClass().getName()); logger.info("starting initial load transmission servlet for "+getClass().getName());
initialLoadTestServerThread = replicaReplicator.startInitialLoadTransmissionServlet(); initialLoadTestServerThread = replicaReplicator.startInitialLoadTransmissionServlet();
@@ -136,16 +143,15 @@ public abstract class AbstractServerReplicationTest<ReplicableInterface extends
* any initialization that {@link #persistenceSetUp(boolean)} may have performed can be assumed to have taken place in the implementations * any initialization that {@link #persistenceSetUp(boolean)} may have performed can be assumed to have taken place in the implementations
* of this method. * of this method.
*/ */
abstract protected ReplicableImpl createNewMaster() throws Exception; protected abstract ReplicableImpl createNewMaster() throws Exception;
/** /**
* Creates a new replica replicable instance. This method is called only after {@link #persistenceSetUp(boolean)} has been called. Therefore, * Creates a new replica replicable instance. This method is called only after {@link #persistenceSetUp(boolean)} has been called. Therefore,
* any initialization that {@link #persistenceSetUp(boolean)} may have performed can be assumed to have taken place in the implementations * any initialization that {@link #persistenceSetUp(boolean)} may have performed can be assumed to have taken place in the implementations
* of this method. * of this method.
*/ */
abstract protected ReplicableImpl createNewReplica(); protected abstract ReplicableImpl createNewReplica() throws Exception;
@After
public void tearDown() throws Exception { public void tearDown() throws Exception {
logger.info("starting to tearDown() test"); logger.info("starting to tearDown() test");
persistenceTearDown(); persistenceTearDown();
@@ -160,7 +166,7 @@ public abstract class AbstractServerReplicationTest<ReplicableInterface extends
try { try {
if (initialLoadTestServerThread != null) { if (initialLoadTestServerThread != null) {
logger.info("found non-null initialLoadTestServerThread that we'll now try to stop..."); logger.info("found non-null initialLoadTestServerThread that we'll now try to stop...");
URLConnection urlConnection = new URL("http://localhost:"+SERVLET_PORT+"/STOP").openConnection(); // stop the initial load test server thread URLConnection urlConnection = new URL("http://localhost:"+servletPort+"/STOP").openConnection(); // stop the initial load test server thread
urlConnection.getInputStream().close(); urlConnection.getInputStream().close();
logger.info("sent and closed STOP request"); logger.info("sent and closed STOP request");
initialLoadTestServerThread.join(10000 /* wait 10s */); initialLoadTestServerThread.join(10000 /* wait 10s */);
@@ -191,16 +197,19 @@ public abstract class AbstractServerReplicationTest<ReplicableInterface extends
private final ReplicaDescriptor replicaDescriptor; private final ReplicaDescriptor replicaDescriptor;
private final ReplicationService masterReplicationService; private final ReplicationService masterReplicationService;
private final ReplicationMasterDescriptor masterDescriptor; private final ReplicationMasterDescriptor masterDescriptor;
private final int servletPort;
public ReplicationServiceTestImpl(String exchangeName, String exchangeHost, ReplicationInstancesManager replicationInstancesManager, public ReplicationServiceTestImpl(String exchangeName, String exchangeHost, ReplicationInstancesManager replicationInstancesManager,
ReplicaDescriptor replicaDescriptor, ReplicableInterface replica, ReplicaDescriptor replicaDescriptor, ReplicableInterface replica,
ReplicableInterface master, ReplicationService masterReplicationService, ReplicationMasterDescriptor masterDescriptor) ReplicableInterface master, ReplicationService masterReplicationService, ReplicationMasterDescriptor masterDescriptor,
int servletPort)
throws IOException { throws IOException {
super(exchangeName, exchangeHost, 0, replicationInstancesManager, new SingletonReplicablesProvider(replica)); super(exchangeName, exchangeHost, 0, replicationInstancesManager, new SingletonReplicablesProvider(replica));
this.replicaDescriptor = replicaDescriptor; this.replicaDescriptor = replicaDescriptor;
this.master = master; this.master = master;
this.masterReplicationService = masterReplicationService; this.masterReplicationService = masterReplicationService;
this.masterDescriptor = masterDescriptor; this.masterDescriptor = masterDescriptor;
this.servletPort = servletPort;
} }
private Thread startInitialLoadTransmissionServlet() throws InterruptedException { private Thread startInitialLoadTransmissionServlet() throws InterruptedException {
@@ -209,7 +218,7 @@ public abstract class AbstractServerReplicationTest<ReplicableInterface extends
public void run() { public void run() {
ServerSocket ss = null; ServerSocket ss = null;
try { try {
ss = new ServerSocket(SERVLET_PORT); ss = new ServerSocket(servletPort);
synchronized (listening) { synchronized (listening) {
listening[0] = true; listening[0] = true;
listening.notifyAll(); listening.notifyAll();
@@ -317,4 +326,20 @@ public abstract class AbstractServerReplicationTest<ReplicableInterface extends
} }
} }
} }
public ReplicableImpl getReplica() {
return replica;
}
public ReplicableImpl getMaster() {
return master;
}
public ReplicationServiceTestImpl<ReplicableInterface> getReplicaReplicator() {
return replicaReplicator;
}
public ReplicationMasterDescriptor getMasterDescriptor() {
return masterDescriptor;
}
} }
@@ -0,0 +1,25 @@
package com.sap.sse.replication.testsupport;
import java.util.HashSet;
import java.util.Set;
import org.junit.After;
import org.junit.Before;
public abstract class AbstractServerWithMultipleServicesReplicationTest {
protected final Set<AbstractServerReplicationTestSetUp<?, ?>> testSetUps = new HashSet<>();
@Before
public void setUp() throws Exception {
for (AbstractServerReplicationTestSetUp<?, ?> testSetUp : testSetUps) {
testSetUp.setUp();
}
}
@After
public void tearDown() throws Exception {
for (AbstractServerReplicationTestSetUp<?, ?> testSetUp : testSetUps) {
testSetUp.tearDown();
}
}
}
@@ -0,0 +1,63 @@
package com.sap.sse.replication.testsupport;
import java.io.IOException;
import org.junit.After;
import org.junit.Before;
import com.sap.sse.common.Util.Pair;
import com.sap.sse.replication.Replicable;
import com.sap.sse.replication.ReplicationMasterDescriptor;
import com.sap.sse.replication.testsupport.AbstractServerReplicationTestSetUp.ReplicationServiceTestImpl;
public abstract class AbstractServerWithSingleServiceReplicationTest<ReplicableInterface extends Replicable<?, ?>, ReplicableImpl extends ReplicableInterface> {
protected final AbstractServerReplicationTestSetUp<ReplicableInterface, ReplicableImpl> testSetUp;
protected ReplicableImpl replica;
protected ReplicableImpl master;
protected ReplicationServiceTestImpl<ReplicableInterface> replicaReplicator;
protected ReplicationMasterDescriptor masterDescriptor;
public AbstractServerWithSingleServiceReplicationTest(AbstractServerReplicationTestSetUp<ReplicableInterface, ReplicableImpl> testSetUp) {
this.testSetUp = testSetUp;
}
@Before
public void setUp() throws Exception {
testSetUp.setUp();
master = testSetUp.getMaster();
replica = testSetUp.getReplica();
replicaReplicator = testSetUp.getReplicaReplicator();
masterDescriptor = testSetUp.getMasterDescriptor();
}
protected Pair<ReplicationServiceTestImpl<ReplicableInterface>, ReplicationMasterDescriptor> basicSetUp(boolean dropDB,
ReplicableImpl master, ReplicableImpl replica) throws Exception {
return testSetUp.basicSetUp(dropDB, master, replica);
}
@After
public void tearDown() throws Exception {
testSetUp.tearDown();
}
protected ReplicableImpl createNewMaster() throws Exception {
return testSetUp.createNewMaster();
}
protected ReplicableImpl createNewReplica() throws Exception {
return testSetUp.createNewReplica();
}
protected void persistenceSetUp(boolean dropDB) {
testSetUp.persistenceSetUp(dropDB);
}
protected void persistenceTearDown() {
testSetUp.persistenceTearDown();
}
public void stopReplicatingToMaster() throws IOException {
testSetUp.stopReplicatingToMaster();
}
}
@@ -12,4 +12,4 @@ Require-Bundle: org.junit4;bundle-version="4.8.2",
com.rabbitmq.client, com.rabbitmq.client,
org.mongodb.mongo-java-driver;bundle-version="2.13.0", org.mongodb.mongo-java-driver;bundle-version="2.13.0",
com.sap.sse.replication.testsupport, com.sap.sse.replication.testsupport,
com.sap.sse.replication.testsupport com.sap.sse.mail.replication.testsupport
@@ -4,31 +4,37 @@ import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import com.sap.sse.mongodb.MongoDBService; import com.sap.sse.mongodb.MongoDBService;
import com.sap.sse.replication.testsupport.AbstractServerReplicationTest; import com.sap.sse.replication.testsupport.AbstractServerWithSingleServiceReplicationTest;
import com.sap.sse.security.SecurityService; import com.sap.sse.security.SecurityService;
import com.sap.sse.security.impl.SecurityServiceImpl; import com.sap.sse.security.impl.SecurityServiceImpl;
import com.sap.sse.security.userstore.mongodb.UserStoreImpl; import com.sap.sse.security.userstore.mongodb.UserStoreImpl;
public abstract class AbstractSecurityReplicationTest extends AbstractServerReplicationTest<SecurityService, SecurityServiceImpl> { public abstract class AbstractSecurityReplicationTest extends AbstractServerWithSingleServiceReplicationTest<SecurityService, SecurityServiceImpl> {
private MongoDBService mongoDBService; public AbstractSecurityReplicationTest() {
super(new ServerReplicationTestSetUp());
}
@Override private static class ServerReplicationTestSetUp extends com.sap.sse.replication.testsupport.AbstractServerReplicationTestSetUp<SecurityService, SecurityServiceImpl> {
protected void persistenceSetUp(boolean dropDB) { private MongoDBService mongoDBService;
mongoDBService = MongoDBService.INSTANCE;
if (dropDB) { @Override
mongoDBService.getDB().dropDatabase(); protected void persistenceSetUp(boolean dropDB) {
mongoDBService = MongoDBService.INSTANCE;
if (dropDB) {
mongoDBService.getDB().dropDatabase();
}
}
@Override
protected SecurityServiceImpl createNewMaster() throws MalformedURLException, IOException, InterruptedException {
SecurityServiceImpl result = new SecurityServiceImpl(new UserStoreImpl());
result.clearReplicaState();
return result;
}
@Override
protected SecurityServiceImpl createNewReplica() {
return new SecurityServiceImpl(new UserStoreImpl());
} }
} }
@Override
protected SecurityServiceImpl createNewMaster() throws MalformedURLException, IOException, InterruptedException {
SecurityServiceImpl result = new SecurityServiceImpl(new UserStoreImpl());
result.clearReplicaState();
return result;
}
@Override
protected SecurityServiceImpl createNewReplica() {
return new SecurityServiceImpl(new UserStoreImpl());
}
} }