openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1798967 - /openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/events/async/ObserversAsyncTest.java
Date Fri, 16 Jun 2017 17:40:35 GMT
Author: rmannibucau
Date: Fri Jun 16 17:40:34 2017
New Revision: 1798967

URL: http://svn.apache.org/viewvc?rev=1798967&view=rev
Log:
let's use really more observers to ensure concurrency is not that dependent of the machine

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=1798967&r1=1798966&r2=1798967&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:40:34 2017
@@ -18,47 +18,73 @@
  */
 package org.apache.webbeans.test.events.async;
 
-import javax.enterprise.context.RequestScoped;
-import javax.enterprise.event.ObservesAsync;
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.spi.AfterBeanDiscovery;
+import javax.enterprise.inject.spi.Extension;
 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.ForkJoinPool;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicReference;
+import java.util.stream.IntStream;
 
 import org.apache.webbeans.test.AbstractUnitTest;
-import org.junit.Assert;
 import org.junit.Test;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
 public class ObserversAsyncTest extends AbstractUnitTest
 {
     @Test
     public void testAsyncEventExceptionHandling() throws ExecutionException, InterruptedException
     {
-        startContainer(Observer1.class, Observer2.class);
+        final int count = 100 + ForkJoinPool.getCommonPoolParallelism() * 10;
 
-        final AtomicReference<Throwable> observerException = new AtomicReference<>();
+        final VisitorCollectorEvent event = new VisitorCollectorEvent();
+
+        addExtension(new Extension() {
+            void addABunchOfObserversAtLeastMoreThanThreads(@Observes final AfterBeanDiscovery
afterBeanDiscovery)
+            {
+                IntStream.range(0, count)
+                        .forEach(i -> afterBeanDiscovery.addObserverMethod()
+                                .observedType(VisitorCollectorEvent.class)
+                                .async(true)
+                                .notifyWith(e ->
+                                {
+                                    if (i % 2 == 0 && (i < 30 || i > 70))
+                                    {
+                                        sleep(500);
+                                    }
+
+                                    final String name = "Observer" + i;
+                                    event.visiting(name);
+                                    throw new IllegalStateException(name);
+                                }));
+            }
+        });
+        startContainer();
 
-        BlockingQueue<Throwable> queue = new LinkedBlockingQueue<>();
+        final AtomicReference<Throwable> observerException = new AtomicReference<>();
 
         long start = System.nanoTime();
 
-        VisitorCollectorEvent event = new VisitorCollectorEvent();
         CompletableFuture<VisitorCollectorEvent> completionStage = getBeanManager().getEvent().fireAsync(event)
             .exceptionally(e ->
             {
                 observerException.set(e);
-                return null;
+                return event;
             })
             .toCompletableFuture();
 
         VisitorCollectorEvent visitorCollectorEvent = completionStage.get();
 
-        Assert.assertEquals(2, visitorCollectorEvent.getVisitors().size());
+        assertNotNull(observerException.get());
+        assertNotNull(visitorCollectorEvent);
+        assertEquals(count, visitorCollectorEvent.getVisitors().size());
 
         long end = System.nanoTime();
         long durationMs = TimeUnit.NANOSECONDS.toMillis(end - start);
@@ -81,28 +107,6 @@ public class ObserversAsyncTest extends
         }
     }
 
-    @RequestScoped
-    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(@ObservesAsync VisitorCollectorEvent visitorCollector)
-        {
-            sleep(2000L);
-            visitorCollector.visiting(getClass().getSimpleName());
-            //X throw new IllegalStateException("Observer2");
-        }
-    }
-
     private static void sleep(long time)
     {
         try



Mime
View raw message