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()));
}
}
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());
}
}
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()));
}
}
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"));
}
}
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());
}
}
Aggregations