Search in sources :

Example 11 with TopologyEvent

use of org.apache.sling.discovery.TopologyEvent in project sling by apache.

the class NoClusterDiscoveryServiceTest method testListenerAfter.

@Test
public void testListenerAfter() throws Exception {
    final DiscoveryService service = this.createService(true);
    final List<TopologyEvent> events = new ArrayList<TopologyEvent>();
    final TopologyEventListener listener = new TopologyEventListener() {

        @Override
        public void handleTopologyEvent(final TopologyEvent event) {
            events.add(event);
        }
    };
    invoke(service, "bindTopologyEventListener", new Class[] { TopologyEventListener.class }, new Object[] { listener });
    assertEquals(1, events.size());
    assertEquals(TopologyEvent.Type.TOPOLOGY_INIT, events.get(0).getType());
    assertNotNull(events.get(0).getNewView());
    assertNull(events.get(0).getOldView());
}
Also used : TopologyEvent(org.apache.sling.discovery.TopologyEvent) ArrayList(java.util.ArrayList) DiscoveryService(org.apache.sling.discovery.DiscoveryService) TopologyEventListener(org.apache.sling.discovery.TopologyEventListener) Test(org.junit.Test)

Example 12 with TopologyEvent

use of org.apache.sling.discovery.TopologyEvent in project sling by apache.

the class ChaosTest method setupChaosThreads.

/**
     * Setup chaos thread(s)
     *
     * Chaos is right now created by sending topology changing/changed events randomly
     */
private void setupChaosThreads(final List<Thread> threads, final AtomicLong finishedThreads) {
    final List<TopologyView> views = new ArrayList<>();
    // register topology listener
    final ServiceRegistration<TopologyEventListener> reg = this.bc.registerService(TopologyEventListener.class, new TopologyEventListener() {

        @Override
        public void handleTopologyEvent(final TopologyEvent event) {
            if (event.getType() == Type.TOPOLOGY_INIT) {
                views.add(event.getNewView());
            }
        }
    }, null);
    while (views.isEmpty()) {
        this.sleep(10);
    }
    reg.unregister();
    final TopologyView view = views.get(0);
    try {
        final Collection<ServiceReference<TopologyEventListener>> refs = this.bc.getServiceReferences(TopologyEventListener.class, null);
        assertNotNull(refs);
        assertFalse(refs.isEmpty());
        TopologyEventListener found = null;
        for (final ServiceReference<TopologyEventListener> ref : refs) {
            final TopologyEventListener listener = this.bc.getService(ref);
            if (listener != null && listener.getClass().getName().equals("org.apache.sling.event.impl.jobs.config.TopologyHandler")) {
                found = listener;
                break;
            }
            bc.ungetService(ref);
        }
        assertNotNull(found);
        final TopologyEventListener tel = found;
        threads.add(new Thread() {

            private final Random random = new Random();

            @Override
            public void run() {
                final long startTime = System.currentTimeMillis();
                // this thread runs 30 seconds longer than the job creation thread
                final long endTime = startTime + (DURATION + 30) * 1000;
                while (System.currentTimeMillis() < endTime) {
                    final int sleepTime = random.nextInt(25) + 15;
                    try {
                        Thread.sleep(sleepTime * 1000);
                    } catch (final InterruptedException ie) {
                        Thread.currentThread().interrupt();
                    }
                    tel.handleTopologyEvent(new TopologyEvent(Type.TOPOLOGY_CHANGING, view, null));
                    final int changingTime = random.nextInt(20) + 3;
                    try {
                        Thread.sleep(changingTime * 1000);
                    } catch (final InterruptedException ie) {
                        Thread.currentThread().interrupt();
                    }
                    tel.handleTopologyEvent(new TopologyEvent(Type.TOPOLOGY_CHANGED, view, view));
                }
                tel.getClass().getName();
                finishedThreads.incrementAndGet();
            }
        });
    } catch (InvalidSyntaxException e) {
        e.printStackTrace();
    }
}
Also used : TopologyEvent(org.apache.sling.discovery.TopologyEvent) ArrayList(java.util.ArrayList) ServiceReference(org.osgi.framework.ServiceReference) Random(java.util.Random) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) TopologyView(org.apache.sling.discovery.TopologyView) TopologyEventListener(org.apache.sling.discovery.TopologyEventListener)

Example 13 with TopologyEvent

use of org.apache.sling.discovery.TopologyEvent in project sling by apache.

the class NoClusterDiscoveryServiceTest method testListenerBefore.

@Test
public void testListenerBefore() throws Exception {
    final DiscoveryService service = this.createService(false);
    final List<TopologyEvent> events = new ArrayList<TopologyEvent>();
    final TopologyEventListener listener = new TopologyEventListener() {

        @Override
        public void handleTopologyEvent(final TopologyEvent event) {
            events.add(event);
        }
    };
    invoke(service, "bindTopologyEventListener", new Class[] { TopologyEventListener.class }, new Object[] { listener });
    assertEquals(0, events.size());
    invoke(service, "activate");
    assertEquals(1, events.size());
    assertEquals(TopologyEvent.Type.TOPOLOGY_INIT, events.get(0).getType());
    assertNotNull(events.get(0).getNewView());
    assertNull(events.get(0).getOldView());
}
Also used : TopologyEvent(org.apache.sling.discovery.TopologyEvent) ArrayList(java.util.ArrayList) DiscoveryService(org.apache.sling.discovery.DiscoveryService) TopologyEventListener(org.apache.sling.discovery.TopologyEventListener) Test(org.junit.Test)

Example 14 with TopologyEvent

use of org.apache.sling.discovery.TopologyEvent in project sling by apache.

the class NoClusterDiscoveryServiceTest method testPropertyChanges.

@Test
public void testPropertyChanges() throws Exception {
    final DiscoveryService service = this.createService(true);
    final List<TopologyEvent> events = new ArrayList<TopologyEvent>();
    final TopologyEventListener listener = new TopologyEventListener() {

        @Override
        public void handleTopologyEvent(final TopologyEvent event) {
            events.add(event);
        }
    };
    invoke(service, "bindTopologyEventListener", new Class[] { TopologyEventListener.class }, new Object[] { listener });
    events.clear();
    final PropertyProvider provider = new PropertyProvider() {

        @Override
        public String getProperty(final String name) {
            if ("a".equals(name)) {
                return "1";
            }
            if ("b".equals(name)) {
                return "2";
            }
            if ("c".equals(name)) {
                return "3";
            }
            return null;
        }
    };
    final Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(PropertyProvider.PROPERTY_PROPERTIES, new String[] { "a", "b", "c" });
    properties.put(Constants.SERVICE_ID, 1L);
    invoke(service, "bindPropertyProvider", new Class[] { PropertyProvider.class, Map.class }, new Object[] { provider, properties });
    assertEquals(1, events.size());
    assertEquals(TopologyEvent.Type.PROPERTIES_CHANGED, events.get(0).getType());
    assertNotNull(events.get(0).getNewView());
    assertTrue(events.get(0).getNewView().isCurrent());
    assertNotNull(events.get(0).getOldView());
    assertFalse(events.get(0).getOldView().isCurrent());
    // test properties
    assertEquals("1", events.get(0).getNewView().getLocalInstance().getProperty("a"));
    assertEquals("2", events.get(0).getNewView().getLocalInstance().getProperty("b"));
    assertEquals("3", events.get(0).getNewView().getLocalInstance().getProperty("c"));
    assertNull(events.get(0).getOldView().getLocalInstance().getProperty("a"));
    assertNull(events.get(0).getOldView().getLocalInstance().getProperty("b"));
    assertNull(events.get(0).getOldView().getLocalInstance().getProperty("c"));
    events.clear();
    invoke(service, "unbindPropertyProvider", new Class[] { PropertyProvider.class, Map.class }, new Object[] { provider, properties });
    assertEquals(1, events.size());
    assertEquals(TopologyEvent.Type.PROPERTIES_CHANGED, events.get(0).getType());
    assertNotNull(events.get(0).getNewView());
    assertTrue(events.get(0).getNewView().isCurrent());
    assertNotNull(events.get(0).getOldView());
    assertFalse(events.get(0).getOldView().isCurrent());
    assertEquals("1", events.get(0).getOldView().getLocalInstance().getProperty("a"));
    assertEquals("2", events.get(0).getOldView().getLocalInstance().getProperty("b"));
    assertEquals("3", events.get(0).getOldView().getLocalInstance().getProperty("c"));
    assertNull(events.get(0).getNewView().getLocalInstance().getProperty("a"));
    assertNull(events.get(0).getNewView().getLocalInstance().getProperty("b"));
    assertNull(events.get(0).getNewView().getLocalInstance().getProperty("c"));
}
Also used : PropertyProvider(org.apache.sling.discovery.PropertyProvider) HashMap(java.util.HashMap) TopologyEvent(org.apache.sling.discovery.TopologyEvent) ArrayList(java.util.ArrayList) DiscoveryService(org.apache.sling.discovery.DiscoveryService) TopologyEventListener(org.apache.sling.discovery.TopologyEventListener) Test(org.junit.Test)

Example 15 with TopologyEvent

use of org.apache.sling.discovery.TopologyEvent in project sling by apache.

the class TopologyHandlerTest method testHandleTopologyEvent.

@Test
public void testHandleTopologyEvent() {
    TopologyView oldView = new TopologyViewImpl();
    TopologyView newView = newViewWithInstanceDescription(true);
    TopologyEvent event = new TopologyEvent(TopologyEvent.Type.PROPERTIES_CHANGED, oldView, newView);
    underTest.handleTopologyEvent(event);
    assertTrue(QuartzJobExecutor.IS_LEADER.get());
    assertFalse(QuartzJobExecutor.DISCOVERY_INFO_AVAILABLE.get());
    event = new TopologyEvent(TopologyEvent.Type.TOPOLOGY_INIT, null, newView);
    underTest.handleTopologyEvent(event);
    assertTrue(QuartzJobExecutor.IS_LEADER.get());
    assertTrue(QuartzJobExecutor.DISCOVERY_INFO_AVAILABLE.get());
    event = new TopologyEvent(TopologyEvent.Type.TOPOLOGY_CHANGED, oldView, newView);
    underTest.handleTopologyEvent(event);
    assertTrue(QuartzJobExecutor.IS_LEADER.get());
    assertTrue(QuartzJobExecutor.DISCOVERY_INFO_AVAILABLE.get());
    newView = newViewWithInstanceDescription(false);
    event = new TopologyEvent(TopologyEvent.Type.TOPOLOGY_CHANGED, oldView, newView);
    underTest.handleTopologyEvent(event);
    assertFalse(QuartzJobExecutor.IS_LEADER.get());
    assertTrue(QuartzJobExecutor.DISCOVERY_INFO_AVAILABLE.get());
    event = new TopologyEvent(TopologyEvent.Type.TOPOLOGY_CHANGING, oldView, null);
    underTest.handleTopologyEvent(event);
    assertFalse(QuartzJobExecutor.IS_LEADER.get());
    assertFalse(QuartzJobExecutor.DISCOVERY_INFO_AVAILABLE.get());
}
Also used : TopologyViewImpl(org.apache.sling.discovery.impl.topology.TopologyViewImpl) TopologyEvent(org.apache.sling.discovery.TopologyEvent) TopologyView(org.apache.sling.discovery.TopologyView) Test(org.junit.Test)

Aggregations

TopologyEvent (org.apache.sling.discovery.TopologyEvent)21 TopologyEventListener (org.apache.sling.discovery.TopologyEventListener)10 Test (org.junit.Test)10 TopologyView (org.apache.sling.discovery.TopologyView)7 ArrayList (java.util.ArrayList)5 Type (org.apache.sling.discovery.TopologyEvent.Type)4 DiscoveryService (org.apache.sling.discovery.DiscoveryService)3 InstanceDescription (org.apache.sling.discovery.InstanceDescription)2 InitDelayingTopologyEventListener (org.apache.sling.discovery.commons.InitDelayingTopologyEventListener)2 DummyTopologyView (org.apache.sling.discovery.commons.providers.DummyTopologyView)2 HashMap (java.util.HashMap)1 Random (java.util.Random)1 Semaphore (java.util.concurrent.Semaphore)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 Level (org.apache.log4j.Level)1 PersistenceException (org.apache.sling.api.resource.PersistenceException)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 Scheduler (org.apache.sling.commons.scheduler.Scheduler)1 PropertyProvider (org.apache.sling.discovery.PropertyProvider)1