use of org.apache.sling.discovery.commons.providers.DefaultInstanceDescription in project sling by apache.
the class TopologyHelper method cloneTopologyView.
public static DefaultTopologyView cloneTopologyView(DefaultTopologyView view, String newLeader) throws NoSuchFieldException {
final DefaultTopologyView clone = cloneTopologyView(view);
final DefaultClusterView cluster = (DefaultClusterView) clone.getClusterViews().iterator().next();
for (Iterator it = cluster.getInstances().iterator(); it.hasNext(); ) {
DefaultInstanceDescription id = (DefaultInstanceDescription) it.next();
PrivateAccessor.setField(id, "isLeader", id.getSlingId().equals(newLeader));
}
return clone;
}
use of org.apache.sling.discovery.commons.providers.DefaultInstanceDescription in project sling by apache.
the class TestHelper method newView.
public static DummyTopologyView newView(String syncId, String clusterId, boolean isCurrent, String leaderId, String localId, String... slingIds) {
DummyTopologyView topology = new DummyTopologyView(syncId);
DefaultClusterView cluster = new DefaultClusterView(clusterId);
for (String slingId : slingIds) {
DefaultInstanceDescription id = new DefaultInstanceDescription(cluster, slingId.equals(leaderId), slingId.equals(localId), slingId, new HashMap<String, String>());
topology.addInstanceDescription(id);
}
if (!isCurrent) {
topology.setNotCurrent();
}
return topology;
}
use of org.apache.sling.discovery.commons.providers.DefaultInstanceDescription in project sling by apache.
the class TestViewStateManager method testChangedPropertiesChanged.
@Test
public void testChangedPropertiesChanged() throws Exception {
final DummyListener listener = new DummyListener();
mgr.installMinEventDelayHandler(new DiscoveryService() {
@Override
public TopologyView getTopology() {
throw new IllegalStateException("not yet impl");
}
}, new DummyScheduler(), 1);
mgr.handleActivated();
TestHelper.assertNoEvents(listener);
mgr.bind(listener);
TestHelper.assertNoEvents(listener);
mgr.handleChanging();
TestHelper.assertNoEvents(listener);
final BaseTopologyView view1 = new DummyTopologyView().addInstance();
InstanceDescription instance1 = view1.getInstances().iterator().next();
ClusterView cluster1 = instance1.getClusterView();
mgr.handleNewView(view1);
assertEvents(listener, EventHelper.newInitEvent(view1));
DefaultClusterView cluster2 = new DefaultClusterView(new String(cluster1.getId()));
final BaseTopologyView view2 = new DummyTopologyView(view1.getLocalClusterSyncTokenId()).addInstance(instance1.getSlingId(), cluster2, instance1.isLeader(), instance1.isLocal());
DefaultInstanceDescription instance2 = (DefaultInstanceDescription) view2.getLocalInstance();
instance2.setProperty("foo", "bar");
mgr.handleNewView(view2);
assertEvents(listener, EventHelper.newPropertiesChangedEvent(view1, view2));
}
use of org.apache.sling.discovery.commons.providers.DefaultInstanceDescription in project sling by apache.
the class TestViewStateManager method testOnlyDiffersInProperties.
@Test
public void testOnlyDiffersInProperties() throws Exception {
final org.apache.log4j.Logger discoveryLogger = RootLogger.getLogger("org.apache.sling.discovery");
// changed from Level.DEBUG
discoveryLogger.setLevel(Level.INFO);
logger.info("testOnlyDiffersInProperties: start");
final String slingId1 = UUID.randomUUID().toString();
final String slingId2 = UUID.randomUUID().toString();
final String slingId3 = UUID.randomUUID().toString();
final String clusterId = UUID.randomUUID().toString();
final DefaultClusterView cluster = new DefaultClusterView(clusterId);
final DummyTopologyView view1 = new DummyTopologyView().addInstance(slingId1, cluster, true, true).addInstance(slingId2, cluster, false, false).addInstance(slingId3, cluster, false, false);
final DummyTopologyView view2 = DummyTopologyView.clone(view1).removeInstance(slingId2);
final DummyTopologyView view3 = DummyTopologyView.clone(view1).removeInstance(slingId2).removeInstance(slingId3);
DummyTopologyView view1Cloned = DummyTopologyView.clone(view1);
logger.info("testOnlyDiffersInProperties: handleNewView(view1)");
mgr.handleNewView(view1);
logger.info("testOnlyDiffersInProperties: handleActivated()");
mgr.handleActivated();
assertEquals(0, mgr.waitForAsyncEvents(5000));
assertFalse(mgr.onlyDiffersInProperties(view1));
assertFalse(mgr.onlyDiffersInProperties(view2));
assertFalse(mgr.onlyDiffersInProperties(view3));
logger.info("testOnlyDiffersInProperties: handleNewView(view2)");
mgr.handleNewView(view2);
assertEquals(0, mgr.waitForAsyncEvents(5000));
assertFalse(mgr.onlyDiffersInProperties(view1));
assertFalse(mgr.onlyDiffersInProperties(view2));
assertFalse(mgr.onlyDiffersInProperties(view3));
logger.info("testOnlyDiffersInProperties: handleNewView(view3)");
mgr.handleNewView(view3);
assertEquals(0, mgr.waitForAsyncEvents(5000));
assertFalse(mgr.onlyDiffersInProperties(view1));
assertFalse(mgr.onlyDiffersInProperties(view2));
assertFalse(mgr.onlyDiffersInProperties(view3));
final DummyTopologyView view4 = DummyTopologyView.clone(view1Cloned);
final DummyTopologyView view5 = DummyTopologyView.clone(view1Cloned);
final DummyTopologyView view6 = DummyTopologyView.clone(view1Cloned);
logger.info("testOnlyDiffersInProperties: handleNewView(view1cloned)");
mgr.handleNewView(view1Cloned);
assertEquals(0, mgr.waitForAsyncEvents(5000));
DefaultInstanceDescription i4_1 = (DefaultInstanceDescription) view4.getInstance(slingId1);
i4_1.setProperty("a", "b");
logger.info("testOnlyDiffersInProperties: onlyDiffersInProperties(view4)");
assertTrue(mgr.onlyDiffersInProperties(view4));
DefaultInstanceDescription i5_1 = (DefaultInstanceDescription) view5.getInstance(slingId1);
i5_1.setProperty("a", "b");
logger.info("testOnlyDiffersInProperties: onlyDiffersInProperties(view5)");
assertTrue(mgr.onlyDiffersInProperties(view5));
DummyTopologyView view4Cloned = DummyTopologyView.clone(view4);
mgr.handleNewView(view4);
assertEquals(0, mgr.waitForAsyncEvents(5000));
logger.info("testOnlyDiffersInProperties: onlyDiffersInProperties(view4Cloned)");
assertFalse(mgr.onlyDiffersInProperties(view4Cloned));
logger.info("testOnlyDiffersInProperties: onlyDiffersInProperties(view5)");
assertFalse(mgr.onlyDiffersInProperties(view5));
DefaultInstanceDescription i6_1 = (DefaultInstanceDescription) view6.getInstance(slingId1);
i6_1.setProperty("a", "c");
logger.info("testOnlyDiffersInProperties: onlyDiffersInProperties(view6)");
assertTrue(mgr.onlyDiffersInProperties(view6));
String originalId = view6.getLocalClusterSyncTokenId();
view6.setId(UUID.randomUUID().toString());
logger.info("testOnlyDiffersInProperties: onlyDiffersInProperties(view6) [2]");
assertFalse(mgr.onlyDiffersInProperties(view6));
view6.setId(originalId);
logger.info("testOnlyDiffersInProperties: onlyDiffersInProperties(view6) [3]");
assertTrue(mgr.onlyDiffersInProperties(view6));
view6.setId(null);
logger.info("testOnlyDiffersInProperties: onlyDiffersInProperties(view6) [4]");
assertFalse(mgr.onlyDiffersInProperties(view6));
view6.setId(originalId);
logger.info("testOnlyDiffersInProperties: onlyDiffersInProperties(view6) [5]");
assertTrue(mgr.onlyDiffersInProperties(view6));
// hack: we're modifying the view *in the ViewStateManagerImpl* here!!:
view4.setId(null);
view6.setId(null);
logger.info("testOnlyDiffersInProperties: onlyDiffersInProperties(view6) [6]");
assertTrue(mgr.onlyDiffersInProperties(view6));
view6.setId(originalId);
logger.info("testOnlyDiffersInProperties: onlyDiffersInProperties(view6) [7]");
assertFalse(mgr.onlyDiffersInProperties(view6));
}
use of org.apache.sling.discovery.commons.providers.DefaultInstanceDescription in project sling by apache.
the class TestViewStateManager method testPropertiesChanged.
@Test
public void testPropertiesChanged() throws Exception {
final DummyListener listener = new DummyListener();
mgr.handleActivated();
mgr.bind(listener);
mgr.handleChanging();
DummyTopologyView oldView = new DummyTopologyView().addInstance();
DefaultInstanceDescription localInstance = (DefaultInstanceDescription) oldView.getLocalInstance();
localInstance.setProperty("foo", "bar1");
mgr.handleNewView(oldView);
TopologyEvent initEvent = EventHelper.newInitEvent(oldView.clone());
assertEvents(listener, initEvent);
DummyTopologyView newView = oldView.clone();
oldView.setNotCurrent();
localInstance = (DefaultInstanceDescription) newView.getLocalInstance();
localInstance.setProperty("foo", "bar2");
mgr.handleNewView(newView);
Thread.sleep(2000);
TopologyEvent propertiesChangedEvent = EventHelper.newPropertiesChangedEvent(oldView.clone(), newView.clone());
assertEvents(listener, propertiesChangedEvent);
}
Aggregations