Search in sources :

Example 1 with PlatformEvent

use of org.eclipse.scout.rt.platform.PlatformEvent in project scout.rt by eclipse.

the class PlatformImplementorTest method doTestAwaitPlatformStartedFailsInState.

protected void doTestAwaitPlatformStartedFailsInState(final State platformStartFailsInState) throws Exception {
    // create platform listener that fails in given state
    BeanMetaData bean = new BeanMetaData(IPlatformListener.class).withApplicationScoped(true).withInitialInstance(new IPlatformListener() {

        @Override
        public void stateChanged(PlatformEvent event) {
            if (event.getState() == platformStartFailsInState) {
                throw new TestingPlatformStartupException();
            }
        }
    });
    // create platform instance and start it in another thread
    final TestingPlatformImplementor platform = new TestingPlatformImplementor(bean);
    Future<?> platformStartFuture = startPlatformInAnotherThread(platform);
    // current thread is expected to be suspended
    assertAwaitPlatformStarted(platform, platformStartFailsInState == null);
    // wait until platform starter has been finished and either expect no failures or the intended startup exception
    if (platformStartFailsInState == null) {
        platformStartFuture.get();
    } else {
        try {
            platformStartFuture.get();
            fail("Platform is not expected to be started");
        } catch (ExecutionException e) {
            assertSame(TestingPlatformStartupException.class, e.getCause().getClass());
        }
    }
    // again awaitPlatformStartedFails
    assertAwaitPlatformStarted(platform, platformStartFailsInState == null);
}
Also used : BeanMetaData(org.eclipse.scout.rt.platform.BeanMetaData) PlatformEvent(org.eclipse.scout.rt.platform.PlatformEvent) ExecutionException(java.util.concurrent.ExecutionException) IPlatformListener(org.eclipse.scout.rt.platform.IPlatformListener)

Example 2 with PlatformEvent

use of org.eclipse.scout.rt.platform.PlatformEvent in project scout.rt by eclipse.

the class PlatformImplementorTest method testAwaitPlatformStartedFailsInPlatformStopping.

@Test
public void testAwaitPlatformStartedFailsInPlatformStopping() throws Exception {
    final CountDownLatch stoppingLatch = new CountDownLatch(1);
    final CountDownLatch continueStoppingLatch = new CountDownLatch(1);
    // crate platform listener that signals stopping state
    BeanMetaData bean = new BeanMetaData(IPlatformListener.class).withApplicationScoped(true).withInitialInstance(new IPlatformListener() {

        @Override
        public void stateChanged(PlatformEvent event) {
            if (event.getState() == State.PlatformStopping) {
                stoppingLatch.countDown();
                try {
                    continueStoppingLatch.await();
                } catch (InterruptedException e) {
                // nop
                }
            }
        }
    });
    final TestingPlatformImplementor platform = new TestingPlatformImplementor(bean);
    platform.start();
    // expect platform started
    platform.awaitPlatformStarted();
    Future<?> platformStopFuture = s_executor.submit(new Runnable() {

        @Override
        public void run() {
            platform.stop();
        }
    });
    stoppingLatch.await();
    try {
        platform.awaitPlatformStarted();
    } catch (PlatformException e) {
        assertEquals("The platform is stopping.", e.getMessage());
    }
    continueStoppingLatch.countDown();
    platformStopFuture.get();
}
Also used : BeanMetaData(org.eclipse.scout.rt.platform.BeanMetaData) PlatformEvent(org.eclipse.scout.rt.platform.PlatformEvent) PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException) CountDownLatch(java.util.concurrent.CountDownLatch) IPlatformListener(org.eclipse.scout.rt.platform.IPlatformListener) Test(org.junit.Test)

Example 3 with PlatformEvent

use of org.eclipse.scout.rt.platform.PlatformEvent in project scout.rt by eclipse.

the class PlatformImplementor method fireStateEvent.

@SuppressWarnings("squid:S1181")
protected void fireStateEvent(State newState) {
    if (m_beanManager == null) {
        // can happen if there is an error creating the bean manager. cannot move to status invalid. just do nothing.
        return;
    }
    PlatformEvent event = new PlatformEvent(this, newState);
    try {
        for (IBean<IPlatformListener> bean : m_beanManager.getBeans(IPlatformListener.class)) {
            IPlatformListener listener = bean.getInstance();
            listener.stateChanged(event);
        }
    } catch (RuntimeException | Error e) {
        LOG.error("Error during event listener notification.", e);
        changeState(State.PlatformInvalid, true);
        throw e;
    }
}
Also used : PlatformEvent(org.eclipse.scout.rt.platform.PlatformEvent) IPlatformListener(org.eclipse.scout.rt.platform.IPlatformListener)

Aggregations

IPlatformListener (org.eclipse.scout.rt.platform.IPlatformListener)3 PlatformEvent (org.eclipse.scout.rt.platform.PlatformEvent)3 BeanMetaData (org.eclipse.scout.rt.platform.BeanMetaData)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1 PlatformException (org.eclipse.scout.rt.platform.exception.PlatformException)1 Test (org.junit.Test)1