Search in sources :

Example 61 with WeldContainer

use of org.jboss.weld.environment.se.WeldContainer in project core by weld.

the class NotificationTimeoutTest method testMultipleObserverNotificationTimeout.

@Test
public void testMultipleObserverNotificationTimeout() throws InterruptedException {
    SUCCESSION_OF_EVENTS.clear();
    try (WeldContainer container = new Weld().initialize()) {
        BlockingQueue<Throwable> synchronizer = new LinkedBlockingQueue<>();
        CountDownLatch countdown = new CountDownLatch(2);
        container.event().select(CountDownLatch.class).fireAsync(countdown, NotificationOptions.of(WeldNotificationOptions.TIMEOUT, 2000l)).exceptionally((Throwable t) -> {
            // handle TimeoutException we got here
            SUCCESSION_OF_EVENTS.add("Timeout");
            synchronizer.add(t);
            return null;
        });
        Throwable fromSynchronizer = synchronizer.poll(5, TimeUnit.SECONDS);
        Assert.assertNotNull(fromSynchronizer);
        Assert.assertTrue(fromSynchronizer.getCause().toString().contains(TimeoutException.class.getSimpleName()));
        // wait for the observers to finish their jobs
        countdown.await();
        // assert contents AND order of events
        Assert.assertTrue(SUCCESSION_OF_EVENTS.equals(getExpectedListOfEvents()));
    }
}
Also used : WeldContainer(org.jboss.weld.environment.se.WeldContainer) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) Weld(org.jboss.weld.environment.se.Weld) Test(org.junit.Test)

Example 62 with WeldContainer

use of org.jboss.weld.environment.se.WeldContainer in project core by weld.

the class NotificationTimeoutTest method testBadTimeoutInputThrowsException.

@Test
public void testBadTimeoutInputThrowsException() throws InterruptedException {
    SUCCESSION_OF_EVENTS.clear();
    CountDownLatch latch = new CountDownLatch(1);
    try (WeldContainer container = new Weld().initialize()) {
        container.event().select(CountDownLatch.class).fireAsync(latch, NotificationOptions.of(WeldNotificationOptions.TIMEOUT, 1.2345));
        Assert.fail("Bad input valut should throw IllegalArgumentException.");
    } catch (IllegalArgumentException iae) {
        // expected, should throw IAE
        // assert that no observers were notified
        Assert.assertFalse(latch.await(500, TimeUnit.MILLISECONDS));
        Assert.assertTrue(SUCCESSION_OF_EVENTS.isEmpty());
    }
}
Also used : WeldContainer(org.jboss.weld.environment.se.WeldContainer) CountDownLatch(java.util.concurrent.CountDownLatch) Weld(org.jboss.weld.environment.se.Weld) Test(org.junit.Test)

Example 63 with WeldContainer

use of org.jboss.weld.environment.se.WeldContainer in project core by weld.

the class NotificationTimeoutTest method testMultipleObserverParallelNotificationTimeout.

@Test
public void testMultipleObserverParallelNotificationTimeout() throws InterruptedException {
    SUCCESSION_OF_EVENTS.clear();
    try (WeldContainer container = new Weld().initialize()) {
        BlockingQueue<Throwable> synchronizer = new LinkedBlockingQueue<>();
        CountDownLatch countdown = new CountDownLatch(2);
        NotificationOptions options = NotificationOptions.builder().set(WeldNotificationOptions.TIMEOUT, "2000").set(WeldNotificationOptions.MODE, WeldNotificationOptions.NotificationMode.PARALLEL).build();
        container.event().select(CountDownLatch.class).fireAsync(countdown, options).exceptionally((Throwable t) -> {
            // handle TimeoutException we got here
            SUCCESSION_OF_EVENTS.add("Timeout");
            synchronizer.add(t);
            return null;
        });
        Throwable fromSynchronizer = synchronizer.poll(5, TimeUnit.SECONDS);
        Assert.assertNotNull(fromSynchronizer);
        Assert.assertTrue(fromSynchronizer.getCause().toString().contains(TimeoutException.class.getSimpleName()));
        // wait for the observers to finish their jobs
        countdown.await();
        // with parallel execution, we can only assert that all observers were notified (the order might differ)
        Assert.assertTrue(SUCCESSION_OF_EVENTS.size() == 3);
        Assert.assertTrue(SUCCESSION_OF_EVENTS.containsAll(getExpectedListOfEvents()));
    }
}
Also used : WeldNotificationOptions(org.jboss.weld.events.WeldNotificationOptions) NotificationOptions(javax.enterprise.event.NotificationOptions) WeldContainer(org.jboss.weld.environment.se.WeldContainer) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) Weld(org.jboss.weld.environment.se.Weld) Test(org.junit.Test)

Example 64 with WeldContainer

use of org.jboss.weld.environment.se.WeldContainer in project core by weld.

the class NotificationTimeoutTest method testSingleObserverNotificationTimeout.

@Test
public void testSingleObserverNotificationTimeout() throws InterruptedException {
    SUCCESSION_OF_EVENTS.clear();
    // this deployment omits the FAST observer so as to FORCE timeout - e.g. this deployment has only one observer
    Weld weld = new Weld().disableDiscovery().addBeanClasses(LazyObserver.class);
    try (WeldContainer container = weld.initialize()) {
        BlockingQueue<Throwable> synchronizer = new LinkedBlockingQueue<>();
        CountDownLatch countdown = new CountDownLatch(1);
        container.event().select(CountDownLatch.class).fireAsync(countdown, NotificationOptions.of(WeldNotificationOptions.TIMEOUT, new Long(1000l))).exceptionally((Throwable t) -> {
            SUCCESSION_OF_EVENTS.add("Timeout");
            synchronizer.add(t);
            return null;
        });
        Throwable fromSynchronizer = synchronizer.poll(5, TimeUnit.SECONDS);
        Assert.assertNotNull(fromSynchronizer);
        Assert.assertTrue(fromSynchronizer.getCause().toString().contains(TimeoutException.class.getSimpleName()));
        // wait for the observer to finish the jobs
        countdown.await();
        // assert that two events happened and timeout came first
        Assert.assertTrue(SUCCESSION_OF_EVENTS.size() == 2);
        Assert.assertTrue(SUCCESSION_OF_EVENTS.get(0).equals("Timeout"));
    }
}
Also used : WeldContainer(org.jboss.weld.environment.se.WeldContainer) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) Weld(org.jboss.weld.environment.se.Weld) Test(org.junit.Test)

Example 65 with WeldContainer

use of org.jboss.weld.environment.se.WeldContainer in project core by weld.

the class ImplicitScanBeanArchiveDirectoryTest method testDiscovery.

@Test
public void testDiscovery() {
    try (WeldContainer container = new Weld().scanClasspathEntries().initialize()) {
        AlphaFromDirectory alpha = container.select(AlphaFromDirectory.class).get();
        assertNotNull(alpha);
        assertEquals(1, alpha.ping());
    }
}
Also used : WeldContainer(org.jboss.weld.environment.se.WeldContainer) Weld(org.jboss.weld.environment.se.Weld) Test(org.junit.Test)

Aggregations

WeldContainer (org.jboss.weld.environment.se.WeldContainer)115 Weld (org.jboss.weld.environment.se.Weld)100 Test (org.junit.Test)98 CountDownLatch (java.util.concurrent.CountDownLatch)8 Bean (javax.enterprise.inject.spi.Bean)8 BeanManager (javax.enterprise.inject.spi.BeanManager)8 Type (java.lang.reflect.Type)7 URLClassLoader (java.net.URLClassLoader)7 ArrayList (java.util.ArrayList)7 URL (java.net.URL)6 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)6 Fabric8Extension (io.fabric8.cdi.Fabric8Extension)4 ParameterizedType (java.lang.reflect.ParameterizedType)4 List (java.util.List)4 CreationalContext (javax.enterprise.context.spi.CreationalContext)4 BeanManagerImpl (org.jboss.weld.manager.BeanManagerImpl)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 File (java.io.File)3 PrintStream (java.io.PrintStream)3 ServiceRegistry (org.jboss.weld.bootstrap.api.ServiceRegistry)3