mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-09-30 09:26:44 +00:00
made JMS test work locally with mocked in-VM connection
This commit is contained in:
+80
-27
@@ -8,8 +8,12 @@ import java.io.ObjectOutputStream;
|
||||
import java.io.PipedInputStream;
|
||||
import java.io.PipedOutputStream;
|
||||
import java.net.InetAddress;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import javax.jms.Connection;
|
||||
import javax.jms.JMSException;
|
||||
import javax.jms.Session;
|
||||
import javax.jms.Topic;
|
||||
import javax.jms.TopicSubscriber;
|
||||
@@ -24,12 +28,12 @@ import com.sap.sailing.domain.common.impl.Util;
|
||||
import com.sap.sailing.server.RacingEventService;
|
||||
import com.sap.sailing.server.impl.RacingEventServiceImpl;
|
||||
import com.sap.sailing.server.replication.ReplicaDescriptor;
|
||||
import com.sap.sailing.server.replication.ReplicationMasterDescriptor;
|
||||
import com.sap.sailing.server.replication.ReplicationService;
|
||||
import com.sap.sailing.server.replication.impl.HasRacingEventService;
|
||||
import com.sap.sailing.server.replication.impl.MessageBrokerConfiguration;
|
||||
import com.sap.sailing.server.replication.impl.MessageBrokerManager;
|
||||
import com.sap.sailing.server.replication.impl.ReplicationInstancesManager;
|
||||
import com.sap.sailing.server.replication.impl.ReplicationMasterDescriptorImpl;
|
||||
import com.sap.sailing.server.replication.impl.ReplicationServiceImpl;
|
||||
import com.sap.sailing.server.replication.impl.Replicator;
|
||||
|
||||
@@ -54,8 +58,29 @@ public class ServerReplicationTest {
|
||||
ReplicationService masterReplicator = new ReplicationServiceImpl(rim, brokerMgr, master);
|
||||
ReplicaDescriptor replicaDescriptor = new ReplicaDescriptor(InetAddress.getLocalHost());
|
||||
masterReplicator.registerReplica(replicaDescriptor);
|
||||
ReplicationMasterDescriptorImpl masterDescriptor = new ReplicationMasterDescriptorImpl("localhost", 8888, 61616);
|
||||
ReplicationService replicaReplicator = new ReplicationServiceImpl(rim, brokerMgr, replica);
|
||||
ReplicationMasterDescriptor masterDescriptor = new ReplicationMasterDescriptor() {
|
||||
@Override
|
||||
public URL getReplicationRegistrationRequestURL() throws MalformedURLException {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
@Override
|
||||
public URL getInitialLoadURL() throws MalformedURLException {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TopicSubscriber getTopicSubscriber(String clientID) throws JMSException, UnknownHostException {
|
||||
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
|
||||
ActiveMQConnection.DEFAULT_PASSWORD, "vm://localhost-jms-connection");
|
||||
connectionFactory.setClientID(clientID);
|
||||
Connection connection = connectionFactory.createConnection();
|
||||
connection.start();
|
||||
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
|
||||
Topic topic = session.createTopic(ReplicationService.SAILING_SERVER_REPLICATION_TOPIC);
|
||||
return session.createDurableSubscriber(topic, InetAddress.getLocalHost().getHostAddress());
|
||||
}
|
||||
};
|
||||
ReplicationService replicaReplicator = new ReplicationServiceTestImpl(resolveAgainst, rim, brokerMgr, replicaDescriptor, replica, master, masterReplicator);
|
||||
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
|
||||
ActiveMQConnection.DEFAULT_PASSWORD, "vm://localhost-jms-connection");
|
||||
connectionFactory.setClientID("Test Client");
|
||||
@@ -68,7 +93,7 @@ public class ServerReplicationTest {
|
||||
replicaReplicator.startToReplicateFrom(masterDescriptor);
|
||||
}
|
||||
|
||||
private Replicator createReplicator(ReplicationMasterDescriptorImpl masterDescriptor, final RacingEventService master) {
|
||||
private Replicator createReplicator(ReplicationMasterDescriptor masterDescriptor, final RacingEventService master) {
|
||||
return new Replicator(masterDescriptor, new HasRacingEventService() {
|
||||
@Override
|
||||
public RacingEventService getRacingEventService() {
|
||||
@@ -79,35 +104,63 @@ public class ServerReplicationTest {
|
||||
|
||||
@Test
|
||||
public void testBasicInitialLoad() throws Exception {
|
||||
initialLoad();
|
||||
assertEquals(Util.size(master.getAllEvents()), Util.size(replica.getAllEvents()));
|
||||
assertEquals(master.getLeaderboardGroups().size(), replica.getLeaderboardGroups().size());
|
||||
assertEquals(master.getLeaderboards().size(), replica.getLeaderboards().size());
|
||||
assertEquals(master.getLeaderboards().keySet(), replica.getLeaderboards().keySet());
|
||||
}
|
||||
|
||||
/**
|
||||
* Clones the {@link #master}'s state to the {@link #replica} using
|
||||
* {@link RacingEventServiceImpl#serializeForInitialReplication(ObjectOutputStream)} and
|
||||
* {@link RacingEventServiceImpl#initiallyFillFrom(ObjectInputStream)} through a piped input/output stream.
|
||||
*/
|
||||
private void initialLoad() throws IOException, ClassNotFoundException {
|
||||
PipedOutputStream pos = new PipedOutputStream();
|
||||
PipedInputStream pis = new PipedInputStream(pos);
|
||||
final ObjectOutputStream oos = new ObjectOutputStream(pos);
|
||||
new Thread("clone writer") {
|
||||
public void run() {
|
||||
try {
|
||||
master.serializeForInitialReplication(oos);
|
||||
oos.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException(e);
|
||||
private static class ReplicationServiceTestImpl extends ReplicationServiceImpl {
|
||||
private final DomainFactory resolveAgainst;
|
||||
private final RacingEventService master;
|
||||
private final ReplicaDescriptor replicaDescriptor;
|
||||
private final ReplicationService masterReplicationService;
|
||||
|
||||
public ReplicationServiceTestImpl(DomainFactory resolveAgainst,
|
||||
ReplicationInstancesManager replicationInstancesManager, MessageBrokerManager messageBrokerManager,
|
||||
ReplicaDescriptor replicaDescriptor, RacingEventService replica, RacingEventService master, ReplicationService masterReplicationService) {
|
||||
super(replicationInstancesManager, messageBrokerManager, replica);
|
||||
this.resolveAgainst = resolveAgainst;
|
||||
this.replicaDescriptor = replicaDescriptor;
|
||||
this.master = master;
|
||||
this.masterReplicationService = masterReplicationService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ignore the master descriptor and replicate from the local master passed to the constructor instead.
|
||||
*/
|
||||
@Override
|
||||
public void startToReplicateFrom(ReplicationMasterDescriptor master) throws IOException,
|
||||
ClassNotFoundException, JMSException {
|
||||
masterReplicationService.registerReplica(replicaDescriptor);
|
||||
TopicSubscriber replicationSubscription = master.getTopicSubscriber(replicaDescriptor.getUuid().toString());
|
||||
replicationSubscription.setMessageListener(new Replicator(master, this));
|
||||
initialLoad();
|
||||
}
|
||||
|
||||
/**
|
||||
* Clones the {@link #master}'s state to the {@link #replica} using
|
||||
* {@link RacingEventServiceImpl#serializeForInitialReplication(ObjectOutputStream)} and
|
||||
* {@link RacingEventServiceImpl#initiallyFillFrom(ObjectInputStream)} through a piped input/output stream.
|
||||
*/
|
||||
private void initialLoad() throws IOException, ClassNotFoundException {
|
||||
PipedOutputStream pos = new PipedOutputStream();
|
||||
PipedInputStream pis = new PipedInputStream(pos);
|
||||
final ObjectOutputStream oos = new ObjectOutputStream(pos);
|
||||
new Thread("clone writer") {
|
||||
public void run() {
|
||||
try {
|
||||
master.serializeForInitialReplication(oos);
|
||||
oos.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}.start();
|
||||
ObjectInputStream dis = resolveAgainst.createObjectInputStreamResolvingAgainstThisFactory(pis);
|
||||
replica.initiallyFillFrom(dis);
|
||||
dis.close();
|
||||
}.start();
|
||||
ObjectInputStream dis = resolveAgainst.createObjectInputStreamResolvingAgainstThisFactory(pis);
|
||||
getRacingEventService().initiallyFillFrom(dis);
|
||||
dis.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+6
-6
@@ -54,7 +54,7 @@ public class ReplicationServiceImpl implements ReplicationService, OperationExec
|
||||
|
||||
private ServiceTracker<RacingEventService, RacingEventService> racingEventServiceTracker;
|
||||
|
||||
private final RacingEventService master;
|
||||
private final RacingEventService localService;
|
||||
|
||||
/**
|
||||
* The UUIDs with which this replica is registered by the master identified by the corresponding key
|
||||
@@ -69,7 +69,7 @@ public class ReplicationServiceImpl implements ReplicationService, OperationExec
|
||||
racingEventServiceTracker = new ServiceTracker<RacingEventService, RacingEventService>(
|
||||
Activator.getDefaultContext(), RacingEventService.class.getName(), null);
|
||||
racingEventServiceTracker.open();
|
||||
master = null;
|
||||
localService = null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -77,18 +77,18 @@ public class ReplicationServiceImpl implements ReplicationService, OperationExec
|
||||
* an OSGi service tracker to discover the {@link RacingEventService}, the service to replicate is "injected" here.
|
||||
*/
|
||||
public ReplicationServiceImpl(final ReplicationInstancesManager replicationInstancesManager,
|
||||
final MessageBrokerManager messageBrokerManager, RacingEventService master) {
|
||||
final MessageBrokerManager messageBrokerManager, RacingEventService localService) {
|
||||
this.replicationInstancesManager = replicationInstancesManager;
|
||||
replicaUUIDs = new HashMap<ReplicationMasterDescriptor, String>();
|
||||
this.messageBrokerManager = messageBrokerManager;
|
||||
this.master = master;
|
||||
this.localService = localService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RacingEventService getRacingEventService() {
|
||||
RacingEventService result;
|
||||
if (master != null) {
|
||||
result = master;
|
||||
if (localService != null) {
|
||||
result = localService;
|
||||
} else {
|
||||
result = racingEventServiceTracker.getService();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user