You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by ro...@apache.org on 2019/11/22 13:31:10 UTC

[james-project] 01/03: JAMES-2937 Calling several time the RabbitMQ endpoint should not fail

This is an automated email from the ASF dual-hosted git repository.

rouazana pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 52e3cea8d9c991613b8ab0b9217793cdd2e95b95
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Fri Nov 22 17:18:07 2019 +0700

    JAMES-2937 Calling several time the RabbitMQ endpoint should not fail
    
    Channel was not given back to the pool, causing repeated calling of this
    endpoint to fail
---
 .../rabbitmq/ReactorRabbitMQChannelPool.java       | 23 +++++++++++++++-------
 .../backends/rabbitmq/RabbitMQHealthCheckTest.java | 14 +++++++++++++
 .../integration/WebAdminServerIntegrationTest.java | 14 +++++++++++++
 3 files changed, 44 insertions(+), 7 deletions(-)

diff --git a/backends-common/rabbitmq/src/main/java/org/apache/james/backends/rabbitmq/ReactorRabbitMQChannelPool.java b/backends-common/rabbitmq/src/main/java/org/apache/james/backends/rabbitmq/ReactorRabbitMQChannelPool.java
index 645a68a..a5dbea6 100644
--- a/backends-common/rabbitmq/src/main/java/org/apache/james/backends/rabbitmq/ReactorRabbitMQChannelPool.java
+++ b/backends-common/rabbitmq/src/main/java/org/apache/james/backends/rabbitmq/ReactorRabbitMQChannelPool.java
@@ -137,12 +137,14 @@ public class ReactorRabbitMQChannelPool implements ChannelPool, Startable {
 
     @Override
     public Mono<? extends Channel> getChannelMono() {
-        return Mono.fromCallable(() -> {
-            Channel channel = pool.borrowObject(MAXIMUM_BORROW_TIMEOUT_IN_MS);
-            Preconditions.checkArgument(channel.isOpen());
-            borrowedChannels.add(channel);
-            return channel;
-        });
+        return Mono.fromCallable(this::borrow);
+    }
+
+    private Channel borrow() throws Exception {
+        Channel channel = pool.borrowObject(MAXIMUM_BORROW_TIMEOUT_IN_MS);
+        Preconditions.checkArgument(channel.isOpen());
+        borrowedChannels.add(channel);
+        return channel;
     }
 
     @Override
@@ -186,10 +188,17 @@ public class ReactorRabbitMQChannelPool implements ChannelPool, Startable {
     }
 
     public boolean tryChannel() {
+        Channel channel = null;
         try {
-            return getChannelMono().block().isOpen();
+            channel = borrow();
+            return channel.isOpen();
         } catch (Throwable t) {
             return false;
+        } finally {
+            if (channel != null) {
+                borrowedChannels.remove(channel);
+                pool.returnObject(channel);
+            }
         }
     }
 }
diff --git a/backends-common/rabbitmq/src/test/java/org/apache/james/backends/rabbitmq/RabbitMQHealthCheckTest.java b/backends-common/rabbitmq/src/test/java/org/apache/james/backends/rabbitmq/RabbitMQHealthCheckTest.java
index 9f1d9f8..769741a 100644
--- a/backends-common/rabbitmq/src/test/java/org/apache/james/backends/rabbitmq/RabbitMQHealthCheckTest.java
+++ b/backends-common/rabbitmq/src/test/java/org/apache/james/backends/rabbitmq/RabbitMQHealthCheckTest.java
@@ -52,6 +52,20 @@ class RabbitMQHealthCheckTest {
     }
 
     @Test
+    void checkShouldReturnHealthyWhenCalledSeveralTime() {
+        healthCheck.check();
+        healthCheck.check();
+        healthCheck.check();
+        healthCheck.check();
+        healthCheck.check();
+        healthCheck.check();
+        healthCheck.check();
+        Result check = healthCheck.check();
+
+        assertThat(check.isHealthy()).isTrue();
+    }
+
+    @Test
     void checkShouldReturnUnhealthyWhenRabbitMQIsNotRunning(DockerRabbitMQ rabbitMQ) throws Exception {
         rabbitMQ.stopApp();
 
diff --git a/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/WebAdminServerIntegrationTest.java b/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/WebAdminServerIntegrationTest.java
index fe302ef..e60e848 100644
--- a/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/WebAdminServerIntegrationTest.java
+++ b/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/WebAdminServerIntegrationTest.java
@@ -126,6 +126,20 @@ public class WebAdminServerIntegrationTest {
     }
 
     @Test
+    public void healthCheckShouldReturn200WhenCalledRepeatedly() {
+        given().get(HealthCheckRoutes.HEALTHCHECK);
+        given().get(HealthCheckRoutes.HEALTHCHECK);
+        given().get(HealthCheckRoutes.HEALTHCHECK);
+        given().get(HealthCheckRoutes.HEALTHCHECK);
+        given().get(HealthCheckRoutes.HEALTHCHECK);
+
+        when()
+            .get(HealthCheckRoutes.HEALTHCHECK)
+        .then()
+            .statusCode(HttpStatus.OK_200);
+    }
+
+    @Test
     public void mailRepositoriesRoutesShouldBeExposed() {
         when()
             .get(MailRepositoriesRoutes.MAIL_REPOSITORIES)


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org