openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r1798965 - /openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/events/async/ObserversAsyncTest.java
Date Fri, 16 Jun 2017 17:16:26 GMT
Author: struberg
Date: Fri Jun 16 17:16:26 2017
New Revision: 1798965

URL: http://svn.apache.org/viewvc?rev=1798965&view=rev
Log:
OWB-1182 improve unit test for async observer

Modified:
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/events/async/ObserversAsyncTest.java

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/events/async/ObserversAsyncTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/events/async/ObserversAsyncTest.java?rev=1798965&r1=1798964&r2=1798965&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/events/async/ObserversAsyncTest.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/events/async/ObserversAsyncTest.java
Fri Jun 16 17:16:26 2017
@@ -18,15 +18,52 @@
  */
 package org.apache.webbeans.test.events.async;
 
-import javax.annotation.Priority;
 import javax.enterprise.context.RequestScoped;
-import javax.enterprise.event.Observes;
+import javax.enterprise.event.ObservesAsync;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.apache.webbeans.test.AbstractUnitTest;
+import org.junit.Assert;
+import org.junit.Test;
 
-public class ObserversAsyncTest
+public class ObserversAsyncTest extends AbstractUnitTest
 {
+    @Test
+    public void testAsyncEventExceptionHandling() throws ExecutionException, InterruptedException
+    {
+        startContainer(Observer1.class, Observer2.class);
+
+        final AtomicReference<Throwable> observerException = new AtomicReference<>();
+
+        BlockingQueue<Throwable> queue = new LinkedBlockingQueue<>();
+
+        long start = System.nanoTime();
+
+        VisitorCollectorEvent event = new VisitorCollectorEvent();
+        CompletableFuture<VisitorCollectorEvent> completionStage = getBeanManager().getEvent().fireAsync(event)
+            .exceptionally(e ->
+            {
+                observerException.set(e);
+                return null;
+            })
+            .toCompletableFuture();
+
+        VisitorCollectorEvent visitorCollectorEvent = completionStage.get();
+
+        Assert.assertEquals(2, visitorCollectorEvent.getVisitors().size());
+
+        long end = System.nanoTime();
+        long durationMs = TimeUnit.NANOSECONDS.toMillis(end - start);
+        System.out.println("took ms: " + durationMs);
+    }
 
 
     public static class VisitorCollectorEvent
@@ -45,16 +82,28 @@ public class ObserversAsyncTest
     }
 
     @RequestScoped
-    public class Observer1
+    public static class Observer1
+    {
+        public void visit(@ObservesAsync VisitorCollectorEvent visitorCollector)
+        {
+            sleep(100L);
+            visitorCollector.visiting(getClass().getSimpleName());
+            throw new IllegalStateException("Observer1");
+        }
+    }
+
+    @RequestScoped
+    public static class Observer2
     {
-        public void visit(@Observes @Priority(1) VisitorCollectorEvent visitorCollector)
+        public void visit(@ObservesAsync VisitorCollectorEvent visitorCollector)
         {
-            sleep(10L);
+            sleep(2000L);
             visitorCollector.visiting(getClass().getSimpleName());
+            //X throw new IllegalStateException("Observer2");
         }
     }
 
-    private void sleep(long time)
+    private static void sleep(long time)
     {
         try
         {



Mime
View raw message