curator-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [curator] shayshim commented on a change in pull request #320: [CURATOR-533] - improve circuit breaking behavior
Date Sat, 03 Aug 2019 22:25:20 GMT
shayshim commented on a change in pull request #320: [CURATOR-533] - improve circuit breaking
behavior
URL: https://github.com/apache/curator/pull/320#discussion_r310364467
 
 

 ##########
 File path: curator-recipes/src/test/java/org/apache/curator/framework/recipes/leader/TestLeaderLatch.java
 ##########
 @@ -43,60 +43,119 @@
 import org.testng.annotations.Test;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
 import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.Callable;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ExecutorCompletionService;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
+import java.util.concurrent.Semaphore;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicLong;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
 
 public class TestLeaderLatch extends BaseClassForTests
 {
     private static final String PATH_NAME = "/one/two/me";
     private static final int MAX_LOOPS = 5;
 
+    private static class Holder
+    {
+        final AtomicInteger stateChangeCount = new AtomicInteger(0);
+        final CountDownLatch isLockedLatch = new CountDownLatch(1);
+        volatile LeaderLatch latch;
+    }
+
     @Test
     public void testWithCircuitBreaker() throws Exception
     {
+        final int threadQty = 5;
+
+        ExecutorService executorService = Executors.newFixedThreadPool(threadQty);
+        List<Holder> holders = Collections.emptyList();
         Timing2 timing = new Timing2();
-        ConnectionStateListenerDecorator decorator = ConnectionStateListenerDecorator.circuitBreaking(new
RetryForever(timing.multiple(2).milliseconds()));
-        try ( CuratorFramework client = CuratorFrameworkFactory.builder()
+        ConnectionStateListenerManagerFactory managerFactory = ConnectionStateListenerManagerFactory.circuitBreaking(new
RetryForever(timing.multiple(2).milliseconds()));
+        CuratorFramework client = CuratorFrameworkFactory.builder()
             .connectString(server.getConnectString())
             .retryPolicy(new RetryOneTime(1))
-            .connectionStateListenerDecorator(decorator)
+            .connectionStateListenerManagerFactory(managerFactory)
             .connectionTimeoutMs(timing.connection())
             .sessionTimeoutMs(timing.session())
-            .build() )
-        {
+            .build();
+        try {
             client.start();
-            AtomicInteger resetCount = new AtomicInteger(0);
-            try ( LeaderLatch latch = new LeaderLatch(client, "/foo/bar")
+            client.create().forPath("/hey");
+
+            Semaphore lostSemaphore = new Semaphore(0);
+            ConnectionStateListener undecoratedListener = new ConnectionStateListener()
             {
                 @Override
-                void reset() throws Exception
+                public void stateChanged(CuratorFramework client, ConnectionState newState)
                 {
-                    resetCount.incrementAndGet();
-                    super.reset();
+                    if ( newState == ConnectionState.LOST )
+                    {
+                        lostSemaphore.release();
+                    }
                 }
-            } )
-            {
-                latch.start();
-                Assert.assertTrue(latch.await(timing.forWaiting().milliseconds(), TimeUnit.MILLISECONDS));
 
-                for ( int i = 0; i < 5; ++i )
+                @Override
+                public boolean doNotProxy()
                 {
-                    server.stop();
-                    server.restart();
-                    timing.sleepABit();
+                    return true;
                 }
-                Assert.assertTrue(latch.await(timing.forWaiting().milliseconds(), TimeUnit.MILLISECONDS));
-                Assert.assertEquals(resetCount.get(), 2);
+            };
+            client.getConnectionStateListenable().addListener(undecoratedListener);
+
+            holders = IntStream.range(0, threadQty)
+                .mapToObj(index -> {
+                    Holder holder = new Holder();
+                    holder.latch = new LeaderLatch(client, "/foo/bar/" + index)
+                    {
+                        @Override
+                        protected void handleStateChange(ConnectionState newState)
+                        {
+                            holder.stateChangeCount.incrementAndGet();
+                            super.handleStateChange(newState);
+                        }
+                    };
+                    return holder;
+                })
+                .collect(Collectors.toList());
+
+            holders.forEach(holder -> {
+                executorService.submit(() -> {
+                    holder.latch.start();
+                    Assert.assertTrue(holder.latch.await(timing.forWaiting().milliseconds(),
TimeUnit.MILLISECONDS));
+                    holder.isLockedLatch.countDown();
+                    return null;
+                });
+                timing.awaitLatch(holder.isLockedLatch);
+            });
+
+            for ( int i = 0; i < 5; ++i )
 
 Review comment:
   5 is threadQty, right?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message