[ 
https://issues.apache.org/jira/browse/CAMEL-12449?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16440552#comment-16440552
 ] 

ASF GitHub Bot commented on CAMEL-12449:
----------------------------------------

davsclaus closed pull request #2297: CAMEL-12449: DefaultServiceLoadBalancer 
throws IndexOutOfBoundsException
URL: https://github.com/apache/camel/pull/2297
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/camel-core/src/main/java/org/apache/camel/impl/cloud/DefaultServiceLoadBalancer.java
 
b/camel-core/src/main/java/org/apache/camel/impl/cloud/DefaultServiceLoadBalancer.java
index 19a55fa31a0..51d33cd0c59 100644
--- 
a/camel-core/src/main/java/org/apache/camel/impl/cloud/DefaultServiceLoadBalancer.java
+++ 
b/camel-core/src/main/java/org/apache/camel/impl/cloud/DefaultServiceLoadBalancer.java
@@ -136,7 +136,7 @@ protected void doStop() throws Exception {
             // filter services
             services = serviceFilter.apply(services);
             // let the client service chooser find which server to use
-            service = services.size() > 1 ? serviceChooser.choose(services) : 
services.get(0);
+            service = services.isEmpty() ? null : services.size() > 1 ? 
serviceChooser.choose(services) : services.get(0);
             if (service == null) {
                 throw new RejectedExecutionException("No active services with 
name " + serviceName);
             }
diff --git 
a/camel-core/src/test/java/org/apache/camel/impl/cloud/LoadBalancerTest.java 
b/camel-core/src/test/java/org/apache/camel/impl/cloud/LoadBalancerTest.java
index a78c291c582..1b30c29f324 100644
--- a/camel-core/src/test/java/org/apache/camel/impl/cloud/LoadBalancerTest.java
+++ b/camel-core/src/test/java/org/apache/camel/impl/cloud/LoadBalancerTest.java
@@ -16,22 +16,31 @@
  */
 package org.apache.camel.impl.cloud;
 
+import java.util.concurrent.RejectedExecutionException;
 import java.util.stream.Collectors;
 
-import org.apache.camel.ContextTestSupport;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.junit.BeforeClass;
 import org.junit.Test;
 
-public class LoadBalancerTest extends ContextTestSupport {
-    @Test
-    public void testLoadBalancer() throws Exception {
-        StaticServiceDiscovery serviceDiscovery = new StaticServiceDiscovery();
+import static junit.framework.TestCase.assertEquals;
+
+public class LoadBalancerTest {
+
+    private static StaticServiceDiscovery serviceDiscovery = new 
StaticServiceDiscovery();
+
+    @BeforeClass
+    public static void setUp() {
         serviceDiscovery.addServer("no-name", "127.0.0.1", 2001);
         serviceDiscovery.addServer("no-name", "127.0.0.1", 2002);
         serviceDiscovery.addServer("no-name", "127.0.0.1", 1001);
         serviceDiscovery.addServer("no-name", "127.0.0.1", 1002);
+    }
 
+    @Test
+    public void testLoadBalancer() throws Exception {
         DefaultServiceLoadBalancer loadBalancer = new 
DefaultServiceLoadBalancer();
-        loadBalancer.setCamelContext(context);
+        loadBalancer.setCamelContext(new DefaultCamelContext());
         loadBalancer.setServiceDiscovery(serviceDiscovery);
         loadBalancer.setServiceFilter(services -> services.stream().filter(s 
-> s.getPort() < 2000).collect(Collectors.toList()));
         loadBalancer.setServiceChooser(new RoundRobinServiceChooser());
@@ -44,4 +53,14 @@ public void testLoadBalancer() throws Exception {
             return false;
         });
     }
+
+    @Test(expected = RejectedExecutionException.class)
+    public void testNoActiveServices() throws Exception {
+        DefaultServiceLoadBalancer loadBalancer = new 
DefaultServiceLoadBalancer();
+        loadBalancer.setCamelContext(new DefaultCamelContext());
+        loadBalancer.setServiceDiscovery(serviceDiscovery);
+        loadBalancer.setServiceFilter(services -> services.stream().filter(s 
-> s.getPort() < 1000).collect(Collectors.toList()));
+        loadBalancer.setServiceChooser(new RoundRobinServiceChooser());
+        loadBalancer.process("no-name", service -> false);
+    }
 }


 

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


> DefaultServiceLoadBalancer throws IndexOutOfBoundsException after applying 
> ServiceFilter
> ----------------------------------------------------------------------------------------
>
>                 Key: CAMEL-12449
>                 URL: https://issues.apache.org/jira/browse/CAMEL-12449
>             Project: Camel
>          Issue Type: Bug
>          Components: camel-core
>    Affects Versions: 2.21.0
>            Reporter: Vadim R
>            Priority: Major
>
> If no services remains after applying ServiceFilter in 
> DefaultServiceLoadBalancer, expecting exception 
> RejectedExecutionException("No active services with name " + serviceName), 
> but throws IndexOutOfBoundsException.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to